Elffuss BraidHub

kikocis/ncnn public

master · 14.2 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_requantize(const ncnn::Mat& a, int scale_in_data_size, int scale_out_data_size, int bias_data_size, int activation_type, float alpha, float beta)
18{
19 ncnn::ParamDict pd;
20 pd.set(0, scale_in_data_size);
21 pd.set(1, scale_out_data_size);
22 pd.set(2, bias_data_size);
23
24 ncnn::Mat activation_params(2);
25 activation_params[0] = alpha;
26 activation_params[1] = beta;
27 pd.set(3, activation_type);
28 pd.set(4, activation_params);
29
30 std::vector<ncnn::Mat> weights(bias_data_size ? 3 : 2);
31 weights[0] = RandomMat(scale_in_data_size);
32 weights[1] = RandomMat(scale_out_data_size);
33 if (bias_data_size)
34 weights[2] = RandomMat(bias_data_size);
35
36 Randomize(weights[0], 0.0001, 0.001);
37 Randomize(weights[1], 10, 100);
38
39 int flag = TEST_LAYER_DISABLE_AUTO_INPUT_CASTING;
40 int ret = test_layer("Requantize", pd, weights, a, 1, 0, flag);
41 if (ret != 0)
42 {
43 fprintf(stderr, "test_requantize failed a.dims=%d a=(%d %d %d) scale_in_data_size=%d scale_out_data_size=%d bias_data_size=%d act=%d actparams=[%f,%f]\n", a.dims, a.w, a.h, a.c, scale_in_data_size, scale_out_data_size, bias_data_size, activation_type, activation_params[0], activation_params[1]);
44 }
45
46 return ret;
47}
48
49static int test_requantize(const ncnn::Mat& a, int scale_in_data_size, int scale_out_data_size, int bias_data_size)
50{
51 return 0
52 || test_requantize(a, scale_in_data_size, scale_out_data_size, bias_data_size, 0, 0.f, 0.f)
53 || test_requantize(a, scale_in_data_size, scale_out_data_size, bias_data_size, 1, 0.f, 0.f)
54 || test_requantize(a, scale_in_data_size, scale_out_data_size, bias_data_size, 2, RandomFloat(0, 1), 0.f)
55 || test_requantize(a, scale_in_data_size, scale_out_data_size, bias_data_size, 3, RandomFloat(-1, 0), RandomFloat(0, 1))
56 || test_requantize(a, scale_in_data_size, scale_out_data_size, bias_data_size, 4, 0.f, 0.f)
57 || test_requantize(a, scale_in_data_size, scale_out_data_size, bias_data_size, 5, 0.f, 0.f);
58}
59
60static int test_requantize_pack8(const ncnn::Mat& a, int scale_in_data_size, int scale_out_data_size, int bias_data_size, int activation_type, float alpha, float beta)
61{
62 ncnn::ParamDict pd;
63 pd.set(0, scale_in_data_size);
64 pd.set(1, scale_out_data_size);
65 pd.set(2, bias_data_size);
66
67 ncnn::Mat activation_params(2);
68 activation_params[0] = alpha;
69 activation_params[1] = beta;
70 pd.set(3, activation_type);
71 pd.set(4, activation_params);
72
73 std::vector<ncnn::Mat> weights(bias_data_size ? 3 : 2);
74 weights[0] = RandomMat(scale_in_data_size);
75 weights[1] = RandomMat(scale_out_data_size);
76 if (bias_data_size)
77 weights[2] = RandomMat(bias_data_size);
78
79 Randomize(weights[0], 0.0001, 0.001);
80 Randomize(weights[1], 10, 100);
81
82 int flag = TEST_LAYER_DISABLE_AUTO_INPUT_CASTING | TEST_LAYER_ENABLE_FORCE_INPUT_PACK8;
83 int ret = test_layer("Requantize", pd, weights, a, 1, 0, flag);
84 if (ret != 0)
85 {
86 fprintf(stderr, "test_requantize_pack8 failed a.dims=%d a=(%d %d %d) scale_in_data_size=%d scale_out_data_size=%d bias_data_size=%d act=%d actparams=[%f,%f]\n", a.dims, a.w, a.h, a.c, scale_in_data_size, scale_out_data_size, bias_data_size, activation_type, activation_params[0], activation_params[1]);
87 }
88
89 return ret;
90}
91
92static int test_requantize_pack8(const ncnn::Mat& a, int scale_in_data_size, int scale_out_data_size, int bias_data_size)
93{
94 return 0
95 || test_requantize_pack8(a, scale_in_data_size, scale_out_data_size, bias_data_size, 0, 0.f, 0.f)
96 || test_requantize_pack8(a, scale_in_data_size, scale_out_data_size, bias_data_size, 1, 0.f, 0.f)
97 || test_requantize_pack8(a, scale_in_data_size, scale_out_data_size, bias_data_size, 2, RandomFloat(0, 1), 0.f)
98 || test_requantize_pack8(a, scale_in_data_size, scale_out_data_size, bias_data_size, 3, RandomFloat(-1, 0), RandomFloat(0, 1))
99 || test_requantize_pack8(a, scale_in_data_size, scale_out_data_size, bias_data_size, 4, 0.f, 0.f)
100 || test_requantize_pack8(a, scale_in_data_size, scale_out_data_size, bias_data_size, 5, 0.f, 0.f);
101}
102
103static int test_requantize_0()
104{
105 return 0
106 || test_requantize(RandomIntMat(5, 7, 24), 1, 1, 24)
107 || test_requantize(RandomIntMat(5, 7, 24), 1, 1, 1)
108 || test_requantize(RandomIntMat(5, 7, 24), 1, 1, 0)
109 || test_requantize(RandomIntMat(5, 7, 24), 24, 24, 24)
110 || test_requantize(RandomIntMat(5, 7, 24), 24, 24, 1)
111 || test_requantize(RandomIntMat(5, 7, 24), 24, 24, 0)
112 || test_requantize(RandomIntMat(5, 7, 24), 1, 24, 24)
113 || test_requantize(RandomIntMat(5, 7, 24), 1, 24, 1)
114 || test_requantize(RandomIntMat(5, 7, 24), 1, 24, 0)
115 || test_requantize(RandomIntMat(5, 7, 24), 24, 1, 24)
116 || test_requantize(RandomIntMat(5, 7, 24), 24, 1, 1)
117 || test_requantize(RandomIntMat(5, 7, 24), 24, 1, 0)
118 || test_requantize(RandomIntMat(7, 9, 12), 1, 1, 12)
119 || test_requantize(RandomIntMat(7, 9, 12), 1, 1, 1)
120 || test_requantize(RandomIntMat(7, 9, 12), 1, 1, 0)
121 || test_requantize(RandomIntMat(7, 9, 12), 12, 12, 12)
122 || test_requantize(RandomIntMat(7, 9, 12), 12, 12, 1)
123 || test_requantize(RandomIntMat(7, 9, 12), 12, 12, 0)
124 || test_requantize(RandomIntMat(7, 9, 12), 1, 12, 12)
125 || test_requantize(RandomIntMat(7, 9, 12), 1, 12, 1)
126 || test_requantize(RandomIntMat(7, 9, 12), 1, 12, 0)
127 || test_requantize(RandomIntMat(7, 9, 12), 12, 1, 12)
128 || test_requantize(RandomIntMat(7, 9, 12), 12, 1, 1)
129 || test_requantize(RandomIntMat(7, 9, 12), 12, 1, 0)
130 || test_requantize(RandomIntMat(3, 5, 13), 1, 1, 13)
131 || test_requantize(RandomIntMat(3, 5, 13), 1, 1, 1)
132 || test_requantize(RandomIntMat(3, 5, 13), 1, 1, 0)
133 || test_requantize(RandomIntMat(3, 5, 13), 13, 13, 13)
134 || test_requantize(RandomIntMat(3, 5, 13), 13, 13, 1)
135 || test_requantize(RandomIntMat(3, 5, 13), 13, 13, 0)
136 || test_requantize(RandomIntMat(3, 5, 13), 1, 13, 13)
137 || test_requantize(RandomIntMat(3, 5, 13), 1, 13, 1)
138 || test_requantize(RandomIntMat(3, 5, 13), 1, 13, 0)
139 || test_requantize(RandomIntMat(3, 5, 13), 13, 1, 13)
140 || test_requantize(RandomIntMat(3, 5, 13), 13, 1, 1)
141 || test_requantize(RandomIntMat(3, 5, 13), 13, 1, 0);
142}
143
144static int test_requantize_1()
145{
146 return 0
147 || test_requantize(RandomIntMat(15, 24), 1, 1, 24)
148 || test_requantize(RandomIntMat(15, 24), 1, 1, 1)
149 || test_requantize(RandomIntMat(15, 24), 1, 1, 0)
150 || test_requantize(RandomIntMat(15, 24), 24, 24, 24)
151 || test_requantize(RandomIntMat(15, 24), 24, 24, 1)
152 || test_requantize(RandomIntMat(15, 24), 24, 24, 0)
153 || test_requantize(RandomIntMat(15, 24), 1, 24, 24)
154 || test_requantize(RandomIntMat(15, 24), 1, 24, 1)
155 || test_requantize(RandomIntMat(15, 24), 1, 24, 0)
156 || test_requantize(RandomIntMat(15, 24), 24, 1, 24)
157 || test_requantize(RandomIntMat(15, 24), 24, 1, 1)
158 || test_requantize(RandomIntMat(15, 24), 24, 1, 0)
159 || test_requantize(RandomIntMat(17, 12), 1, 1, 12)
160 || test_requantize(RandomIntMat(17, 12), 1, 1, 1)
161 || test_requantize(RandomIntMat(17, 12), 1, 1, 0)
162 || test_requantize(RandomIntMat(17, 12), 12, 12, 12)
163 || test_requantize(RandomIntMat(17, 12), 12, 12, 1)
164 || test_requantize(RandomIntMat(17, 12), 12, 12, 0)
165 || test_requantize(RandomIntMat(17, 12), 1, 12, 12)
166 || test_requantize(RandomIntMat(17, 12), 1, 12, 1)
167 || test_requantize(RandomIntMat(17, 12), 1, 12, 0)
168 || test_requantize(RandomIntMat(17, 12), 12, 1, 12)
169 || test_requantize(RandomIntMat(17, 12), 12, 1, 1)
170 || test_requantize(RandomIntMat(17, 12), 12, 1, 0)
171 || test_requantize(RandomIntMat(19, 15), 1, 1, 15)
172 || test_requantize(RandomIntMat(19, 15), 1, 1, 1)
173 || test_requantize(RandomIntMat(19, 15), 1, 1, 0)
174 || test_requantize(RandomIntMat(19, 15), 15, 15, 15)
175 || test_requantize(RandomIntMat(19, 15), 15, 15, 1)
176 || test_requantize(RandomIntMat(19, 15), 15, 15, 0)
177 || test_requantize(RandomIntMat(19, 15), 1, 15, 15)
178 || test_requantize(RandomIntMat(19, 15), 1, 15, 1)
179 || test_requantize(RandomIntMat(19, 15), 1, 15, 0)
180 || test_requantize(RandomIntMat(19, 15), 15, 1, 15)
181 || test_requantize(RandomIntMat(19, 15), 15, 1, 1)
182 || test_requantize(RandomIntMat(19, 15), 15, 1, 0);
183}
184
185static int test_requantize_2()
186{
187 return 0
188 || test_requantize(RandomIntMat(128), 1, 1, 128)
189 || test_requantize(RandomIntMat(128), 1, 1, 1)
190 || test_requantize(RandomIntMat(128), 1, 1, 0)
191 || test_requantize(RandomIntMat(128), 128, 128, 128)
192 || test_requantize(RandomIntMat(128), 128, 128, 1)
193 || test_requantize(RandomIntMat(128), 128, 128, 0)
194 || test_requantize(RandomIntMat(128), 1, 128, 128)
195 || test_requantize(RandomIntMat(128), 1, 128, 1)
196 || test_requantize(RandomIntMat(128), 1, 128, 0)
197 || test_requantize(RandomIntMat(128), 128, 1, 128)
198 || test_requantize(RandomIntMat(128), 128, 1, 1)
199 || test_requantize(RandomIntMat(128), 128, 1, 0)
200 || test_requantize(RandomIntMat(124), 1, 1, 124)
201 || test_requantize(RandomIntMat(124), 1, 1, 1)
202 || test_requantize(RandomIntMat(124), 1, 1, 0)
203 || test_requantize(RandomIntMat(124), 124, 124, 124)
204 || test_requantize(RandomIntMat(124), 124, 124, 1)
205 || test_requantize(RandomIntMat(124), 124, 124, 0)
206 || test_requantize(RandomIntMat(124), 1, 124, 124)
207 || test_requantize(RandomIntMat(124), 1, 124, 1)
208 || test_requantize(RandomIntMat(124), 1, 124, 0)
209 || test_requantize(RandomIntMat(124), 124, 1, 124)
210 || test_requantize(RandomIntMat(124), 124, 1, 1)
211 || test_requantize(RandomIntMat(124), 124, 1, 0)
212 || test_requantize(RandomIntMat(127), 1, 1, 127)
213 || test_requantize(RandomIntMat(127), 1, 1, 1)
214 || test_requantize(RandomIntMat(127), 1, 1, 0)
215 || test_requantize(RandomIntMat(127), 127, 127, 127)
216 || test_requantize(RandomIntMat(127), 127, 127, 1)
217 || test_requantize(RandomIntMat(127), 127, 127, 0)
218 || test_requantize(RandomIntMat(127), 1, 127, 127)
219 || test_requantize(RandomIntMat(127), 1, 127, 1)
220 || test_requantize(RandomIntMat(127), 1, 127, 0)
221 || test_requantize(RandomIntMat(127), 127, 1, 127)
222 || test_requantize(RandomIntMat(127), 127, 1, 1)
223 || test_requantize(RandomIntMat(127), 127, 1, 0);
224}
225
226static int test_requantize_3()
227{
228 return 0
229 || test_requantize_pack8(RandomIntMat(5, 7, 24), 1, 1, 24)
230 || test_requantize_pack8(RandomIntMat(5, 7, 24), 1, 1, 1)
231 || test_requantize_pack8(RandomIntMat(5, 7, 24), 1, 1, 0)
232 || test_requantize_pack8(RandomIntMat(5, 7, 24), 24, 24, 24)
233 || test_requantize_pack8(RandomIntMat(5, 7, 24), 24, 24, 1)
234 || test_requantize_pack8(RandomIntMat(5, 7, 24), 24, 24, 0)
235 || test_requantize_pack8(RandomIntMat(5, 7, 24), 1, 24, 24)
236 || test_requantize_pack8(RandomIntMat(5, 7, 24), 1, 24, 1)
237 || test_requantize_pack8(RandomIntMat(5, 7, 24), 1, 24, 0)
238 || test_requantize_pack8(RandomIntMat(5, 7, 24), 24, 1, 24)
239 || test_requantize_pack8(RandomIntMat(5, 7, 24), 24, 1, 1)
240 || test_requantize_pack8(RandomIntMat(5, 7, 24), 24, 1, 0)
241 || test_requantize_pack8(RandomIntMat(15, 24), 1, 1, 24)
242 || test_requantize_pack8(RandomIntMat(15, 24), 1, 1, 1)
243 || test_requantize_pack8(RandomIntMat(15, 24), 1, 1, 0)
244 || test_requantize_pack8(RandomIntMat(15, 24), 24, 24, 24)
245 || test_requantize_pack8(RandomIntMat(15, 24), 24, 24, 1)
246 || test_requantize_pack8(RandomIntMat(15, 24), 24, 24, 0)
247 || test_requantize_pack8(RandomIntMat(15, 24), 1, 24, 24)
248 || test_requantize_pack8(RandomIntMat(15, 24), 1, 24, 1)
249 || test_requantize_pack8(RandomIntMat(15, 24), 1, 24, 0)
250 || test_requantize_pack8(RandomIntMat(15, 24), 24, 1, 24)
251 || test_requantize_pack8(RandomIntMat(15, 24), 24, 1, 1)
252 || test_requantize_pack8(RandomIntMat(15, 24), 24, 1, 0)
253 || test_requantize_pack8(RandomIntMat(128), 1, 1, 128)
254 || test_requantize_pack8(RandomIntMat(128), 1, 1, 1)
255 || test_requantize_pack8(RandomIntMat(128), 1, 1, 0)
256 || test_requantize_pack8(RandomIntMat(128), 128, 128, 128)
257 || test_requantize_pack8(RandomIntMat(128), 128, 128, 1)
258 || test_requantize_pack8(RandomIntMat(128), 128, 128, 0)
259 || test_requantize_pack8(RandomIntMat(128), 1, 128, 128)
260 || test_requantize_pack8(RandomIntMat(128), 1, 128, 1)
261 || test_requantize_pack8(RandomIntMat(128), 1, 128, 0)
262 || test_requantize_pack8(RandomIntMat(128), 128, 1, 128)
263 || test_requantize_pack8(RandomIntMat(128), 128, 1, 1)
264 || test_requantize_pack8(RandomIntMat(128), 128, 1, 0);
265}
266
267int main()
268{
269 SRAND(7767517);
270
271 return 0
272 || test_requantize_0()
273 || test_requantize_1()
274 || test_requantize_2()
275 || test_requantize_3();
276}