rlx-metal 0.2.14

Metal backend for RLX — Apple Silicon GPU via Metal Performance Shaders + custom MSL kernels
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
// RLX — versatile ML compiler + runtime.
// Copyright (C) 2026 Eugene Hauptmann, Nataliya Kosmyna.
// SPDX-License-Identifier: MIT OR Apache-2.0

//! GPU scaled dot-product attention backward (recomputes scores + softmax).

use crate::kernels::Kernels;
use crate::mtl::{Buffer, ComputeCommandEncoderRef, MTLSize};
use rlx_ir::Graph;
use rlx_ir::op::{AttentionBwdWrt, Op};

pub fn scratch_bytes(graph: &Graph) -> usize {
    let mut max = 0usize;
    for node in graph.nodes() {
        // Both the per-`wrt` op and the IR-level fused `AttentionBackwardAll`
        // (from `FuseAttentionBackwardAll`) drive the same batched kernels and
        // so need the same scores+dp+ds scratch. Sizing off only `AttentionBackward`
        // would leave the fused path with a 0-sized scratch (writes land at arena
        // offset 0 → corruption), so match both.
        let num_heads = match &node.op {
            Op::AttentionBackward { num_heads, .. }
            | Op::AttentionBackwardAll { num_heads, .. } => *num_heads,
            _ => continue,
        };
        {
            let q_shape = &graph.node(node.inputs[0]).shape;
            let k_shape = &graph.node(node.inputs[1]).shape;
            let sq = q_shape.dim(1).unwrap_static();
            let sk = if k_shape.rank() >= 2 {
                k_shape.dim(1).unwrap_static()
            } else {
                k_shape.dim(0).unwrap_static()
            };
            let ss = sq.saturating_mul(sk);
            // scores + dp + ds, sized for `tile` heads processed in parallel.
            // (The fused batched path uses `tile·3·ss`; the per-wrt fallback uses
            // `3·ss` ≤ that since tile ≥ 1.) `batch·num_heads` bounds the tile.
            let batch = q_shape.dim(0).unwrap_static();
            let tile = attn_bwd_tile(batch.saturating_mul(num_heads), ss);
            max = max.max(tile * 3 * ss * std::mem::size_of::<f32>());
        }
    }
    max
}

pub fn use_gpu(mask_kind: u32, bhsd: u32, sq: usize, sk: usize, scratch_off: usize) -> bool {
    if scratch_off == 0 {
        return false;
    }
    if bhsd != 0 {
        return false;
    }
    if sq == 0 || sk == 0 {
        return false;
    }
    if rlx_ir::env::var("RLX_METAL_ATTN_BWD_GPU")
        .map(|v| v == "0")
        .unwrap_or(false)
    {
        return false;
    }
    // Custom / bias masks still use CPU reference for now.
    !matches!(mask_kind, 2 | 4)
}

