oxibonsai-kernels 0.2.1

1-bit Q1_0_g128 compute kernels (dequant, GEMV, GEMM) for OxiBonsai
Documentation
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
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
//! # CudaGraph — FLUX.2 DiT glue-op encode methods
//!
//! Host-side launchers + public `encode_*` entry points for the six DiT glue
//! kernels in [`CUDA_IMAGEN_DIT_GLUE_SRC`](super::super::cuda_imagen_dit_glue_kernels::CUDA_IMAGEN_DIT_GLUE_SRC):
//! `modulate_f32`, `gated_residual_add_f32`, `layer_norm_f32`,
//! `rms_norm_heads_f32`, `swiglu_f32`, `rope_interleaved_f32`.
//!
//! Each op exposes:
//! - an `unsafe fn launch_dit_*` device-level launcher (operates on
//!   `CudaSlice<f32>` device buffers — the building block the **resident** DiT
//!   forward (Phase 2) chains without host round-trips, mirroring
//!   `launch_gemm_f32`); and
//! - a public host-API `encode_dit_*` (upload → launch → download) that is the
//!   parity unit-test surface and a drop-in for the per-op CPU references in
//!   `oxibonsai-image::math` / `::blocks`.
//!
//! Parity-first plain FP32 (double-accumulated reductions); validated at
//! cos ≥ 0.999 against the CPU ports.

#![cfg(all(
    feature = "native-cuda",
    any(target_os = "linux", target_os = "windows")
))]

use cudarc::driver::{CudaSlice, LaunchConfig, PushKernelArg};

use super::types::CudaGraphError;

use super::cudagraph_type::CudaGraph;

/// Threads per block for the element-wise grid-stride kernels.
const DIT_GLUE_BLOCK: u32 = 256;
/// Cap on grid blocks for the element-wise kernels (grid-stride covers the rest).
const DIT_GLUE_MAX_GRID: u32 = 4096;

/// `grid` for an element-wise kernel over `n` items: `ceil(n/block)`, capped.
fn elementwise_grid(n: usize) -> u32 {
    let blocks = (n as u64).div_ceil(DIT_GLUE_BLOCK as u64);
    blocks.clamp(1, DIT_GLUE_MAX_GRID as u64) as u32
}

impl CudaGraph {
    // ── modulate ───────────────────────────────────────────────────────────

    /// Launch `modulate_f32` (`x = (1+scale)*x + shift`, in place) on `self.stream`.
    ///
    /// # Safety
    /// `d_x` holds ≥ `rows*dim` f32; `d_shift`/`d_scale` hold ≥ `dim` f32; all on
    /// `self.stream`.
    pub(crate) unsafe fn launch_dit_modulate(
        &self,
        d_x: &mut CudaSlice<f32>,
        d_shift: &CudaSlice<f32>,
        d_scale: &CudaSlice<f32>,
        rows: u32,
        dim: u32,
    ) -> Result<(), CudaGraphError> {
        let cfg = LaunchConfig {
            grid_dim: (elementwise_grid(rows as usize * dim as usize), 1, 1),
            block_dim: (DIT_GLUE_BLOCK, 1, 1),
            shared_mem_bytes: 0,
        };
        self.stream
            .launch_builder(&self.modules.dit_modulate)
            .arg(d_x)
            .arg(d_shift)
            .arg(d_scale)
            .arg(&rows)
            .arg(&dim)
            .launch(cfg)
            .map(|_| ())
            .map_err(|e| CudaGraphError::DriverError(format!("modulate_f32 launch: {e}")))
    }

    /// Modulate `x` `[rows, dim]` in place: `x = (1 + scale) * x + shift`
    /// (`scale`/`shift` length `dim`). Host-API wrapper (mirrors
    /// `oxibonsai-image::math::modulate_inplace`).
    ///
    /// # Errors
    /// [`CudaGraphError::DriverError`] on a length mismatch or buffer/launch error.
    pub fn encode_dit_modulate(
        &self,
        x: &mut [f32],
        shift: &[f32],
        scale: &[f32],
        rows: usize,
        dim: usize,
    ) -> Result<(), CudaGraphError> {
        let expected = rows.checked_mul(dim).ok_or_else(|| {
            CudaGraphError::DriverError("encode_dit_modulate: rows*dim overflow".into())
        })?;
        if x.len() != expected || shift.len() != dim || scale.len() != dim {
            return Err(CudaGraphError::DriverError(format!(
                "encode_dit_modulate: bad lens (x {} != {expected}, shift {} / scale {} != dim {dim})",
                x.len(),
                shift.len(),
                scale.len()
            )));
        }
        if expected == 0 {
            return Ok(());
        }
        let mut d_x = self.htod(x)?;
        let d_shift = self.htod(shift)?;
        let d_scale = self.htod(scale)?;
        unsafe {
            self.launch_dit_modulate(&mut d_x, &d_shift, &d_scale, rows as u32, dim as u32)?;
        }
        self.dtoh_sync(&d_x, x)
    }

