interplane.Get Omega
Read in your language

ggml

[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)

Brought by drakestapleton

branch master Branches Tags History

ggml / src / ggml-opencl / kernels / mul_mv_f16_f32_l4.cl

1237 lines · 37917 bytes Raw Blame History
1#pragma OPENCL EXTENSION cl_khr_fp16 : enable
2
3#ifdef cl_intel_subgroups
4#pragma OPENCL EXTENSION cl_intel_subgroups : enable
5#else
6#pragma OPENCL EXTENSION cl_khr_subgroups : enable
7#endif
8
9#ifdef cl_intel_required_subgroup_size
10#pragma OPENCL EXTENSION cl_intel_required_subgroup_size : enable
11#define INTEL_GPU 1
12#define REQD_SUBGROUP_SIZE_16 __attribute__((intel_reqd_sub_group_size(16)))
13#define REQD_SUBGROUP_SIZE_32 __attribute__((intel_reqd_sub_group_size(32)))
14#elif defined(cl_qcom_reqd_sub_group_size)
15#pragma OPENCL EXTENSION cl_qcom_reqd_sub_group_size : enable
16#define ADRENO_GPU 1
17#define REQD_SUBGROUP_SIZE_64 __attribute__((qcom_reqd_sub_group_size("half")))
18#define REQD_SUBGROUP_SIZE_128 __attribute__((qcom_reqd_sub_group_size("full")))
19#endif
20
21#ifdef cl_khr_subgroup_shuffle
22#pragma OPENCL EXTENSION cl_khr_subgroup_shuffle : enable
23#define HAS_SUBGROUP_SHUFFLE 1
24#elif defined(cl_qcom_subgroup_shuffle)
25#pragma OPENCL EXTENSION cl_qcom_subgroup_shuffle : enable
26#define HAS_SUBGROUP_SHUFFLE 1
27// Adreno compilers that expose only cl_qcom_subgroup_shuffle do not declare the KHR
28// name, so calling it is an implicit declaration and the program fails to build.
29// Route it to the qcom builtin.
30#define sub_group_shuffle_xor(val, mask) qcom_sub_group_shuffle_xor((val), (mask), CLK_SUB_GROUP_SHUFFLE_WIDTH_WAVE_SIZE_QCOM, 0.0f)
31#endif
32
33// Assumes row size (ne00) is a multiple of 4
34#ifdef ADRENO_GPU
35REQD_SUBGROUP_SIZE_64
36#endif
37kernel void kernel_mul_mat_f16_f32_l4(
38 global char * src0,
39 ulong offset0,
40 global char * src1,
41 ulong offset1,
42 global float * dst,
43 ulong offsetd,
44 int ne00,
45 int ne01,
46 int ne02,
47 ulong nb00,
48 ulong nb01,
49 ulong nb02,
50 ulong nb03,
51 int ne10,
52 int ne11,
53 int ne12,
54 ulong nb10,
55 ulong nb11,
56 ulong nb12,
57 ulong nb13,
58 int ne0,
59 int ne1,
60 int r2,
61 int r3
62) {
63 src0 = (global char*)((global char*)src0 + offset0);
64 src1 = (global char*)((global char*)src1 + offset1);
65 dst = (global float*)((global char*)dst + offsetd);
66
67 int nrows = ne11;
68 int r0 = get_group_id(0);
69 int im = get_group_id(2);
70
71 int i12 = im%ne12;
72 int i13 = im/ne12;
73
74 ulong offset_src0 = r0*nb01 + (i12/r2)*nb02 + (i13/r3)*nb03;
75
76 global half4 * x4 = (global half4 *) (src0 + offset_src0);
77
78 for (int r1 = 0; r1 < nrows; ++r1) {
79 ulong offset_src1 = r1*nb11 + (i12 )*nb12 + (i13 )*nb13;
80
81 global float4 * y4 = (global float4 *) (src1 + offset_src1);
82
83 float sumf = 0;
84 for (int i = get_sub_group_local_id(); i < ne00/4; i += get_max_sub_group_size()) {
85 sumf += convert_float(x4[i].s0) * y4[i].s0;
86 sumf += convert_float(x4[i].s1) * y4[i].s1;
87 sumf += convert_float(x4[i].s2) * y4[i].s2;
88 sumf += convert_float(x4[i].s3) * y4[i].s3;
89 }
90
91 float all_sum = sub_group_reduce_add(sumf);
92 if (get_sub_group_local_id() == 0) {
93 dst[im*ne1*ne0 + r1*ne0 + r0] = all_sum;
94 }
95 }
96}
97
98// Each subgroup produces DR_NDST outputs, assumes ne11 == 1
99#define MUL_MAT_F16_F32_L4_DR_NDST 4
100
101#ifdef ADRENO_GPU
102REQD_SUBGROUP_SIZE_64
103#endif
104kernel void kernel_mul_mat_f16_f32_l4_dr(
105 global char * src0,
106 ulong offset0,
107 global char * src1,
108 ulong offset1,
109 global float * dst,
110 ulong offsetd,
111 int ne00,
112 int ne01,
113 int ne02,
114 ulong nb00,
115 ulong nb01,
116 ulong nb02,
117 ulong nb03,
118 int ne10,
119 int ne11,
120 int ne12,
121 ulong nb10,
122 ulong nb11,
123 ulong nb12,
124 ulong nb13,
125 int ne0,
126 int ne1,
127 int r2,
128 int r3
129) {
130 src0 = (global char*)((global char*)src0 + offset0);
131 src1 = (global char*)((global char*)src1 + offset1);
132 dst = (global float*)((global char*)dst + offsetd);
133
134 const int r0_base = get_group_id(0) * MUL_MAT_F16_F32_L4_DR_NDST;
135 const int im = get_group_id(2);
136
137 const int i12 = im % ne12;
138 const int i13 = im / ne12;
139
140 // assume ne11 == 1
141 const ulong offset_src1 = i12*nb12 + i13*nb13;
142 global float4 * y4 = (global float4 *)(src1 + offset_src1);
143
144 global half4 * x4[MUL_MAT_F16_F32_L4_DR_NDST];
145 float sumf[MUL_MAT_F16_F32_L4_DR_NDST];
146
147 const ulong k_head_off = (i12/r2)*nb02 + (i13/r3)*nb03;
148
149 #pragma unroll
150 for (int n = 0; n < MUL_MAT_F16_F32_L4_DR_NDST; ++n) {
151 int r0 = r0_base + n;
152 int r0c = r0 < ne01 ? r0 : 0;
153 ulong off = (ulong)r0c*nb01 + k_head_off;
154 x4[n] = (global half4 *)(src0 + off);
155 sumf[n] = 0.0f;
156 }
157
158 const int n_chunks = ne00 / 4;
159 const int sg_size = get_max_sub_group_size();
160 const int lid = get_sub_group_local_id();
161
162 for (int i = lid; i < n_chunks; i += sg_size) {
163 float4 q = y4[i];
164 #pragma unroll
165 for (int n = 0; n < MUL_MAT_F16_F32_L4_DR_NDST; ++n) {
166 float4 k = convert_float4(x4[n][i]);
167 sumf[n] = mad(k.s0, q.s0, sumf[n]);
168 sumf[n] = mad(k.s1, q.s1, sumf[n]);
169 sumf[n] = mad(k.s2, q.s2, sumf[n]);
170 sumf[n] = mad(k.s3, q.s3, sumf[n]);
171 }
172 }
173
174 #pragma unroll
175 for (int n = 0; n < MUL_MAT_F16_F32_L4_DR_NDST; ++n) {
176 float reduced = sub_group_reduce_add(sumf[n]);
177 int r0 = r0_base + n;
178 if (lid == 0 && r0 < ne01) {
179 dst[im*ne1*ne0 + r0] = reduced;
180 }
181 }
182}
183
184// Kernels for decoding, Adreno only for now
185#define MUL_MAT_F16_F32_L4_DR_LS_R2_MAX 8
186
187#ifdef ADRENO_GPU
188#pragma OPENCL EXTENSION cl_qcom_subgroup_shuffle : enable
189#define sub_group_shuffle_xor(val, mask) qcom_sub_group_shuffle_xor((val), (mask), CLK_SUB_GROUP_SHUFFLE_WIDTH_WAVE_SIZE_QCOM, 0.0f)
190
191REQD_SUBGROUP_SIZE_64
192kernel void kernel_mul_mat_f16_f32_l4_dr_ls(
193 global char * src0,
194 ulong offset0,
195 global char * src1,
196 ulong offset1,
197 global float * dst,
198 ulong offsetd,
199 int ne00,
200 int ne01,
201 int ne02,
202 ulong nb00,
203 ulong nb01,
204 ulong nb02,
205 ulong nb03,
206 int ne10,
207 int ne11,
208 int ne12,
209 ulong nb10,
210 ulong nb11,
211 ulong nb12,
212 ulong nb13,
213 int ne0,
214 int ne1,
215 int r2,
216 int r3
217) {
218 src0 = (global char*)((global char*)src0 + offset0);
219 src1 = (global char*)((global char*)src1 + offset1);
220 dst = (global float*)((global char*)dst + offsetd);
221
222 const int r0_base = get_group_id(0) * 2;
223 const int kv_grp = get_group_id(2); // KV head group; im = kv_grp*r2 + q
224
225 const int i12_kv = kv_grp % ne02;
226 const int i13_kv = kv_grp / ne02;
227
228 const int lid = get_sub_group_local_id();
229 const int subhalf = lid >> 5; // 0 or 1 (which K row in the WG)
230 const int intra = lid & 31; // 0..31 (lane within the half)
231
232 const int r0 = r0_base + subhalf;
233 const int r0c = r0 < ne01 ? r0 : 0; // clamp OOB to row 0; skip write below
234
235 // K row pointer for this lane (one K row per half-wave).
236 const ulong k_off = (ulong)r0c*nb01 + (ulong)i12_kv*nb02 + (ulong)i13_kv*nb03;
237 global half4 * x4 = (global half4 *)(src0 + k_off);
238
239 global float4 * y4[MUL_MAT_F16_F32_L4_DR_LS_R2_MAX];
240 #pragma unroll
241 for (int q = 0; q < MUL_MAT_F16_F32_L4_DR_LS_R2_MAX; ++q) {
242 const int i12_q = i12_kv*r2 + q;
243 const ulong q_off = (ulong)i12_q*nb12 + (ulong)i13_kv*nb13;
244 y4[q] = (global float4 *)(src1 + q_off);
245 }
246
247 float partial[MUL_MAT_F16_F32_L4_DR_LS_R2_MAX];
248 #pragma unroll
249 for (int q = 0; q < MUL_MAT_F16_F32_L4_DR_LS_R2_MAX; ++q) {
250 partial[q] = 0.0f;
251 }
252
253 const int n_chunks = ne00 / 4;
254
255 for (int i = intra; i < n_chunks; i += 32) {
256 float4 k = convert_float4(x4[i]);
257
258 #pragma unroll
259 for (int q = 0; q < MUL_MAT_F16_F32_L4_DR_LS_R2_MAX; ++q) {
260 if (q < r2) {
261 float4 v = y4[q][i];
262 partial[q] = mad(k.s0, v.s0, partial[q]);
263 partial[q] = mad(k.s1, v.s1, partial[q]);
264 partial[q] = mad(k.s2, v.s2, partial[q]);
265 partial[q] = mad(k.s3, v.s3, partial[q]);
266 }
267 }
268 }
269
270 // half-wave reduction
271 #pragma unroll
272 for (int q = 0; q < MUL_MAT_F16_F32_L4_DR_LS_R2_MAX; ++q) {
273 if (q < r2) {
274 partial[q] += sub_group_shuffle_xor(partial[q], 1u);
275 partial[q] += sub_group_shuffle_xor(partial[q], 2u);
276 partial[q] += sub_group_shuffle_xor(partial[q], 4u);
277 partial[q] += sub_group_shuffle_xor(partial[q], 8u);
278 partial[q] += sub_group_shuffle_xor(partial[q], 16u);
279 }
280 }
281
282 if (intra == 0 && r0 < ne01) {
283 #pragma unroll
284 for (int q = 0; q < MUL_MAT_F16_F32_L4_DR_LS_R2_MAX; ++q) {
285 if (q < r2) {
286 const int im = i12_kv*r2 + q + i13_kv*ne12;
287 dst[im*ne1*ne0 + r0] = partial[q];
288 }
289 }
290 }
291}
292
293REQD_SUBGROUP_SIZE_64
294kernel void kernel_mul_mat_f16_f32_l4_dr_lq(
295 global char * src0,
296 ulong offset0,
297 global char * src1,
298 ulong offset1,
299 global float * dst,
300 ulong offsetd,
301 int ne00,
302 int ne01,
303 int ne02,
304 ulong nb00,
305 ulong nb01,
306 ulong nb02,
307 ulong nb03,
308 int ne10,
309 int ne11,
310 int ne12,
311 ulong nb10,
312 ulong nb11,
313 ulong nb12,
314 ulong nb13,
315 int ne0,
316 int ne1,
317 int r2,
318 int r3
319) {
320 src0 = (global char*)((global char*)src0 + offset0);
321 src1 = (global char*)((global char*)src1 + offset1);
322 dst = (global float*)((global char*)dst + offsetd);
323
324 const int r0_base = get_group_id(0) * 4;
325 const int kv_grp = get_group_id(2);
326
327 const int i12_kv = kv_grp % ne02;
328 const int i13_kv = kv_grp / ne02;
329
330 const int lid = get_sub_group_local_id();
331 const int subq = lid >> 4; // 0..3 (which K row)
332 const int intra = lid & 15; // 0..15 (lane within quarter)
333
334 const int r0 = r0_base + subq;
335 const int r0c = r0 < ne01 ? r0 : 0;
336
337 const ulong k_off = (ulong)r0c*nb01 + (ulong)i12_kv*nb02 + (ulong)i13_kv*nb03;
338 global half4 * x4 = (global half4 *)(src0 + k_off);
339
340 global float4 * y4[MUL_MAT_F16_F32_L4_DR_LS_R2_MAX];
341 #pragma unroll
342 for (int q = 0; q < MUL_MAT_F16_F32_L4_DR_LS_R2_MAX; ++q) {
343 const int i12_q = i12_kv*r2 + q;
344 const ulong q_off = (ulong)i12_q*nb12 + (ulong)i13_kv*nb13;
345 y4[q] = (global float4 *)(src1 + q_off);
346 }
347
348 float partial[MUL_MAT_F16_F32_L4_DR_LS_R2_MAX];
349 #pragma unroll
350 for (int q = 0; q < MUL_MAT_F16_F32_L4_DR_LS_R2_MAX; ++q) {
351 partial[q] = 0.0f;
352 }
353
354 const int n_chunks = ne00 / 4;
355
356 for (int i = intra; i < n_chunks; i += 16) {
357 float4 k = convert_float4(x4[i]);
358
359 #pragma unroll
360 for (int q = 0; q < MUL_MAT_F16_F32_L4_DR_LS_R2_MAX; ++q) {
361 if (q < r2) {
362 float4 v = y4[q][i];
363 partial[q] = mad(k.s0, v.s0, partial[q]);
364 partial[q] = mad(k.s1, v.s1, partial[q]);
365 partial[q] = mad(k.s2, v.s2, partial[q]);
366 partial[q] = mad(k.s3, v.s3, partial[q]);
367 }
368 }
369 }
370
371 // quarter-wave reduction
372 #pragma unroll
373 for (int q = 0; q < MUL_MAT_F16_F32_L4_DR_LS_R2_MAX; ++q) {
374 if (q < r2) {
375 partial[q] += sub_group_shuffle_xor(partial[q], 1u);
376 partial[q] += sub_group_shuffle_xor(partial[q], 2u);
377 partial[q] += sub_group_shuffle_xor(partial[q], 4u);
378 partial[q] += sub_group_shuffle_xor(partial[q], 8u);
379 }
380 }
381
382 if (intra == 0 && r0 < ne01) {
383 #pragma unroll
384 for (int q = 0; q < MUL_MAT_F16_F32_L4_DR_LS_R2_MAX; ++q) {
385 if (q < r2) {
386 const int im = i12_kv*r2 + q + i13_kv*ne12;
387 dst[im*ne1*ne0 + r0] = partial[q];
388 }
389 }
390 }
391}
392#endif // ADRENO_GPU
393
394#define N_ROWS_PER_WG 8
395#define N_OUTS_PER_WG 8
396
397#ifdef ADRENO_GPU
398REQD_SUBGROUP_SIZE_64
399#endif
400kernel void kernel_mul_mat_f16_f32_l4_x8(
401 global char * src0,
402 ulong offset0,
403 global char * src1,
404 ulong offset1,
405 global float * dst,
406 ulong offsetd,
407 int ne00,
408 int ne01,
409 int ne02,
410 ulong nb00,
411 ulong nb01,
412 ulong nb02,
413 ulong nb03,
414 int ne10,
415 int ne11,
416 int ne12,
417 ulong nb10,
418 ulong nb11,
419 ulong nb12,
420 ulong nb13,
421 int ne0,
422 int ne1,
423 int r2,
424 int r3
425) {
426 src0 = (global char *)((global char *)src0 + offset0);
427 src1 = (global char *)((global char *)src1 + offset1);
428 dst = (global float*)((global char *)dst + offsetd);
429
430 const int sgs_lid = get_sub_group_local_id();
431 const int sgs_sz = get_max_sub_group_size();
432
433 const int r0_base = get_group_id(0) * N_ROWS_PER_WG;
434 const int im = get_group_id(2);
435
436 const int i12 = im % ne12;
437 const int i13 = im / ne12;
438
439 const ulong offset_src1 = (i12) * nb12 + (i13) * nb13;
440 global float4 * y4 = (global float4 *)(src1 + offset_src1);
441
442 __local float4 q_loc[64]; // ne00/4 max for sub_group_size 64
443 if (sgs_lid < ne00 / 4) {
444 q_loc[sgs_lid] = y4[sgs_lid];
445 }
446 barrier(CLK_LOCAL_MEM_FENCE);
447
448 #pragma unroll
449 for (int dr = 0; dr < N_ROWS_PER_WG; ++dr) {
450 const int r0 = r0_base + dr;
451 if (r0 >= ne01) return;
452
453 const ulong offset_src0 = r0 * nb01 + (i12 / r2) * nb02 + (i13 / r3) * nb03;
454 global half4 * x4 = (global half4 *)(src0 + offset_src0);
455
456 float sumf = 0.0f;
457 for (int i = sgs_lid; i < ne00 / 4; i += sgs_sz) {
458 const half4 k4 = x4[i];
459 const float4 q = q_loc[i];
460 sumf += convert_float(k4.s0) * q.s0
461 + convert_float(k4.s1) * q.s1
462 + convert_float(k4.s2) * q.s2
463 + convert_float(k4.s3) * q.s3;
464 }
465
466 const float all_sum = sub_group_reduce_add(sumf);
467 if (sgs_lid == 0) {
468 dst[im * ne1 * ne0 + r0] = all_sum; // ne11 == 1, so r1==0
469 }
470 }
471}
472
473#ifdef ADRENO_GPU
474REQD_SUBGROUP_SIZE_64
475#endif
476kernel void kernel_mul_mat_f16_f32_l4_y8(
477 global char * src0,
478 ulong offset0,
479 global char * src1,
480 ulong offset1,
481 global float * dst,
482 ulong offsetd,
483 int ne00,
484 int ne01,
485 int ne02,
486 ulong nb00,
487 ulong nb01,
488 ulong nb02,
489 ulong nb03,
490 int ne10,
491 int ne11,
492 int ne12,
493 ulong nb10,
494 ulong nb11,
495 ulong nb12,
496 ulong nb13,
497 int ne0,
498 int ne1,
499 int r2,
500 int r3
501) {
502 src0 = (global char *)((global char *)src0 + offset0);
503 src1 = (global char *)((global char *)src1 + offset1);
504 dst = (global float*)((global char *)dst + offsetd);
505
506 const int sgs_lid = get_sub_group_local_id();
507 const int sgs_sz = get_max_sub_group_size();
508
509 const int r0_base = get_group_id(0) * N_OUTS_PER_WG;
510 const int im = get_group_id(2);
511
512 const int i12 = im % ne12;
513 const int i13 = im / ne12;
514
515 const ulong offset_src1 = (i12) * nb12 + (i13) * nb13;
516 global float4 * y4 = (global float4 *)(src1 + offset_src1);
517
518 global half4 * x4_o[N_OUTS_PER_WG];
519 #pragma unroll
520 for (int o = 0; o < N_OUTS_PER_WG; ++o) {
521 const int r0 = r0_base + o;
522 const int r0c = (r0 < ne01) ? r0 : 0;
523 const ulong off = r0c * nb01 + (i12 / r2) * nb02 + (i13 / r3) * nb03;
524 x4_o[o] = (global half4 *)(src0 + off);
525 }
526
527 float sum[N_OUTS_PER_WG] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
528
529 for (int i = sgs_lid; i < ne00 / 4; i += sgs_sz) {
530 const float4 q4 = y4[i];
531 #pragma unroll
532 for (int o = 0; o < N_OUTS_PER_WG; ++o) {
533 const half4 v4 = x4_o[o][i];
534 sum[o] += convert_float(v4.s0) * q4.s0
535 + convert_float(v4.s1) * q4.s1
536 + convert_float(v4.s2) * q4.s2
537 + convert_float(v4.s3) * q4.s3;
538 }
539 }
540
541 #pragma unroll
542 for (int o = 0; o < N_OUTS_PER_WG; ++o) {
543 const int r0 = r0_base + o;
544 const float s = sub_group_reduce_add(sum[o]);
545 if (sgs_lid == 0 && r0 < ne01) {
546 dst[im * ne1 * ne0 + r0] = s;
547 }
548 }
549}
550
551#define N_OUTS_PAIR 8
552#define N_PAIRS_PAIR (N_OUTS_PAIR / 2)
553
554#ifdef ADRENO_GPU
555REQD_SUBGROUP_SIZE_64
556#endif
557kernel void kernel_mul_mat_f16_f32_l4_x8_pair(
558 global char * src0,
559 ulong offset0,
560 global char * src1,
561 ulong offset1,
562 global float * dst,
563 ulong offsetd,
564 int ne00,
565 int ne01,
566 int ne02,
567 ulong nb00,
568 ulong nb01,
569 ulong nb02,
570 ulong nb03,
571 int ne10,
572 int ne11,
573 int ne12,
574 ulong nb10,
575 ulong nb11,
576 ulong nb12,
577 ulong nb13,
578 int ne0,
579 int ne1,
580 int r2,
581 int r3
582) {
583 src0 = (global char *)((global char *)src0 + offset0);
584 src1 = (global char *)((global char *)src1 + offset1);
585 dst = (global float*)((global char *)dst + offsetd);
586
587 const int sgs_lid = get_sub_group_local_id();
588 const int half_id = sgs_lid >> 5; // 0 = lower half, 1 = upper half
589 const int lane_h = sgs_lid & 31; // lane 0..31 within half
590
591 const int r0_base = get_group_id(0) * N_OUTS_PAIR;
592 const int im = get_group_id(2);
593
594 const int i12 = im % ne12;
595 const int i13 = im / ne12;
596
597 const ulong offset_src1 = (i12) * nb12 + (i13) * nb13;
598 global float4 * y4 = (global float4 *)(src1 + offset_src1);
599
600 __local float4 q_loc[64]; // ne00/4 max for sub_group_size 64
601 if (sgs_lid < ne00 / 4) {
602 q_loc[sgs_lid] = y4[sgs_lid];
603 }
604 barrier(CLK_LOCAL_MEM_FENCE);
605
606 const int dk_vec = ne00 / 4;
607
608 #pragma unroll
609 for (int p = 0; p < N_PAIRS_PAIR; ++p) {
610 const int r0 = r0_base + 2 * p + half_id;
611
612 const ulong offset_src0 = r0 * nb01 + (i12 / r2) * nb02 + (i13 / r3) * nb03;
613 global half4 * x4 = (global half4 *)(src0 + offset_src0);
614
615 float sumf = 0.0f;
616 for (int i = lane_h; i < dk_vec; i += 32) {
617 const half4 k4 = x4[i];
618 const float4 q = q_loc[i];
619 sumf += convert_float(k4.s0) * q.s0
620 + convert_float(k4.s1) * q.s1
621 + convert_float(k4.s2) * q.s2
622 + convert_float(k4.s3) * q.s3;
623 }
624
625 sumf += sub_group_shuffle_xor(sumf, 16);
626 sumf += sub_group_shuffle_xor(sumf, 8);
627 sumf += sub_group_shuffle_xor(sumf, 4);
628 sumf += sub_group_shuffle_xor(sumf, 2);
629 sumf += sub_group_shuffle_xor(sumf, 1);
630
631 if (lane_h == 0) {
632 dst[im * ne1 * ne0 + r0] = sumf;
633 }
634 }
635}
636
637#define N_K_ROWS_GQA 16
638#define GQA_RATIO_GQA 8
639#define LANES_PER_QH 8 // 64 / GQA_RATIO_GQA
640#define DK_VEC_GQA 32 // DK / 4 for DK=128
641
642#ifdef ADRENO_GPU
643REQD_SUBGROUP_SIZE_64
644#endif
645kernel void kernel_mul_mat_f16_f32_l4_x8_gqa4(
646 global char * src0,
647 ulong offset0,
648 global char * src1,
649 ulong offset1,
650 global float * dst,
651 ulong offsetd,
652 int ne00,
653 int ne01,
654 int ne02,
655 ulong nb00,
656 ulong nb01,
657 ulong nb02,
658 ulong nb03,
659 int ne10,
660 int ne11,
661 int ne12,
662 ulong nb10,
663 ulong nb11,
664 ulong nb12,
665 ulong nb13,
666 int ne0,
667 int ne1,
668 int r2,
669 int r3
670) {
671 src0 = (global char *)((global char *)src0 + offset0);
672 src1 = (global char *)((global char *)src1 + offset1);
673 dst = (global float*)((global char *)dst + offsetd);
674
675 const int sgs_lid = get_sub_group_local_id();
676 const int q_id = sgs_lid >> 3; // 0..7: which Q-head (8 per WG)
677 const int lane_q = sgs_lid & 7; // 0..7: lane within Q-head partition
678
679 const int r0_base = get_group_id(0) * N_K_ROWS_GQA;
680 const int im_kv = get_group_id(2);
681
682 const int i02 = im_kv % ne02; // K-head index (also K2 batch)
683 const int i03 = im_kv / ne02; // n13 batch index
684
685 const int q_head_lo = i02 * GQA_RATIO_GQA;
686
687 __local float4 q_loc[GQA_RATIO_GQA * DK_VEC_GQA]; // 4 × 32 = 128 float4
688 #pragma unroll
689 for (int qh = 0; qh < GQA_RATIO_GQA; ++qh) {
690 const int qh_idx = q_head_lo + qh;
691 global float4 * y4 = (global float4 *)(src1 + qh_idx * nb12 + i03 * nb13);
692
693 if (sgs_lid < DK_VEC_GQA) {
694 q_loc[qh * DK_VEC_GQA + sgs_lid] = y4[sgs_lid];
695 }
696 }
697 barrier(CLK_LOCAL_MEM_FENCE);
698
699 // K base offset for this WG. All 8 K-rows × 4 Q-heads share this K-head.
700 const ulong offset_src0_base = (i02) * nb02 + (i03 / r3) * nb03;
701
702 #pragma unroll
703 for (int dr = 0; dr < N_K_ROWS_GQA; ++dr) {
704 const int r0 = r0_base + dr;
705
706 const ulong offset_src0 = r0 * nb01 + offset_src0_base;
707 global half4 * x4 = (global half4 *)(src0 + offset_src0);
708
709 float sumf = 0.0f;
710 #pragma unroll
711 for (int t = 0; t < 4; ++t) {
712 const int i = lane_q + t * LANES_PER_QH; // 8, 16, 24-step
713 const half4 k4 = x4[i];
714 const float4 q = q_loc[q_id * DK_VEC_GQA + i];
715 sumf += convert_float(k4.s0) * q.s0
716 + convert_float(k4.s1) * q.s1
717 + convert_float(k4.s2) * q.s2
718 + convert_float(k4.s3) * q.s3;
719 }
720
721 sumf += sub_group_shuffle_xor(sumf, 4);
722 sumf += sub_group_shuffle_xor(sumf, 2);
723 sumf += sub_group_shuffle_xor(sumf, 1);
724
725 if (lane_q == 0) {
726 const int im_out = i03 * ne12 + (q_head_lo + q_id);
727 dst[im_out * ne1 * ne0 + r0] = sumf;
728 }
729 }
730}
731
732#define N_DV_ROWS_Y8GQA 8
733#define GQA_RATIO_Y8GQA 8
734
735#ifdef ADRENO_GPU
736REQD_SUBGROUP_SIZE_64
737#endif
738kernel void kernel_mul_mat_f16_f32_l4_y8_gqa(
739 global char * src0,
740 ulong offset0,
741 global char * src1,
742 ulong offset1,
743 global float * dst,
744 ulong offsetd,
745 int ne00,
746 int ne01,
747 int ne02,
748 ulong nb00,
749 ulong nb01,
750 ulong nb02,
751 ulong nb03,
752 int ne10,
753 int ne11,
754 int ne12,
755 ulong nb10,
756 ulong nb11,
757 ulong nb12,
758 ulong nb13,
759 int ne0,
760 int ne1,
761 int r2,
762 int r3
763) {
764 src0 = (global char *)((global char *)src0 + offset0);
765 src1 = (global char *)((global char *)src1 + offset1);
766 dst = (global float*)((global char *)dst + offsetd);
767
768 const int sgs_lid = get_sub_group_local_id();
769 const int sgs_sz = get_max_sub_group_size();
770
771 const int r0_base = get_group_id(0) * N_DV_ROWS_Y8GQA;
772 const int im_kv = get_group_id(2);
773
774 const int i02 = im_kv % ne02; // K-head index
775 const int i03 = im_kv / ne02; // n13 batch index
776
777 // GQA Q-heads sharing this K-head.
778 const int q_head_lo = i02 * GQA_RATIO_Y8GQA;
779
780 global float4 * y4_q[GQA_RATIO_Y8GQA];
781 #pragma unroll
782 for (int qh = 0; qh < GQA_RATIO_Y8GQA; ++qh) {
783 const int qh_idx = q_head_lo + qh;
784 y4_q[qh] = (global float4 *)(src1 + qh_idx * nb12 + i03 * nb13);
785 }
786
787 global half4 * x4_o[N_DV_ROWS_Y8GQA];
788 #pragma unroll
789 for (int o = 0; o < N_DV_ROWS_Y8GQA; ++o) {
790 const int r0 = r0_base + o;
791 const int r0c = (r0 < ne01) ? r0 : 0;
792 const ulong off = r0c * nb01 + (i02) * nb02 + (i03 / r3) * nb03;
793 x4_o[o] = (global half4 *)(src0 + off);
794 }
795
796 float sum[N_DV_ROWS_Y8GQA][GQA_RATIO_Y8GQA] = { {0.0f} };
797
798 for (int i = sgs_lid; i < ne00 / 4; i += sgs_sz) {
799 // load 8 V values (one per DV row), same K-head, K-pos = i.
800 half4 v[N_DV_ROWS_Y8GQA];
801 #pragma unroll
802 for (int o = 0; o < N_DV_ROWS_Y8GQA; ++o) {
803 v[o] = x4_o[o][i];
804 }
805
806 // load 8 softmax values (one per Q-head).
807 float4 q[GQA_RATIO_Y8GQA];
808 #pragma unroll
809 for (int qh = 0; qh < GQA_RATIO_Y8GQA; ++qh) {
810 q[qh] = y4_q[qh][i];
811 }
812
813 #pragma unroll
814 for (int o = 0; o < N_DV_ROWS_Y8GQA; ++o) {
815 const float4 vf = (float4)(convert_float(v[o].s0),
816 convert_float(v[o].s1),
817 convert_float(v[o].s2),
818 convert_float(v[o].s3));
819 #pragma unroll
820 for (int qh = 0; qh < GQA_RATIO_Y8GQA; ++qh) {
821 sum[o][qh] += vf.s0 * q[qh].s0
822 + vf.s1 * q[qh].s1
823 + vf.s2 * q[qh].s2
824 + vf.s3 * q[qh].s3;
825 }
826 }
827 }
828
829 #pragma unroll
830 for (int o = 0; o < N_DV_ROWS_Y8GQA; ++o) {
831 const int r0 = r0_base + o;
832 #pragma unroll
833 for (int qh = 0; qh < GQA_RATIO_Y8GQA; ++qh) {
834 const float s = sub_group_reduce_add(sum[o][qh]);
835 if (sgs_lid == 0 && r0 < ne01) {
836 const int im_out = i03 * ne12 + (q_head_lo + qh);
837 dst[im_out * ne1 * ne0 + r0] = s;
838 }
839 }
840 }
841}
842
843#ifdef ADRENO_GPU
844REQD_SUBGROUP_SIZE_64
845#endif
846kernel void kernel_mul_mat_f16_f32_l4_x8_gqa4_img(
847 __read_only image1d_buffer_t src0_img,
848 global char * src1,
849 ulong offset1,
850 global float * dst,
851 ulong offsetd,
852 int ne00,
853 int ne01,
854 int ne02,
855 ulong nb01,
856 ulong nb02,
857 ulong nb03,
858 int ne10,
859 int ne11,
860 int ne12,
861 ulong nb10,
862 ulong nb11,
863 ulong nb12,
864 ulong nb13,
865 int ne0,
866 int ne1,
867 int r2,
868 int r3
869) {
870 src1 = (global char *)((global char *)src1 + offset1);
871 dst = (global float*)((global char *)dst + offsetd);
872
873 const int sgs_lid = get_sub_group_local_id();
874 const int q_id = sgs_lid >> 3; // 0..7: which Q-head (8 per WG)
875 const int lane_q = sgs_lid & 7; // 0..7: lane within Q-head partition
876
877 const int r0_base = get_group_id(0) * N_K_ROWS_GQA;
878 const int im_kv = get_group_id(2);
879
880 const int i02 = im_kv % ne02;
881 const int i03 = im_kv / ne02;
882
883 const int q_head_lo = i02 * GQA_RATIO_GQA;
884
885 __local float4 q_loc[GQA_RATIO_GQA * DK_VEC_GQA];
886 #pragma unroll
887 for (int qh = 0; qh < GQA_RATIO_GQA; ++qh) {
888 const int qh_idx = q_head_lo + qh;
889 global float4 * y4 = (global float4 *)(src1 + qh_idx * nb12 + i03 * nb13);
890 if (sgs_lid < DK_VEC_GQA) {
891 q_loc[qh * DK_VEC_GQA + sgs_lid] = y4[sgs_lid];
892 }
893 }
894 barrier(CLK_LOCAL_MEM_FENCE);
895
896 const int pitch_px_row = (int)(nb01 >> 4);
897 const int pitch_px_head = (int)(nb02 >> 4);
898 const int pitch_px_n13 = (int)(nb03 >> 4);
899
900 const int head_px_base = i02 * pitch_px_head + (i03 / r3) * pitch_px_n13;
901
902 #pragma unroll
903 for (int dr = 0; dr < N_K_ROWS_GQA; ++dr) {
904 const int r0 = r0_base + dr;
905 const int row_px_base = r0 * pitch_px_row + head_px_base;
906
907 float sumf = 0.0f;
908 #pragma unroll
909 for (int t = 0; t < 2; ++t) {
910 const int p = lane_q + t * LANES_PER_QH; // pixel idx in row, 0..15
911 const half8 k8 = as_half8(read_imagef(src0_img, row_px_base + p));
912 const int i0 = 2 * p; // first half4 idx
913 const float4 qa = q_loc[q_id * DK_VEC_GQA + i0 ];
914 const float4 qb = q_loc[q_id * DK_VEC_GQA + i0 + 1];
915 sumf += convert_float(k8.s0) * qa.s0
916 + convert_float(k8.s1) * qa.s1
917 + convert_float(k8.s2) * qa.s2
918 + convert_float(k8.s3) * qa.s3
919 + convert_float(k8.s4) * qb.s0
920 + convert_float(k8.s5) * qb.s1
921 + convert_float(k8.s6) * qb.s2
922 + convert_float(k8.s7) * qb.s3;
923 }
924
925 sumf += sub_group_shuffle_xor(sumf, 4);
926 sumf += sub_group_shuffle_xor(sumf, 2);
927 sumf += sub_group_shuffle_xor(sumf, 1);
928
929 if (lane_q == 0) {
930 const int im_out = i03 * ne12 + (q_head_lo + q_id);
931 dst[im_out * ne1 * ne0 + r0] = sumf;
932 }
933 }
934}
935
936#ifdef ADRENO_GPU
937REQD_SUBGROUP_SIZE_64
938#endif
939kernel void kernel_mul_mat_f16_f32_l4_y8_gqa_img(
940 __read_only image1d_buffer_t src0_img,
941 global char * src1,
942 ulong offset1,
943 global float * dst,
944 ulong offsetd,
945 int ne00,
946 int ne01,
947 int ne02,
948 ulong nb01,
949 ulong nb02,
950 ulong nb03,
951 int ne10,
952 int ne11,
953 int ne12,
954 ulong nb10,
955 ulong nb11,
956 ulong nb12,
957 ulong nb13,
958 int ne0,
959 int ne1,
960 int r2,
961 int r3
962) {
963 src1 = (global char *)((global char *)src1 + offset1);
964 dst = (global float*)((global char *)dst + offsetd);
965
966 const int sgs_lid = get_sub_group_local_id();
967 const int sgs_sz = get_max_sub_group_size();
968
969 const int r0_base = get_group_id(0) * N_DV_ROWS_Y8GQA;
970 const int im_kv = get_group_id(2);
971
972 const int i02 = im_kv % ne02;
973 const int i03 = im_kv / ne02;
974
975 const int q_head_lo = i02 * GQA_RATIO_Y8GQA;
976
977 // Q (= softmax(KQ)) base pointers per Q-head
978 global float4 * y4_q[GQA_RATIO_Y8GQA];
979 #pragma unroll
980 for (int qh = 0; qh < GQA_RATIO_Y8GQA; ++qh) {
981 const int qh_idx = q_head_lo + qh;
982 y4_q[qh] = (global float4 *)(src1 + qh_idx * nb12 + i03 * nb13);
983 }
984
985 const int pitch_px_row = (int)(nb01 >> 3);
986 const int pitch_px_head = (int)(nb02 >> 3);
987 const int pitch_px_n13 = (int)(nb03 >> 3);
988
989 const int head_px_base = i02 * pitch_px_head + (i03 / r3) * pitch_px_n13;
990
991 // per-DV-row pixel base
992 int row_px_base[N_DV_ROWS_Y8GQA];
993 #pragma unroll
994 for (int o = 0; o < N_DV_ROWS_Y8GQA; ++o) {
995 const int r0 = r0_base + o;
996 const int r0c = (r0 < ne01) ? r0 : 0;
997 row_px_base[o] = r0c * pitch_px_row + head_px_base;
998 }
999
1000 float sum[N_DV_ROWS_Y8GQA][GQA_RATIO_Y8GQA] = { {0.0f} };
1001
1002 for (int i = sgs_lid; i < ne00 / 4; i += sgs_sz) {
1003 half4 v[N_DV_ROWS_Y8GQA];
1004
1005 #pragma unroll
1006 for (int o = 0; o < N_DV_ROWS_Y8GQA; ++o) {
1007 v[o] = read_imageh(src0_img, row_px_base[o] + i);
1008 }
1009
1010 float4 q[GQA_RATIO_Y8GQA];
1011 #pragma unroll
1012 for (int qh = 0; qh < GQA_RATIO_Y8GQA; ++qh) {
1013 q[qh] = y4_q[qh][i];
1014 }
1015 // 64 mads.
1016 #pragma unroll
1017 for (int o = 0; o < N_DV_ROWS_Y8GQA; ++o) {
1018 const float4 vf = (float4)(convert_float(v[o].s0),
1019 convert_float(v[o].s1),
1020 convert_float(v[o].s2),
1021 convert_float(v[o].s3));
1022 #pragma unroll
1023 for (int qh = 0; qh < GQA_RATIO_Y8GQA; ++qh) {
1024 sum[o][qh] += vf.s0 * q[qh].s0
1025 + vf.s1 * q[qh].s1
1026 + vf.s2 * q[qh].s2
1027 + vf.s3 * q[qh].s3;
1028 }
1029 }
1030 }
1031
1032 #pragma unroll
1033 for (int o = 0; o < N_DV_ROWS_Y8GQA; ++o) {
1034 const int r0 = r0_base + o;
1035 #pragma unroll
1036 for (int qh = 0; qh < GQA_RATIO_Y8GQA; ++qh) {
1037 const float s = sub_group_reduce_add(sum[o][qh]);
1038 if (sgs_lid == 0 && r0 < ne01) {
1039 const int im_out = i03 * ne12 + (q_head_lo + qh);
1040 dst[im_out * ne1 * ne0 + r0] = s;
1041 }
1042 }
1043 }
1044}
1045
1046#define N_K_ROWS_GQA_R4 16
1047#define GQA_RATIO_R4 4
1048#define LANES_PER_QH_R4 16 // = 64 / GQA_RATIO_R4
1049#define DK_VEC_R4 32 // DK / 4 for DK=128
1050
1051#ifdef ADRENO_GPU
1052REQD_SUBGROUP_SIZE_64
1053#endif
1054kernel void kernel_mul_mat_f16_f32_l4_x8_gqa_r4_img(
1055 __read_only image1d_buffer_t src0_img,
1056 global char * src1,
1057 ulong offset1,
1058 global float * dst,
1059 ulong offsetd,
1060 int ne00,
1061 int ne01,
1062 int ne02,
1063 ulong nb01,
1064 ulong nb02,
1065 ulong nb03,
1066 int ne10,
1067 int ne11,
1068 int ne12,
1069 ulong nb10,
1070 ulong nb11,
1071 ulong nb12,
1072 ulong nb13,
1073 int ne0,
1074 int ne1,
1075 int r2,
1076 int r3
1077) {
1078 src1 = (global char *)((global char *)src1 + offset1);
1079 dst = (global float*)((global char *)dst + offsetd);
1080
1081 const int sgs_lid = get_sub_group_local_id();
1082 const int q_id = sgs_lid >> 4; // 0..3
1083 const int lane_q = sgs_lid & 15; // 0..15
1084
1085 const int r0_base = get_group_id(0) * N_K_ROWS_GQA_R4;
1086 const int im_kv = get_group_id(2);
1087
1088 const int i02 = im_kv % ne02;
1089 const int i03 = im_kv / ne02;
1090
1091 const int q_head_lo = i02 * GQA_RATIO_R4;
1092
1093 __local float4 q_loc[GQA_RATIO_R4 * DK_VEC_R4];
1094 #pragma unroll
1095 for (int qh = 0; qh < GQA_RATIO_R4; ++qh) {
1096 const int qh_idx = q_head_lo + qh;
1097 global float4 * y4 = (global float4 *)(src1 + qh_idx * nb12 + i03 * nb13);
1098 if (sgs_lid < DK_VEC_R4) {
1099 q_loc[qh * DK_VEC_R4 + sgs_lid] = y4[sgs_lid];
1100 }
1101 }
1102 barrier(CLK_LOCAL_MEM_FENCE);
1103
1104 const int pitch_px_row = (int)(nb01 >> 4);
1105 const int pitch_px_head = (int)(nb02 >> 4);
1106 const int pitch_px_n13 = (int)(nb03 >> 4);
1107
1108 const int head_px_base = i02 * pitch_px_head + (i03 / r3) * pitch_px_n13;
1109
1110 #pragma unroll
1111 for (int dr = 0; dr < N_K_ROWS_GQA_R4; ++dr) {
1112 const int r0 = r0_base + dr;
1113 const int row_px_base = r0 * pitch_px_row + head_px_base;
1114
1115 const int p = lane_q;
1116 const half8 k8 = as_half8(read_imagef(src0_img, row_px_base + p));
1117 const int i0 = 2 * p;
1118 const float4 qa = q_loc[q_id * DK_VEC_R4 + i0 ];
1119 const float4 qb = q_loc[q_id * DK_VEC_R4 + i0 + 1];
1120
1121 float sumf =
1122 convert_float(k8.s0) * qa.s0
1123 + convert_float(k8.s1) * qa.s1
1124 + convert_float(k8.s2) * qa.s2
1125 + convert_float(k8.s3) * qa.s3
1126 + convert_float(k8.s4) * qb.s0
1127 + convert_float(k8.s5) * qb.s1
1128 + convert_float(k8.s6) * qb.s2
1129 + convert_float(k8.s7) * qb.s3;
1130
1131 sumf += sub_group_shuffle_xor(sumf, 8);
1132 sumf += sub_group_shuffle_xor(sumf, 4);
1133 sumf += sub_group_shuffle_xor(sumf, 2);
1134 sumf += sub_group_shuffle_xor(sumf, 1);
1135
1136 if (lane_q == 0) {
1137 const int im_out = i03 * ne12 + (q_head_lo + q_id);
1138 dst[im_out * ne1 * ne0 + r0] = sumf;
1139 }
1140 }
1141}
1142
1143#define N_K_ROWS_GQA_R2_DK256 16
1144#define GQA_RATIO_R2 2
1145#define LANES_PER_QH_R2 32 // = 64 / GQA_RATIO_R2
1146#define DK_VEC_DK256 64 // DK / 4 for DK=256
1147
1148#ifdef ADRENO_GPU
1149REQD_SUBGROUP_SIZE_64
1150#endif
1151kernel void kernel_mul_mat_f16_f32_l4_x8_gqa_r2_dk256_img(
1152 __read_only image1d_buffer_t src0_img,
1153 global char * src1,
1154 ulong offset1,
1155 global float * dst,
1156 ulong offsetd,
1157 int ne00,
1158 int ne01,
1159 int ne02,
1160 ulong nb01,
1161 ulong nb02,
1162 ulong nb03,
1163 int ne10,
1164 int ne11,
1165 int ne12,
1166 ulong nb10,
1167 ulong nb11,
1168 ulong nb12,
1169 ulong nb13,
1170 int ne0,
1171 int ne1,
1172 int r2,
1173 int r3
1174) {
1175 src1 = (global char *)((global char *)src1 + offset1);
1176 dst = (global float*)((global char *)dst + offsetd);
1177
1178 const int sgs_lid = get_sub_group_local_id();
1179 const int q_id = sgs_lid >> 5; // 0..1
1180 const int lane_q = sgs_lid & 31; // 0..31
1181
1182 const int r0_base = get_group_id(0) * N_K_ROWS_GQA_R2_DK256;
1183 const int im_kv = get_group_id(2);
1184
1185 const int i02 = im_kv % ne02;
1186 const int i03 = im_kv / ne02;
1187
1188 const int q_head_lo = i02 * GQA_RATIO_R2;
1189
1190 __local float4 q_loc[GQA_RATIO_R2 * DK_VEC_DK256];
1191 #pragma unroll
1192 for (int qh = 0; qh < GQA_RATIO_R2; ++qh) {
1193 const int qh_idx = q_head_lo + qh;
1194 global float4 * y4 = (global float4 *)(src1 + qh_idx * nb12 + i03 * nb13);
1195 q_loc[qh * DK_VEC_DK256 + sgs_lid] = y4[sgs_lid];
1196 }
1197 barrier(CLK_LOCAL_MEM_FENCE);
1198
1199 const int pitch_px_row = (int)(nb01 >> 4);
1200 const int pitch_px_head = (int)(nb02 >> 4);
1201 const int pitch_px_n13 = (int)(nb03 >> 4);
1202
1203 const int head_px_base = i02 * pitch_px_head + (i03 / r3) * pitch_px_n13;
1204
1205 #pragma unroll
1206 for (int dr = 0; dr < N_K_ROWS_GQA_R2_DK256; ++dr) {
1207 const int r0 = r0_base + dr;
1208 const int row_px_base = r0 * pitch_px_row + head_px_base;
1209
1210 const int p = lane_q;
1211 const half8 k8 = as_half8(read_imagef(src0_img, row_px_base + p));
1212 const int i0 = 2 * p;
1213 const float4 qa = q_loc[q_id * DK_VEC_DK256 + i0 ];
1214 const float4 qb = q_loc[q_id * DK_VEC_DK256 + i0 + 1];
1215
1216 float sumf =
1217 convert_float(k8.s0) * qa.s0
1218 + convert_float(k8.s1) * qa.s1
1219 + convert_float(k8.s2) * qa.s2
1220 + convert_float(k8.s3) * qa.s3
1221 + convert_float(k8.s4) * qb.s0
1222 + convert_float(k8.s5) * qb.s1
1223 + convert_float(k8.s6) * qb.s2
1224 + convert_float(k8.s7) * qb.s3;
1225
1226 sumf += sub_group_shuffle_xor(sumf, 16);
1227 sumf += sub_group_shuffle_xor(sumf, 8);
1228 sumf += sub_group_shuffle_xor(sumf, 4);
1229 sumf += sub_group_shuffle_xor(sumf, 2);
1230 sumf += sub_group_shuffle_xor(sumf, 1);
1231
1232 if (lane_q == 0) {
1233 const int im_out = i03 * ne12 + (q_head_lo + q_id);
1234 dst[im_out * ne1 * ne0 + r0] = sumf;
1235 }
1236 }
1237}