#[allow(clippy::too_many_arguments)]
pub fn encode_attention_bwd(
    enc: &ComputeCommandEncoderRef,
    kk: &Kernels,
    buffer: &Buffer,
    q: usize,
    k: usize,
    v: usize,
    dy: usize,
    out: usize,
    scratch: usize,
    batch: u32,
    sq: u32,
    sk: u32,
    heads: u32,
    head_dim: u32,
    mask_kind: u32,
    window: u32,
    wrt: u32,
) {
    let ss = (sq as usize).saturating_mul(sk as usize);
    let scores = scratch;
    let dp = scratch + ss * 4;
    let ds = scratch + ss * 8;
    let wrt_ir = match wrt {
        0 => AttentionBwdWrt::Query,
        1 => AttentionBwdWrt::Key,
        _ => AttentionBwdWrt::Value,
    };
    let scale = (head_dim as f32).sqrt().recip();

    for bi in 0..batch {
        for hi in 0..heads {
            let hs = heads * head_dim;
            let q_off = ((bi * sq * hs + hi * head_dim) * 4) as usize;
            let k_off = ((bi * sk * hs + hi * head_dim) * 4) as usize;
            let out_off = match wrt_ir {
                AttentionBwdWrt::Key | AttentionBwdWrt::Value => k_off,
                AttentionBwdWrt::Query => q_off,
            };

            encode_scores(
                enc,
                kk,
                buffer,
                q + q_off,
                k + k_off,
                scores,
                sq,
                sk,
                heads,
                head_dim,
                scale,
                mask_kind,
                window,
            );
            encode_softmax_rows(enc, kk, buffer, scores, sq, sk, mask_kind, sq);
            match wrt_ir {
                AttentionBwdWrt::Value => encode_dv(
                    enc,
                    kk,
                    buffer,
                    scores,
                    dy + q_off,
                    out + out_off,
                    sq,
                    sk,
                    heads,
                    head_dim,
                ),
                AttentionBwdWrt::Query => {
                    encode_dp(
                        enc,
                        kk,
                        buffer,
                        dy + q_off,
                        v + k_off,
                        dp,
                        sq,
                        sk,
                        heads,
                        head_dim,
                    );
                    encode_ds(enc, kk, buffer, scores, dp, ds, sq, sk, scale);
                    encode_dq(
                        enc,
                        kk,
                        buffer,
                        ds,
                        k + k_off,
                        out + out_off,
                        sq,
                        sk,
                        heads,
                        head_dim,
                    );
                }
                AttentionBwdWrt::Key => {
                    encode_dp(
                        enc,
                        kk,
                        buffer,
                        dy + q_off,
                        v + k_off,
                        dp,
                        sq,
                        sk,
                        heads,
                        head_dim,
                    );
                    encode_ds(enc, kk, buffer, scores, dp, ds, sq, sk, scale);
                    encode_dk(
                        enc,
                        kk,
                        buffer,
                        ds,
                        q + q_off,
                        out + out_off,
                        sq,
                        sk,
                        heads,
                        head_dim,
                    );
                }
            }
        }
    }
}

/// Heads processed per parallel dispatch. Scratch is `tile · 3 · seq²` floats;
/// cap the tile to a ~384 MB budget so it stays bounded at large seq, and never
/// exceed the total (batch·heads) count.
fn attn_bwd_tile(total_bh: usize, ss: usize) -> usize {
    const BUDGET_FLOATS: usize = 96 * 1024 * 1024; // 3·ss·tile ≤ this ⇒ ≤384 MB
    (BUDGET_FLOATS / (3 * ss.max(1))).clamp(1, total_bh.max(1))
}