    // ── gated residual add ───────────────────────────────────────────────────

    /// Launch `gated_residual_add_f32` (`h += gate*delta`, in place).
    ///
    /// # Safety
    /// `d_h`/`d_delta` hold ≥ `rows*dim` f32; `d_gate` ≥ `dim`; all on `self.stream`.
    pub(crate) unsafe fn launch_dit_gated_residual_add(
        &self,
        d_h: &mut CudaSlice<f32>,
        d_delta: &CudaSlice<f32>,
        d_gate: &CudaSlice<f32>,
        rows: u32,
        dim: u32,
    ) -> Result<(), CudaGraphError> {
        let cfg = LaunchConfig {
            grid_dim: (elementwise_grid(rows as usize * dim as usize), 1, 1),
            block_dim: (DIT_GLUE_BLOCK, 1, 1),
            shared_mem_bytes: 0,
        };
        self.stream
            .launch_builder(&self.modules.dit_gated_residual_add)
            .arg(d_h)
            .arg(d_delta)
            .arg(d_gate)
            .arg(&rows)
            .arg(&dim)
            .launch(cfg)
            .map(|_| ())
            .map_err(|e| CudaGraphError::DriverError(format!("gated_residual_add_f32 launch: {e}")))
    }

    /// `h += gate * delta` over `[rows, dim]` (`gate` length `dim`), in place.
    /// Mirrors `oxibonsai-image::blocks::gated_residual_add`.
    ///
    /// # Errors
    /// [`CudaGraphError::DriverError`] on a length mismatch or buffer/launch error.
    pub fn encode_dit_gated_residual_add(
        &self,
        h: &mut [f32],
        delta: &[f32],
        gate: &[f32],
        rows: usize,
        dim: usize,
    ) -> Result<(), CudaGraphError> {
        let expected = rows.checked_mul(dim).ok_or_else(|| {
            CudaGraphError::DriverError("encode_dit_gated_residual_add: rows*dim overflow".into())
        })?;
        if h.len() != expected || delta.len() != expected || gate.len() != dim {
            return Err(CudaGraphError::DriverError(format!(
                "encode_dit_gated_residual_add: bad lens (h {} / delta {} != {expected}, gate {} != dim {dim})",
                h.len(),
                delta.len(),
                gate.len()
            )));
        }
        if expected == 0 {
            return Ok(());
        }
        let mut d_h = self.htod(h)?;
        let d_delta = self.htod(delta)?;
        let d_gate = self.htod(gate)?;
        unsafe {
            self.launch_dit_gated_residual_add(
                &mut d_h,
                &d_delta,
                &d_gate,
                rows as u32,
                dim as u32,
            )?;
        }
        self.dtoh_sync(&d_h, h)
    }

    // ── layer norm (affine = false) ──────────────────────────────────────────

    /// Launch `layer_norm_f32` (in place, one block per row).
    ///
    /// # Safety
    /// `d_x` holds ≥ `rows*dim` f32 on `self.stream`.
    pub(crate) unsafe fn launch_dit_layer_norm(
        &self,
        d_x: &mut CudaSlice<f32>,
        rows: u32,
        dim: u32,
        eps: f32,
    ) -> Result<(), CudaGraphError> {
        let cfg = LaunchConfig {
            grid_dim: (rows.max(1), 1, 1),
            block_dim: (256, 1, 1), // == LN_THREADS
            shared_mem_bytes: 0,
        };
        self.stream
            .launch_builder(&self.modules.dit_layer_norm)
            .arg(d_x)
            .arg(&rows)
            .arg(&dim)
            .arg(&eps)
            .launch(cfg)
            .map(|_| ())
            .map_err(|e| CudaGraphError::DriverError(format!("layer_norm_f32 launch: {e}")))
    }

