Elffuss BraidHub

kikocis/ncnn public

master · 9.1 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2019 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_convolutiondepthwise_dynamic(int w, int h, int c, int outch, int kernel, int dilation, int stride, int pad, int bias, int group)
18{
19 ncnn::Mat a = RandomMat(w, h, c);
20
21 ncnn::ParamDict pd;
22 pd.set(0, 0);
23 pd.set(1, 0);
24 pd.set(2, dilation);
25 pd.set(3, stride);
26 pd.set(4, pad);
27 pd.set(5, bias);
28 pd.set(6, 0);
29 pd.set(7, group);
30 pd.set(19, 1); // dynamic weight
31
32 int activation_type = RAND() % 7; // 0 1 2 3 4 5 6
33 ncnn::Mat activation_params(2);
34 activation_params[0] = (activation_type == 6) ? RandomFloat(0, 1) : RandomFloat(-1, 0); // alpha
35 activation_params[1] = RandomFloat(0, 1); // beta
36 pd.set(9, activation_type);
37 pd.set(10, activation_params);
38
39 std::vector<ncnn::Mat> as(bias ? 3 : 2);
40 as[0] = a;
41 as[1] = RandomMat(kernel, kernel, c / group, outch);
42 if (bias)
43 as[2] = RandomMat(outch);
44
45 std::vector<ncnn::Mat> weights(0);
46
47 int ret = test_layer("ConvolutionDepthWise", pd, weights, as);
48 if (ret != 0)
49 {
50 fprintf(stderr, "test_convolutiondepthwise_dynamic failed w=%d h=%d c=%d outch=%d kernel=%d dilation=%d stride=%d pad=%d bias=%d group=%d act=%d actparams=[%f,%f]\n", w, h, c, outch, kernel, dilation, stride, pad, bias, group, activation_type, activation_params[0], activation_params[1]);
51 }
52
53 return ret;
54}
55
56static int test_convolutiondepthwise_2()
57{
58 static const int kdsp[7][4] = {
59 {1, 1, 1, 0},
60 {1, 1, 2, 0},
61 {2, 1, 1, 1},
62 {2, 1, 2, -233},
63 {3, 1, 1, 1},
64 {3, 1, 2, 1},
65 {3, 2, 1, -234},
66 };
67
68 for (int i = 0; i < 7; i++)
69 {
70 const int k = kdsp[i][0];
71 const int d = kdsp[i][1];
72 const int s = kdsp[i][2];
73 const int p = kdsp[i][3];
74
75 int ret = 0
76 || test_convolutiondepthwise_dynamic(11, 10, 1, 1, k, d, s, p, 1, 1)
77 || test_convolutiondepthwise_dynamic(11, 10, 2, 2, k, d, s, p, 0, 1)
78 || test_convolutiondepthwise_dynamic(11, 10, 2, 2, k, d, s, p, 1, 2)
79 || test_convolutiondepthwise_dynamic(11, 10, 3, 3, k, d, s, p, 0, 3)
80 || test_convolutiondepthwise_dynamic(11, 10, 4, 2, k, d, s, p, 1, 2)
81 || test_convolutiondepthwise_dynamic(11, 10, 4, 4, k, d, s, p, 0, 4)
82 || test_convolutiondepthwise_dynamic(11, 10, 7, 7, k, d, s, p, 1, 7)
83 || test_convolutiondepthwise_dynamic(11, 10, 8, 8, k, d, s, p, 0, 2)
84 || test_convolutiondepthwise_dynamic(11, 10, 8, 8, k, d, s, p, 1, 8)
85 || test_convolutiondepthwise_dynamic(11, 10, 12, 12, k, d, s, p, 0, 4)
86 || test_convolutiondepthwise_dynamic(11, 10, 15, 15, k, d, s, p, 1, 15)
87 || test_convolutiondepthwise_dynamic(11, 10, 16, 8, k, d, s, p, 0, 2)
88 || test_convolutiondepthwise_dynamic(11, 10, 16, 16, k, d, s, p, 1, 16);
89
90 if (ret != 0)
91 return -1;
92 }
93
94 return 0;
95}
96
97#if NCNN_INT8
98static int test_convolutiondepthwise_int8(int w, int h, int c, int outch, int kernel, int dilation, int stride, int pad, int bias, int group, bool requant = false)
99{
100 ncnn::Mat a = RandomMat(w, h, c);
101
102 ncnn::ParamDict pd;
103 pd.set(0, outch);
104 pd.set(1, kernel);
105 pd.set(2, dilation);
106 pd.set(3, stride);
107 pd.set(4, pad);
108 pd.set(5, bias);
109 pd.set(6, outch / group * c / group * kernel * kernel * group);
110 pd.set(7, group);
111 pd.set(8, requant ? 101 : 1); // int8_scale_term
112
113 int activation_type = RAND() % 7; // 0 1 2 3 4 5 6
114 ncnn::Mat activation_params(2);
115 activation_params[0] = (activation_type == 6) ? RandomFloat(0, 1) : RandomFloat(-1, 0); // alpha
116 activation_params[1] = RandomFloat(0, 1); // beta
117 pd.set(9, activation_type);
118 pd.set(10, activation_params);
119
120 std::vector<ncnn::Mat> weights(bias ? 5 : 4);
121 weights[0] = RandomMat(outch / group * c / group * kernel * kernel * group);
122 ncnn::Mat weight_scales = scales_mat(weights[0], group, c * kernel * kernel / group, c * kernel * kernel / group);
123 ncnn::Mat input_scales = scales_mat(a, 1, w * h * c, a.cstep);
124 ncnn::Mat top_scales = requant ? scales_mat(a, 1, w * h * c, a.cstep) : ncnn::Mat();
125 if (bias)
126 {
127 weights[1] = RandomMat(outch);
128 weights[2] = weight_scales;
129 weights[3] = input_scales;
130 weights[4] = top_scales;
131 }
132 else
133 {
134 weights[1] = weight_scales;
135 weights[2] = input_scales;
136 weights[3] = top_scales;
137 }
138
139 int flag = TEST_LAYER_DISABLE_GPU_TESTING;
140 int ret = test_layer("ConvolutionDepthWise", pd, weights, a, requant ? 1.0f : 0.001f, 0, flag);
141 if (ret != 0)
142 {
143 fprintf(stderr, "test_convolutiondepthwise_int8 failed w=%d h=%d c=%d outch=%d kernel=%d dilation=%d stride=%d pad=%d bias=%d group=%d requant=%d act=%d actparams=[%f,%f]\n", w, h, c, outch, kernel, dilation, stride, pad, bias, group, requant, activation_type, activation_params[0], activation_params[1]);
144 }
145
146 return ret;
147}
148
149static int test_convolutiondepthwise_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, 2},
160 {4, 1, 2, -233},
161 {4, 2, 1, -234},
162 {5, 1, 1, -234},
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_convolutiondepthwise_int8(15, 7, 1, 1, k, d, s, p, 1, 1)
179 || test_convolutiondepthwise_int8(15, 7, 2, 2, k, d, s, p, 0, 1)
180 || test_convolutiondepthwise_int8(15, 7, 2, 2, k, d, s, p, 1, 2)
181 || test_convolutiondepthwise_int8(15, 7, 3, 3, k, d, s, p, 0, 3)
182 || test_convolutiondepthwise_int8(15, 7, 4, 2, k, d, s, p, 1, 2)
183 || test_convolutiondepthwise_int8(15, 7, 4, 4, k, d, s, p, 0, 4)
184 || test_convolutiondepthwise_int8(15, 7, 7, 7, k, d, s, p, 1, 7)
185 || test_convolutiondepthwise_int8(15, 7, 8, 8, k, d, s, p, 0, 2)
186 || test_convolutiondepthwise_int8(15, 7, 8, 8, k, d, s, p, 1, 8)
187 || test_convolutiondepthwise_int8(15, 7, 12, 12, k, d, s, p, 0, 4)
188 || test_convolutiondepthwise_int8(15, 7, 15, 15, k, d, s, p, 1, 15)
189 || test_convolutiondepthwise_int8(15, 7, 16, 8, k, d, s, p, 0, 2)
190 || test_convolutiondepthwise_int8(15, 7, 16, 16, k, d, s, p, 1, 16);
191
192 if (ret != 0)
193 return -1;
194 }
195
196 for (int i = 0; i < 16; i++)
197 {
198 const int k = kdsp[i][0];
199 const int d = kdsp[i][1];
200 const int s = kdsp[i][2];
201 const int p = kdsp[i][3];
202
203 int ret = 0
204 || test_convolutiondepthwise_int8(9, 7, 1, 1, k, d, s, p, 1, 1, true)
205 || test_convolutiondepthwise_int8(9, 7, 2, 2, k, d, s, p, 0, 1, true)
206 || test_convolutiondepthwise_int8(9, 7, 2, 2, k, d, s, p, 1, 2, true)
207 || test_convolutiondepthwise_int8(9, 7, 3, 3, k, d, s, p, 0, 3, true)
208 || test_convolutiondepthwise_int8(9, 7, 4, 2, k, d, s, p, 1, 2, true)
209 || test_convolutiondepthwise_int8(9, 7, 4, 4, k, d, s, p, 0, 4, true)
210 || test_convolutiondepthwise_int8(9, 7, 7, 7, k, d, s, p, 1, 7, true)
211 || test_convolutiondepthwise_int8(9, 7, 8, 8, k, d, s, p, 0, 2, true)
212 || test_convolutiondepthwise_int8(9, 7, 8, 8, k, d, s, p, 1, 8, true)
213 || test_convolutiondepthwise_int8(9, 7, 12, 12, k, d, s, p, 0, 4, true)
214 || test_convolutiondepthwise_int8(9, 7, 15, 15, k, d, s, p, 1, 15, true)
215 || test_convolutiondepthwise_int8(9, 7, 16, 8, k, d, s, p, 0, 2, true)
216 || test_convolutiondepthwise_int8(9, 7, 16, 16, k, d, s, p, 1, 16, true);
217
218 if (ret != 0)
219 return -1;
220 }
221
222 return 0;
223}
224#endif // NCNN_INT8
225
226int main()
227{
228 SRAND(7767517);
229
230#if NCNN_INT8
231 return test_convolutiondepthwise_1() || test_convolutiondepthwise_2();
232#else
233 return test_convolutiondepthwise_2();
234#endif
235}