/// Fused **and parallel** backward for all three gradients. The earlier fused
/// version removed the 3×/2× recompute but still walked (batch,head) SERIALLY —
/// the shared scratch made Metal (which hazard-tracks at buffer granularity)
/// serialize every (b,h). Here each stage (scores → softmax → dv/dp → ds →
/// dq/dk) is ONE 3D dispatch whose `grid.z` runs `tile` heads **in parallel**,
/// so the GPU is filled the way the forward `fused_attn_block` fills it. Heads
/// run in tiles to bound scratch; scores/dp/ds are laid out `[tile][sq][sk]`.
#[allow(clippy::too_many_arguments)]
pub fn encode_attention_bwd_all(
    enc: &ComputeCommandEncoderRef,
    kk: &Kernels,
    buffer: &Buffer,
    q: usize,
    k: usize,
    v: usize,
    dy: usize,
    out_dq: usize,
    out_dk: usize,
    out_dv: usize,
    scratch: usize,
    batch: u32,
    sq: u32,
    sk: u32,
    heads: u32,
    head_dim: u32,
    mask_kind: u32,
    window: u32,
) {
    let ss = (sq as usize).saturating_mul(sk as usize);
    let total_bh = (batch as usize) * (heads as usize);
    let tile = attn_bwd_tile(total_bh, ss);
    let scale = (head_dim as f32).sqrt().recip();

    // Opt-in fused flash-backward: one kernel, S/P/dP/dS on-chip (no scores/dp/ds
    // device scratch), dQ direct + dK/dV atomic. Gated to the shapes the kernel's
    // threadgroup tiles support (sk≤256, head_dim≤64) and causal/none masks.
    if rlx_ir::env::flag("RLX_METAL_ATTN_BWD_FUSED")
        && mask_kind <= 1
        && sk <= 256
        && head_dim <= 64
        && sq == sk
    {
        let set_u = |enc: &ComputeCommandEncoderRef, idx: u64, v: u32| {
            enc.set_bytes(idx, 4, &v as *const u32 as *const _);
        };
        let hs = heads * head_dim;
        let dkdv_elems = batch * sk * hs;
        // Zero dK / dV (accumulated via atomics below).
        for off in [out_dk, out_dv] {
            enc.set_compute_pipeline_state(&kk.scatter_add_zero);
            enc.set_buffer(0, Some(buffer), off as u64);
            set_u(enc, 1, dkdv_elems);
            enc.dispatch_threads(
                MTLSize {
                    width: dkdv_elems as u64,
                    height: 1,
                    depth: 1,
                },
                MTLSize {
                    width: 256.min(dkdv_elems as u64).max(1),
                    height: 1,
                    depth: 1,
                },
            );
        }
        // Benchmark probe: stop after the dK/dV zeroing to measure its isolated cost.
        if rlx_ir::env::flag("RLX_METAL_ATTN_BWD_FUSED_ZEROONLY") {
            return;
        }
        enc.set_compute_pipeline_state(&kk.attn_bwd_fused_f32);
        enc.set_buffer(0, Some(buffer), q as u64);
        enc.set_buffer(1, Some(buffer), k as u64);
        enc.set_buffer(2, Some(buffer), v as u64);
        enc.set_buffer(3, Some(buffer), dy as u64);
        enc.set_buffer(4, Some(buffer), out_dq as u64);
        enc.set_buffer(5, Some(buffer), out_dk as u64);
        enc.set_buffer(6, Some(buffer), out_dv as u64);
        set_u(enc, 7, sq);
        set_u(enc, 8, sk);
        set_u(enc, 9, heads);
        set_u(enc, 10, head_dim);
        enc.set_bytes(11, 4, &scale as *const f32 as *const _);
        set_u(enc, 12, mask_kind);
        const BR: u32 = 8;
        let q_tiles = sq.div_ceil(BR);
        enc.dispatch_thread_groups(
            MTLSize {
                width: q_tiles as u64,
                height: heads as u64,
                depth: batch as u64,
            },
            MTLSize {
                width: 64,
                height: 1,
                depth: 1,
            },
        );
        return;
    }

    let scores = scratch;
    let dp = scratch + tile * ss * 4;
    let ds = scratch + 2 * tile * ss * 4;
    let set_u = |enc: &ComputeCommandEncoderRef, idx: u64, v: u32| {
        enc.set_bytes(idx, 4, &v as *const u32 as *const _);
    };

    let mut b = 0usize;
    while b < total_bh {
        let cur = tile.min(total_bh - b) as u64;
        let tb = b as u32;

        // scores = QKᵀ·scale (+ causal/window mask). grid [sk, sq, cur].
        enc.set_compute_pipeline_state(&kk.attn_bwd_scores_batched_f32);
        enc.set_buffer(0, Some(buffer), q as u64);
        enc.set_buffer(1, Some(buffer), k as u64);
        enc.set_buffer(2, Some(buffer), scores as u64);
        set_u(enc, 3, sq);
        set_u(enc, 4, sk);
        set_u(enc, 5, heads);
        set_u(enc, 6, head_dim);
        enc.set_bytes(7, 4, &scale as *const f32 as *const _);
        set_u(enc, 8, mask_kind);
        set_u(enc, 9, window);
        set_u(enc, 10, tb);
        enc.dispatch_threads(
            MTLSize {
                width: sk as u64,
                height: sq as u64,
                depth: cur,
            },
            MTLSize {
                width: 8.min(sk as u64),
                height: 8.min(sq as u64),
                depth: 1,
            },
        );

        // softmax over each row (cur·sq rows of sk, contiguous from `scores`).
        encode_softmax_rows(
            enc,
            kk,
            buffer,
            scores,
            (cur as u32) * sq,
            sk,
            mask_kind,
            sq,
        );

        // dv = scoresᵀ·dy. grid [head_dim, sk, cur].
        enc.set_compute_pipeline_state(&kk.attn_bwd_dv_batched_f32);
        enc.set_buffer(0, Some(buffer), scores as u64);
        enc.set_buffer(1, Some(buffer), dy as u64);
        enc.set_buffer(2, Some(buffer), out_dv as u64);
        set_u(enc, 3, sq);
        set_u(enc, 4, sk);
        set_u(enc, 5, heads);
        set_u(enc, 6, head_dim);
        set_u(enc, 7, tb);
        set_u(enc, 8, mask_kind);
        set_u(enc, 9, window);
        enc.dispatch_threads(
            MTLSize {
                width: head_dim as u64,
                height: sk as u64,
                depth: cur,
            },
            MTLSize {
                width: 8.min(head_dim as u64),
                height: 8.min(sk as u64),
                depth: 1,
            },
        );

        // dp = dy·vᵀ. grid [sk, sq, cur].
        enc.set_compute_pipeline_state(&kk.attn_bwd_dp_batched_f32);
        enc.set_buffer(0, Some(buffer), dy as u64);
        enc.set_buffer(1, Some(buffer), v as u64);
        enc.set_buffer(2, Some(buffer), dp as u64);
        set_u(enc, 3, sq);
        set_u(enc, 4, sk);
        set_u(enc, 5, heads);
        set_u(enc, 6, head_dim);
        set_u(enc, 7, tb);
        set_u(enc, 8, mask_kind);
        set_u(enc, 9, window);
        enc.dispatch_threads(
            MTLSize {
                width: sk as u64,
                height: sq as u64,
                depth: cur,
            },
            MTLSize {
                width: 8.min(sk as u64),
                height: 8.min(sq as u64),
                depth: 1,
            },
        );

        // ds = softmax-jacobian(scores, dp). One threadgroup per (slot,row).
        let mut tg_w: u64 = 1;
        while tg_w * 2 <= sk as u64 && tg_w * 2 <= 256 {
            tg_w *= 2;
        }
        enc.set_compute_pipeline_state(&kk.attn_bwd_ds_batched_f32);
        enc.set_buffer(0, Some(buffer), scores as u64);
        enc.set_buffer(1, Some(buffer), dp as u64);
        enc.set_buffer(2, Some(buffer), ds as u64);
        set_u(enc, 3, sq);
        set_u(enc, 4, sk);
        enc.set_bytes(5, 4, &scale as *const f32 as *const _);
        set_u(enc, 6, mask_kind);
        set_u(enc, 7, window);
        enc.dispatch_thread_groups(
            MTLSize {
                width: cur * sq as u64,
                height: 1,
                depth: 1,
            },
            MTLSize {
                width: tg_w,
                height: 1,
                depth: 1,
            },
        );

        // dq = ds·k. grid [head_dim, sq, cur].
        enc.set_compute_pipeline_state(&kk.attn_bwd_dq_batched_f32);
        enc.set_buffer(0, Some(buffer), ds as u64);
        enc.set_buffer(1, Some(buffer), k as u64);
        enc.set_buffer(2, Some(buffer), out_dq as u64);
        set_u(enc, 3, sq);
        set_u(enc, 4, sk);
        set_u(enc, 5, heads);
        set_u(enc, 6, head_dim);
        set_u(enc, 7, tb);
        set_u(enc, 8, mask_kind);
        set_u(enc, 9, window);
        enc.dispatch_threads(
            MTLSize {
                width: head_dim as u64,
                height: sq as u64,
                depth: cur,
            },
            MTLSize {
                width: 8.min(head_dim as u64),
                height: 8.min(sq as u64),
                depth: 1,
            },
        );

        // dk = dsᵀ·q. grid [head_dim, sk, cur].
        enc.set_compute_pipeline_state(&kk.attn_bwd_dk_batched_f32);
        enc.set_buffer(0, Some(buffer), ds as u64);
        enc.set_buffer(1, Some(buffer), q as u64);
        enc.set_buffer(2, Some(buffer), out_dk as u64);
        set_u(enc, 3, sq);
        set_u(enc, 4, sk);
        set_u(enc, 5, heads);
        set_u(enc, 6, head_dim);
        set_u(enc, 7, tb);
        set_u(enc, 8, mask_kind);
        set_u(enc, 9, window);
        enc.dispatch_threads(
            MTLSize {
                width: head_dim as u64,
                height: sk as u64,
                depth: cur,
            },
            MTLSize {
                width: 8.min(head_dim as u64),
                height: 8.min(sk as u64),
                depth: 1,
            },
        );

        b += cur as usize;
    }
}