    /// LayerNorm (affine = false) of `x` `[rows, dim]` in place: per row,
    /// `(x - mean) / sqrt(var + eps)` (population variance). Mirrors
    /// `oxibonsai-image::math::layer_norm_inplace`.
    ///
    /// # Errors
    /// [`CudaGraphError::DriverError`] on a length mismatch or buffer/launch error.
    pub fn encode_dit_layer_norm(
        &self,
        x: &mut [f32],
        rows: usize,
        dim: usize,
        eps: f32,
    ) -> Result<(), CudaGraphError> {
        let expected = rows.checked_mul(dim).ok_or_else(|| {
            CudaGraphError::DriverError("encode_dit_layer_norm: rows*dim overflow".into())
        })?;
        if x.len() != expected {
            return Err(CudaGraphError::DriverError(format!(
                "encode_dit_layer_norm: x len {} != rows*dim {expected}",
                x.len()
            )));
        }
        if expected == 0 || dim == 0 {
            return Ok(());
        }
        let mut d_x = self.htod(x)?;
        unsafe {
            self.launch_dit_layer_norm(&mut d_x, rows as u32, dim as u32, eps)?;
        }
        self.dtoh_sync(&d_x, x)
    }

    // ── per-head RMS norm (QK-RMSNorm) ───────────────────────────────────────

    /// Launch `rms_norm_heads_f32` (in place, one block per `head_dim` chunk).
    ///
    /// # Safety
    /// `d_x` holds ≥ `rows*head_dim` f32; `d_weight` ≥ `head_dim`; on `self.stream`.
    pub(crate) unsafe fn launch_dit_rms_norm_heads(
        &self,
        d_x: &mut CudaSlice<f32>,
        d_weight: &CudaSlice<f32>,
        rows: u32,
        head_dim: u32,
        eps: f32,
    ) -> Result<(), CudaGraphError> {
        let cfg = LaunchConfig {
            grid_dim: (rows.max(1), 1, 1),
            block_dim: (128, 1, 1), // == RMS_THREADS
            shared_mem_bytes: 0,
        };
        self.stream
            .launch_builder(&self.modules.dit_rms_norm_heads)
            .arg(d_x)
            .arg(d_weight)
            .arg(&rows)
            .arg(&head_dim)
            .arg(&eps)
            .launch(cfg)
            .map(|_| ())
            .map_err(|e| CudaGraphError::DriverError(format!("rms_norm_heads_f32 launch: {e}")))
    }

    /// Per-head QK-RMSNorm of `x` `[rows, head_dim]` (`rows = num_heads*seq`):
    /// `weight * x / sqrt(mean(x^2) + eps)` per `head_dim` chunk. In place.
    /// Mirrors `oxibonsai-image::math::rms_norm_heads_inplace`.
    ///
    /// # Errors
    /// [`CudaGraphError::DriverError`] on a length mismatch or buffer/launch error.
    pub fn encode_dit_rms_norm_heads(
        &self,
        x: &mut [f32],
        weight: &[f32],
        rows: usize,
        head_dim: usize,
        eps: f32,
    ) -> Result<(), CudaGraphError> {
        let expected = rows.checked_mul(head_dim).ok_or_else(|| {
            CudaGraphError::DriverError("encode_dit_rms_norm_heads: rows*head_dim overflow".into())
        })?;
        if x.len() != expected || weight.len() != head_dim {
            return Err(CudaGraphError::DriverError(format!(
                "encode_dit_rms_norm_heads: bad lens (x {} != {expected}, weight {} != head_dim {head_dim})",
                x.len(),
                weight.len()
            )));
        }
        if expected == 0 || head_dim == 0 {
            return Ok(());
        }
        let mut d_x = self.htod(x)?;
        let d_w = self.htod(weight)?;
        unsafe {
            self.launch_dit_rms_norm_heads(&mut d_x, &d_w, rows as u32, head_dim as u32, eps)?;
        }
        self.dtoh_sync(&d_x, x)
    }

    // ── SwiGLU ───────────────────────────────────────────────────────────────

    /// Launch `swiglu_f32`: `out[rows, half] = silu(x[:, :half]) * x[:, half:]`.
    ///
    /// # Safety
    /// `d_x` holds ≥ `rows*2*half` f32; `d_out` ≥ `rows*half`; on `self.stream`.
    pub(crate) unsafe fn launch_dit_swiglu(
        &self,
        d_x: &CudaSlice<f32>,
        d_out: &mut CudaSlice<f32>,
        rows: u32,
        half: u32,
    ) -> Result<(), CudaGraphError> {
        let cfg = LaunchConfig {
            grid_dim: (elementwise_grid(rows as usize * half as usize), 1, 1),
            block_dim: (DIT_GLUE_BLOCK, 1, 1),
            shared_mem_bytes: 0,
        };
        self.stream
            .launch_builder(&self.modules.dit_swiglu)
            .arg(d_x)
            .arg(d_out)
            .arg(&rows)
            .arg(&half)
            .launch(cfg)
            .map(|_| ())
            .map_err(|e| CudaGraphError::DriverError(format!("swiglu_f32 launch: {e}")))
    }

