Elffuss BraidHub

kikocis/ncnn public

master · 17.5 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 "gemm.h"
16
17namespace ncnn {
18
19Gemm::Gemm()
20{
21 one_blob_only = false;
22 support_inplace = false;
23}
24
25int Gemm::load_param(const ParamDict& pd)
26{
27 alpha = pd.get(0, 1.f);
28 beta = pd.get(1, 1.f);
29 transA = pd.get(2, 0);
30 transB = pd.get(3, 0);
31 constantA = pd.get(4, 0);
32 constantB = pd.get(5, 0);
33 constantC = pd.get(6, 0);
34 constantM = pd.get(7, 0);
35 constantN = pd.get(8, 0);
36 constantK = pd.get(9, 0);
37 constant_broadcast_type_C = pd.get(10, 0);
38 output_N1M = pd.get(11, 0);
39 output_elempack = pd.get(12, 0);
40 output_elemtype = pd.get(13, 0);
41 output_transpose = pd.get(14, 0);
42 int8_scale_term = pd.get(18, 0);
43 constant_TILE_M = pd.get(20, 0);
44 constant_TILE_N = pd.get(21, 0);
45 constant_TILE_K = pd.get(22, 0);
46
47 if (int8_scale_term)
48 {
49#if !NCNN_INT8
50 NCNN_LOGE("please build ncnn with NCNN_INT8 enabled for int8 inference");
51 return -1;
52#endif
53 }
54
55 if (constantA == 1 && (constantM == 0 || constantK == 0))
56 {
57 NCNN_LOGE("constantM and constantK must be non-zero when constantA enabled");
58 return -1;
59 }
60
61 if (constantB == 1 && (constantN == 0 || constantK == 0))
62 {
63 NCNN_LOGE("constantN and constantK must be non-zero when constantB enabled");
64 return -1;
65 }
66
67 if (constantC == 1 && (constant_broadcast_type_C < -1 || constant_broadcast_type_C > 4))
68 {
69 NCNN_LOGE("constant_broadcast_type_C must be -1 or 0~4 when constantC enabled");
70 return -1;
71 }
72
73 if (constantA == 0 && constantB == 1 && constantC == 1)
74 one_blob_only = true;
75
76 if (constantA == 1 && constantB == 0 && constantC == 1)
77 one_blob_only = true;
78
79 if (constantA == 1 && constantB == 1 && constantC == 0)
80 one_blob_only = true;
81
82 return 0;
83}
84
85int Gemm::load_model(const ModelBin& mb)
86{
87 if (constantA == 1)
88 {
89 if (transA == 0)
90 A_data = mb.load(constantK, constantM, 0);
91 else
92 A_data = mb.load(constantM, constantK, 0);
93 if (A_data.empty())
94 return -100;
95 }
96
97 if (constantB == 1)
98 {
99 if (transB == 0)
100 B_data = mb.load(constantN, constantK, 0);
101 else
102 B_data = mb.load(constantK, constantN, 0);
103 if (B_data.empty())
104 return -100;
105 }
106
107 if (constantC == 1 && constant_broadcast_type_C != -1)
108 {
109 if (constant_broadcast_type_C == 0)
110 C_data = mb.load(1, 0);
111 if (constant_broadcast_type_C == 1)
112 C_data = mb.load(constantM, 0);
113 if (constant_broadcast_type_C == 2)
114 C_data = mb.load(1, constantM, 0);
115 if (constant_broadcast_type_C == 3)
116 C_data = mb.load(constantN, constantM, 0);
117 if (constant_broadcast_type_C == 4)
118 C_data = mb.load(constantN, 1, 0);
119 if (C_data.empty())
120 return -100;
121 }
122
123#if NCNN_INT8
124 if (int8_scale_term)
125 {
126 if (constantA == 1)
127 {
128 A_data_int8_scales = mb.load(constantM, 1);
129 }
130
131 if (constantB == 1)
132 {
133 B_data_int8_scale = mb.load(1, 1)[0];
134 }
135 }
136#endif // NCNN_INT8
137
138 return 0;
139}
140
141static void gemm_transB(const Mat& A, const Mat& BT, const Mat& C, Mat& top_blob, float alpha, float beta, int broadcast_type_C, int output_transpose, const Option& opt)
142{
143 const int M = A.dims == 3 ? A.c : A.h;
144 const int N = BT.dims == 3 ? BT.c : BT.h;
145 const int K = A.w; // assert A.w == BT.w
146
147 #pragma omp parallel for num_threads(opt.num_threads)
148 for (int i = 0; i < M; i++)
149 {
150 const int out_hstep = top_blob.dims == 3 ? (int)top_blob.cstep : top_blob.w;
151
152 const int A_hstep = A.dims == 3 ? (int)A.cstep : A.w;
153 const int BT_hstep = BT.dims == 3 ? (int)BT.cstep : BT.w;
154
155 const float* ptrA = (const float*)A + i * A_hstep;
156 const float* ptrC = C;
157
158 for (int j = 0; j < N; j++)
159 {
160 const float* ptrBT = (const float*)BT + j * BT_hstep;
161
162 float sum = 0.f;
163 if (ptrC)
164 {
165 if (broadcast_type_C == 0)
166 {
167 sum = ptrC[0];
168 }
169 if (broadcast_type_C == 1)
170 {
171 sum = ptrC[i];
172 }
173 if (broadcast_type_C == 2)
174 {
175 sum = ptrC[i];
176 }
177 if (broadcast_type_C == 3)
178 {
179 sum = ptrC[i * N + j];
180 }
181 if (broadcast_type_C == 4)
182 {
183 sum = ptrC[j];
184 }
185
186 sum *= beta;
187 }
188
189 for (int k = 0; k < K; k++)
190 {
191 sum += ptrA[k] * ptrBT[k];
192 }
193
194 sum *= alpha;
195
196 if (output_transpose)
197 {
198 top_blob[j * out_hstep + i] = sum;
199 }
200 else
201 {
202 top_blob[i * out_hstep + j] = sum;
203 }
204 }
205 }
206}
207
208#if NCNN_INT8
209static inline signed char float2int8(float v)
210{
211 int int32 = static_cast<int>(round(v));
212 if (int32 > 127) return 127;
213 if (int32 < -127) return -127;
214 return (signed char)int32;
215}
216
217static void gemm_transB_int8(const Mat& A_int8, const Mat& BT_int8, const Mat& A_int8_scales, float BT_int8_scale, const Mat& C, Mat& top_blob, float alpha, float beta, int broadcast_type_C, int output_transpose, const Option& opt)
218{
219 const int M = A_int8.h;
220 const int N = BT_int8.h;
221 const int K = A_int8.w; // assert A_int8.w == BT_int8.w
222
223 // NCNN_LOGE("naive ds %f %f", A_int8_scales[0], BT_int8_scale);
224
225 // #pragma omp parallel for num_threads(opt.num_threads)
226 for (int i = 0; i < M; i++)
227 {
228 const int out_hstep = top_blob.dims == 3 ? (int)top_blob.cstep : top_blob.w;
229
230 const signed char* ptrA = A_int8.row<const signed char>(i);
231 const float* ptrC = C;
232
233 const float descale = 1.f / (A_int8_scales[i] * BT_int8_scale);
234
235 // NCNN_LOGE("descale %f", descale);
236
237 for (int j = 0; j < N; j++)
238 {
239 const signed char* ptrBT = BT_int8.row<const signed char>(j);
240
241 int sum = 0;
242 for (int k = 0; k < K; k++)
243 {
244 // NCNN_LOGE("ptrA[%d] %d", k, ptrA[k]);
245 sum += ptrA[k] * ptrBT[k];
246 }
247
248 float sum_fp32 = sum * descale;
249
250 if (ptrC)
251 {
252 float c = 0.f;
253 if (broadcast_type_C == 0)
254 {
255 c = ptrC[0];
256 }
257 if (broadcast_type_C == 1)
258 {
259 c = ptrC[i];
260 }
261 if (broadcast_type_C == 2)
262 {
263 c = ptrC[i];
264 }
265 if (broadcast_type_C == 3)
266 {
267 c = ptrC[i * N + j];
268 }
269 if (broadcast_type_C == 4)
270 {
271 c = ptrC[j];
272 }
273
274 sum_fp32 += c * beta;
275 }
276
277 sum_fp32 *= alpha;
278
279 if (output_transpose)
280 {
281 top_blob[j * out_hstep + i] = sum_fp32;
282 }
283 else
284 {
285 top_blob[i * out_hstep + j] = sum_fp32;
286 }
287 }
288 }
289}
290#endif // NCNN_INT8
291
292int Gemm::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
293{
294 std::vector<Mat> bottom_blobs(1, bottom_blob);
295 std::vector<Mat> top_blobs(1, top_blob);
296 int ret = forward(bottom_blobs, top_blobs, opt);
297 top_blob = top_blobs[0];
298 return ret;
299}
300
301int Gemm::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
302{
303#if NCNN_INT8
304 if (int8_scale_term)
305 {
306 return forward_int8(bottom_blobs, top_blobs, opt);
307 }
308#endif // NCNN_INT8
309
310 const Mat& A0 = constantA ? A_data : bottom_blobs[0];
311 const Mat& B0 = constantB ? B_data : constantA ? bottom_blobs[0] : bottom_blobs[1];
312
313 size_t elemsize = A0.elemsize;
314
315 Mat A;
316 if (transA == 0)
317 {
318 A = A0;
319 }
320 else
321 {
322 // transpose A to row-major
323 A.create((A0.dims == 3 ? A0.c : A0.h), A0.w, elemsize, opt.workspace_allocator);
324
325 const int A0_hstep = A0.dims == 3 ? (int)A0.cstep : A0.w;
326
327 for (int i = 0; i < A.h; i++)
328 {
329 float* ptr = A.row(i);
330 for (int j = 0; j < A.w; j++)
331 {
332 ptr[j] = A0[j * A0_hstep + i];
333 }
334 }
335 }
336
337 Mat BT;
338 if (transB == 0)
339 {
340 // transpose B to col-major
341 BT.create((B0.dims == 3 ? B0.c : B0.h), B0.w, elemsize, opt.workspace_allocator);
342
343 const int B0_hstep = B0.dims == 3 ? (int)B0.cstep : B0.w;
344
345 for (int i = 0; i < BT.h; i++)
346 {
347 float* ptr = BT.row(i);
348 for (int j = 0; j < BT.w; j++)
349 {
350 ptr[j] = B0[j * B0_hstep + i];
351 }
352 }
353 }
354 else
355 {
356 BT = B0;
357 }
358
359 const int M = A.dims == 3 ? A.c : A.h;
360 const int N = BT.dims == 3 ? BT.c : BT.h;
361
362 Mat C;
363 int broadcast_type_C = 0;
364 if (constantC)
365 {
366 C = C_data;
367 broadcast_type_C = constant_broadcast_type_C;
368 }
369 else
370 {
371 if (constantA && constantB && bottom_blobs.size() == 1)
372 {
373 C = bottom_blobs[0];
374 }
375 else if ((constantA || constantB) && bottom_blobs.size() == 2)
376 {
377 C = bottom_blobs[1];
378 }
379 else if (bottom_blobs.size() == 3)
380 {
381 C = bottom_blobs[2];
382 }
383
384 if (!C.empty())
385 {
386 if (C.dims == 1 && C.w == 1)
387 {
388 // scalar
389 broadcast_type_C = 0;
390 }
391 if (C.dims == 1 && C.w == M)
392 {
393 // M
394 // auto broadcast from h to w is the ncnn-style convention
395 broadcast_type_C = 1;
396 }
397 if (C.dims == 1 && C.w == N)
398 {
399 // N
400 broadcast_type_C = 4;
401 }
402 if (C.dims == 2 && C.w == 1 && C.h == M)
403 {
404 // Mx1
405 broadcast_type_C = 2;
406 }
407 if (C.dims == 2 && C.w == N && C.h == M)
408 {
409 // MxN
410 broadcast_type_C = 3;
411 }
412 if (C.dims == 2 && C.w == N && C.h == 1)
413 {
414 // 1xN
415 broadcast_type_C = 4;
416 }
417 }
418 }
419
420 Mat& top_blob = top_blobs[0];
421 if (output_transpose)
422 {
423 if (output_N1M)
424 top_blob.create(M, 1, N, elemsize, opt.blob_allocator);
425 else
426 top_blob.create(M, N, elemsize, opt.blob_allocator);
427 }
428 else
429 {
430 if (output_N1M)
431 top_blob.create(N, 1, M, elemsize, opt.blob_allocator);
432 else
433 top_blob.create(N, M, elemsize, opt.blob_allocator);
434 }
435 if (top_blob.empty())
436 return -100;
437
438 gemm_transB(A, BT, C, top_blob, alpha, beta, broadcast_type_C, output_transpose, opt);
439
440 return 0;
441}
442
443#if NCNN_INT8
444int Gemm::forward_int8(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
445{
446 const Mat& A0 = constantA ? A_data : bottom_blobs[0];
447 const Mat& B0 = constantB ? B_data : constantA ? bottom_blobs[0] : bottom_blobs[1];
448
449 Mat A;
450 if (transA == 0)
451 {
452 A = A0;
453 }
454 else
455 {
456 // transpose A to row-major
457 if (A0.elemsize == 1)
458 {
459 A.create(A0.h, A0.w, (size_t)1u, 1, opt.workspace_allocator);
460
461 for (int i = 0; i < A.h; i++)
462 {
463 signed char* ptr = A.row<signed char>(i);
464 for (int j = 0; j < A.w; j++)
465 {
466 ptr[j] = A0.row<const signed char>(j)[i];
467 }
468 }
469 }
470 else
471 {
472 A.create(A0.dims == 3 ? A0.c : A0.h, A0.w, (size_t)4u, 1, opt.workspace_allocator);
473
474 for (int i = 0; i < A.h; i++)
475 {
476 float* ptr = A.row(i);
477 for (int j = 0; j < A.w; j++)
478 {
479 ptr[j] = A0.dims == 3 ? A0.channel(j)[i] : A0.row(j)[i];
480 }
481 }
482 }
483 }
484
485 // dynamic quantize A
486 Mat A_int8 = A;
487 Mat A_int8_scales = A_data_int8_scales;
488 if (A_int8.elemsize != 1)
489 {
490 A_int8.create(A.w, A.dims == 3 ? A.c : A.h, (size_t)1u, 1, opt.workspace_allocator);
491 A_int8_scales.create(A_int8.h, (size_t)4u, 1, opt.workspace_allocator);
492
493 for (int i = 0; i < A_int8.h; i++)
494 {
495 const int A_hstep = A.dims == 3 ? (int)A.cstep : A.w;
496 const float* ptr = (const float*)A + i * A_hstep;
497
498 float absmax = 0.f;
499 for (int k = 0; k < A_int8.w; k++)
500 {
501 absmax = std::max(absmax, (float)fabs(ptr[k]));
502 }
503
504 // NCNN_LOGE("A[%d] absmax %f", i, absmax);
505
506 float A_int8_scale = absmax == 0.f ? 1.f : 127.f / absmax;
507 A_int8_scales[i] = A_int8_scale;
508
509 signed char* ptrAi = A_int8.row<signed char>(i);
510
511 for (int k = 0; k < A_int8.w; k++)
512 {
513 ptrAi[k] = float2int8(ptr[k] * A_int8_scale);
514 }
515 }
516 }
517
518 // dynamic quantize B
519 Mat B0_int8 = B0;
520 float B_int8_scale = B_data_int8_scale;
521 if (B0_int8.elemsize != 1)
522 {
523 B0_int8.create(B0.w, B0.dims == 3 ? B0.c : B0.h, (size_t)1u, 1, opt.workspace_allocator);
524
525 float absmax = 0.f;
526 for (int i = 0; i < B0_int8.h; i++)
527 {
528 const int B_hstep = B0.dims == 3 ? (int)B0.cstep : B0.w;
529 const float* ptr = (const float*)B0 + i * B_hstep;
530
531 for (int k = 0; k < B0_int8.w; k++)
532 {
533 absmax = std::max(absmax, (float)fabs(ptr[k]));
534 }
535 }
536
537 // NCNN_LOGE("B0 absmax %f", absmax);
538
539 B_int8_scale = absmax == 0.f ? 1.f : 127.f / absmax;
540
541 for (int i = 0; i < B0_int8.h; i++)
542 {
543 const int B_hstep = B0.dims == 3 ? (int)B0.cstep : B0.w;
544 const float* ptr = (const float*)B0 + i * B_hstep;
545
546 signed char* ptrBi = B0_int8.row<signed char>(i);
547
548 for (int k = 0; k < B0_int8.w; k++)
549 {
550 ptrBi[k] = float2int8(ptr[k] * B_int8_scale);
551 }
552 }
553 }
554
555 Mat BT_int8;
556 if (transB == 0)
557 {
558 // transpose B to col-major
559 BT_int8.create(B0_int8.h, B0_int8.w, (size_t)1u, 1, opt.workspace_allocator);
560
561 for (int i = 0; i < BT_int8.h; i++)
562 {
563 signed char* ptr = BT_int8.row<signed char>(i);
564 for (int j = 0; j < BT_int8.w; j++)
565 {
566 ptr[j] = B0_int8.row<const signed char>(j)[i];
567 }
568 }
569 }
570 else
571 {
572 BT_int8 = B0_int8;
573 }
574
575 const int M = A_int8.h;
576 const int N = BT_int8.h;
577
578 Mat C;
579 int broadcast_type_C = 0;
580 if (constantC)
581 {
582 C = C_data;
583 broadcast_type_C = constant_broadcast_type_C;
584 }
585 else
586 {
587 if (constantA && constantB && bottom_blobs.size() == 1)
588 {
589 C = bottom_blobs[0];
590 }
591 else if ((constantA || constantB) && bottom_blobs.size() == 2)
592 {
593 C = bottom_blobs[1];
594 }
595 else if (bottom_blobs.size() == 3)
596 {
597 C = bottom_blobs[2];
598 }
599
600 if (!C.empty())
601 {
602 if (C.dims == 1 && C.w == 1)
603 {
604 // scalar
605 broadcast_type_C = 0;
606 }
607 if (C.dims == 1 && C.w == M)
608 {
609 // M
610 // auto broadcast from h to w is the ncnn-style convention
611 broadcast_type_C = 1;
612 }
613 if (C.dims == 1 && C.w == N)
614 {
615 // N
616 broadcast_type_C = 4;
617 }
618 if (C.dims == 2 && C.w == 1 && C.h == M)
619 {
620 // Mx1
621 broadcast_type_C = 2;
622 }
623 if (C.dims == 2 && C.w == N && C.h == M)
624 {
625 // MxN
626 broadcast_type_C = 3;
627 }
628 if (C.dims == 2 && C.w == N && C.h == 1)
629 {
630 // 1xN
631 broadcast_type_C = 4;
632 }
633 }
634 }
635
636 Mat& top_blob = top_blobs[0];
637 if (output_transpose)
638 {
639 if (output_N1M)
640 top_blob.create(M, 1, N, 4u, opt.blob_allocator);
641 else
642 top_blob.create(M, N, 4u, opt.blob_allocator);
643 }
644 else
645 {
646 if (output_N1M)
647 top_blob.create(N, 1, M, 4u, opt.blob_allocator);
648 else
649 top_blob.create(N, M, 4u, opt.blob_allocator);
650 }
651 if (top_blob.empty())
652 return -100;
653
654 gemm_transB_int8(A_int8, BT_int8, A_int8_scales, B_int8_scale, C, top_blob, alpha, beta, broadcast_type_C, output_transpose, opt);
655
656 return 0;
657}
658#endif // NCNN_INT8
659
660} // namespace ncnn