fn encode_softmax_rows(
    enc: &ComputeCommandEncoderRef,
    kk: &Kernels,
    buffer: &Buffer,
    data: usize,
    rows: u32,
    cols: u32,
    mask_kind: u32,
    sq: u32,
) {
    let mut tg_w: u64 = 1;
    while tg_w * 2 <= cols as u64 && tg_w * 2 <= 256 {
        tg_w *= 2;
    }
    // Causal (mask_kind==1): each row `slot*sq + qi` only softmaxes over its band
    // [0, qi]; the masked sentinels underflow to exactly 0, so this is bit-identical
    // to the full softmax on the band that dv/ds read — ~2× less work.
    if mask_kind == 1 {
        enc.set_compute_pipeline_state(&kk.softmax_lastax_causal);
        enc.set_buffer(0, Some(buffer), data as u64);
        enc.set_bytes(1, 4, &cols as *const u32 as *const _);
        enc.set_bytes(2, 4, &sq as *const u32 as *const _);
    } else {
        enc.set_compute_pipeline_state(&kk.softmax_lastax);
        enc.set_buffer(0, Some(buffer), data as u64);
        enc.set_bytes(1, 4, &cols as *const u32 as *const _);
    }
    enc.dispatch_threads(
        MTLSize {
            width: tg_w * rows as u64,
            height: 1,
            depth: 1,
        },
        MTLSize {
            width: tg_w,
            height: 1,
            depth: 1,
        },
    );
}