    /// SwiGLU over `x` `[rows, 2*half]` → `[rows, half]` = `silu(gate) * up`.
    /// Mirrors `oxibonsai-image::math::swiglu`.
    ///
    /// # Errors
    /// [`CudaGraphError::DriverError`] on a length mismatch or buffer/launch error.
    pub fn encode_dit_swiglu(
        &self,
        x: &[f32],
        out: &mut [f32],
        rows: usize,
        half: usize,
    ) -> Result<(), CudaGraphError> {
        let in_len = rows
            .checked_mul(2)
            .and_then(|x| x.checked_mul(half))
            .ok_or_else(|| {
                CudaGraphError::DriverError("encode_dit_swiglu: rows*2*half overflow".into())
            })?;
        let out_len = rows.checked_mul(half).ok_or_else(|| {
            CudaGraphError::DriverError("encode_dit_swiglu: rows*half overflow".into())
        })?;
        if x.len() != in_len || out.len() != out_len {
            return Err(CudaGraphError::DriverError(format!(
                "encode_dit_swiglu: bad lens (x {} != {in_len}, out {} != {out_len})",
                x.len(),
                out.len()
            )));
        }
        if out_len == 0 {
            return Ok(());
        }
        let d_x = self.htod(x)?;
        let mut d_out = self.alloc_zeros(out_len)?;
        unsafe {
            self.launch_dit_swiglu(&d_x, &mut d_out, rows as u32, half as u32)?;
        }
        self.dtoh_sync(&d_out, out)
    }

    // ── interleaved RoPE ─────────────────────────────────────────────────────

    /// Launch `rope_interleaved_f32` (in place) over head-major
    /// `x[num_heads, seq, head_dim]` with `cost`/`sint` `[seq, head_dim/2]`.
    ///
    /// # Safety
    /// `d_x` holds ≥ `num_heads*seq*head_dim` f32; `d_cos`/`d_sin` ≥
    /// `seq*head_dim/2`; on `self.stream`.
    pub(crate) unsafe fn launch_dit_rope(
        &self,
        d_x: &mut CudaSlice<f32>,
        d_cos: &CudaSlice<f32>,
        d_sin: &CudaSlice<f32>,
        num_heads: u32,
        seq: u32,
        head_dim: u32,
    ) -> Result<(), CudaGraphError> {
        let pairs = num_heads as usize * seq as usize * (head_dim as usize / 2);
        let cfg = LaunchConfig {
            grid_dim: (elementwise_grid(pairs), 1, 1),
            block_dim: (DIT_GLUE_BLOCK, 1, 1),
            shared_mem_bytes: 0,
        };
        self.stream
            .launch_builder(&self.modules.dit_rope_interleaved)
            .arg(d_x)
            .arg(d_cos)
            .arg(d_sin)
            .arg(&num_heads)
            .arg(&seq)
            .arg(&head_dim)
            .launch(cfg)
            .map(|_| ())
            .map_err(|e| CudaGraphError::DriverError(format!("rope_interleaved_f32 launch: {e}")))
    }

    /// Interleaved RoPE on head-major `x[num_heads, seq, head_dim]` in place;
    /// `cos`/`sin` are `[seq, head_dim/2]`. Mirrors
    /// `oxibonsai-image::math::apply_rope_inplace`.
    ///
    /// # Errors
    /// [`CudaGraphError::DriverError`] on a length/parity mismatch or buffer/launch
    /// error.
    pub fn encode_dit_rope(
        &self,
        x: &mut [f32],
        cos: &[f32],
        sin: &[f32],
        num_heads: usize,
        seq: usize,
        head_dim: usize,
    ) -> Result<(), CudaGraphError> {
        if head_dim % 2 != 0 {
            return Err(CudaGraphError::DriverError(format!(
                "encode_dit_rope: head_dim {head_dim} must be even"
            )));
        }
        let half = head_dim / 2;
        let expected = num_heads
            .checked_mul(seq)
            .and_then(|x| x.checked_mul(head_dim))
            .ok_or_else(|| CudaGraphError::DriverError("encode_dit_rope: x len overflow".into()))?;
        let tab = seq.checked_mul(half).ok_or_else(|| {
            CudaGraphError::DriverError("encode_dit_rope: seq*half overflow".into())
        })?;
        if x.len() != expected || cos.len() != tab || sin.len() != tab {
            return Err(CudaGraphError::DriverError(format!(
                "encode_dit_rope: bad lens (x {} != {expected}, cos {} / sin {} != seq*half {tab})",
                x.len(),
                cos.len(),
                sin.len()
            )));
        }
        if expected == 0 {
            return Ok(());
        }
        let mut d_x = self.htod(x)?;
        let d_cos = self.htod(cos)?;
        let d_sin = self.htod(sin)?;
        unsafe {
            self.launch_dit_rope(
                &mut d_x,
                &d_cos,
                &d_sin,
                num_heads as u32,
                seq as u32,
                head_dim as u32,
            )?;
        }
        self.dtoh_sync(&d_x, x)
    }

