Elffuss BraidHub

kikocis/ncnn public

master · 11.8 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2020 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_gemm(int M, int N, int K, float alpha, int transA, int transB, int output_transpose, int constantA, int constantB, int output_N1M = 0)
18{
19 ncnn::ParamDict pd;
20 pd.set(0, alpha);
21 pd.set(1, 1.f); // beta
22 pd.set(2, transA);
23 pd.set(3, transB);
24 pd.set(4, constantA);
25 pd.set(5, constantB);
26 pd.set(6, 1);
27 pd.set(7, M);
28 pd.set(8, N);
29 pd.set(9, K);
30 pd.set(10, -1);
31 pd.set(11, output_N1M);
32 pd.set(14, output_transpose);
33
34 std::vector<ncnn::Mat> weights;
35 if (constantA) weights.push_back(transA ? (output_N1M ? ncnn::Mat(M, 1, K) : ncnn::Mat(M, K)) : (output_N1M ? ncnn::Mat(K, 1, M) : ncnn::Mat(K, M)));
36 if (constantB) weights.push_back(transB ? (output_N1M ? ncnn::Mat(K, 1, N) : ncnn::Mat(K, N)) : (output_N1M ? ncnn::Mat(N, 1, K) : ncnn::Mat(N, K)));
37
38 std::vector<ncnn::Mat> a;
39 if (!constantA) a.push_back(transA ? (output_N1M ? ncnn::Mat(M, 1, K) : ncnn::Mat(M, K)) : (output_N1M ? ncnn::Mat(K, 1, M) : ncnn::Mat(K, M)));
40 if (!constantB) a.push_back(transB ? (output_N1M ? ncnn::Mat(K, 1, N) : ncnn::Mat(K, N)) : (output_N1M ? ncnn::Mat(N, 1, K) : ncnn::Mat(N, K)));
41
42 for (size_t i = 0; i < weights.size(); i++)
43 {
44 Randomize(weights[i]);
45 }
46
47 for (size_t i = 0; i < a.size(); i++)
48 {
49 Randomize(a[i]);
50 }
51
52 int ret = test_layer("Gemm", pd, weights, a);
53 if (ret != 0)
54 {
55 fprintf(stderr, "test_gemm failed M=%d N=%d K=%d alpha=%f transA=%d transB=%d output_transpose=%d constantA=%d constantB=%d output_N1M=%d\n", M, N, K, alpha, transA, transB, output_transpose, constantA, constantB, output_N1M);
56 }
57
58 return ret;
59}
60
61static int test_gemm_bias(int M, int N, int K, const ncnn::Mat& C, float alpha, float beta, int transA, int transB, int output_transpose, int constantA, int constantB, int constantC)
62{
63 int broadcast_type_C = 0;
64 if (C.dims == 1 && C.w == 1)
65 {
66 // scalar
67 broadcast_type_C = 0;
68 }
69 if (C.dims == 1 && C.w == M)
70 {
71 // M
72 // auto broadcast from h to w is the ncnn-style convention
73 broadcast_type_C = 1;
74 }
75 if (C.dims == 1 && C.w == N)
76 {
77 // N
78 broadcast_type_C = 4;
79 }
80 if (C.dims == 2 && C.w == 1 && C.h == M)
81 {
82 // Mx1
83 broadcast_type_C = 2;
84 }
85 if (C.dims == 2 && C.w == N && C.h == M)
86 {
87 // MxN
88 broadcast_type_C = 3;
89 }
90 if (C.dims == 2 && C.w == N && C.h == 1)
91 {
92 // 1xN
93 broadcast_type_C = 4;
94 }
95
96 ncnn::ParamDict pd;
97 pd.set(0, alpha);
98 pd.set(1, beta);
99 pd.set(2, transA);
100 pd.set(3, transB);
101 pd.set(4, constantA);
102 pd.set(5, constantB);
103 pd.set(6, constantC);
104 pd.set(7, M);
105 pd.set(8, N);
106 pd.set(9, K);
107 pd.set(10, broadcast_type_C);
108 pd.set(14, output_transpose);
109
110 std::vector<ncnn::Mat> weights;
111 if (constantA) weights.push_back(transA ? ncnn::Mat(M, K) : ncnn::Mat(K, M));
112 if (constantB) weights.push_back(transB ? ncnn::Mat(K, N) : ncnn::Mat(N, K));
113 if (constantC) weights.push_back(C);
114
115 std::vector<ncnn::Mat> a;
116 if (!constantA) a.push_back(transA ? ncnn::Mat(M, K) : ncnn::Mat(K, M));
117 if (!constantB) a.push_back(transB ? ncnn::Mat(K, N) : ncnn::Mat(N, K));
118 if (!constantC) a.push_back(C);
119
120 for (size_t i = 0; i < weights.size(); i++)
121 {
122 Randomize(weights[i]);
123 }
124
125 for (size_t i = 0; i < a.size(); i++)
126 {
127 Randomize(a[i]);
128 }
129
130 int ret = test_layer("Gemm", pd, weights, a);
131 if (ret != 0)
132 {
133 fprintf(stderr, "test_gemm_bias failed M=%d N=%d K=%d C.dims=%d C=(%d %d %d) alpha=%f beta=%f transA=%d transB=%d output_transpose=%d constantA=%d constantB=%d constantC=%d\n", M, N, K, C.dims, C.w, C.h, C.c, alpha, beta, transA, transB, output_transpose, constantA, constantB, constantC);
134 }
135
136 return ret;
137}
138
139static int test_gemm_0(int M, int N, int K)
140{
141 return 0
142 || test_gemm(M, N, K, 2.1f, 0, 0, 0, 0, 0)
143 || test_gemm(M, N, K, 3.1f, 0, 1, 0, 0, 0)
144 || test_gemm(M, N, K, 4.1f, 1, 0, 0, 0, 0)
145 || test_gemm(M, N, K, 5.1f, 1, 1, 0, 0, 0)
146 || test_gemm(M, N, K, 2.1f, 0, 0, 1, 0, 0)
147 || test_gemm(M, N, K, 3.1f, 0, 1, 1, 0, 0)
148 || test_gemm(M, N, K, 4.1f, 1, 0, 1, 0, 0)
149 || test_gemm(M, N, K, 5.1f, 1, 1, 1, 0, 0)
150
151 || test_gemm(M, N, K, 1.7f, 0, 1, 0, 0, 0, 1)
152 || test_gemm(M, N, K, 1.7f, 1, 1, 0, 1, 0, 1)
153 || test_gemm(M, N, K, 1.9f, 0, 0, 0, 0, 1, 1)
154 || test_gemm(M, N, K, 1.9f, 1, 0, 0, 1, 1, 1)
155 || test_gemm(M, N, K, 1.7f, 0, 1, 1, 1, 0, 1)
156 || test_gemm(M, N, K, 1.7f, 1, 1, 1, 0, 1, 1)
157 || test_gemm(M, N, K, 1.9f, 0, 0, 1, 1, 1, 1)
158 || test_gemm(M, N, K, 1.9f, 1, 0, 1, 0, 0, 1)
159
160 || test_gemm(M, N, K, 2.1f, 0, 0, 0, 1, 0)
161 || test_gemm(M, N, K, 3.1f, 0, 1, 0, 1, 0)
162 || test_gemm(M, N, K, 4.1f, 1, 0, 0, 1, 0)
163 || test_gemm(M, N, K, 5.1f, 1, 1, 0, 1, 0)
164 || test_gemm(M, N, K, 2.1f, 0, 0, 1, 1, 0)
165 || test_gemm(M, N, K, 3.1f, 0, 1, 1, 1, 0)
166 || test_gemm(M, N, K, 4.1f, 1, 0, 1, 1, 0)
167 || test_gemm(M, N, K, 5.1f, 1, 1, 1, 1, 0)
168
169 || test_gemm(M, N, K, 2.1f, 0, 0, 0, 0, 1)
170 || test_gemm(M, N, K, 3.1f, 0, 1, 0, 0, 1)
171 || test_gemm(M, N, K, 4.1f, 1, 0, 0, 0, 1)
172 || test_gemm(M, N, K, 5.1f, 1, 1, 0, 0, 1)
173 || test_gemm(M, N, K, 2.1f, 0, 0, 1, 0, 1)
174 || test_gemm(M, N, K, 3.1f, 0, 1, 1, 0, 1)
175 || test_gemm(M, N, K, 4.1f, 1, 0, 1, 0, 1)
176 || test_gemm(M, N, K, 5.1f, 1, 1, 1, 0, 1)
177
178 || test_gemm(M, N, K, 2.1f, 0, 0, 0, 1, 1)
179 || test_gemm(M, N, K, 3.1f, 0, 1, 0, 1, 1)
180 || test_gemm(M, N, K, 4.1f, 1, 0, 0, 1, 1)
181 || test_gemm(M, N, K, 5.1f, 1, 1, 0, 1, 1)
182 || test_gemm(M, N, K, 2.1f, 0, 0, 1, 1, 1)
183 || test_gemm(M, N, K, 3.1f, 0, 1, 1, 1, 1)
184 || test_gemm(M, N, K, 4.1f, 1, 0, 1, 1, 1)
185 || test_gemm(M, N, K, 5.1f, 1, 1, 1, 1, 1);
186}
187
188static int test_gemm_1(int M, int N, int K)
189{
190 return 0
191 || test_gemm_bias(M, N, K, RandomMat(1), 2.1f, 0.5f, 0, 0, 0, 0, 0, 0)
192 || test_gemm_bias(M, N, K, RandomMat(M), 3.1f, 0.6f, 0, 1, 0, 0, 0, 0)
193 || test_gemm_bias(M, N, K, RandomMat(1, M), 4.1f, 0.7f, 1, 0, 1, 0, 0, 0)
194 || test_gemm_bias(M, N, K, RandomMat(N, M), 5.1f, 0.8f, 1, 1, 1, 0, 0, 0)
195 || test_gemm_bias(M, N, K, RandomMat(N, 1), 2.1f, 0.5f, 0, 0, 0, 0, 0, 0)
196 || test_gemm_bias(M, N, K, RandomMat(N), 3.1f, 0.6f, 0, 1, 0, 0, 0, 0)
197
198 || test_gemm_bias(M, N, K, RandomMat(1), 2.1f, 0.5f, 0, 0, 0, 1, 0, 0)
199 || test_gemm_bias(M, N, K, RandomMat(M), 3.1f, 0.6f, 0, 1, 0, 1, 0, 0)
200 || test_gemm_bias(M, N, K, RandomMat(1, M), 4.1f, 0.7f, 1, 0, 1, 1, 0, 0)
201 || test_gemm_bias(M, N, K, RandomMat(N, M), 5.1f, 0.8f, 1, 1, 1, 1, 0, 0)
202 || test_gemm_bias(M, N, K, RandomMat(N, 1), 2.1f, 0.5f, 0, 0, 0, 1, 0, 0)
203 || test_gemm_bias(M, N, K, RandomMat(N), 3.1f, 0.6f, 0, 1, 0, 1, 0, 0)
204
205 || test_gemm_bias(M, N, K, RandomMat(1), 2.1f, 0.5f, 0, 0, 0, 0, 1, 0)
206 || test_gemm_bias(M, N, K, RandomMat(M), 3.1f, 0.6f, 0, 1, 0, 0, 1, 0)
207 || test_gemm_bias(M, N, K, RandomMat(1, M), 4.1f, 0.7f, 1, 0, 1, 0, 1, 0)
208 || test_gemm_bias(M, N, K, RandomMat(N, M), 5.1f, 0.8f, 1, 1, 1, 0, 1, 0)
209 || test_gemm_bias(M, N, K, RandomMat(N, 1), 2.1f, 0.5f, 0, 0, 0, 0, 1, 0)
210 || test_gemm_bias(M, N, K, RandomMat(N), 3.1f, 0.6f, 0, 1, 0, 0, 1, 0)
211
212 || test_gemm_bias(M, N, K, RandomMat(1), 2.1f, 0.5f, 0, 0, 0, 1, 1, 0)
213 || test_gemm_bias(M, N, K, RandomMat(M), 3.1f, 0.6f, 0, 1, 0, 1, 1, 0)
214 || test_gemm_bias(M, N, K, RandomMat(1, M), 4.1f, 0.7f, 1, 0, 1, 1, 1, 0)
215 || test_gemm_bias(M, N, K, RandomMat(N, M), 5.1f, 0.8f, 1, 1, 1, 1, 1, 0)
216 || test_gemm_bias(M, N, K, RandomMat(N, 1), 2.1f, 0.5f, 0, 0, 0, 1, 1, 0)
217 || test_gemm_bias(M, N, K, RandomMat(N), 3.1f, 0.6f, 0, 1, 0, 1, 1, 0)
218
219 || test_gemm_bias(M, N, K, RandomMat(1), 2.1f, 0.5f, 0, 0, 0, 0, 0, 1)
220 || test_gemm_bias(M, N, K, RandomMat(M), 3.1f, 0.6f, 0, 1, 0, 0, 0, 1)
221 || test_gemm_bias(M, N, K, RandomMat(1, M), 4.1f, 0.7f, 1, 0, 1, 0, 0, 1)
222 || test_gemm_bias(M, N, K, RandomMat(N, M), 5.1f, 0.8f, 1, 1, 1, 0, 0, 1)
223 || test_gemm_bias(M, N, K, RandomMat(N, 1), 2.1f, 0.5f, 0, 0, 0, 0, 0, 1)
224 || test_gemm_bias(M, N, K, RandomMat(N), 3.1f, 0.6f, 0, 1, 0, 0, 0, 1)
225
226 || test_gemm_bias(M, N, K, RandomMat(1), 2.1f, 0.5f, 0, 0, 0, 1, 0, 1)
227 || test_gemm_bias(M, N, K, RandomMat(M), 3.1f, 0.6f, 0, 1, 0, 1, 0, 1)
228 || test_gemm_bias(M, N, K, RandomMat(1, M), 4.1f, 0.7f, 1, 0, 1, 1, 0, 1)
229 || test_gemm_bias(M, N, K, RandomMat(N, M), 5.1f, 0.8f, 1, 1, 1, 1, 0, 1)
230 || test_gemm_bias(M, N, K, RandomMat(N, 1), 2.1f, 0.5f, 0, 0, 0, 1, 0, 1)
231 || test_gemm_bias(M, N, K, RandomMat(N), 3.1f, 0.6f, 0, 1, 0, 1, 0, 1)
232
233 || test_gemm_bias(M, N, K, RandomMat(1), 2.1f, 0.5f, 0, 0, 0, 0, 1, 1)
234 || test_gemm_bias(M, N, K, RandomMat(M), 3.1f, 0.6f, 0, 1, 0, 0, 1, 1)
235 || test_gemm_bias(M, N, K, RandomMat(1, M), 4.1f, 0.7f, 1, 0, 1, 0, 1, 1)
236 || test_gemm_bias(M, N, K, RandomMat(N, M), 5.1f, 0.8f, 1, 1, 1, 0, 1, 1)
237 || test_gemm_bias(M, N, K, RandomMat(N, 1), 2.1f, 0.5f, 0, 0, 0, 0, 1, 1)
238 || test_gemm_bias(M, N, K, RandomMat(N), 3.1f, 0.6f, 0, 1, 0, 0, 1, 1)
239
240 || test_gemm_bias(M, N, K, RandomMat(1), 2.1f, 0.5f, 0, 0, 0, 1, 1, 1)
241 || test_gemm_bias(M, N, K, RandomMat(M), 3.1f, 0.6f, 0, 1, 0, 1, 1, 1)
242 || test_gemm_bias(M, N, K, RandomMat(1, M), 4.1f, 0.7f, 1, 0, 1, 1, 1, 1)
243 || test_gemm_bias(M, N, K, RandomMat(N, M), 5.1f, 0.8f, 1, 1, 1, 1, 1, 1)
244 || test_gemm_bias(M, N, K, RandomMat(N, 1), 2.1f, 0.5f, 0, 0, 0, 1, 1, 1)
245 || test_gemm_bias(M, N, K, RandomMat(N), 3.1f, 0.6f, 0, 1, 0, 1, 1, 1);
246}
247
248int main()
249{
250 SRAND(7767517);
251
252 int mnk[][3] = {
253 {1, 1, 1},
254 {2, 2, 2},
255 {3, 3, 3},
256 {4, 4, 4},
257 {5, 5, 5},
258 {6, 6, 6},
259 {7, 7, 7},
260 {8, 8, 8},
261 {15, 15, 15},
262 {16, 16, 16},
263 {31, 31, 31},
264 {40, 40, 40},
265 {1, 1, 23},
266 {1, 31, 1},
267 {23, 1, 1},
268 {12, 12, 23},
269 {12, 31, 12},
270 {23, 12, 12},
271 {1, 1, 47},
272 {1, 35, 1},
273 {47, 1, 1},
274 {24, 24, 47},
275 {24, 35, 24},
276 {47, 24, 24},
277 {1, 35, 47},
278 {23, 31, 1},
279 {23, 1, 23},
280 {23, 31, 23},
281 {31, 7, 3},
282 {28, 20, 7},
283 {32, 32, 9},
284 {44, 19, 7},
285 {47, 35, 48},
286 {47, 48, 47},
287 {48, 35, 47}
288 };
289
290 int mnk_count = sizeof(mnk) / sizeof(int) / 3;
291
292 for (int i = 0; i < mnk_count; i++)
293 {
294 int M = mnk[i][0];
295 int N = mnk[i][1];
296 int K = mnk[i][2];
297
298 int ret = 0
299 || test_gemm_0(M, N, K)
300 || test_gemm_1(M, N, K);
301
302 if (ret != 0)
303 return ret;
304 }
305
306 return 0;
307}