#[allow(clippy::too_many_arguments)]
fn encode_scores(
    enc: &ComputeCommandEncoderRef,
    kk: &Kernels,
    buffer: &Buffer,
    q: usize,
    k: usize,
    scores: usize,
    sq: u32,
    sk: u32,
    heads: u32,
    head_dim: u32,
    scale: f32,
    mask_kind: u32,
    window: u32,
) {
    let hs = heads * head_dim;
    enc.set_compute_pipeline_state(&kk.attn_bwd_scores_f32);
    enc.set_buffer(0, Some(buffer), q as u64);
    enc.set_buffer(1, Some(buffer), k as u64);
    enc.set_buffer(2, Some(buffer), scores as u64);
    enc.set_bytes(3, 4, &sq as *const u32 as *const _);
    enc.set_bytes(4, 4, &sk as *const u32 as *const _);
    enc.set_bytes(5, 4, &hs as *const u32 as *const _);
    enc.set_bytes(6, 4, &head_dim as *const u32 as *const _);
    enc.set_bytes(7, 4, &scale as *const f32 as *const _);
    enc.set_bytes(8, 4, &mask_kind as *const u32 as *const _);
    enc.set_bytes(9, 4, &window as *const u32 as *const _);
    enc.dispatch_threads(
        MTLSize {
            width: sk as u64,
            height: sq as u64,
            depth: 1,
        },
        MTLSize {
            width: 8.min(sk as u64),
            height: 8.min(sq as u64),
            depth: 1,
        },
    );
}

#[allow(clippy::too_many_arguments)]
fn encode_dp(
    enc: &ComputeCommandEncoderRef,
    kk: &Kernels,
    buffer: &Buffer,
    dy: usize,
    v: usize,
    dp: usize,
    sq: u32,
    sk: u32,
    heads: u32,
    head_dim: u32,
) {
    let hs = heads * head_dim;
    enc.set_compute_pipeline_state(&kk.attn_bwd_dp_f32);
    enc.set_buffer(0, Some(buffer), dy as u64);
    enc.set_buffer(1, Some(buffer), v as u64);
    enc.set_buffer(2, Some(buffer), dp as u64);
    enc.set_bytes(3, 4, &sq as *const u32 as *const _);
    enc.set_bytes(4, 4, &sk as *const u32 as *const _);
    enc.set_bytes(5, 4, &hs as *const u32 as *const _);
    enc.set_bytes(6, 4, &head_dim as *const u32 as *const _);
    enc.dispatch_threads(
        MTLSize {
            width: sk as u64,
            height: sq as u64,
            depth: 1,
        },
        MTLSize {
            width: 8.min(sk as u64),
            height: 8.min(sq as u64),
            depth: 1,
        },
    );
}