    // ── reshape: tokens → head-major ─────────────────────────────────────────

    /// Launch `tokens_to_heads_f32` (gather a token-major slice into head-major).
    ///
    /// # Safety
    /// `d_dst` holds ≥ `num_heads*seq*head_dim` f32; `d_src` covers
    /// `(seq-1)*src_stride + src_off + num_heads*head_dim`; on `self.stream`.
    #[allow(clippy::too_many_arguments)]
    pub(crate) unsafe fn launch_dit_tokens_to_heads(
        &self,
        d_src: &CudaSlice<f32>,
        d_dst: &mut CudaSlice<f32>,
        seq: u32,
        num_heads: u32,
        head_dim: u32,
        src_stride: u32,
        src_off: u32,
    ) -> Result<(), CudaGraphError> {
        let cfg = LaunchConfig {
            grid_dim: (
                elementwise_grid(num_heads as usize * seq as usize * head_dim as usize),
                1,
                1,
            ),
            block_dim: (DIT_GLUE_BLOCK, 1, 1),
            shared_mem_bytes: 0,
        };
        self.stream
            .launch_builder(&self.modules.dit_tokens_to_heads)
            .arg(d_src)
            .arg(d_dst)
            .arg(&seq)
            .arg(&num_heads)
            .arg(&head_dim)
            .arg(&src_stride)
            .arg(&src_off)
            .launch(cfg)
            .map(|_| ())
            .map_err(|e| CudaGraphError::DriverError(format!("tokens_to_heads_f32 launch: {e}")))
    }

    /// Reshape a token-major `[seq, hidden]` slice (`hidden = num_heads*head_dim`,
    /// at column `src_off`, row stride `src_stride`) into a head-major contiguous
    /// `[num_heads, seq, head_dim]` buffer. With `src_stride = hidden, src_off = 0`
    /// this is exactly `oxibonsai-image::math::to_heads`.
    ///
    /// # Errors
    /// [`CudaGraphError::DriverError`] on a length mismatch or buffer/launch error.
    #[allow(clippy::too_many_arguments)]
    pub fn encode_dit_tokens_to_heads(
        &self,
        src: &[f32],
        out: &mut [f32],
        seq: usize,
        num_heads: usize,
        head_dim: usize,
        src_stride: usize,
        src_off: usize,
    ) -> Result<(), CudaGraphError> {
        let out_len = num_heads
            .checked_mul(seq)
            .and_then(|x| x.checked_mul(head_dim))
            .ok_or_else(|| {
                CudaGraphError::DriverError("encode_dit_tokens_to_heads: out overflow".into())
            })?;
        let hidden = num_heads * head_dim;
        let need = if seq == 0 {
            0
        } else {
            (seq - 1) * src_stride + src_off + hidden
        };
        if out.len() != out_len || src.len() < need {
            return Err(CudaGraphError::DriverError(format!(
                "encode_dit_tokens_to_heads: bad lens (out {} != {out_len}, src {} < {need})",
                out.len(),
                src.len()
            )));
        }
        if out_len == 0 {
            return Ok(());
        }
        let d_src = self.htod(src)?;
        let mut d_out = self.alloc_zeros(out_len)?;
        unsafe {
            self.launch_dit_tokens_to_heads(
                &d_src,
                &mut d_out,
                seq as u32,
                num_heads as u32,
                head_dim as u32,
                src_stride as u32,
                src_off as u32,
            )?;
        }
        self.dtoh_sync(&d_out, out)
    }

    // ── reshape: strided per-row slice copy ──────────────────────────────────

    /// Launch `strided_row_copy_f32`: `dst[t,dst_off+j] = src[t,src_off+j]`.
    ///
    /// # Safety
    /// `d_dst`/`d_src` cover their `(rows-1)*stride + off + cols` ranges; on
    /// `self.stream`.
    #[allow(clippy::too_many_arguments)]
    pub(crate) unsafe fn launch_dit_strided_row_copy(
        &self,
        d_dst: &mut CudaSlice<f32>,
        d_src: &CudaSlice<f32>,
        rows: u32,
        cols: u32,
        dst_stride: u32,
        dst_off: u32,
        src_stride: u32,
        src_off: u32,
    ) -> Result<(), CudaGraphError> {
        let cfg = LaunchConfig {
            grid_dim: (elementwise_grid(rows as usize * cols as usize), 1, 1),
            block_dim: (DIT_GLUE_BLOCK, 1, 1),
            shared_mem_bytes: 0,
        };
        self.stream
            .launch_builder(&self.modules.dit_strided_row_copy)
            .arg(d_dst)
            .arg(d_src)
            .arg(&rows)
            .arg(&cols)
            .arg(&dst_stride)
            .arg(&dst_off)
            .arg(&src_stride)
            .arg(&src_off)
            .launch(cfg)
            .map(|_| ())
            .map_err(|e| CudaGraphError::DriverError(format!("strided_row_copy_f32 launch: {e}")))
    }

