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 / sdpa_xmem_f32_f16_os8.cl

871 lines · 39716 bytes Raw Blame History
1#pragma OPENCL EXTENSION cl_khr_fp16 : enable
2#pragma OPENCL EXTENSION cl_qcom_subgroup_uniform_load : enable
3#pragma OPENCL EXTENSION cl_qcom_subgroup_constant_load : enable
4
5#define bool2 uchar2
6#define bool3 uchar3
7#define bool4 uchar4
8
9__constant sampler_t smp_none = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_NONE | CLK_FILTER_NEAREST;
10__constant sampler_t smp_zero = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP | CLK_FILTER_NEAREST;
11
12__kernel void adreno_xmem_attn_q_f32_to_img_scaled(const global void * src_void,
13 ulong src_offset,
14 write_only image2d_t dst_image2d,
15 const float scale,
16 const int d_head,
17 const int n_q,
18 const int n_head,
19 const int n_head_kv,
20 const int n_batch,
21 const ulong src_nb1,
22 const ulong src_nb2,
23 const ulong src_nb3) {
24 const int x = get_global_id(0);
25 const int flat_h = get_global_id(1);
26 const int d = get_global_id(2);
27
28 const int heads_total = n_head * n_batch;
29 const int kpack = d_head / 4;
30
31 if (x >= n_q || flat_h >= heads_total || d >= kpack) {
32 return;
33 }
34
35 const int batch = flat_h / n_head;
36 const int head = flat_h % n_head;
37 const int gqa = n_head / n_head_kv;
38 const int head_kv = head / gqa;
39 const int head_group = head - head_kv * gqa;
40 const int compact_h = batch * n_head_kv + head_kv;
41 const int compact_x = head_group * n_q + x;
42 const int c = d * 4;
43
44 const global char * src_base = (const global char *) src_void + src_offset;
45 const global float * row_ptr = (const global float *) (src_base + batch * src_nb3 + head * src_nb2 + x * src_nb1);
46
47 half4 out = (half4) (0.0h);
48 out.x = convert_half(row_ptr[c + 0] * scale);
49 if (c + 1 < d_head) {
50 out.y = convert_half(row_ptr[c + 1] * scale);
51 }
52 if (c + 2 < d_head) {
53 out.z = convert_half(row_ptr[c + 2] * scale);
54 }
55 if (c + 3 < d_head) {
56 out.w = convert_half(row_ptr[c + 3] * scale);
57 }
58
59 write_imageh(dst_image2d, (int2) (compact_x, compact_h * kpack + d), out);
60}
61
62__kernel void adreno_xmem_attn_kv_f32_to_img_gqa(const global void * src_void,
63 ulong src_offset,
64 write_only image2d_t dst_image2d,
65 const int d_head,
66 const int n_kv,
67 const int n_kv_padded,
68 const int n_head_kv,
69 const int n_batch,
70 const ulong src_nb1,
71 const ulong src_nb2,
72 const ulong src_nb3) {
73 const int x = get_global_id(0);
74 const int flat_h = get_global_id(1);
75 const int d = get_global_id(2);
76
77 const int kv_heads_total = n_head_kv * n_batch;
78 const int kpack = d_head / 4;
79
80 if (x >= n_kv_padded || flat_h >= kv_heads_total || d >= kpack) {
81 return;
82 }
83
84 const int batch = flat_h / n_head_kv;
85 const int head_kv = flat_h % n_head_kv;
86 const int c = d * 4;
87
88 half4 out = (half4) (0.0h);
89 if (x < n_kv) {
90 const global char * src_base = (const global char *) src_void + src_offset;
91 const global float * row_ptr =
92 (const global float *) (src_base + batch * src_nb3 + head_kv * src_nb2 + x * src_nb1);
93 out.x = convert_half(row_ptr[c + 0]);
94 if (c + 1 < d_head) {
95 out.y = convert_half(row_ptr[c + 1]);
96 }
97 if (c + 2 < d_head) {
98 out.z = convert_half(row_ptr[c + 2]);
99 }
100 if (c + 3 < d_head) {
101 out.w = convert_half(row_ptr[c + 3]);
102 }
103 }
104
105 write_imageh(dst_image2d, (int2) (x, flat_h * kpack + d), out);
106}
107
108__kernel void adreno_xmem_attn_kv_f16_to_img_gqa(const global void * src_void,
109 ulong src_offset,
110 write_only image2d_t dst_image2d,
111 const int d_head,
112 const int n_kv,
113 const int n_kv_padded,
114 const int n_head_kv,
115 const int n_batch,
116 const ulong src_nb1,
117 const ulong src_nb2,
118 const ulong src_nb3) {
119 const int x = get_global_id(0);
120 const int flat_h = get_global_id(1);
121 const int d = get_global_id(2);
122
123 const int kv_heads_total = n_head_kv * n_batch;
124 const int kpack = d_head / 4;
125
126 if (x >= n_kv_padded || flat_h >= kv_heads_total || d >= kpack) {
127 return;
128 }
129
130 const int batch = flat_h / n_head_kv;
131 const int head_kv = flat_h % n_head_kv;
132 const int c = d * 4;
133
134 half4 out = (half4) (0.0h);
135 if (x < n_kv) {
136 const global char * src_base = (const global char *) src_void + src_offset;
137 const global half * row_ptr =
138 (const global half *) (src_base + batch * src_nb3 + head_kv * src_nb2 + x * src_nb1);
139 out.x = row_ptr[c + 0];
140 if (c + 1 < d_head) {
141 out.y = row_ptr[c + 1];
142 }
143 if (c + 2 < d_head) {
144 out.z = row_ptr[c + 2];
145 }
146 if (c + 3 < d_head) {
147 out.w = row_ptr[c + 3];
148 }
149 }
150
151 write_imageh(dst_image2d, (int2) (x, flat_h * kpack + d), out);
152}
153
154__kernel void adreno_xmem_attn_img_to_f32(global void * dst_void,
155 ulong dst_offset,
156 read_only image2d_t src_image2d,
157 const int d_head,
158 const int n_q,
159 const int n_head,
160 const int n_head_kv,
161 const int n_batch,
162 const ulong dst_nb1,
163 const ulong dst_nb2,
164 const ulong dst_nb3) {
165 const int x = get_global_id(0);
166 const int flat_h = get_global_id(1);
167 const int d = get_global_id(2);
168
169 const int heads_total = n_head * n_batch;
170 const int kpack = d_head / 4;
171
172 if (x >= n_q || flat_h >= heads_total || d >= kpack) {
173 return;
174 }
175
176 const int batch = flat_h / n_head;
177 const int head = flat_h % n_head;
178 const int gqa = n_head / n_head_kv;
179 const int head_kv = head / gqa;
180 const int head_group = head - head_kv * gqa;
181 const int compact_h = batch * n_head_kv + head_kv;
182 const int compact_x = head_group * n_q + x;
183 const int c = d * 4;
184
185 global char * dst_base = (global char *) dst_void + dst_offset;
186 global float * row_ptr = (global float *) (dst_base + batch * dst_nb3 + x * dst_nb2 + head * dst_nb1);
187
188 const half4 in_value = read_imageh(src_image2d, smp_zero, (int2) (compact_x, compact_h * kpack + d));
189 row_ptr[c + 0] = convert_float(in_value.x);
190 if (c + 1 < d_head) {
191 row_ptr[c + 1] = convert_float(in_value.y);
192 }
193 if (c + 2 < d_head) {
194 row_ptr[c + 2] = convert_float(in_value.z);
195 }
196 if (c + 3 < d_head) {
197 row_ptr[c + 3] = convert_float(in_value.w);
198 }
199}
200
201__kernel void adreno_xmem_attn_k_gather(global half4 * dst_tensor_buffer,
202 read_only image2d_t src_tensor_image2d,
203 const int4 shared_int4_0,
204 const int4 shared_int4_1) {
205 int X = get_global_id(0);
206 int Y = get_global_id(1);
207 int S = get_global_id(2);
208 if (X >= shared_int4_0.w || Y >= shared_int4_0.y || S >= shared_int4_0.z) {
209 return;
210 }
211 half temps[4];
212 temps[0] = (half) (0.f);
213 temps[1] = (half) (0.f);
214 temps[2] = (half) (0.f);
215 temps[3] = (half) (0.f);
216 for (int i = 0; i < 4; ++i) {
217 int dst_channel = S * 4 + i;
218 if (dst_channel < shared_int4_0.x) {
219 int s_y = Y;
220 int s_x = dst_channel;
221 int s_c = X;
222 {
223 int slice_coord_TMP = (s_c) / 4;
224 int sub_ch_coord_TMP = (s_c) % 4;
225 half4 src_TMP = read_imageh(src_tensor_image2d, smp_zero,
226 (int2) ((s_x), ((s_y) *shared_int4_1.x + (slice_coord_TMP))));
227 temps[i] = (half[4]){ src_TMP.x, src_TMP.y, src_TMP.z, src_TMP.w }[sub_ch_coord_TMP];
228 };
229 }
230 }
231 half4 result;
232 result.x = temps[0];
233 result.y = temps[1];
234 result.z = temps[2];
235 result.w = temps[3];
236 dst_tensor_buffer[(((S) *shared_int4_0.y + (Y)) * shared_int4_0.w + (X))] = result;
237}
238
239__kernel void adreno_xmem_attn_pack_k(global half4 * dst_tensor_buffer,
240 read_only image1d_buffer_t src_image_buffer,
241 const int4 shared_int4_0,
242 const int4 shared_int4_1,
243 const int4 shared_int4_2) {
244 int linear_index = get_global_id(0);
245 if (linear_index >= shared_int4_0.y) {
246 return;
247 }
248 if (get_global_id(1) != 0) {
249 return;
250 }
251 if (get_global_id(2) != 0) {
252 return;
253 }
254 int dst_o_sp_i_ogroup = linear_index;
255 int dst_ogroup = dst_o_sp_i_ogroup % shared_int4_0.x;
256 int dst_o_sp_i = dst_o_sp_i_ogroup / shared_int4_0.x;
257 int dst_i = dst_o_sp_i % shared_int4_0.z;
258 int dst_o_sp = dst_o_sp_i / shared_int4_0.z;
259 int dst_sp = dst_o_sp % shared_int4_1.x;
260 int dst_o = dst_o_sp / shared_int4_1.x;
261 int i_slice = dst_i;
262 int o_slice = dst_o * shared_int4_0.x + dst_ogroup;
263 int spatial_linear = dst_sp;
264 int W = spatial_linear % shared_int4_1.y;
265 int H = spatial_linear / shared_int4_1.y;
266 half4 w0 = (half4) (0);
267 half4 w1 = (half4) (0);
268 half4 w2 = (half4) (0);
269 half4 w3 = (half4) (0);
270
271 if (i_slice * 4 < shared_int4_0.w && o_slice < shared_int4_1.w) {
272 w0 = read_imageh(src_image_buffer, (((o_slice) *shared_int4_1.z + (W)) * shared_int4_2.x + (i_slice * 4)));
273 }
274 if (i_slice * 4 + 1 < shared_int4_0.w && o_slice < shared_int4_1.w) {
275 w1 = read_imageh(src_image_buffer, (((o_slice) *shared_int4_1.z + (W)) * shared_int4_2.x + (i_slice * 4 + 1)));
276 }
277 if (i_slice * 4 + 2 < shared_int4_0.w && o_slice < shared_int4_1.w) {
278 w2 = read_imageh(src_image_buffer, (((o_slice) *shared_int4_1.z + (W)) * shared_int4_2.x + (i_slice * 4 + 2)));
279 }
280 if (i_slice * 4 + 3 < shared_int4_0.w && o_slice < shared_int4_1.w) {
281 w3 = read_imageh(src_image_buffer, (((o_slice) *shared_int4_1.z + (W)) * shared_int4_2.x + (i_slice * 4 + 3)));
282 }
283 half4 r0 = w0;
284 half4 r1 = w1;
285 half4 r2 = w2;
286 half4 r3 = w3;
287 dst_tensor_buffer[linear_index * 4 + 0] = r0;
288 dst_tensor_buffer[linear_index * 4 + 1] = r1;
289 dst_tensor_buffer[linear_index * 4 + 2] = r2;
290 dst_tensor_buffer[linear_index * 4 + 3] = r3;
291}
292
293__attribute__((qcom_max_concurrent_subgroups(12))) __kernel void adreno_xmem_attn_qk_gemm(
294 global half4 * dst_tensor_buffer,
295 constant half8 * weights_buffer __attribute__((sub_group_uniform)),
296 constant half8 * xmem_buffer __attribute__((max_constant_size((6144)))),
297 read_only image2d_t src_tensor_image2d,
298 const int4 shared_int4_0,
299 const int4 shared_int4_1,
300 const int4 shared_int4_2) {
301 int X = get_group_id(1) * get_local_size(0) + get_local_id(0);
302 int Y = get_group_id(2) * get_local_size(1) + get_local_id(1);
303 int Z = get_group_id(0) * get_local_size(2) + get_local_id(2);
304 if (X >= shared_int4_0.z || Y >= shared_int4_0.x) {
305 return;
306 }
307 if (Z * 8 >= shared_int4_0.y) {
308 return;
309 }
310
311 half4 r0 = (half4) (0.f);
312 half4 r1 = (half4) (0.f);
313 half4 r2 = (half4) (0.f);
314 half4 r3 = (half4) (0.f);
315 half4 r4 = (half4) (0.f);
316 half4 r5 = (half4) (0.f);
317 half4 r6 = (half4) (0.f);
318 half4 r7 = (half4) (0.f);
319 int x_coord = mad24(X, shared_int4_2.y, shared_int4_1.y);
320 int y_coord = mad24(Y, shared_int4_2.z, shared_int4_1.z);
321 int coord_x, coord_y, coord_s;
322 int f_offset = (Z * shared_int4_1.w + Y) * shared_int4_1.x * 32;
323
324 int subgroup_id = (int) ((0x1F & qcom_get_physical_sub_group_id()));
325 subgroup_id = subgroup_id % 12;
326 int c_offset = mul24(subgroup_id, shared_int4_0.w);
327 __constant half16 * weights_cache = (__constant half16 *) &xmem_buffer[c_offset];
328 coord_y = Y;
329 coord_x = X;
330 coord_s = 0;
331 do {
332 half4 src0 =
333 read_imageh(src_tensor_image2d, smp_zero, (int2) ((coord_x), ((coord_y) *shared_int4_2.x + (coord_s))));
334 coord_s++;
335 half4 src1 =
336 read_imageh(src_tensor_image2d, smp_zero, (int2) ((coord_x), ((coord_y) *shared_int4_2.x + (coord_s))));
337 coord_s++;
338 qcom_sub_group_constant_load8(xmem_buffer, weights_buffer, c_offset, f_offset >> 1, 32);
339 f_offset += 64;
340 qcom_sub_group_sync(QCOM_CLK_CONST_LOAD_SYNC);
341 r0 += src0.x * weights_cache[0].s0123;
342 r0 += src0.y * weights_cache[0].s4567;
343 r0 += src0.z * weights_cache[0].s89ab;
344 r0 += src0.w * weights_cache[0].scdef;
345 r1 += src0.x * weights_cache[1].s0123;
346 r1 += src0.y * weights_cache[1].s4567;
347 r1 += src0.z * weights_cache[1].s89ab;
348 r1 += src0.w * weights_cache[1].scdef;
349 r2 += src0.x * weights_cache[2].s0123;
350 r2 += src0.y * weights_cache[2].s4567;
351 r2 += src0.z * weights_cache[2].s89ab;
352 r2 += src0.w * weights_cache[2].scdef;
353 r3 += src0.x * weights_cache[3].s0123;
354 r3 += src0.y * weights_cache[3].s4567;
355 r3 += src0.z * weights_cache[3].s89ab;
356 r3 += src0.w * weights_cache[3].scdef;
357 r4 += src0.x * weights_cache[4].s0123;
358 r4 += src0.y * weights_cache[4].s4567;
359 r4 += src0.z * weights_cache[4].s89ab;
360 r4 += src0.w * weights_cache[4].scdef;
361 r5 += src0.x * weights_cache[5].s0123;
362 r5 += src0.y * weights_cache[5].s4567;
363 r5 += src0.z * weights_cache[5].s89ab;
364 r5 += src0.w * weights_cache[5].scdef;
365 r6 += src0.x * weights_cache[6].s0123;
366 r6 += src0.y * weights_cache[6].s4567;
367 r6 += src0.z * weights_cache[6].s89ab;
368 r6 += src0.w * weights_cache[6].scdef;
369 r7 += src0.x * weights_cache[7].s0123;
370 r7 += src0.y * weights_cache[7].s4567;
371 r7 += src0.z * weights_cache[7].s89ab;
372 r7 += src0.w * weights_cache[7].scdef;
373 r0 += src1.x * weights_cache[8].s0123;
374 r0 += src1.y * weights_cache[8].s4567;
375 r0 += src1.z * weights_cache[8].s89ab;
376 r0 += src1.w * weights_cache[8].scdef;
377 r1 += src1.x * weights_cache[9].s0123;
378 r1 += src1.y * weights_cache[9].s4567;
379 r1 += src1.z * weights_cache[9].s89ab;
380 r1 += src1.w * weights_cache[9].scdef;
381 r2 += src1.x * weights_cache[10].s0123;
382 r2 += src1.y * weights_cache[10].s4567;
383 r2 += src1.z * weights_cache[10].s89ab;
384 r2 += src1.w * weights_cache[10].scdef;
385 r3 += src1.x * weights_cache[11].s0123;
386 r3 += src1.y * weights_cache[11].s4567;
387 r3 += src1.z * weights_cache[11].s89ab;
388 r3 += src1.w * weights_cache[11].scdef;
389 r4 += src1.x * weights_cache[12].s0123;
390 r4 += src1.y * weights_cache[12].s4567;
391 r4 += src1.z * weights_cache[12].s89ab;
392 r4 += src1.w * weights_cache[12].scdef;
393 r5 += src1.x * weights_cache[13].s0123;
394 r5 += src1.y * weights_cache[13].s4567;
395 r5 += src1.z * weights_cache[13].s89ab;
396 r5 += src1.w * weights_cache[13].scdef;
397 r6 += src1.x * weights_cache[14].s0123;
398 r6 += src1.y * weights_cache[14].s4567;
399 r6 += src1.z * weights_cache[14].s89ab;
400 r6 += src1.w * weights_cache[14].scdef;
401 r7 += src1.x * weights_cache[15].s0123;
402 r7 += src1.y * weights_cache[15].s4567;
403 r7 += src1.z * weights_cache[15].s89ab;
404 r7 += src1.w * weights_cache[15].scdef;
405 } while (coord_s < shared_int4_2.x);
406
407 coord_s = mul24(Z, 8);
408 coord_x = X;
409 coord_y = Y;
410 if (coord_s < shared_int4_0.y) {
411 half4 res = convert_half4(r0);
412 if (coord_s < 0) {
413 res += read_imageh(src_tensor_image2d, smp_zero, (int2) ((0), ((0) * shared_int4_2.x + (0))));
414 }
415 dst_tensor_buffer[(((coord_s) *shared_int4_0.x + (coord_y)) * shared_int4_0.z + (coord_x))] = res;
416 coord_s++;
417 }
418 if (coord_s < shared_int4_0.y) {
419 half4 res = convert_half4(r1);
420 if (coord_s < 0) {
421 res += read_imageh(src_tensor_image2d, smp_zero, (int2) ((0), ((0) * shared_int4_2.x + (0))));
422 }
423 dst_tensor_buffer[(((coord_s) *shared_int4_0.x + (coord_y)) * shared_int4_0.z + (coord_x))] = res;
424 coord_s++;
425 }
426 if (coord_s < shared_int4_0.y) {
427 half4 res = convert_half4(r2);
428 if (coord_s < 0) {
429 res += read_imageh(src_tensor_image2d, smp_zero, (int2) ((0), ((0) * shared_int4_2.x + (0))));
430 }
431 dst_tensor_buffer[(((coord_s) *shared_int4_0.x + (coord_y)) * shared_int4_0.z + (coord_x))] = res;
432 coord_s++;
433 }
434 if (coord_s < shared_int4_0.y) {
435 half4 res = convert_half4(r3);
436 if (coord_s < 0) {
437 res += read_imageh(src_tensor_image2d, smp_zero, (int2) ((0), ((0) * shared_int4_2.x + (0))));
438 }
439 dst_tensor_buffer[(((coord_s) *shared_int4_0.x + (coord_y)) * shared_int4_0.z + (coord_x))] = res;
440 coord_s++;
441 }
442 if (coord_s < shared_int4_0.y) {
443 half4 res = convert_half4(r4);
444 if (coord_s < 0) {
445 res += read_imageh(src_tensor_image2d, smp_zero, (int2) ((0), ((0) * shared_int4_2.x + (0))));
446 }
447 dst_tensor_buffer[(((coord_s) *shared_int4_0.x + (coord_y)) * shared_int4_0.z + (coord_x))] = res;
448 coord_s++;
449 }
450 if (coord_s < shared_int4_0.y) {
451 half4 res = convert_half4(r5);
452 if (coord_s < 0) {
453 res += read_imageh(src_tensor_image2d, smp_zero, (int2) ((0), ((0) * shared_int4_2.x + (0))));
454 }
455 dst_tensor_buffer[(((coord_s) *shared_int4_0.x + (coord_y)) * shared_int4_0.z + (coord_x))] = res;
456 coord_s++;
457 }
458 if (coord_s < shared_int4_0.y) {
459 half4 res = convert_half4(r6);
460 if (coord_s < 0) {
461 res += read_imageh(src_tensor_image2d, smp_zero, (int2) ((0), ((0) * shared_int4_2.x + (0))));
462 }
463 dst_tensor_buffer[(((coord_s) *shared_int4_0.x + (coord_y)) * shared_int4_0.z + (coord_x))] = res;
464 coord_s++;
465 }
466 if (coord_s < shared_int4_0.y) {
467 half4 res = convert_half4(r7);
468 if (coord_s < 0) {
469 res += read_imageh(src_tensor_image2d, smp_zero, (int2) ((0), ((0) * shared_int4_2.x + (0))));
470 }
471 dst_tensor_buffer[(((coord_s) *shared_int4_0.x + (coord_y)) * shared_int4_0.z + (coord_x))] = res;
472 coord_s++;
473 }
474}
475
476__kernel void adreno_xmem_attn_softmax_reduce_basic(read_only image1d_buffer_t src_tensor_image_buffer,
477 write_only image2d_t dst_tensor_image2d,
478 const int4 shared_int4_0,
479 const int4 shared_int4_1) {
480 int X = get_global_id(0);
481 int Y = get_global_id(1);
482 if (X >= shared_int4_0.z || Y >= shared_int4_0.x) {
483 return;
484 }
485 float sum = 0.0f;
486 int end_channel = shared_int4_0.w;
487 int end_slice = (end_channel + 3) / 4;
488 int start_channel = 0;
489 int start_slice = start_channel / 4;
490 bool need_per_channels_check = start_channel % 4 != 0 || end_channel % 4 != 0;
491 float maximum;
492 {
493 int slice_coord_TMP = (start_channel) / 4;
494 int sub_ch_coord_TMP = (start_channel) % 4;
495 float4 src_TMP = convert_float4(
496 read_imageh(src_tensor_image_buffer, ((slice_coord_TMP) *shared_int4_1.x + (Y)) * shared_int4_1.y + (X)));
497 maximum = (float[4]){ src_TMP.x, src_TMP.y, src_TMP.z, src_TMP.w }[sub_ch_coord_TMP];
498 };
499 for (int d = start_slice; d < end_slice; d += 1) {
500 float4 mask_dot = (float4) (1.f);
501 float4 src =
502 convert_float4(read_imageh(src_tensor_image_buffer, ((d) *shared_int4_1.x + (Y)) * shared_int4_1.y + (X)));
503 if (need_per_channels_check && (d == start_slice || d == end_slice - 1)) {
504 if (d * 4 + 0 < start_channel || d * 4 + 0 >= end_channel) {
505 mask_dot.x = 0.f;
506 src.x = maximum;
507 }
508 if (d * 4 + 1 < start_channel || d * 4 + 1 >= end_channel) {
509 mask_dot.y = 0.f;
510 src.y = maximum;
511 }
512 if (d * 4 + 2 < start_channel || d * 4 + 2 >= end_channel) {
513 mask_dot.z = 0.f;
514 src.z = maximum;
515 }
516 if (d * 4 + 3 < start_channel || d * 4 + 3 >= end_channel) {
517 mask_dot.w = 0.f;
518 src.w = maximum;
519 }
520 }
521 float new_max = max(src.x, src.y);
522 new_max = max(new_max, src.z);
523 new_max = max(new_max, src.w);
524 new_max = max(new_max, maximum);
525 float scale = native_exp(maximum - new_max);
526 maximum = new_max;
527 sum *= scale;
528 float4 exp_res = native_exp(src - maximum);
529 sum += dot(mask_dot, exp_res);
530 }
531 if (!isfinite(maximum) || sum == 0.0f) {
532 write_imageh(dst_tensor_image2d, (int2) (X, Y), (half4) (0.0h));
533 return;
534 }
535 write_imageh(dst_tensor_image2d, (int2) (X, Y),
536 (half4) (convert_half(1.0f / sum), convert_half(maximum), 0.0h, 0.0h));
537}
538
539__kernel void adreno_xmem_attn_softmax_apply_basic(global half4 * dst_tensor_buffer,
540 read_only image1d_buffer_t src_tensor_image_buffer,
541 read_only image2d_t src_tensor_1_image2d,
542 const int4 shared_int4_0,
543 const int4 shared_int4_1) {
544 int X = get_global_id(0);
545 int Y = get_global_id(1);
546 int Z = get_global_id(2);
547 if (X >= shared_int4_0.z || Y >= shared_int4_0.x || Z >= shared_int4_0.y) {
548 return;
549 }
550 half4 src = read_imageh(src_tensor_image_buffer, ((Z) *shared_int4_1.x + (Y)) * shared_int4_1.y + (X));
551 {
552 half4 src_final;
553 {
554 {
555 half4 exp_val = read_imageh(src_tensor_1_image2d, smp_zero, (int2) (X, Y));
556 src_final = exp(src - exp_val.y) * exp_val.x;
557 const int k = Z * 4;
558 const int n_kv = shared_int4_1.z;
559 if (k + 0 >= n_kv) {
560 src_final.x = 0.0h;
561 }
562 if (k + 1 >= n_kv) {
563 src_final.y = 0.0h;
564 }
565 if (k + 2 >= n_kv) {
566 src_final.z = 0.0h;
567 }
568 if (k + 3 >= n_kv) {
569 src_final.w = 0.0h;
570 }
571 }
572 }
573 dst_tensor_buffer[(((Z) *shared_int4_0.x + (Y)) * shared_int4_0.z + (X))] = src_final;
574 };
575}
576
577__kernel void adreno_xmem_attn_mask_scores(global half4 * dst_score_tensor_buffer,
578 read_only image1d_buffer_t src_score_image_buffer,
579 const global half * mask,
580 const ulong mask_offset,
581 const int q_width,
582 const int n_q,
583 const int n_kv,
584 const int n_kv_padded,
585 const int kv_heads_total,
586 const int n_head,
587 const int n_head_kv,
588 const ulong mask_nb1,
589 const ulong mask_nb2,
590 const ulong mask_nb3,
591 const int mask_ne2,
592 const int mask_ne3) {
593 const int X = get_global_id(0);
594 const int Y = get_global_id(1);
595 const int Z = get_global_id(2);
596 const int npack = n_kv_padded / 4;
597 if (X >= q_width || Y >= kv_heads_total || Z >= npack) {
598 return;
599 }
600
601 const int gqa = n_head / n_head_kv;
602 const int head_kv = Y % n_head_kv;
603 const int batch = Y / n_head_kv;
604 const int head_group = X / n_q;
605 const int q = X - head_group * n_q;
606 const int head = head_kv * gqa + head_group;
607 const int mask_head_idx = head % mask_ne2;
608 const int mask_batch_idx = batch % mask_ne3;
609 const global char * mask_base = (const global char *) mask + mask_offset;
610 const global half * mask_row = (const global half *) (mask_base + mask_batch_idx * mask_nb3 +
611 mask_head_idx * mask_nb2 + q * mask_nb1);
612
613 const half4 score = read_imageh(src_score_image_buffer, ((Z * kv_heads_total + Y) * q_width + X));
614 float vals[4] = {
615 convert_float(score.x),
616 convert_float(score.y),
617 convert_float(score.z),
618 convert_float(score.w),
619 };
620
621 for (int lane = 0; lane < 4; ++lane) {
622 const int k_idx = Z * 4 + lane;
623 if (k_idx >= n_kv) {
624 vals[lane] = -INFINITY;
625 } else {
626 vals[lane] += convert_float(mask_row[k_idx]);
627 }
628 }
629
630 dst_score_tensor_buffer[((Z * kv_heads_total + Y) * q_width + X)] =
631 (half4) (convert_half(vals[0]), convert_half(vals[1]), convert_half(vals[2]), convert_half(vals[3]));
632}
633
634__kernel void adreno_xmem_attn_pack_v(global half4 * dst_tensor_buffer,
635 read_only image2d_t src_image2d,
636 const int4 shared_int4_0,
637 const int4 shared_int4_1) {
638 int linear_index = get_global_id(0);
639 if (linear_index >= shared_int4_0.y) {
640 return;
641 }
642 if (get_global_id(1) != 0) {
643 return;
644 }
645 if (get_global_id(2) != 0) {
646 return;
647 }
648 int dst_o_sp_i_ogroup = linear_index;
649 int dst_ogroup = dst_o_sp_i_ogroup % shared_int4_0.x;
650 int dst_o_sp_i = dst_o_sp_i_ogroup / shared_int4_0.x;
651 int dst_i = dst_o_sp_i % shared_int4_0.z;
652 int dst_o_sp = dst_o_sp_i / shared_int4_0.z;
653 int dst_sp = dst_o_sp % shared_int4_1.x;
654 int dst_o = dst_o_sp / shared_int4_1.x;
655 int i_slice = dst_i;
656 int o_slice = dst_o * shared_int4_0.x + dst_ogroup;
657 int spatial_linear = dst_sp;
658 int W = spatial_linear % shared_int4_1.y;
659 int H = spatial_linear / shared_int4_1.y;
660 half4 w0 = (half4) (0);
661 half4 w1 = (half4) (0);
662 half4 w2 = (half4) (0);
663 half4 w3 = (half4) (0);
664
665 if (i_slice * 4 < shared_int4_0.w && o_slice < shared_int4_1.z) {
666 w0 = read_imageh(src_image2d, smp_zero, (int2) ((i_slice * 4), ((W) *shared_int4_1.z + (o_slice))));
667 }
668 if (i_slice * 4 + 1 < shared_int4_0.w && o_slice < shared_int4_1.z) {
669 w1 = read_imageh(src_image2d, smp_zero, (int2) ((i_slice * 4 + 1), ((W) *shared_int4_1.z + (o_slice))));
670 }
671 if (i_slice * 4 + 2 < shared_int4_0.w && o_slice < shared_int4_1.z) {
672 w2 = read_imageh(src_image2d, smp_zero, (int2) ((i_slice * 4 + 2), ((W) *shared_int4_1.z + (o_slice))));
673 }
674 if (i_slice * 4 + 3 < shared_int4_0.w && o_slice < shared_int4_1.z) {
675 w3 = read_imageh(src_image2d, smp_zero, (int2) ((i_slice * 4 + 3), ((W) *shared_int4_1.z + (o_slice))));
676 }
677 half4 r0 = w0;
678 half4 r1 = w1;
679 half4 r2 = w2;
680 half4 r3 = w3;
681 dst_tensor_buffer[linear_index * 4 + 0] = r0;
682 dst_tensor_buffer[linear_index * 4 + 1] = r1;
683 dst_tensor_buffer[linear_index * 4 + 2] = r2;
684 dst_tensor_buffer[linear_index * 4 + 3] = r3;
685}
686
687__attribute__((qcom_max_concurrent_subgroups(12))) __kernel void adreno_xmem_attn_pv_gemm(
688 constant half8 * weights_buffer __attribute__((sub_group_uniform)),
689 constant half8 * xmem_buffer __attribute__((max_constant_size((6144)))),
690 read_only image1d_buffer_t src_tensor_image_buffer,
691 write_only image2d_t dst_tensor_image2d,
692 const int4 shared_int4_0,
693 const int4 shared_int4_1,
694 const int4 shared_int4_2,
695 const int4 shared_int4_3) {
696 int X = get_group_id(1) * get_local_size(0) + get_local_id(0);
697 int Y = get_group_id(2) * get_local_size(1) + get_local_id(1);
698 int Z = get_group_id(0) * get_local_size(2) + get_local_id(2);
699 if (X >= shared_int4_0.z || Y >= shared_int4_0.x) {
700 return;
701 }
702 if (Z * 8 >= shared_int4_0.y) {
703 return;
704 }
705
706 half4 r0 = (half4) (0.f);
707 half4 r1 = (half4) (0.f);
708 half4 r2 = (half4) (0.f);
709 half4 r3 = (half4) (0.f);
710 half4 r4 = (half4) (0.f);
711 half4 r5 = (half4) (0.f);
712 half4 r6 = (half4) (0.f);
713 half4 r7 = (half4) (0.f);
714 int x_coord = mad24(X, shared_int4_2.w, shared_int4_1.y);
715 int y_coord = mad24(Y, shared_int4_3.x, shared_int4_1.z);
716 int coord_x, coord_y, coord_s;
717 int f_offset = (Z * shared_int4_1.w + Y) * shared_int4_1.x * 32;
718
719 int subgroup_id = (int) ((0x1F & qcom_get_physical_sub_group_id()));
720 subgroup_id = subgroup_id % 12;
721 int c_offset = mul24(subgroup_id, shared_int4_0.w);
722 __constant half16 * weights_cache = (__constant half16 *) &xmem_buffer[c_offset];
723 coord_y = Y;
724 coord_x = X;
725 int addr = (((0) * shared_int4_1.w + (coord_y)) * shared_int4_2.z + (coord_x));
726 int dz = shared_int4_2.x;
727 coord_s = 0;
728 do {
729 half4 src0 = read_imageh(src_tensor_image_buffer, addr);
730 addr += dz;
731 coord_s++;
732 half4 src1 = read_imageh(src_tensor_image_buffer, addr);
733 addr += dz;
734 coord_s++;
735 qcom_sub_group_constant_load8(xmem_buffer, weights_buffer, c_offset, f_offset >> 1, 32);
736 f_offset += 64;
737 qcom_sub_group_sync(QCOM_CLK_CONST_LOAD_SYNC);
738 r0 += src0.x * weights_cache[0].s0123;
739 r0 += src0.y * weights_cache[0].s4567;
740 r0 += src0.z * weights_cache[0].s89ab;
741 r0 += src0.w * weights_cache[0].scdef;
742 r1 += src0.x * weights_cache[1].s0123;
743 r1 += src0.y * weights_cache[1].s4567;
744 r1 += src0.z * weights_cache[1].s89ab;
745 r1 += src0.w * weights_cache[1].scdef;
746 r2 += src0.x * weights_cache[2].s0123;
747 r2 += src0.y * weights_cache[2].s4567;
748 r2 += src0.z * weights_cache[2].s89ab;
749 r2 += src0.w * weights_cache[2].scdef;
750 r3 += src0.x * weights_cache[3].s0123;
751 r3 += src0.y * weights_cache[3].s4567;
752 r3 += src0.z * weights_cache[3].s89ab;
753 r3 += src0.w * weights_cache[3].scdef;
754 r4 += src0.x * weights_cache[4].s0123;
755 r4 += src0.y * weights_cache[4].s4567;
756 r4 += src0.z * weights_cache[4].s89ab;
757 r4 += src0.w * weights_cache[4].scdef;
758 r5 += src0.x * weights_cache[5].s0123;
759 r5 += src0.y * weights_cache[5].s4567;
760 r5 += src0.z * weights_cache[5].s89ab;
761 r5 += src0.w * weights_cache[5].scdef;
762 r6 += src0.x * weights_cache[6].s0123;
763 r6 += src0.y * weights_cache[6].s4567;
764 r6 += src0.z * weights_cache[6].s89ab;
765 r6 += src0.w * weights_cache[6].scdef;
766 r7 += src0.x * weights_cache[7].s0123;
767 r7 += src0.y * weights_cache[7].s4567;
768 r7 += src0.z * weights_cache[7].s89ab;
769 r7 += src0.w * weights_cache[7].scdef;
770 r0 += src1.x * weights_cache[8].s0123;
771 r0 += src1.y * weights_cache[8].s4567;
772 r0 += src1.z * weights_cache[8].s89ab;
773 r0 += src1.w * weights_cache[8].scdef;
774 r1 += src1.x * weights_cache[9].s0123;
775 r1 += src1.y * weights_cache[9].s4567;
776 r1 += src1.z * weights_cache[9].s89ab;
777 r1 += src1.w * weights_cache[9].scdef;
778 r2 += src1.x * weights_cache[10].s0123;
779 r2 += src1.y * weights_cache[10].s4567;
780 r2 += src1.z * weights_cache[10].s89ab;
781 r2 += src1.w * weights_cache[10].scdef;
782 r3 += src1.x * weights_cache[11].s0123;
783 r3 += src1.y * weights_cache[11].s4567;
784 r3 += src1.z * weights_cache[11].s89ab;
785 r3 += src1.w * weights_cache[11].scdef;
786 r4 += src1.x * weights_cache[12].s0123;
787 r4 += src1.y * weights_cache[12].s4567;
788 r4 += src1.z * weights_cache[12].s89ab;
789 r4 += src1.w * weights_cache[12].scdef;
790 r5 += src1.x * weights_cache[13].s0123;
791 r5 += src1.y * weights_cache[13].s4567;
792 r5 += src1.z * weights_cache[13].s89ab;
793 r5 += src1.w * weights_cache[13].scdef;
794 r6 += src1.x * weights_cache[14].s0123;
795 r6 += src1.y * weights_cache[14].s4567;
796 r6 += src1.z * weights_cache[14].s89ab;
797 r6 += src1.w * weights_cache[14].scdef;
798 r7 += src1.x * weights_cache[15].s0123;
799 r7 += src1.y * weights_cache[15].s4567;
800 r7 += src1.z * weights_cache[15].s89ab;
801 r7 += src1.w * weights_cache[15].scdef;
802 } while (coord_s < shared_int4_2.y);
803
804 coord_s = mul24(Z, 8);
805 coord_x = X;
806 coord_y = Y;
807 if (coord_s < shared_int4_0.y) {
808 half4 res = convert_half4(r0);
809 if (coord_s < 0) {
810 res += read_imageh(src_tensor_image_buffer, ((0) * shared_int4_1.w + (0)) * shared_int4_2.z + (0));
811 }
812 write_imageh(dst_tensor_image2d, (int2) ((coord_x), ((coord_y) *shared_int4_0.y + (coord_s))), res);
813 coord_s++;
814 }
815 if (coord_s < shared_int4_0.y) {
816 half4 res = convert_half4(r1);
817 if (coord_s < 0) {
818 res += read_imageh(src_tensor_image_buffer, ((0) * shared_int4_1.w + (0)) * shared_int4_2.z + (0));
819 }
820 write_imageh(dst_tensor_image2d, (int2) ((coord_x), ((coord_y) *shared_int4_0.y + (coord_s))), res);
821 coord_s++;
822 }
823 if (coord_s < shared_int4_0.y) {
824 half4 res = convert_half4(r2);
825 if (coord_s < 0) {
826 res += read_imageh(src_tensor_image_buffer, ((0) * shared_int4_1.w + (0)) * shared_int4_2.z + (0));
827 }
828 write_imageh(dst_tensor_image2d, (int2) ((coord_x), ((coord_y) *shared_int4_0.y + (coord_s))), res);
829 coord_s++;
830 }
831 if (coord_s < shared_int4_0.y) {
832 half4 res = convert_half4(r3);
833 if (coord_s < 0) {
834 res += read_imageh(src_tensor_image_buffer, ((0) * shared_int4_1.w + (0)) * shared_int4_2.z + (0));
835 }
836 write_imageh(dst_tensor_image2d, (int2) ((coord_x), ((coord_y) *shared_int4_0.y + (coord_s))), res);
837 coord_s++;
838 }
839 if (coord_s < shared_int4_0.y) {
840 half4 res = convert_half4(r4);
841 if (coord_s < 0) {
842 res += read_imageh(src_tensor_image_buffer, ((0) * shared_int4_1.w + (0)) * shared_int4_2.z + (0));
843 }
844 write_imageh(dst_tensor_image2d, (int2) ((coord_x), ((coord_y) *shared_int4_0.y + (coord_s))), res);
845 coord_s++;
846 }
847 if (coord_s < shared_int4_0.y) {
848 half4 res = convert_half4(r5);
849 if (coord_s < 0) {
850 res += read_imageh(src_tensor_image_buffer, ((0) * shared_int4_1.w + (0)) * shared_int4_2.z + (0));
851 }
852 write_imageh(dst_tensor_image2d, (int2) ((coord_x), ((coord_y) *shared_int4_0.y + (coord_s))), res);
853 coord_s++;
854 }
855 if (coord_s < shared_int4_0.y) {
856 half4 res = convert_half4(r6);
857 if (coord_s < 0) {
858 res += read_imageh(src_tensor_image_buffer, ((0) * shared_int4_1.w + (0)) * shared_int4_2.z + (0));
859 }
860 write_imageh(dst_tensor_image2d, (int2) ((coord_x), ((coord_y) *shared_int4_0.y + (coord_s))), res);
861 coord_s++;
862 }
863 if (coord_s < shared_int4_0.y) {
864 half4 res = convert_half4(r7);
865 if (coord_s < 0) {
866 res += read_imageh(src_tensor_image_buffer, ((0) * shared_int4_1.w + (0)) * shared_int4_2.z + (0));
867 }
868 write_imageh(dst_tensor_image2d, (int2) ((coord_x), ((coord_y) *shared_int4_0.y + (coord_s))), res);
869 coord_s++;
870 }
871}