#[allow(clippy::too_many_arguments)]
fn encode_ds(
    enc: &ComputeCommandEncoderRef,
    kk: &Kernels,
    buffer: &Buffer,
    scores: usize,
    dp: usize,
    ds: usize,
    sq: u32,
    sk: u32,
    scale: f32,
) {
    let mut tg_w: u64 = 1;
    while tg_w * 2 <= sk as u64 && tg_w * 2 <= 256 {
        tg_w *= 2;
    }
    enc.set_compute_pipeline_state(&kk.attn_bwd_ds_f32);
    enc.set_buffer(0, Some(buffer), scores as u64);
    enc.set_buffer(1, Some(buffer), dp as u64);
    enc.set_buffer(2, Some(buffer), ds as u64);
    enc.set_bytes(3, 4, &sq as *const u32 as *const _);
    enc.set_bytes(4, 4, &sk as *const u32 as *const _);
    enc.set_bytes(5, 4, &scale as *const f32 as *const _);
    enc.dispatch_thread_groups(
        MTLSize {
            width: sq as u64,
            height: 1,
            depth: 1,
        },
        MTLSize {
            width: tg_w,
            height: 1,
            depth: 1,
        },
    );
}

#[allow(clippy::too_many_arguments)]
fn encode_dv(
    enc: &ComputeCommandEncoderRef,
    kk: &Kernels,
    buffer: &Buffer,
    scores: usize,
    dy: usize,
    out: usize,
    sq: u32,
    sk: u32,
    heads: u32,
    head_dim: u32,
) {
    let hs = heads * head_dim;
    enc.set_compute_pipeline_state(&kk.attn_bwd_dv_f32);
    enc.set_buffer(0, Some(buffer), scores as u64);
    enc.set_buffer(1, Some(buffer), dy as u64);
    enc.set_buffer(2, Some(buffer), out as u64);
    enc.set_bytes(3, 4, &sq as *const u32 as *const _);
    enc.set_bytes(4, 4, &sk as *const u32 as *const _);
    enc.set_bytes(5, 4, &hs as *const u32 as *const _);
    enc.set_bytes(6, 4, &head_dim as *const u32 as *const _);
    enc.dispatch_threads(
        MTLSize {
            width: head_dim as u64,
            height: sk as u64,
            depth: 1,
        },
        MTLSize {
            width: 8.min(head_dim as u64),
            height: 8.min(sk as u64),
            depth: 1,
        },
    );
}

#[allow(clippy::too_many_arguments)]
fn encode_dq(
    enc: &ComputeCommandEncoderRef,
    kk: &Kernels,
    buffer: &Buffer,
    ds: usize,
    k: usize,
    out: usize,
    sq: u32,
    sk: u32,
    heads: u32,
    head_dim: u32,
) {
    let hs = heads * head_dim;
    enc.set_compute_pipeline_state(&kk.attn_bwd_dq_f32);
    enc.set_buffer(0, Some(buffer), ds as u64);
    enc.set_buffer(1, Some(buffer), k as u64);
    enc.set_buffer(2, Some(buffer), out as u64);
    enc.set_bytes(3, 4, &sq as *const u32 as *const _);
    enc.set_bytes(4, 4, &sk as *const u32 as *const _);
    enc.set_bytes(5, 4, &hs as *const u32 as *const _);
    enc.set_bytes(6, 4, &head_dim as *const u32 as *const _);
    enc.dispatch_threads(
        MTLSize {
            width: head_dim as u64,
            height: sq as u64,
            depth: 1,
        },
        MTLSize {
            width: 8.min(head_dim as u64),
            height: 8.min(sq as u64),
            depth: 1,
        },
    );
}

#[allow(clippy::too_many_arguments)]
fn encode_dk(
    enc: &ComputeCommandEncoderRef,
    kk: &Kernels,
    buffer: &Buffer,
    ds: usize,
    q: usize,
    out: usize,
    sq: u32,
    sk: u32,
    heads: u32,
    head_dim: u32,
) {
    let hs = heads * head_dim;
    enc.set_compute_pipeline_state(&kk.attn_bwd_dk_f32);
    enc.set_buffer(0, Some(buffer), ds as u64);
    enc.set_buffer(1, Some(buffer), q as u64);
    enc.set_buffer(2, Some(buffer), out as u64);
    enc.set_bytes(3, 4, &sq as *const u32 as *const _);
    enc.set_bytes(4, 4, &sk as *const u32 as *const _);
    enc.set_bytes(5, 4, &hs as *const u32 as *const _);
    enc.set_bytes(6, 4, &head_dim as *const u32 as *const _);
    enc.dispatch_threads(
        MTLSize {
            width: head_dim as u64,
            height: sk as u64,
            depth: 1,
        },
        MTLSize {
            width: 8.min(head_dim as u64),
            height: 8.min(sk as u64),
            depth: 1,
        },
    );
}