    /// Per-row slice copy `dst[t, dst_off+j] = src[t, src_off+j]` (`j < cols`,
    /// `t < rows`). Extracts the mlp slab from a fused proj / builds the
    /// `[attn ‖ gated]` concat without per-row host copies.
    ///
    /// # Errors
    /// [`CudaGraphError::DriverError`] on a range mismatch or buffer/launch error.
    #[allow(clippy::too_many_arguments)]
    pub fn encode_dit_strided_row_copy(
        &self,
        dst: &mut [f32],
        src: &[f32],
        rows: usize,
        cols: usize,
        dst_stride: usize,
        dst_off: usize,
        src_stride: usize,
        src_off: usize,
    ) -> Result<(), CudaGraphError> {
        if rows == 0 || cols == 0 {
            return Ok(());
        }
        let dst_need = (rows - 1) * dst_stride + dst_off + cols;
        let src_need = (rows - 1) * src_stride + src_off + cols;
        if dst.len() < dst_need || src.len() < src_need {
            return Err(CudaGraphError::DriverError(format!(
                "encode_dit_strided_row_copy: short (dst {} < {dst_need}, src {} < {src_need})",
                dst.len(),
                src.len()
            )));
        }
        let mut d_dst = self.htod(dst)?;
        let d_src = self.htod(src)?;
        unsafe {
            self.launch_dit_strided_row_copy(
                &mut d_dst,
                &d_src,
                rows as u32,
                cols as u32,
                dst_stride as u32,
                dst_off as u32,
                src_stride as u32,
                src_off as u32,
            )?;
        }
        self.dtoh_sync(&d_dst, dst)
    }

    // ── small shared host<->device helpers ──────────────────────────────────

    /// Upload a host slice to a fresh device buffer.
    pub(crate) fn htod(&self, src: &[f32]) -> Result<CudaSlice<f32>, CudaGraphError> {
        self.stream
            .clone_htod(src)
            .map_err(|e| CudaGraphError::DriverError(format!("dit glue htod: {e}")))
    }

    /// Allocate a zeroed device buffer.
    pub(crate) fn alloc_zeros(&self, len: usize) -> Result<CudaSlice<f32>, CudaGraphError> {
        self.stream
            .alloc_zeros::<f32>(len)
            .map_err(|e| CudaGraphError::DriverError(format!("dit glue alloc_zeros: {e}")))
    }

    /// Download a device buffer into `dst` and synchronize.
    pub(crate) fn dtoh_sync(
        &self,
        src: &CudaSlice<f32>,
        dst: &mut [f32],
    ) -> Result<(), CudaGraphError> {
        self.stream
            .memcpy_dtoh(src, dst)
            .map_err(|e| CudaGraphError::DriverError(format!("dit glue dtoh: {e}")))?;
        self.stream
            .synchronize()
            .map_err(|e| CudaGraphError::DriverError(format!("dit glue sync: {e}")))
    }
}

#[cfg(test)]
mod tests {
    use super::CudaGraph;

    fn cosine(a: &[f32], b: &[f32]) -> f32 {
        let mut dot = 0f64;
        let mut na = 0f64;
        let mut nb = 0f64;
        for (&x, &y) in a.iter().zip(b.iter()) {
            dot += x as f64 * y as f64;
            na += x as f64 * x as f64;
            nb += y as f64 * y as f64;
        }
        if na == 0.0 || nb == 0.0 {
            return 1.0;
        }
        (dot / (na.sqrt() * nb.sqrt())) as f32
    }
    fn max_abs(a: &[f32], b: &[f32]) -> f32 {
        a.iter()
            .zip(b.iter())
            .fold(0f32, |m, (&x, &y)| m.max((x - y).abs()))
    }
    /// Deterministic LCG fill in [-0.5, 0.5).
    fn lcg_fill(buf: &mut [f32], seed: u64) {
        let mut s = seed | 1;
        for v in buf.iter_mut() {
            s = s
                .wrapping_mul(6364136223846793005)
                .wrapping_add(1442695040888963407);
            *v = ((s >> 33) as f32 / (1u64 << 31) as f32) - 0.5;
        }
    }

