Elffuss BraidHub

kikocis/ncnn public

e5ca598b9f4c · 22.4 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 "gemm_arm.h"
16
17#if __ARM_NEON
18#include <arm_neon.h>
19#endif // __ARM_NEON
20
21#include "arm_usability.h"
22
23#include "cpu.h"
24
25namespace ncnn {
26
27#include "gemm_bf16s_fp16s.h"
28#include "gemm_fp16s.h"
29
30extern void pack_A_tile(const Mat& A, Mat& AT, int i, int max_ii, int k, int max_kk);
31
32static int gemm_arm_fp16s(const Mat& A, const Mat& B, const Mat& C, Mat& top_blob, int broadcast_type_C, int transA, int transB, int output_transpose, float alpha, int constant_TILE_M, int constant_TILE_N, int constant_TILE_K, int nT, const Option& opt)
33{
34 const int M = transA ? A.w : (A.dims == 3 ? A.c : A.h) * A.elempack;
35 const int K = transA ? (A.dims == 3 ? A.c : A.h) * A.elempack : A.w;
36 const int N = transB ? (B.dims == 3 ? B.c : B.h) * B.elempack : B.w;
37
38 // NCNN_LOGE("M/N/K = %d %d %d", M, N, K);
39
40 int TILE_M, TILE_N, TILE_K;
41 get_optimal_tile_mnk_bf16s_fp16s(M, N, K, constant_TILE_M, constant_TILE_N, constant_TILE_K, TILE_M, TILE_N, TILE_K, nT);
42
43 // NCNN_LOGE("TILE M/N/K = %d %d %d", TILE_M, TILE_N, TILE_K);
44
45 int nn_M = (M + TILE_M - 1) / TILE_M;
46 int nn_N = (N + TILE_N - 1) / TILE_N;
47 int nn_K = (K + TILE_K - 1) / TILE_K;
48
49 Mat ATX(TILE_K * TILE_M, (K + TILE_K - 1) / TILE_K, nT, 2u, opt.workspace_allocator);
50 if (ATX.empty())
51 return -100;
52 Mat BT(TILE_K * TILE_N, (K + TILE_K - 1) / TILE_K, (N + TILE_N - 1) / TILE_N, 2u, opt.workspace_allocator);
53 if (BT.empty())
54 return -100;
55
56 const int nn_NK = nn_N * nn_K;
57
58 // pack B
59 #pragma omp parallel for num_threads(nT)
60 for (int ppjk = 0; ppjk < nn_NK; ppjk++)
61 {
62 const int ppj = ppjk / nn_K;
63 const int ppk = ppjk % nn_K;
64
65 const int j = ppj * TILE_N;
66 const int k = ppk * TILE_K;
67
68 const int max_jj = std::min((N - j), TILE_N);
69 const int max_kk = std::min((K - k), TILE_K);
70
71 Mat BT_tile = BT.channel(j / TILE_N).row_range(k / TILE_K, 1);
72
73 if (transB)
74 {
75 pack_B_tile_bf16_fp16(B, BT_tile, j, max_jj, k, max_kk);
76 }
77 else
78 {
79 transpose_pack_B_tile_bf16_fp16(B, BT_tile, j, max_jj, k, max_kk);
80 }
81 }
82
83 Mat topT;
84 if (K > TILE_K || broadcast_type_C == 3 || output_transpose)
85 {
86 topT.create(TILE_N * TILE_M, 1, nT, 4u, opt.workspace_allocator);
87 if (topT.empty())
88 return -100;
89 }
90
91 #pragma omp parallel for num_threads(nT)
92 for (int ppi = 0; ppi < nn_M; ppi++)
93 {
94 const int i = ppi * TILE_M;
95
96 // shadowed variable for less openmp task args
97 const int M = transA ? A.w : (A.dims == 3 ? A.c : A.h) * A.elempack;
98 const int K = transA ? (A.dims == 3 ? A.c : A.h) * A.elempack : A.w;
99
100 const int max_ii = std::min((M - i), TILE_M);
101
102 Mat topT_tile;
103 if (K > TILE_K || broadcast_type_C == 3 || output_transpose)
104 topT_tile = topT.channel(get_omp_thread_num());
105
106 for (int j = 0; j < N; j += TILE_N)
107 {
108 const int max_jj = std::min((N - j), TILE_N);
109
110 if (broadcast_type_C == 3)
111 {
112 pack_A_tile(C, topT_tile, i, max_ii, j, max_jj);
113 }
114
115 const Mat& CT_tile = broadcast_type_C == 3 ? topT_tile : C;
116
117 for (int k = 0; k < K; k += TILE_K)
118 {
119 const int max_kk = std::min((K - k), TILE_K);
120
121 // NCNN_LOGE("max_ii/jj/kk = %d %d %d", max_ii, max_jj, max_kk);
122
123 Mat AT_tile = ATX.channel(get_omp_thread_num()).row_range(k / TILE_K, 1);
124
125 Mat BT_tile = BT.channel(j / TILE_N).row_range(k / TILE_K, 1);
126
127 if (j == 0)
128 {
129 if (transA)
130 {
131 transpose_pack_A_tile_bf16_fp16(A, AT_tile, i, max_ii, k, max_kk);
132 }
133 else
134 {
135 pack_A_tile_bf16_fp16(A, AT_tile, i, max_ii, k, max_kk);
136 }
137 }
138
139 bool k_end = !output_transpose && k + TILE_K >= K;
140 float _alpha = k + TILE_K >= K ? alpha : 1.f;
141
142 gemm_transB_packed_tile_fp16s(AT_tile, BT_tile, CT_tile, topT_tile, top_blob, broadcast_type_C, _alpha, i, max_ii, j, max_jj, k, max_kk, k_end);
143 }
144
145 if (output_transpose)
146 {
147 transpose_unpack_output_tile_fp32_to_fp16(topT_tile, top_blob, i, max_ii, j, max_jj);
148 }
149 }
150 }
151
152 return 0;
153}
154
155static int gemm_AT_arm_fp16s(const Mat& AT, const Mat& B, const Mat& C, Mat& top_blob, int broadcast_type_C, int M, int K, int transB, int output_transpose, float alpha, int constant_TILE_M, int constant_TILE_N, int constant_TILE_K, int nT, const Option& opt)
156{
157 const int N = transB ? (B.dims == 3 ? B.c : B.h) * B.elempack : B.w;
158
159 // NCNN_LOGE("M/N/K = %d %d %d", M, N, K);
160
161 int TILE_M, TILE_N, TILE_K;
162 get_optimal_tile_mnk_bf16s_fp16s(M, N, K, constant_TILE_M, constant_TILE_N, constant_TILE_K, TILE_M, TILE_N, TILE_K, nT);
163
164 // NCNN_LOGE("TILE M/N/K = %d %d %d", TILE_M, TILE_N, TILE_K);
165
166 int nn_M = (M + TILE_M - 1) / TILE_M;
167 int nn_N = (N + TILE_N - 1) / TILE_N;
168 int nn_K = (K + TILE_K - 1) / TILE_K;
169
170 Mat BT(TILE_K * TILE_N, (K + TILE_K - 1) / TILE_K, (N + TILE_N - 1) / TILE_N, 2u, opt.workspace_allocator);
171 if (BT.empty())
172 return -100;
173
174 const int nn_NK = nn_N * nn_K;
175
176 // pack B
177 #pragma omp parallel for num_threads(nT)
178 for (int ppjk = 0; ppjk < nn_NK; ppjk++)
179 {
180 const int ppj = ppjk / nn_K;
181 const int ppk = ppjk % nn_K;
182
183 const int j = ppj * TILE_N;
184 const int k = ppk * TILE_K;
185
186 const int max_jj = std::min((N - j), TILE_N);
187 const int max_kk = std::min((K - k), TILE_K);
188
189 Mat BT_tile = BT.channel(j / TILE_N).row_range(k / TILE_K, 1);
190
191 if (transB)
192 {
193 pack_B_tile_bf16_fp16(B, BT_tile, j, max_jj, k, max_kk);
194 }
195 else
196 {
197 transpose_pack_B_tile_bf16_fp16(B, BT_tile, j, max_jj, k, max_kk);
198 }
199 }
200
201 Mat topT;
202 if (K > TILE_K || broadcast_type_C == 3 || output_transpose)
203 {
204 topT.create(TILE_N * TILE_M, 1, nT, 4u, opt.workspace_allocator);
205 if (topT.empty())
206 return -100;
207 }
208
209 #pragma omp parallel for num_threads(nT)
210 for (int ppi = 0; ppi < nn_M; ppi++)
211 {
212 const int i = ppi * TILE_M;
213
214 const int max_ii = std::min((M - i), TILE_M);
215
216 Mat topT_tile;
217 if (K > TILE_K || broadcast_type_C == 3 || output_transpose)
218 topT_tile = topT.channel(get_omp_thread_num());
219
220 for (int j = 0; j < N; j += TILE_N)
221 {
222 const int max_jj = std::min((N - j), TILE_N);
223
224 if (broadcast_type_C == 3)
225 {
226 pack_A_tile(C, topT_tile, i, max_ii, j, max_jj);
227 }
228
229 const Mat& CT_tile = broadcast_type_C == 3 ? topT_tile : C;
230
231 for (int k = 0; k < K; k += TILE_K)
232 {
233 const int max_kk = std::min((K - k), TILE_K);
234
235 // NCNN_LOGE("max_ii/jj/kk = %d %d %d", max_ii, max_jj, max_kk);
236
237 Mat AT_tile = AT.channel(i / TILE_M).row_range(k / TILE_K, 1);
238
239 Mat BT_tile = BT.channel(j / TILE_N).row_range(k / TILE_K, 1);
240
241 bool k_end = !output_transpose && k + TILE_K >= K;
242 float _alpha = k + TILE_K >= K ? alpha : 1.f;
243
244 gemm_transB_packed_tile_fp16s(AT_tile, BT_tile, CT_tile, topT_tile, top_blob, broadcast_type_C, _alpha, i, max_ii, j, max_jj, k, max_kk, k_end);
245 }
246
247 if (output_transpose)
248 {
249 transpose_unpack_output_tile_fp32_to_fp16(topT_tile, top_blob, i, max_ii, j, max_jj);
250 }
251 }
252 }
253
254 return 0;
255}
256
257static int gemm_BT_arm_fp16s(const Mat& A, const Mat& BT, const Mat& C, Mat& top_blob, int broadcast_type_C, int N, int K, int transA, int output_transpose, float alpha, int constant_TILE_M, int constant_TILE_N, int constant_TILE_K, int nT, const Option& opt)
258{
259 const int M = transA ? A.w : (A.dims == 3 ? A.c : A.h) * A.elempack;
260
261 // NCNN_LOGE("M/N/K = %d %d %d", M, N, K);
262
263 int TILE_M, TILE_N, TILE_K;
264 get_optimal_tile_mnk_bf16s_fp16s(M, N, K, constant_TILE_M, constant_TILE_N, constant_TILE_K, TILE_M, TILE_N, TILE_K, nT);
265
266 // NCNN_LOGE("TILE M/N/K = %d %d %d", TILE_M, TILE_N, TILE_K);
267
268 int nn_M = (M + TILE_M - 1) / TILE_M;
269 // int nn_N = (N + TILE_N - 1) / TILE_N;
270
271 Mat ATX(TILE_K * TILE_M, (K + TILE_K - 1) / TILE_K, nT, 2u, opt.workspace_allocator);
272 if (ATX.empty())
273 return -100;
274
275 Mat topT;
276 if (K > TILE_K || broadcast_type_C == 3 || output_transpose)
277 {
278 topT.create(TILE_N * TILE_M, 1, nT, 4u, opt.workspace_allocator);
279 if (topT.empty())
280 return -100;
281 }
282
283 #pragma omp parallel for num_threads(nT)
284 for (int ppi = 0; ppi < nn_M; ppi++)
285 {
286 const int i = ppi * TILE_M;
287
288 // shadowed variable for less openmp task args
289 const int M = transA ? A.w : (A.dims == 3 ? A.c : A.h) * A.elempack;
290 const int K = transA ? (A.dims == 3 ? A.c : A.h) * A.elempack : A.w;
291
292 const int max_ii = std::min((M - i), TILE_M);
293
294 Mat topT_tile;
295 if (K > TILE_K || broadcast_type_C == 3 || output_transpose)
296 topT_tile = topT.channel(get_omp_thread_num());
297
298 for (int j = 0; j < N; j += TILE_N)
299 {
300 const int max_jj = std::min((N - j), TILE_N);
301
302 if (broadcast_type_C == 3)
303 {
304 pack_A_tile(C, topT_tile, i, max_ii, j, max_jj);
305 }
306
307 const Mat& CT_tile = broadcast_type_C == 3 ? topT_tile : C;
308
309 for (int k = 0; k < K; k += TILE_K)
310 {
311 const int max_kk = std::min((K - k), TILE_K);
312
313 // NCNN_LOGE("max_ii/jj/kk = %d %d %d", max_ii, max_jj, max_kk);
314
315 Mat AT_tile = ATX.channel(get_omp_thread_num()).row_range(k / TILE_K, 1);
316
317 Mat BT_tile = BT.channel(j / TILE_N).row_range(k / TILE_K, 1);
318
319 if (j == 0)
320 {
321 if (transA)
322 {
323 transpose_pack_A_tile_bf16_fp16(A, AT_tile, i, max_ii, k, max_kk);
324 }
325 else
326 {
327 pack_A_tile_bf16_fp16(A, AT_tile, i, max_ii, k, max_kk);
328 }
329 }
330
331 bool k_end = !output_transpose && k + TILE_K >= K;
332 float _alpha = k + TILE_K >= K ? alpha : 1.f;
333
334 gemm_transB_packed_tile_fp16s(AT_tile, BT_tile, CT_tile, topT_tile, top_blob, broadcast_type_C, _alpha, i, max_ii, j, max_jj, k, max_kk, k_end);
335 }
336
337 if (output_transpose)
338 {
339 transpose_unpack_output_tile_fp32_to_fp16(topT_tile, top_blob, i, max_ii, j, max_jj);
340 }
341 }
342 }
343
344 return 0;
345}
346
347static int gemm_AT_BT_arm_fp16s(const Mat& AT, const Mat& BT, const Mat& C, Mat& top_blob, int broadcast_type_C, int M, int N, int K, int output_transpose, float alpha, int constant_TILE_M, int constant_TILE_N, int constant_TILE_K, int nT, const Option& opt)
348{
349 // NCNN_LOGE("M/N/K = %d %d %d", M, N, K);
350
351 int TILE_M, TILE_N, TILE_K;
352 get_optimal_tile_mnk_bf16s_fp16s(M, N, K, constant_TILE_M, constant_TILE_N, constant_TILE_K, TILE_M, TILE_N, TILE_K, nT);
353
354 // NCNN_LOGE("TILE M/N/K = %d %d %d", TILE_M, TILE_N, TILE_K);
355
356 int nn_M = (M + TILE_M - 1) / TILE_M;
357 // int nn_N = (N + TILE_N - 1) / TILE_N;
358
359 Mat topT;
360 if (K > TILE_K || broadcast_type_C == 3 || output_transpose)
361 {
362 topT.create(TILE_N * TILE_M, 1, nT, 4u, opt.workspace_allocator);
363 if (topT.empty())
364 return -100;
365 }
366
367 #pragma omp parallel for num_threads(nT)
368 for (int ppi = 0; ppi < nn_M; ppi++)
369 {
370 const int i = ppi * TILE_M;
371
372 const int max_ii = std::min((M - i), TILE_M);
373
374 Mat topT_tile;
375 if (K > TILE_K || broadcast_type_C == 3 || output_transpose)
376 topT_tile = topT.channel(get_omp_thread_num());
377
378 for (int j = 0; j < N; j += TILE_N)
379 {
380 const int max_jj = std::min((N - j), TILE_N);
381
382 if (broadcast_type_C == 3)
383 {
384 pack_A_tile(C, topT_tile, i, max_ii, j, max_jj);
385 }
386
387 const Mat& CT_tile = broadcast_type_C == 3 ? topT_tile : C;
388
389 for (int k = 0; k < K; k += TILE_K)
390 {
391 const int max_kk = std::min((K - k), TILE_K);
392
393 // NCNN_LOGE("max_ii/jj/kk = %d %d %d", max_ii, max_jj, max_kk);
394
395 Mat AT_tile = AT.channel(i / TILE_M).row_range(k / TILE_K, 1);
396
397 Mat BT_tile = BT.channel(j / TILE_N).row_range(k / TILE_K, 1);
398
399 bool k_end = !output_transpose && k + TILE_K >= K;
400 float _alpha = k + TILE_K >= K ? alpha : 1.f;
401
402 gemm_transB_packed_tile_fp16s(AT_tile, BT_tile, CT_tile, topT_tile, top_blob, broadcast_type_C, _alpha, i, max_ii, j, max_jj, k, max_kk, k_end);
403 }
404
405 if (output_transpose)
406 {
407 transpose_unpack_output_tile_fp32_to_fp16(topT_tile, top_blob, i, max_ii, j, max_jj);
408 }
409 }
410 }
411
412 return 0;
413}
414
415int Gemm_arm::create_pipeline_fp16s(const Option& opt)
416{
417 if (constantA)
418 {
419 const int M = constantM;
420 const int K = constantK;
421
422 int TILE_M, TILE_N, TILE_K;
423 get_optimal_tile_mnk_bf16s_fp16s(M, 0, K, constant_TILE_M, constant_TILE_N, constant_TILE_K, TILE_M, TILE_N, TILE_K, opt.num_threads);
424
425 const int nn_M = (M + TILE_M - 1) / TILE_M;
426
427 AT_data.create(TILE_K * TILE_M, (K + TILE_K - 1) / TILE_K, (M + TILE_M - 1) / TILE_M, 2u, (Allocator*)0);
428 if (AT_data.empty())
429 return -100;
430
431 #pragma omp parallel for num_threads(opt.num_threads)
432 for (int ppj = 0; ppj < nn_M; ppj++)
433 {
434 const int i = ppj * TILE_M;
435
436 for (int k = 0; k < K; k += TILE_K)
437 {
438 const int max_ii = std::min((M - i), TILE_M);
439 const int max_kk = std::min((K - k), TILE_K);
440
441 Mat AT_tile = AT_data.channel(i / TILE_M).row_range(k / TILE_K, 1);
442
443 if (transA)
444 {
445 transpose_pack_A_tile_fp32_to_fp16(A_data, AT_tile, i, max_ii, k, max_kk);
446 }
447 else
448 {
449 pack_A_tile_fp32_to_fp16(A_data, AT_tile, i, max_ii, k, max_kk);
450 }
451 }
452 }
453
454 if (opt.lightmode)
455 A_data.release();
456 }
457
458 if (constantB)
459 {
460 const int N = constantN;
461 const int K = constantK;
462
463 int TILE_M, TILE_N, TILE_K;
464 get_optimal_tile_mnk_bf16s_fp16s(0, N, K, constant_TILE_M, constant_TILE_N, constant_TILE_K, TILE_M, TILE_N, TILE_K, opt.num_threads);
465
466 const int nn_N = (N + TILE_N - 1) / TILE_N;
467
468 BT_data.create(TILE_K * TILE_N, (K + TILE_K - 1) / TILE_K, (N + TILE_N - 1) / TILE_N, 2u, (Allocator*)0);
469 if (BT_data.empty())
470 return -100;
471
472 #pragma omp parallel for num_threads(opt.num_threads)
473 for (int ppj = 0; ppj < nn_N; ppj++)
474 {
475 const int j = ppj * TILE_N;
476
477 for (int k = 0; k < K; k += TILE_K)
478 {
479 const int max_jj = std::min((N - j), TILE_N);
480 const int max_kk = std::min((K - k), TILE_K);
481
482 Mat BT_tile = BT_data.channel(j / TILE_N).row_range(k / TILE_K, 1);
483
484 if (transB)
485 {
486 pack_B_tile_fp32_to_fp16(B_data, BT_tile, j, max_jj, k, max_kk);
487 }
488 else
489 {
490 transpose_pack_B_tile_fp32_to_fp16(B_data, BT_tile, j, max_jj, k, max_kk);
491 }
492 }
493 }
494
495 if (opt.lightmode)
496 B_data.release();
497 }
498
499 if (constantC && constant_broadcast_type_C != -1)
500 {
501 CT_data = C_data;
502
503#if __ARM_NEON
504 if (constant_broadcast_type_C == 3 && opt.use_packing_layout)
505 {
506 int C_elempack = constantM % 4 == 0 ? 4 : 1;
507 convert_packing(C_data, CT_data, C_elempack, opt);
508 }
509#endif // __ARM_NEON
510
511 // pre-multiply C with beta
512 if (beta != 1.f)
513 {
514 Mat C2;
515 C2.create_like(CT_data);
516
517 const int size = CT_data.total() * CT_data.elempack;
518 for (int i = 0; i < size; i++)
519 {
520 C2[i] = CT_data[i] * beta;
521 }
522
523 CT_data = C2;
524 }
525
526 if (opt.lightmode)
527 C_data.release();
528 }
529
530 if (constantA || constantB || constantC)
531 {
532 nT = opt.num_threads;
533 }
534
535 return 0;
536}
537
538int Gemm_arm::forward_fp16s(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
539{
540 int M;
541 int N;
542 if (constantA && constantB)
543 {
544 M = constantM;
545 N = constantN;
546 }
547 else if (constantA)
548 {
549 const Mat& B = bottom_blobs[0];
550 M = constantM;
551 N = transB ? (B.dims == 3 ? B.c : B.h) * B.elempack : B.w;
552 }
553 else if (constantB)
554 {
555 const Mat& A = bottom_blobs[0];
556 M = transA ? A.w : (A.dims == 3 ? A.c : A.h) * A.elempack;
557 N = constantN;
558 }
559 else
560 {
561 const Mat& A = bottom_blobs[0];
562 const Mat& B = bottom_blobs[1];
563 M = transA ? A.w : (A.dims == 3 ? A.c : A.h) * A.elempack;
564 N = transB ? (B.dims == 3 ? B.c : B.h) * B.elempack : B.w;
565 }
566
567 Mat C;
568 int broadcast_type_C = 0;
569 if (constantC)
570 {
571 C = CT_data;
572 broadcast_type_C = constant_broadcast_type_C;
573 }
574 else
575 {
576 if (constantA && constantB)
577 {
578 C = bottom_blobs.size() == 1 ? bottom_blobs[0] : Mat();
579 }
580 else if (constantA)
581 {
582 C = bottom_blobs.size() == 2 ? bottom_blobs[1] : Mat();
583 }
584 else if (constantB)
585 {
586 C = bottom_blobs.size() == 2 ? bottom_blobs[1] : Mat();
587 }
588 else
589 {
590 C = bottom_blobs.size() == 3 ? bottom_blobs[2] : Mat();
591 }
592
593 if (!C.empty())
594 {
595 if (C.dims == 1 && C.w == 1)
596 {
597 // scalar
598 broadcast_type_C = 0;
599 }
600 if (C.dims == 1 && C.w * C.elempack == M)
601 {
602 // M
603 // auto broadcast from h to w is the ncnn-style convention
604 broadcast_type_C = 1;
605 }
606 if (C.dims == 1 && C.w * C.elempack == N)
607 {
608 // N
609 broadcast_type_C = 4;
610 }
611 if (C.dims == 2 && C.w == 1 && C.h * C.elempack == M)
612 {
613 // Mx1
614 broadcast_type_C = 2;
615 }
616 if (C.dims == 2 && C.w == N && C.h * C.elempack == M)
617 {
618 // MxN
619 broadcast_type_C = 3;
620 }
621 if (C.dims == 2 && C.w == N && C.h * C.elempack == 1)
622 {
623 // 1xN
624 broadcast_type_C = 4;
625 }
626
627 // cast to fp32
628 {
629 Mat CT_data;
630 cast_float16_to_float32(C, CT_data);
631 C = CT_data;
632 }
633
634 // pre-multiply C with beta
635 if (beta != 1.f)
636 {
637 Mat CT_data;
638 CT_data.create_like(C, opt.workspace_allocator);
639
640 const int size = C.total() * C.elempack;
641 for (int i = 0; i < size; i++)
642 {
643 CT_data[i] = C[i] * beta;
644 }
645
646 C = CT_data;
647 }
648 }
649 }
650
651 int out_elempack = 1;
652 if (opt.use_packing_layout)
653 {
654 int outh = output_transpose ? N : M;
655 out_elempack = outh % 4 == 0 ? 4 : 1;
656 }
657 if (output_elempack)
658 out_elempack = output_elempack;
659 size_t out_elemsize = 2u * out_elempack;
660
661 Mat& top_blob = top_blobs[0];
662 if (output_transpose)
663 {
664 if (output_N1M)
665 top_blob.create(M, 1, N / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
666 else
667 top_blob.create(M, N / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
668 }
669 else
670 {
671 if (output_N1M)
672 top_blob.create(N, 1, M / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
673 else
674 top_blob.create(N, M / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
675 }
676 if (top_blob.empty())
677 return -100;
678
679 int _nT = nT ? nT : opt.num_threads;
680 if (nT != 0 && opt.num_threads != nT)
681 {
682 // force num_threads the same as in create_pipeline
683 // so we could use pre-packed A/B from the same tile config
684 NCNN_LOGE("opt.num_threads %d changed, gemm will use load-time value %d", opt.num_threads, nT);
685 }
686
687 int ret = 0;
688 if (constantA && constantB)
689 {
690 ret = gemm_AT_BT_arm_fp16s(AT_data, BT_data, C, top_blob, broadcast_type_C, constantM, constantN, constantK, output_transpose, alpha, constant_TILE_M, constant_TILE_N, constant_TILE_K, _nT, opt);
691 }
692 else if (constantA)
693 {
694 const Mat& B = bottom_blobs[0];
695 ret = gemm_AT_arm_fp16s(AT_data, B, C, top_blob, broadcast_type_C, constantM, constantK, transB, output_transpose, alpha, constant_TILE_M, constant_TILE_N, constant_TILE_K, _nT, opt);
696 }
697 else if (constantB)
698 {
699 const Mat& A = bottom_blobs[0];
700 ret = gemm_BT_arm_fp16s(A, BT_data, C, top_blob, broadcast_type_C, constantN, constantK, transA, output_transpose, alpha, constant_TILE_M, constant_TILE_N, constant_TILE_K, _nT, opt);
701 }
702 else
703 {
704 const Mat& A = bottom_blobs[0];
705 const Mat& B = bottom_blobs[1];
706 ret = gemm_arm_fp16s(A, B, C, top_blob, broadcast_type_C, transA, transB, output_transpose, alpha, constant_TILE_M, constant_TILE_N, constant_TILE_K, _nT, opt);
707 }
708
709 return ret;
710}
711
712} // namespace ncnn