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-metal / kernels / misc.metal

1004 lines · 35438 bytes Raw Blame History
1#include "common.h"
2
3kernel void kernel_argmax_f32(
4 constant ggml_metal_kargs_argmax & args,
5 device const char * src0,
6 device char * dst,
7 threadgroup char * shmem [[threadgroup(0)]],
8 uint tgpig[[threadgroup_position_in_grid]],
9 uint tpitg[[thread_position_in_threadgroup]],
10 uint sgitg[[simdgroup_index_in_threadgroup]],
11 uint tiisg[[thread_index_in_simdgroup]],
12 uint ntg[[threads_per_threadgroup]]) {
13 device const float * x_row = (device const float *) ((device const char *) src0 + tgpig * args.nb01);
14
15 float lmax = -INFINITY;
16 int32_t larg = -1;
17
18 for (int i00 = tpitg; i00 < args.ne00; i00 += ntg) {
19 if (x_row[i00] > lmax) {
20 lmax = x_row[i00];
21 larg = i00;
22 }
23 }
24
25 // find the argmax value in the block
26 float max_val = simd_max(lmax);
27 int32_t arg_val = simd_max(select(-1, larg, lmax == max_val));
28
29 device int32_t * dst_i32 = (device int32_t *) dst;
30
31 threadgroup float * shared_maxval = (threadgroup float *) shmem;
32 threadgroup int32_t * shared_argmax = (threadgroup int32_t *) shmem + N_SIMDWIDTH;
33
34 if (ntg > N_SIMDWIDTH) {
35 if (sgitg == 0) {
36 shared_maxval[tiisg] = -INFINITY;
37 shared_argmax[tiisg] = -1;
38 }
39
40 threadgroup_barrier(mem_flags::mem_threadgroup);
41
42 if (tiisg == 0) {
43 shared_maxval[sgitg] = max_val;
44 shared_argmax[sgitg] = arg_val;
45 }
46
47 threadgroup_barrier(mem_flags::mem_threadgroup);
48
49 max_val = shared_maxval[tiisg];
50 arg_val = shared_argmax[tiisg];
51
52 float max_val_reduced = simd_max(max_val);
53 int32_t arg_val_reduced = simd_max(select(-1, arg_val, max_val == max_val_reduced));
54
55 dst_i32[tgpig] = arg_val_reduced;
56
57 return;
58 }
59
60 dst_i32[tgpig] = arg_val;
61}
62
63kernel void kernel_argmax_part_f32(
64 constant ggml_metal_kargs_argmax & args,
65 device const char * src0,
66 device char * tmp,
67 threadgroup char * shmem [[threadgroup(0)]],
68 uint3 tgpig[[threadgroup_position_in_grid]],
69 uint3 tpitg[[thread_position_in_threadgroup]],
70 uint sgitg[[simdgroup_index_in_threadgroup]],
71 uint tiisg[[thread_index_in_simdgroup]],
72 uint3 ntg[[threads_per_threadgroup]]) {
73 const int row = tgpig.y;
74 const int part = tgpig.x;
75 const int ne00 = (int) args.ne00;
76 const int npart = (int) args.npart;
77 const int chunk = (ne00 + npart - 1) / npart;
78 const int start = part * chunk;
79 const int end = min(start + chunk, ne00);
80
81 device const float * x_row = (device const float *) ((device const char *) src0 + row * args.nb01);
82
83 float lmax = -INFINITY;
84 int32_t larg = -1;
85
86 for (int i00 = start + tpitg.x; i00 < end; i00 += ntg.x) {
87 if (x_row[i00] > lmax) {
88 lmax = x_row[i00];
89 larg = i00;
90 }
91 }
92
93 // find the argmax value in the block
94 float max_val = simd_max(lmax);
95 int32_t arg_val = simd_max(select(-1, larg, lmax == max_val));
96
97 threadgroup float * shared_maxval = (threadgroup float *) shmem;
98 threadgroup int32_t * shared_argmax = (threadgroup int32_t *) shmem + N_SIMDWIDTH;
99
100 if (ntg.x > N_SIMDWIDTH) {
101 if (sgitg == 0) {
102 shared_maxval[tiisg] = -INFINITY;
103 shared_argmax[tiisg] = -1;
104 }
105
106 threadgroup_barrier(mem_flags::mem_threadgroup);
107
108 if (tiisg == 0) {
109 shared_maxval[sgitg] = max_val;
110 shared_argmax[sgitg] = arg_val;
111 }
112
113 threadgroup_barrier(mem_flags::mem_threadgroup);
114
115 max_val = shared_maxval[tiisg];
116 arg_val = shared_argmax[tiisg];
117
118 float max_val_reduced = simd_max(max_val);
119 int32_t arg_val_reduced = simd_max(select(-1, arg_val, max_val == max_val_reduced));
120
121 max_val = max_val_reduced;
122 arg_val = arg_val_reduced;
123 }
124
125 if (tpitg.x == 0) {
126 device float * tmp_max = (device float *) tmp;
127 device int32_t * tmp_arg = (device int32_t *) tmp + npart * (int) args.ne01;
128
129 tmp_max[row*npart + part] = max_val;
130 tmp_arg[row*npart + part] = arg_val;
131 }
132}
133
134kernel void kernel_argmax_final_f32(
135 constant ggml_metal_kargs_argmax & args,
136 device const char * tmp,
137 device char * dst,
138 threadgroup char * shmem [[threadgroup(0)]],
139 uint3 tgpig[[threadgroup_position_in_grid]],
140 uint3 tpitg[[thread_position_in_threadgroup]],
141 uint sgitg[[simdgroup_index_in_threadgroup]],
142 uint tiisg[[thread_index_in_simdgroup]],
143 uint3 ntg[[threads_per_threadgroup]]) {
144 const int row = tgpig.x;
145 const int npart = (int) args.npart;
146
147 device const float * tmp_max = (device const float *) tmp;
148 device const int32_t * tmp_arg = (device const int32_t *) tmp + npart * (int) args.ne01;
149
150 float lmax = -INFINITY;
151 int32_t larg = -1;
152
153 for (int i = tpitg.x; i < npart; i += ntg.x) {
154 if (tmp_max[row*npart + i] > lmax) {
155 lmax = tmp_max[row*npart + i];
156 larg = tmp_arg[row*npart + i];
157 }
158 }
159
160 // find the argmax value in the block
161 float max_val = simd_max(lmax);
162 int32_t arg_val = simd_max(select(-1, larg, lmax == max_val));
163
164 threadgroup float * shared_maxval = (threadgroup float *) shmem;
165 threadgroup int32_t * shared_argmax = (threadgroup int32_t *) shmem + N_SIMDWIDTH;
166
167 if (ntg.x > N_SIMDWIDTH) {
168 if (sgitg == 0) {
169 shared_maxval[tiisg] = -INFINITY;
170 shared_argmax[tiisg] = -1;
171 }
172
173 threadgroup_barrier(mem_flags::mem_threadgroup);
174
175 if (tiisg == 0) {
176 shared_maxval[sgitg] = max_val;
177 shared_argmax[sgitg] = arg_val;
178 }
179
180 threadgroup_barrier(mem_flags::mem_threadgroup);
181
182 max_val = shared_maxval[tiisg];
183 arg_val = shared_argmax[tiisg];
184
185 float max_val_reduced = simd_max(max_val);
186 int32_t arg_val_reduced = simd_max(select(-1, arg_val, max_val == max_val_reduced));
187
188 max_val = max_val_reduced;
189 arg_val = arg_val_reduced;
190 }
191
192 if (tpitg.x == 0) {
193 ((device int32_t *) dst)[row] = arg_val;
194 }
195}
196
197kernel void kernel_diag_f32(
198 constant ggml_metal_kargs_diag & args,
199 device const char * src0,
200 device char * dst,
201 uint3 tgpig[[threadgroup_position_in_grid]],
202 ushort tiitg[[thread_index_in_threadgroup]]) {
203 constexpr short NW = N_SIMDWIDTH;
204
205 const int32_t i3 = tgpig.z;
206 const int32_t i2 = tgpig.y;
207 const int32_t i1 = tgpig.x;
208
209 device const float * src0_ptr = (device const float *)(src0 + i2*args.nb02 + i3*args.nb03);
210 device float * dst_ptr = (device float *)(dst + i1*args.nb01 + i2*args.nb2 + i3*args.nb3);
211
212 for (int i0 = tiitg; i0 < args.ne0; i0 += NW) {
213 dst_ptr[i0] = i0 == i1 ? src0_ptr[i0] : 0.0f;
214 }
215}
216
217kernel void kernel_roll_f32(
218 constant ggml_metal_kargs_roll & args,
219 device const char * src0,
220 device char * dst,
221 uint3 tgpig[[threadgroup_position_in_grid]],
222 uint3 tpitg[[thread_position_in_threadgroup]],
223 uint3 ntg[[threads_per_threadgroup]]) {
224
225 const int64_t i3 = tgpig.z;
226 const int64_t i2 = tgpig.y;
227 const int64_t i1 = tgpig.x;
228
229 device const float * src0_ptr = (device const float *) src0;
230 device float * dst_ptr = (device float *) dst;
231
232 for (int i0 = tpitg.x; i0 < args.ne0; i0 += ntg.x) {
233 // apply shifts and wrap around
234 int64_t i00 = i0 - args.s0;
235 int64_t i01 = i1 - args.s1;
236 int64_t i02 = i2 - args.s2;
237 int64_t i03 = i3 - args.s3;
238
239 if (i00 < 0) { i00 += args.ne00; } else if (i00 >= args.ne00) { i00 -= args.ne00; }
240 if (i01 < 0) { i01 += args.ne01; } else if (i01 >= args.ne01) { i01 -= args.ne01; }
241 if (i02 < 0) { i02 += args.ne02; } else if (i02 >= args.ne02) { i02 -= args.ne02; }
242 if (i03 < 0) { i03 += args.ne03; } else if (i03 >= args.ne03) { i03 -= args.ne03; }
243
244 int64_t src_idx = i03*args.ne02*args.ne01*args.ne00 + i02*args.ne01*args.ne00 + i01*args.ne00 + i00;
245 int64_t dst_idx = i3 *args.ne2 *args.ne1 *args.ne0 + i2 *args.ne1 *args.ne0 + i1 *args.ne0 + i0;
246
247 dst_ptr[dst_idx] = src0_ptr[src_idx];
248 }
249}
250
251constant bool FC_pad_circular [[function_constant(FC_PAD + 0)]];
252
253// circular means on a torus, so the coordinates wrap around
254static inline int32_t wrap_around(int32_t coord, int32_t size) {
255 return (coord + size) % size;
256}
257
258kernel void kernel_pad_f32(
259 constant ggml_metal_kargs_pad & args,
260 device const char * src0,
261 device char * dst,
262 uint3 tgpig[[threadgroup_position_in_grid]],
263 uint3 tpitg[[thread_position_in_threadgroup]],
264 uint3 ntg[[threads_per_threadgroup]]) {
265 const int32_t i3 = tgpig.z;
266 const int32_t i2 = tgpig.y;
267 const int32_t k0 = tgpig.x/args.ne1;
268 const int32_t i1 = tgpig.x - k0*args.ne1;
269
270 const int32_t ne00 = args.ne00;
271 const int32_t ne01 = args.ne01;
272 const int32_t ne02 = args.ne02;
273 const int32_t ne03 = args.ne03;
274
275 int32_t i01 = i1 - args.lp1;
276 int32_t i02 = i2 - args.lp2;
277 int32_t i03 = i3 - args.lp3;
278
279 if (FC_pad_circular) {
280 i01 = wrap_around(i01, ne01);
281 i02 = wrap_around(i02, ne02);
282 i03 = wrap_around(i03, ne03);
283 }
284
285 device float * dst_ptr = (device float *) (dst + i3*args.nb3 + i2*args.nb2 + i1*args.nb1);
286
287 // the row lies in the padded region, so no source row backs it
288 if (i01 < 0 || i01 >= ne01 ||
289 i02 < 0 || i02 >= ne02 ||
290 i03 < 0 || i03 >= ne03) {
291 for (int32_t l0 = 0; l0 < 1024; l0 += ntg.x) {
292 const int32_t i0 = k0*1024 + tpitg.x + l0;
293 if (i0 >= args.ne0) {
294 break;
295 }
296
297 dst_ptr[i0] = 0.0f;
298 }
299
300 return;
301 }
302
303 device const char * src0_row = src0 + i03*args.nb03 + i02*args.nb02 + i01*args.nb01;
304
305 for (int32_t l0 = 0; l0 < 1024; l0 += ntg.x) {
306 const int32_t i0 = k0*1024 + tpitg.x + l0;
307 if (i0 >= args.ne0) {
308 break;
309 }
310
311 int32_t i00 = i0 - args.lp0;
312
313 if (FC_pad_circular) {
314 i00 = wrap_around(i00, ne00);
315 }
316
317 dst_ptr[i0] = i00 >= 0 && i00 < ne00 ? *((device const float *) (src0_row + i00*args.nb00)) : 0.0f;
318 }
319}
320
321// TODO: this is slow - optimize
322kernel void kernel_pad_reflect_1d_f32(
323 constant ggml_metal_kargs_pad_reflect_1d & args,
324 device const char * src0,
325 device char * dst,
326 uint3 tgpig[[threadgroup_position_in_grid]],
327 uint3 tgpg[[threadgroups_per_grid]],
328 uint3 tpitg[[thread_position_in_threadgroup]],
329 uint3 ntg[[threads_per_threadgroup]]) {
330
331 const int64_t i3 = tgpig.z;
332 const int64_t i2 = tgpig.y;
333 const int64_t i1 = tgpig.x;
334
335 const int64_t i03 = i3;
336 const int64_t i02 = i2;
337 const int64_t i01 = i1;
338
339 device const float * src0_ptr = (device const float *) (src0 + i03*args.nb03 + i02*args.nb02 + i01*args.nb01);
340 device float * dst_ptr = (device float *) (dst + i3*args.nb3 + i2*args.nb2 + i1*args.nb1);
341
342 if (i1 < args.ne01 && i2 < args.ne02 && i3 < args.ne03) {
343 for (int i0 = tpitg.x; i0 < args.ne0; i0 += ntg.x) {
344 if (i0 < args.p0) {
345 dst_ptr[i0] = src0_ptr[args.p0 - i0];
346 } else if (i0 < args.ne0 - args.p1) {
347 dst_ptr[i0] = src0_ptr[i0 - args.p0];
348 } else {
349 dst_ptr[i0] = src0_ptr[(args.ne0 - args.p1 - args.p0) - (args.p1 + 1 - (args.ne0 - i0)) - 1];
350 }
351 }
352 }
353}
354
355kernel void kernel_arange_f32(
356 constant ggml_metal_kargs_arange & args,
357 device char * dst,
358 uint3 tgpig[[threadgroup_position_in_grid]],
359 uint3 tpitg[[thread_position_in_threadgroup]],
360 uint3 ntg[[threads_per_threadgroup]]) {
361
362 device float * dst_ptr = (device float *) dst;
363
364 for (int i0 = tpitg.x; i0 < args.ne0; i0 += ntg.x) {
365 dst_ptr[i0] = args.start + args.step * i0;
366 }
367}
368
369kernel void kernel_timestep_embedding_f32(
370 constant ggml_metal_kargs_timestep_embedding & args,
371 device const char * src0,
372 device char * dst,
373 uint3 tgpig[[threadgroup_position_in_grid]],
374 uint3 tpitg[[thread_position_in_threadgroup]],
375 uint3 ntg[[threads_per_threadgroup]]) {
376
377 int i = tgpig.x;
378 device float * embed_data = (device float *)(dst + i*args.nb1);
379
380 int half_ = args.dim / 2;
381 for (int j = tpitg.x; j < half_; j += ntg.x) {
382 float timestep = ((device float *)src0)[i];
383 float freq = (float)exp(-log((float)args.max_period) * j / half_);
384 float arg = timestep * freq;
385 embed_data[j ] = cos(arg);
386 embed_data[j + half_] = sin(arg);
387 }
388
389 if (args.dim % 2 != 0 && tpitg.x == 0) {
390 embed_data[2 * half_] = 0.f;
391 }
392}
393
394kernel void kernel_opt_step_adamw_f32(
395 constant ggml_metal_kargs_opt_step_adamw & args,
396 device float * x,
397 device const float * g,
398 device float * g_m,
399 device float * g_v,
400 device const float * pars,
401 uint gid[[thread_position_in_grid]]) {
402
403 if (gid >= args.np) {
404 return;
405 }
406
407 const float alpha = pars[0];
408 const float beta1 = pars[1];
409 const float beta2 = pars[2];
410 const float eps = pars[3];
411 const float wd = pars[4];
412 const float beta1h = pars[5];
413 const float beta2h = pars[6];
414
415 const float gi = g[gid];
416 const float gmi = g_m[gid] * beta1 + gi * (1.0f - beta1);
417 const float gvi = g_v[gid] * beta2 + gi * gi * (1.0f - beta2);
418
419 g_m[gid] = gmi;
420 g_v[gid] = gvi;
421
422 const float mh = gmi * beta1h;
423 const float vh = sqrt(gvi * beta2h) + eps;
424
425 x[gid] = x[gid] * (1.0f - alpha * wd) - alpha * mh / vh;
426}
427
428kernel void kernel_opt_step_sgd_f32(
429 constant ggml_metal_kargs_opt_step_sgd & args,
430 device float * x,
431 device const float * g,
432 device const float * pars,
433 uint gid[[thread_position_in_grid]]) {
434
435 if (gid >= args.np) {
436 return;
437 }
438
439 x[gid] = x[gid] * (1.0f - pars[0] * pars[1]) - pars[0] * g[gid];
440}
441
442template<typename T>
443kernel void kernel_memset(
444 constant ggml_metal_kargs_memset & args,
445 device T * dst,
446 uint tpig[[thread_position_in_grid]]) {
447 dst[tpig] = args.val;
448}
449
450typedef decltype(kernel_memset<int64_t>) kernel_memset_t;
451
452template [[host_name("kernel_memset_i64")]] kernel kernel_memset_t kernel_memset<int64_t>;
453
454constant short FC_count_equal_nsg [[function_constant(FC_COUNT_EQUAL + 0)]];
455
456template<typename T>
457kernel void kernel_count_equal(
458 constant ggml_metal_kargs_count_equal & args,
459 device const char * src0,
460 device const char * src1,
461 device atomic_int * dst,
462 threadgroup int32_t * shmem_i32 [[threadgroup(0)]],
463 uint3 tgpig[[threadgroup_position_in_grid]],
464 ushort3 tpitg[[thread_position_in_threadgroup]],
465 ushort sgitg[[simdgroup_index_in_threadgroup]],
466 ushort tiisg[[thread_index_in_simdgroup]],
467 ushort3 ntg[[threads_per_threadgroup]]) {
468 const short NSG = FC_count_equal_nsg;
469
470 const int i3 = tgpig.z;
471 const int i2 = tgpig.y;
472 const int i1 = tgpig.x;
473
474 if (i3 >= args.ne03 || i2 >= args.ne02 || i1 >= args.ne01) {
475 return;
476 }
477
478 int sum = 0;
479
480 device const char * base0 = src0 + i1*args.nb01 + i2*args.nb02 + i3*args.nb03;
481 device const char * base1 = src1 + i1*args.nb11 + i2*args.nb12 + i3*args.nb13;
482
483 for (int64_t i0 = tpitg.x; i0 < args.ne00; i0 += ntg.x) {
484 const T v0 = *(device const T *)(base0 + i0*args.nb00);
485 const T v1 = *(device const T *)(base1 + i0*args.nb10);
486 sum += (v0 == v1);
487 }
488
489 sum = simd_sum(sum);
490
491 if (tiisg == 0) {
492 shmem_i32[sgitg] = sum;
493 }
494
495 threadgroup_barrier(mem_flags::mem_threadgroup);
496
497 if (sgitg == 0) {
498 float v = 0.0f;
499 if (tpitg.x < NSG) {
500 v = shmem_i32[tpitg.x];
501 }
502
503 float total = simd_sum(v);
504 if (tpitg.x == 0) {
505 atomic_fetch_add_explicit(dst, (int32_t) total, memory_order_relaxed);
506 }
507 }
508}
509
510typedef decltype(kernel_count_equal<int32_t>) kernel_count_equal_t;
511
512template [[host_name("kernel_count_equal_i32")]] kernel kernel_count_equal_t kernel_count_equal<int32_t>;
513
514template <typename T>
515kernel void kernel_snake(
516 constant ggml_metal_kargs_snake & args,
517 device const T * x,
518 device const float * a,
519 device const float * inv_b,
520 device T * dst,
521 uint tgpig [[threadgroup_position_in_grid]],
522 uint tpitg [[thread_position_in_threadgroup]],
523 uint ntg [[threads_per_threadgroup]]) {
524
525 const int idx = tgpig * ntg + tpitg;
526 if (idx >= args.T * args.C) {
527 return;
528 }
529
530 const int c = idx / args.T; // x is [T, C], a / inv_b collapse to [1, C]
531 const float xi = float(x[idx]);
532 const float si = sin(a[c] * xi);
533 dst[idx] = T(xi + si * si * inv_b[c]);
534}
535
536template [[host_name("kernel_snake_f32")]] kernel void kernel_snake<float>(constant ggml_metal_kargs_snake &, device const float *, device const float *, device const float *, device float *, uint, uint, uint);
537template [[host_name("kernel_snake_f16")]] kernel void kernel_snake<half>(constant ggml_metal_kargs_snake &, device const half *, device const float *, device const float *, device half *, uint, uint, uint);
538#if defined(GGML_METAL_HAS_BF16)
539template [[host_name("kernel_snake_bf16")]] kernel void kernel_snake<bfloat>(constant ggml_metal_kargs_snake &, device const bfloat *, device const float *, device const float *, device bfloat *, uint, uint, uint);
540#endif
541
542template<int N, typename src_t>
543kernel void kernel_fwht(
544 constant ggml_metal_kargs_fwht & args,
545 device const src_t * src,
546 device float * dst,
547 uint3 tgpig[[threadgroup_position_in_grid]],
548 ushort sgitg[[simdgroup_index_in_threadgroup]],
549 ushort tiisg[[thread_index_in_simdgroup]],
550 ushort3 ntg[[threads_per_threadgroup]]) {
551
552 constexpr int NW = N_SIMDWIDTH;
553 constexpr int NE = N / NW;
554
555 const float scale = 1.0f / sqrt((float) N);
556
557 const int sg_per_tg = ntg.x / NW;
558 const int64_t r = tgpig.x * sg_per_tg + sgitg;
559 if (r >= args.nrows) {
560 return;
561 }
562
563 src += r * N;
564 dst += r * N;
565
566 const int lane = tiisg;
567
568 float reg[NE];
569 for (int i = 0; i < NE; i++) {
570 reg[i] = float(src[i*NW + lane])*scale;
571 }
572 for (int i = 1; i < NW; i *= 2) {
573 for (int j = 0; j < NE; j++) {
574 const float val = reg[j];
575 const float val2 = simd_shuffle_xor(val, i);
576 reg[j] = val2 - val + 2*((lane & i) == 0)*val;
577 }
578 }
579
580 for (int i = NW; i < N; i *= 2) {
581 const int step = i / NW;
582 for (int j = 0; j < NE; j += (2 * step)) {
583 for (int k = 0; k < step; k++) {
584 const float x = reg[j + k ];
585 const float y = reg[j + k + step];
586 reg[j + k] = x + y;
587 reg[j + k + step] = x - y;
588 }
589 }
590 }
591
592 for (int i = 0; i < NE; i++) {
593 dst[i*NW + lane] = reg[i];
594 }
595}
596
597// Wide blocks: one row per threadgroup instead of per simdgroup, so each thread keeps
598// N/NT values rather than N/32. Butterflies below the simdgroup width still shuffle;
599// those up to NT go through threadgroup memory; the rest stay in registers.
600// TODO: try avoiding branch https://github.com/ggml-org/llama.cpp/pull/29094#discussion_r4049563223
601// TODO: try to unroll loops
602template<int N, int NT, typename src_t>
603kernel void kernel_fwht_tg(
604 constant ggml_metal_kargs_fwht & args,
605 device const src_t * src,
606 device float * dst,
607 uint3 tgpig[[threadgroup_position_in_grid]],
608 ushort sgitg[[simdgroup_index_in_threadgroup]],
609 ushort tiisg[[thread_index_in_simdgroup]],
610 ushort3 ntg[[threads_per_threadgroup]]) {
611
612 constexpr int NW = N_SIMDWIDTH;
613 constexpr int NE = N / NT;
614
615 threadgroup float shmem[N];
616
617 const float scale = 1.0f / sqrt((float) N);
618
619 const int64_t r = tgpig.x;
620 if (r >= args.nrows) {
621 return;
622 }
623
624 src += r * N;
625 dst += r * N;
626
627 const int tid = sgitg * NW + tiisg;
628
629 float reg[NE];
630 for (int i = 0; i < NE; i++) {
631 reg[i] = float(src[i*NT + tid])*scale;
632 }
633
634 for (int i = 1; i < NW; i *= 2) {
635 for (int j = 0; j < NE; j++) {
636 const float val = reg[j];
637 const float val2 = simd_shuffle_xor(val, i);
638 reg[j] = (tid & i) == 0 ? val2 + val : val2 - val;
639 }
640 }
641
642 for (int i = NW; i < NT; i *= 2) {
643 for (int j = 0; j < NE; j++) {
644 shmem[j*NT + tid] = reg[j];
645 }
646 threadgroup_barrier(mem_flags::mem_threadgroup);
647 for (int j = 0; j < NE; j++) {
648 const float val = reg[j];
649 const float val2 = shmem[j*NT + (tid ^ i)];
650 reg[j] = (tid & i) == 0 ? val2 + val : val2 - val;
651 }
652 threadgroup_barrier(mem_flags::mem_threadgroup);
653 }
654
655 for (int i = NT; i < N; i *= 2) {
656 const int step = i / NT;
657 for (int j = 0; j < NE; j += (2 * step)) {
658 for (int k = 0; k < step; k++) {
659 const float x = reg[j + k ];
660 const float y = reg[j + k + step];
661 reg[j + k] = x + y;
662 reg[j + k + step] = x - y;
663 }
664 }
665 }
666
667 for (int i = 0; i < NE; i++) {
668 dst[i*NT + tid] = reg[i];
669 }
670}
671
672typedef decltype(kernel_fwht<64, float>) kernel_fwht_f32_t;
673typedef decltype(kernel_fwht<64, half>) kernel_fwht_f16_t;
674
675template [[host_name("kernel_fwht_f32_64")]] kernel kernel_fwht_f32_t kernel_fwht<64, float>;
676template [[host_name("kernel_fwht_f32_128")]] kernel kernel_fwht_f32_t kernel_fwht<128, float>;
677template [[host_name("kernel_fwht_f32_256")]] kernel kernel_fwht_f32_t kernel_fwht<256, float>;
678
679template [[host_name("kernel_fwht_f16_64")]] kernel kernel_fwht_f16_t kernel_fwht<64, half>;
680template [[host_name("kernel_fwht_f16_128")]] kernel kernel_fwht_f16_t kernel_fwht<128, half>;
681template [[host_name("kernel_fwht_f16_256")]] kernel kernel_fwht_f16_t kernel_fwht<256, half>;
682
683template [[host_name("kernel_fwht_f32_512")]] kernel kernel_fwht_f32_t kernel_fwht_tg<512, GGML_METAL_FWHT_TG_NT, float>;
684template [[host_name("kernel_fwht_f32_1024")]] kernel kernel_fwht_f32_t kernel_fwht_tg<1024, GGML_METAL_FWHT_TG_NT, float>;
685template [[host_name("kernel_fwht_f32_2048")]] kernel kernel_fwht_f32_t kernel_fwht_tg<2048, GGML_METAL_FWHT_TG_NT, float>;
686template [[host_name("kernel_fwht_f32_4096")]] kernel kernel_fwht_f32_t kernel_fwht_tg<4096, GGML_METAL_FWHT_TG_NT, float>;
687template [[host_name("kernel_fwht_f32_8192")]] kernel kernel_fwht_f32_t kernel_fwht_tg<8192, GGML_METAL_FWHT_TG_NT, float>;
688
689template [[host_name("kernel_fwht_f16_512")]] kernel kernel_fwht_f16_t kernel_fwht_tg<512, GGML_METAL_FWHT_TG_NT, half>;
690template [[host_name("kernel_fwht_f16_1024")]] kernel kernel_fwht_f16_t kernel_fwht_tg<1024, GGML_METAL_FWHT_TG_NT, half>;
691template [[host_name("kernel_fwht_f16_2048")]] kernel kernel_fwht_f16_t kernel_fwht_tg<2048, GGML_METAL_FWHT_TG_NT, half>;
692template [[host_name("kernel_fwht_f16_4096")]] kernel kernel_fwht_f16_t kernel_fwht_tg<4096, GGML_METAL_FWHT_TG_NT, half>;
693template [[host_name("kernel_fwht_f16_8192")]] kernel kernel_fwht_f16_t kernel_fwht_tg<8192, GGML_METAL_FWHT_TG_NT, half>;
694
695constant int FC_dsv4_hc_n_hc [[function_constant(FC_DSV4_HC + 0)]];
696
697kernel void kernel_dsv4_hc_comb_f32(
698 constant ggml_metal_kargs_dsv4_hc_comb & args,
699 device const char * mixes,
700 device const char * scale,
701 device const char * base,
702 device char * dst,
703 uint3 tgpig[[threadgroup_position_in_grid]],
704 ushort tiisg[[thread_index_in_simdgroup]],
705 ushort sgitg[[simdgroup_index_in_threadgroup]],
706 ushort3 ntg[[threads_per_threadgroup]]) {
707 constexpr ushort hc = 4;
708 constexpr ushort comb_offset = 2*hc;
709
710 const int it = tgpig.x*ntg.y + sgitg;
711 if (it >= args.n_tokens) {
712 return;
713 }
714
715 float scale_lane = 0.0f;
716 if (tiisg == 0) {
717 scale_lane = *(device const float *) (scale + 2*args.nb_s0);
718 }
719 const float scale_comb = simd_shuffle(scale_lane, 0);
720
721 float v = 0.0f;
722 if (tiisg < hc*hc) {
723 v = *(device const float *) (mixes + (comb_offset + tiisg)*args.nb_m0 + it*args.nb_m1)*scale_comb
724 + *(device const float *) (base + (comb_offset + tiisg)*args.nb_b0);
725 }
726
727 // Softmax across destinations (the four contiguous lanes for each source).
728 float vmax = max(v, simd_shuffle_xor(v, 1));
729 vmax = max(vmax, simd_shuffle_xor(vmax, 2));
730 v = exp(v - vmax);
731
732 float sum = v + simd_shuffle_xor(v, 1);
733 sum += simd_shuffle_xor(sum, 2);
734 v = v/sum + args.eps;
735
736 // Normalize columns: equal destination indices are four lanes apart.
737 sum = v + simd_shuffle_xor(v, 4);
738 sum += simd_shuffle_xor(sum, 8);
739 v /= sum + args.eps;
740
741 for (int i = 1; i < args.n_iter; ++i) {
742 sum = v + simd_shuffle_xor(v, 1);
743 sum += simd_shuffle_xor(sum, 2);
744 v /= sum + args.eps;
745
746 sum = v + simd_shuffle_xor(v, 4);
747 sum += simd_shuffle_xor(sum, 8);
748 v /= sum + args.eps;
749 }
750
751 if (tiisg < hc*hc) {
752 const ushort idst = tiisg & 3;
753 const ushort isrc = tiisg >> 2;
754 *(device float *) (dst + idst*args.nb_d0 + isrc*args.nb_d1 + it*args.nb_d2) = v;
755 }
756}
757
758kernel void kernel_dsv4_hc_pre_f32(
759 constant ggml_metal_kargs_dsv4_hc_pre & args,
760 device const char * x,
761 device const char * weights,
762 device char * dst,
763 uint3 tgpig[[threadgroup_position_in_grid]],
764 ushort tiisg[[thread_index_in_simdgroup]],
765 ushort sgitg[[simdgroup_index_in_threadgroup]],
766 ushort3 ntg[[threads_per_threadgroup]]) {
767 const int it = tgpig.y;
768 const int i0 = ((int) tgpig.x*ntg.y + sgitg)*32 + tiisg;
769
770 if (i0 >= args.n_embd) {
771 return;
772 }
773
774 device const char * xb = x + i0*args.nb_x0 + it*args.nb_x2;
775 float result = 0.0f;
776 FOR_UNROLL (int ih = 0; ih < FC_dsv4_hc_n_hc; ++ih) {
777 const float xv = *(device const float *) (xb + ih*args.nb_x1);
778 const float wv = *(device const float *) (weights + ih*args.nb_w0 + it*args.nb_w1);
779 result = fma(xv, wv, result);
780 }
781
782 *(device float *) (dst + i0*args.nb_d0 + it*args.nb_d1) = args.scale*result;
783}
784
785kernel void kernel_dsv4_hc_pre_gated_f32(
786 constant ggml_metal_kargs_dsv4_hc_pre & args,
787 device const char * x,
788 device const char * gate,
789 device char * dst,
790 uint3 tgpig[[threadgroup_position_in_grid]],
791 ushort tiisg[[thread_index_in_simdgroup]],
792 ushort sgitg[[simdgroup_index_in_threadgroup]],
793 ushort3 ntg[[threads_per_threadgroup]]) {
794 const int it = tgpig.y;
795 const int i0 = ((int) tgpig.x*ntg.y + sgitg)*32 + tiisg;
796
797 if (i0 >= args.n_embd) {
798 return;
799 }
800
801 device const char * xb = x + i0*args.nb_x0 + it*args.nb_x2;
802 device const char * gb = gate + i0*args.nb_w0 + it*args.nb_w2;
803 float result = 0.0f;
804 FOR_UNROLL (int ih = 0; ih < FC_dsv4_hc_n_hc; ++ih) {
805 const float g = 1.0f/(1.0f + exp(-*(device const float *) (gb + ih*args.nb_w1)));
806 const float xv = *(device const float *) (xb + ih*args.nb_x1);
807 result = fma(xv, g, result);
808 }
809
810 *(device float *) (dst + i0*args.nb_d0 + it*args.nb_d1) = args.scale*result;
811}
812
813kernel void kernel_dsv4_hc_post_nocomb_f32(
814 constant ggml_metal_kargs_dsv4_hc_post & args,
815 device const char * x,
816 device const char * residual,
817 device const char * post,
818 device char * dst,
819 uint3 tgpig[[threadgroup_position_in_grid]],
820 ushort tiisg[[thread_index_in_simdgroup]],
821 ushort sgitg[[simdgroup_index_in_threadgroup]],
822 ushort3 ntg[[threads_per_threadgroup]]) {
823 constexpr ushort hc = 4;
824
825 const int it = tgpig.y;
826 const int i0 = ((int) tgpig.x*ntg.y + sgitg)*32 + tiisg;
827
828 float post_lane = 0.0f;
829 if (tiisg < hc) {
830 post_lane = *(device const float *) (post + tiisg*args.nb_p0 + it*args.nb_p1);
831 }
832
833 float post_reg[hc];
834 FOR_UNROLL (ushort idst = 0; idst < hc; ++idst) {
835 post_reg[idst] = simd_shuffle(post_lane, idst);
836 }
837
838 if (i0 >= args.n_embd) {
839 return;
840 }
841
842 const float xv = *(device const float *) (x + i0*args.nb_x0 + it*args.nb_x1);
843 device const char * rb = residual + i0*args.nb_r0 + it*args.nb_r2;
844 FOR_UNROLL (ushort idst = 0; idst < hc; ++idst) {
845 const float rv = *(device const float *) (rb + idst*args.nb_r1);
846 *(device float *) (dst + i0*args.nb_d0 + idst*args.nb_d1 + it*args.nb_d2) = xv*post_reg[idst] + rv;
847 }
848}
849
850kernel void kernel_dsv4_hc_post_f32(
851 constant ggml_metal_kargs_dsv4_hc_post & args,
852 device const char * x,
853 device const char * residual,
854 device const char * post,
855 device const char * comb,
856 device char * dst,
857 uint3 tgpig[[threadgroup_position_in_grid]],
858 ushort tiisg[[thread_index_in_simdgroup]],
859 ushort sgitg[[simdgroup_index_in_threadgroup]],
860 ushort3 ntg[[threads_per_threadgroup]]) {
861 constexpr ushort hc = 4;
862
863 const int it = tgpig.y;
864 const int i0 = ((int) tgpig.x*ntg.y + sgitg)*32 + tiisg;
865
866 float coeff_lane = 0.0f;
867 if (tiisg < hc) {
868 coeff_lane = *(device const float *) (post + tiisg*args.nb_p0 + it*args.nb_p1);
869 } else if (tiisg < hc + hc*hc) {
870 const ushort idx = tiisg - hc;
871 const ushort idst = idx & 3;
872 const ushort isrc = idx >> 2;
873 coeff_lane = *(device const float *) (comb + idst*args.nb_c0 + isrc*args.nb_c1 + it*args.nb_c2);
874 }
875
876 float post_reg[hc];
877 float comb_reg[hc][hc];
878 FOR_UNROLL (ushort idst = 0; idst < hc; ++idst) {
879 post_reg[idst] = simd_shuffle(coeff_lane, idst);
880 }
881 FOR_UNROLL (ushort isrc = 0; isrc < hc; ++isrc) {
882 FOR_UNROLL (ushort idst = 0; idst < hc; ++idst) {
883 comb_reg[isrc][idst] = simd_shuffle(coeff_lane, hc + idst + hc*isrc);
884 }
885 }
886
887 if (i0 >= args.n_embd) {
888 return;
889 }
890
891 const float xv = *(device const float *) (x + i0*args.nb_x0 + it*args.nb_x1);
892 float result[hc];
893 FOR_UNROLL (ushort idst = 0; idst < hc; ++idst) {
894 result[idst] = xv*post_reg[idst];
895 }
896
897 device const char * rb = residual + i0*args.nb_r0 + it*args.nb_r2;
898 FOR_UNROLL (ushort isrc = 0; isrc < hc; ++isrc) {
899 const float rv = *(device const float *) (rb + isrc*args.nb_r1);
900 FOR_UNROLL (ushort idst = 0; idst < hc; ++idst) {
901 result[idst] = fma(rv, comb_reg[isrc][idst], result[idst]);
902 }
903 }
904
905 FOR_UNROLL (ushort idst = 0; idst < hc; ++idst) {
906 *(device float *) (dst + i0*args.nb_d0 + idst*args.nb_d1 + it*args.nb_d2) = result[idst];
907 }
908}
909
910constant bool FC_top_k_mask_has_score [[function_constant(FC_TOP_K_MASK + 0)]];
911constant bool FC_top_k_mask_has_blocks [[function_constant(FC_TOP_K_MASK + 1)]];
912constant bool FC_top_k_mask_has_extra [[function_constant(FC_TOP_K_MASK + 2)]];
913constant bool FC_top_k_mask_vec [[function_constant(FC_TOP_K_MASK + 3)]];
914
915// the fill runs before the scatter, both are fully parallel over the whole dst and over the picks of one row
916template <typename T, typename T4>
917kernel void kernel_top_k_mask_fill(
918 constant ggml_metal_kargs_top_k_mask & args,
919 device char * dst,
920 uint tid[[thread_position_in_grid]],
921 uint ntot[[threads_per_grid]]) {
922 const T ninf = T(-INFINITY);
923
924 if (FC_top_k_mask_vec) {
925 device T4 * dst_4 = (device T4 *) dst;
926 for (int64_t i = tid; i < args.n_elems/4; i += ntot) {
927 dst_4[i] = T4(ninf);
928 }
929 } else {
930 device T * dst_t = (device T *) dst;
931 for (int64_t i = tid; i < args.n_elems; i += ntot) {
932 dst_t[i] = ninf;
933 }
934 }
935}
936
937typedef decltype(kernel_top_k_mask_fill<half, half4>) kernel_top_k_mask_fill_h_t;
938typedef decltype(kernel_top_k_mask_fill<float, float4>) kernel_top_k_mask_fill_f_t;
939
940template [[host_name("kernel_top_k_mask_fill_f16")]] kernel kernel_top_k_mask_fill_h_t kernel_top_k_mask_fill<half, half4>;
941template [[host_name("kernel_top_k_mask_fill_f32")]] kernel kernel_top_k_mask_fill_f_t kernel_top_k_mask_fill<float, float4>;
942
943// one threadgroup per row: copy the mask cells of the live picks and of the extra cells over the fill
944template <typename T, typename T4>
945kernel void kernel_top_k_mask(
946 constant ggml_metal_kargs_top_k_mask & args,
947 device const char * mask,
948 device const char * top_k,
949 device const char * score,
950 device const char * blocks,
951 device const char * extra,
952 device char * dst,
953 uint3 tgpig[[threadgroup_position_in_grid]],
954 uint3 tpitg[[thread_position_in_threadgroup]],
955 uint3 ntg_max[[threads_per_threadgroup]]) {
956 const int i1 = tgpig.x;
957 const int i2 = tgpig.y;
958 const int i3 = tgpig.z;
959
960 const int tiitg = tpitg.x;
961 const int ntg = ntg_max.x;
962
963 device const char * mask_row = mask + (i1 % args.m1)*args.nbm1 + (i2 % args.m2)*args.nbm2 + (i3 % args.m3)*args.nbm3;
964 device char * dst_row = dst + i1*args.nb1 + i2*args.nb2 + i3*args.nb3;
965 device const char * top_k_row = top_k + i1*args.nbk1 + i2*args.nbk2 + i3*args.nbk3;
966
967 device const T * mask_row_t = (device const T *) mask_row;
968 device T * dst_row_t = (device T *) dst_row;
969
970 for (int i = tiitg; i < args.n_top; i += ntg) {
971 const int32_t p = *(device const int32_t *) (top_k_row + i*args.nbk0);
972 if (p < 0 || p >= args.n_pick) {
973 continue;
974 }
975 if (FC_top_k_mask_has_score && !(*(device const float *) (score + p*args.nbs0 + i1*args.nbs1 + i2*args.nbs2 + i3*args.nbs3) > -INFINITY)) {
976 continue;
977 }
978 if (FC_top_k_mask_has_blocks) {
979 FOR_UNROLL (int k = 0; k < args.n_blk; ++k) {
980 const int32_t c = *(device const int32_t *) (blocks + k*args.nbb0 + p*args.nbb1);
981 if (c >= 0 && c < args.n_kv) {
982 dst_row_t[c] = mask_row_t[c];
983 }
984 }
985 } else if (p < args.n_kv) {
986 dst_row_t[p] = mask_row_t[p];
987 }
988 }
989
990 if (FC_top_k_mask_has_extra) {
991 for (int i = tiitg; i < args.n_extra; i += ntg) {
992 const int32_t c = *(device const int32_t *) (extra + i*args.nbe0 + i1*args.nbe1 + i2*args.nbe2 + i3*args.nbe3);
993 if (c >= 0 && c < args.n_kv) {
994 dst_row_t[c] = mask_row_t[c];
995 }
996 }
997 }
998}
999
1000typedef decltype(kernel_top_k_mask<half, half4>) kernel_top_k_mask_h_t;
1001typedef decltype(kernel_top_k_mask<float, float4>) kernel_top_k_mask_f_t;
1002
1003template [[host_name("kernel_top_k_mask_f16")]] kernel kernel_top_k_mask_h_t kernel_top_k_mask<half, half4>;
1004template [[host_name("kernel_top_k_mask_f32")]] kernel kernel_top_k_mask_f_t kernel_top_k_mask<float, float4>;