    macro_rules! graph_or_skip {
        () => {
            match CudaGraph::global() {
                Ok(g) => g,
                Err(e) => {
                    eprintln!("no GPU, skip: {e}");
                    return;
                }
            }
        };
    }

    #[test]
    fn modulate_matches_cpu() {
        let _serial = super::super::types::gpu_parity_test_guard();
        let g = graph_or_skip!();
        let (rows, dim) = (40usize, 320usize);
        let mut x = vec![0f32; rows * dim];
        let mut shift = vec![0f32; dim];
        let mut scale = vec![0f32; dim];
        lcg_fill(&mut x, 1);
        lcg_fill(&mut shift, 2);
        lcg_fill(&mut scale, 3);
        let mut cpu = x.clone();
        for r in 0..rows {
            for i in 0..dim {
                let idx = r * dim + i;
                cpu[idx] = (1.0 + scale[i]) * cpu[idx] + shift[i];
            }
        }
        g.encode_dit_modulate(&mut x, &shift, &scale, rows, dim)
            .expect("encode_dit_modulate");
        assert!(cosine(&cpu, &x) >= 0.999 && max_abs(&cpu, &x) < 1e-3);
    }

    #[test]
    fn gated_residual_add_matches_cpu() {
        let _serial = super::super::types::gpu_parity_test_guard();
        let g = graph_or_skip!();
        let (rows, dim) = (48usize, 256usize);
        let mut h = vec![0f32; rows * dim];
        let mut delta = vec![0f32; rows * dim];
        let mut gate = vec![0f32; dim];
        lcg_fill(&mut h, 4);
        lcg_fill(&mut delta, 5);
        lcg_fill(&mut gate, 6);
        let mut cpu = h.clone();
        for r in 0..rows {
            for i in 0..dim {
                cpu[r * dim + i] += gate[i] * delta[r * dim + i];
            }
        }
        g.encode_dit_gated_residual_add(&mut h, &delta, &gate, rows, dim)
            .expect("encode_dit_gated_residual_add");
        assert!(cosine(&cpu, &h) >= 0.999 && max_abs(&cpu, &h) < 1e-3);
    }

    #[test]
    fn layer_norm_matches_cpu() {
        let _serial = super::super::types::gpu_parity_test_guard();
        let g = graph_or_skip!();
        let (rows, dim, eps) = (33usize, 512usize, 1e-6f32);
        let mut x = vec![0f32; rows * dim];
        lcg_fill(&mut x, 7);
        let mut cpu = x.clone();
        let inv = 1.0f32 / dim as f32;
        for r in 0..rows {
            let row = &mut cpu[r * dim..(r + 1) * dim];
            let mean: f32 = row.iter().sum::<f32>() * inv;
            let var: f32 = row.iter().map(|&v| (v - mean) * (v - mean)).sum::<f32>() * inv;
            let inv_std = 1.0 / (var + eps).sqrt();
            for v in row.iter_mut() {
                *v = (*v - mean) * inv_std;
            }
        }
        g.encode_dit_layer_norm(&mut x, rows, dim, eps)
            .expect("encode_dit_layer_norm");
        assert!(
            cosine(&cpu, &x) >= 0.999 && max_abs(&cpu, &x) < 1e-3,
            "cos={} mae={}",
            cosine(&cpu, &x),
            max_abs(&cpu, &x)
        );
    }

    #[test]
    fn rms_norm_heads_matches_cpu() {
        let _serial = super::super::types::gpu_parity_test_guard();
        let g = graph_or_skip!();
        let (rows, head_dim, eps) = (50usize, 128usize, 1e-6f32);
        let mut x = vec![0f32; rows * head_dim];
        let mut w = vec![0f32; head_dim];
        lcg_fill(&mut x, 8);
        lcg_fill(&mut w, 9);
        let mut cpu = x.clone();
        let inv = 1.0f32 / head_dim as f32;
        for r in 0..rows {
            let row = &mut cpu[r * head_dim..(r + 1) * head_dim];
            let ms: f32 = row.iter().map(|&v| v * v).sum::<f32>() * inv;
            let inv_rms = 1.0 / (ms + eps).sqrt();
            for (i, v) in row.iter_mut().enumerate() {
                *v = w[i] * *v * inv_rms;
            }
        }
        g.encode_dit_rms_norm_heads(&mut x, &w, rows, head_dim, eps)
            .expect("encode_dit_rms_norm_heads");
        assert!(cosine(&cpu, &x) >= 0.999 && max_abs(&cpu, &x) < 1e-3);
    }

    #[test]
    fn swiglu_matches_cpu() {
        let _serial = super::super::types::gpu_parity_test_guard();
        let g = graph_or_skip!();
        let (rows, half) = (40usize, 384usize);
        let mut x = vec![0f32; rows * 2 * half];
        lcg_fill(&mut x, 10);
        let mut cpu = vec![0f32; rows * half];
        let full = 2 * half;
        for r in 0..rows {
            for i in 0..half {
                let gate = x[r * full + i];
                let up = x[r * full + half + i];
                cpu[r * half + i] = (gate / (1.0 + (-gate).exp())) * up;
            }
        }
        let mut out = vec![0f32; rows * half];
        g.encode_dit_swiglu(&x, &mut out, rows, half)
            .expect("encode_dit_swiglu");
        assert!(cosine(&cpu, &out) >= 0.999 && max_abs(&cpu, &out) < 1e-3);
    }

    #[test]
    fn rope_matches_cpu() {
        let _serial = super::super::types::gpu_parity_test_guard();
        let g = graph_or_skip!();
        let (num_heads, seq, head_dim) = (4usize, 50usize, 128usize);
        let half = head_dim / 2;
        let mut x = vec![0f32; num_heads * seq * head_dim];
        let mut cos = vec![0f32; seq * half];
        let mut sin = vec![0f32; seq * half];
        lcg_fill(&mut x, 11);
        // Real-ish rotary tables: cos/sin of per-position angles.
        for t in 0..seq {
            for i in 0..half {
                let theta = (t as f32) * (0.01 + 0.0003 * i as f32);
                cos[t * half + i] = theta.cos();
                sin[t * half + i] = theta.sin();
            }
        }
        let mut cpu = x.clone();
        for h in 0..num_heads {
            for t in 0..seq {
                let base = (h * seq + t) * head_dim;
                for i in 0..half {
                    let re = cpu[base + 2 * i];
                    let im = cpu[base + 2 * i + 1];
                    let c = cos[t * half + i];
                    let s = sin[t * half + i];
                    cpu[base + 2 * i] = re * c - im * s;
                    cpu[base + 2 * i + 1] = im * c + re * s;
                }
            }
        }
        g.encode_dit_rope(&mut x, &cos, &sin, num_heads, seq, head_dim)
            .expect("encode_dit_rope");
        assert!(cosine(&cpu, &x) >= 0.999 && max_abs(&cpu, &x) < 1e-3);
    }

    #[test]
    fn tokens_to_heads_matches_cpu() {
        let _serial = super::super::types::gpu_parity_test_guard();
        let g = graph_or_skip!();
        let (seq, num_heads, head_dim) = (50usize, 6usize, 64usize);
        let hidden = num_heads * head_dim;
        // Token-major src as a column slice (stride/off) of a wider fused proj.
        let (stride, off) = (hidden + 17, 9usize);
        let mut src = vec![0f32; seq * stride];
        lcg_fill(&mut src, 21);
        let mut cpu = vec![0f32; num_heads * seq * head_dim];
        for h in 0..num_heads {
            for t in 0..seq {
                for d in 0..head_dim {
                    cpu[(h * seq + t) * head_dim + d] = src[t * stride + off + h * head_dim + d];
                }
            }
        }
        let mut out = vec![0f32; num_heads * seq * head_dim];
        g.encode_dit_tokens_to_heads(&src, &mut out, seq, num_heads, head_dim, stride, off)
            .expect("encode_dit_tokens_to_heads");
        assert_eq!(cpu, out, "tokens_to_heads gather must be exact");
    }

    #[test]
    fn strided_row_copy_matches_cpu() {
        let _serial = super::super::types::gpu_parity_test_guard();
        let g = graph_or_skip!();
        let (rows, cols) = (40usize, 128usize);
        let (dst_stride, dst_off) = (cols + 20, 7usize);
        let (src_stride, src_off) = (cols + 13, 5usize);
        let mut dst = vec![0f32; rows * dst_stride];
        let mut src = vec![0f32; rows * src_stride];
        lcg_fill(&mut dst, 22);
        lcg_fill(&mut src, 23);
        let mut cpu = dst.clone();
        for t in 0..rows {
            for j in 0..cols {
                cpu[t * dst_stride + dst_off + j] = src[t * src_stride + src_off + j];
            }
        }
        g.encode_dit_strided_row_copy(
            &mut dst, &src, rows, cols, dst_stride, dst_off, src_stride, src_off,
        )
        .expect("encode_dit_strided_row_copy");
        assert_eq!(cpu, dst, "strided_row_copy must be exact");
    }
}