hf2q 0.1.33

Pure Rust CLI for converting HuggingFace models to hardware-optimized formats and serving them over an OpenAI-compatible API on Apple Silicon
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
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
//! Metal gates for the generation-cue fold (`HF2Q_QWEN_PREFILL_FOLD_CUE`,
//! [`super::prefill_fold`]).
//!
//! The fold is a Class A change: one forward over a content chunk and the
//! generation cue must leave exactly what the two forwards and the
//! checkpoint between them leave. These gates compare, bit for bit:
//! - the checkpoint at the stable boundary (cursors, DeltaNet image) and
//!   the logits of the last content row;
//! - the last-row logits;
//! - the captured target-layer rows of both segments;
//! - the slot afterwards: its DeltaNet state and, through every logits row
//!   of a follow-up chunk, its full-attention rows.
//!
//! They run the synthetic native K-quant model of the joint verify tests on
//! fresh and resumed slots, at chunk sizes on both sides of a 32-row tile
//! boundary, with and without the DFlash capture, in every slot of one- to
//! four-slot TQ and F16 caches, and on a variant whose FFN layers are
//! quantized MoE layers (the arm the fold builds once per segment). A
//! request that restores a folded checkpoint and diverges behind it reads
//! what it reads behind the two-forward checkpoint. The fold is refused, so
//! the scheduler keeps the two forwards, beside a bound GLP vector,
//! dump-bisect and the dense-Q split profile. The ignored real-artifact
//! gate in the middle of the file repeats the comparison on a served model
//! (`HF2Q_QWEN38_GGUF` or `HF2Q_QWEN36_GGUF`) and prints both walls. The
//! scheduler's side is in `serve::api::engine_qwen35`'s
//! `prefill_fold_scheduler_tests`.

use super::kv_cache::HybridKvCache;
use super::model::Qwen35Model;
use super::prefill_prepared::PrefillScope;
// The fixture rescaled so that every routed product of every layer reaches
// the logits: the joint-verify fixture's own scale saturates the residual
// stream after the first layer and would hide the attention the fold splits.
use super::prefill_prepared_tests::with_synthetic_model;
use super::spec_decode::positions_for_range;
use crate::inference::spec_decode::dflash::hidden_capture::DFlashCaptureSession;
use crate::serve::multi_seq_kv::SlotId;

const SLOTS: u32 = 2;
const CAPACITY: u32 = 256;
/// Target layers of the synthetic model the gates capture: DeltaNet layers
/// on and off a full-attention neighbour, and the last full-attention layer.
const CAPTURE_LAYERS: [usize; 4] = [1, 4, 6, 7];
/// Rows of the follow-up chunk that reads the slot back.
const FOLLOW_UP_ROWS: usize = 7;

fn token(slot: u32, position: usize) -> u32 {
    (slot * 131 + position as u32 * 37 + 11) % 512
}

fn tokens(slot: u32, start: usize, len: usize) -> Vec<u32> {
    (start..start + len)
        .map(|position| token(slot, position))
        .collect()
}

fn bits(values: &[f32]) -> Vec<u32> {
    values.iter().map(|value| value.to_bits()).collect()
}

thread_local! {
    /// `(slots, TQ full-attention cache)` of the caches the gates allocate.
    static CACHE_SHAPE: std::cell::Cell<(u32, bool)> =
        const { std::cell::Cell::new((SLOTS, true)) };
}

/// Another cache shape for the gates on this thread, until dropped.
struct CacheShape;

impl CacheShape {
    fn set(slots: u32, tq: bool) -> Self {
        CACHE_SHAPE.with(|cell| cell.set((slots, tq)));
        Self
    }
}

impl Drop for CacheShape {
    fn drop(&mut self) {
        CACHE_SHAPE.with(|cell| cell.set((SLOTS, true)));
    }
}

fn cache(model: &Qwen35Model) -> HybridKvCache {
    let (slots, tq) = CACHE_SHAPE.with(std::cell::Cell::get);
    model
        .with_gpu_cache_mut(|device, _registry| {
            HybridKvCache::new_with_options(&model.cfg, device, CAPACITY, slots, tq)
        })
        .expect("allocate cache")
}

/// Everything a prefill over `[start, start + prefix + cue)` leaves behind.
#[derive(PartialEq)]
struct Outcome {
    boundary_logits: Vec<u32>,
    boundary: (usize, u64, u32, Vec<u32>, Option<u32>, Vec<u32>),
    logits: Vec<u32>,
    /// Captured rows per target layer, prefix rows then cue rows.
    captured: Vec<Vec<u32>>,
    /// The slot's DeltaNet state and cursors after the cue.
    after: (usize, u64, u32, Vec<u32>, Option<u32>, Vec<u32>),
    /// Every logits row of a chunk prefilled after the cue.
    follow_up: Vec<u32>,
}

impl Outcome {
    fn differences(&self, other: &Self) -> Vec<&'static str> {
        [
            (
                "boundary logits",
                self.boundary_logits == other.boundary_logits,
            ),
            ("boundary checkpoint", self.boundary == other.boundary),
            ("last-row logits", self.logits == other.logits),
            ("captured target layers", self.captured == other.captured),
            ("slot state after the cue", self.after == other.after),
            ("follow-up chunk", self.follow_up == other.follow_up),
        ]
        .into_iter()
        .filter_map(|(name, equal)| (!equal).then_some(name))
        .collect()
    }
}

/// How a prefill reaches the end of the cue.
#[derive(Clone, Copy, PartialEq, Debug)]
enum Path {
    /// Content chunk, checkpoint, cue chunk.
    TwoForwards,
    /// One folded forward.
    Folded,
}

/// One forward of `len` rows at `start`, optionally capturing
/// [`CAPTURE_LAYERS`]. Returns the last-row logits and the captured rows per
/// layer.
fn forward(
    model: &Qwen35Model,
    cache: &mut HybridKvCache,
    slot: u32,
    start: usize,
    len: usize,
    capture: bool,
) -> (Vec<u32>, Vec<Vec<u32>>) {
    let hidden = model.cfg.hidden_size as usize;
    let chunk = tokens(slot, start, len);
    let positions = positions_for_range(start as i32, len);
    if !capture {
        let logits = model
            .forward_gpu_last_logits(&chunk, &positions, cache, SlotId(slot))
            .unwrap_or_else(|error| panic!("chunk start={start} n={len}: {error:#}"));
        return (bits(&logits), Vec::new());
    }
    let mut session = DFlashCaptureSession::new(CAPTURE_LAYERS.to_vec(), len, hidden, false);
    let (logits, _hidden) = model
        .forward_gpu_last_logits_with_hidden_dflash(
            &chunk,
            &positions,
            cache,
            Some(&mut session),
            SlotId(slot),
        )
        .unwrap_or_else(|error| panic!("captured chunk start={start} n={len}: {error:#}"));
    let rows = session
        .hidden_output
        .chunks(len * hidden)
        .map(bits)
        .collect();
    (bits(&logits), rows)
}

/// A folded forward as the scheduler runs it: the checkpoint storage is
/// allocated first, then the forward fills it.
#[allow(clippy::too_many_arguments)]
fn folded_forward(
    model: &Qwen35Model,
    tokens: &[u32],
    positions: &[i32],
    cache: &mut HybridKvCache,
    capture: Option<&mut DFlashCaptureSession>,
    slot: SlotId,
    prefix_rows: usize,
    boundary_len: usize,
) -> anyhow::Result<super::forward_gpu::FoldedPrefill> {
    let image = cache.boundary_anchor_image()?;
    model.forward_gpu_last_logits_folded(
        tokens,
        positions,
        cache,
        capture,
        slot,
        prefix_rows,
        boundary_len,
        image,
    )
}

/// Prefill `history` rows in `history_chunks` chunks, then the content and
/// cue rows along `path`, on `slot` of a cache whose other slot holds
/// `peer_rows` rows, on the prepared prefill route.
#[allow(clippy::too_many_arguments)]
fn prefill(
    model: &Qwen35Model,
    path: Path,
    slot: u32,
    history: &[usize],
    prefix: usize,
    cue: usize,
    capture: bool,
    peer_rows: usize,
) -> Outcome {
    prefill_on(
        model, true, path, slot, history, prefix, cue, capture, peer_rows,
    )
}

/// [`prefill`] on the prepared (`true`) or the stock prefill route.
#[allow(clippy::too_many_arguments)]
fn prefill_on(
    model: &Qwen35Model,
    prepared: bool,
    path: Path,
    slot: u32,
    history: &[usize],
    prefix: usize,
    cue: usize,
    capture: bool,
    peer_rows: usize,
) -> Outcome {
    let _scope = PrefillScope::forced(prepared);
    let mut cache = cache(model);
    if peer_rows > 0 {
        forward(model, &mut cache, 1 - slot, 0, peer_rows, false);
    }
    let mut start = 0;
    for &rows in history {
        forward(model, &mut cache, slot, start, rows, false);
        start += rows;
    }
    let boundary_len = start + prefix;
    let (boundary_logits, boundary, logits, captured) = match path {
        Path::TwoForwards => {
            let (boundary_logits, mut captured) =
                forward(model, &mut cache, slot, start, prefix, capture);
            let boundary = cache
                .snapshot_slot_anchor(SlotId(slot), boundary_len)
                .expect("checkpoint at the boundary")
                .test_words();
            let (logits, cue_rows) = forward(model, &mut cache, slot, boundary_len, cue, capture);
            for (layer, rows) in captured.iter_mut().zip(cue_rows) {
                layer.extend(rows);
            }
            (boundary_logits, boundary, logits, captured)
        }
        Path::Folded => {
            let hidden = model.cfg.hidden_size as usize;
            let len = prefix + cue;
            let mut session = capture
                .then(|| DFlashCaptureSession::new(CAPTURE_LAYERS.to_vec(), len, hidden, false));
            let folded = folded_forward(
                model,
                &tokens(slot, start, len),
                &positions_for_range(start as i32, len),
                &mut cache,
                session.as_mut(),
                SlotId(slot),
                prefix,
                boundary_len,
            )
            .unwrap_or_else(|error| {
                panic!("folded chunk start={start} n={prefix}+{cue}: {error:#}")
            });
            let captured = session.map_or_else(Vec::new, |session| {
                session
                    .hidden_output
                    .chunks(len * hidden)
                    .map(bits)
                    .collect()
            });
            (
                bits(&folded.boundary_logits),
                folded.boundary.expect("checkpoint").test_words(),
                bits(&folded.logits),
                captured,
            )
        }
    };
    let end = boundary_len + cue;
    let after = cache
        .snapshot_slot_anchor(SlotId(slot), end)
        .expect("slot state after the cue")
        .test_words();
    let follow_up = model
        .forward_gpu(
            &tokens(slot, end, FOLLOW_UP_ROWS),
            &positions_for_range(end as i32, FOLLOW_UP_ROWS),
            &mut cache,
            SlotId(slot),
        )
        .expect("follow-up chunk");
    Outcome {
        boundary_logits,
        boundary,
        logits,
        captured,
        after,
        follow_up: bits(&follow_up),
    }
}

/// `(history chunks, content rows, cue rows)`: fresh and resumed slots; a
/// content chunk whose last tile takes the cue, one whose cue starts a new
/// tile, one that ends on a tile boundary; the short-chunk direct TQ route
/// and the dequantized-history route for the content rows; the smallest
/// admitted fold and the longest admitted cue.
const SHAPES: [(&[usize], usize, usize); 8] = [
    (&[], 40, 5),
    (&[], 31, 5),
    (&[], 64, 5),
    (&[], 2, 2),
    (&[33], 20, 5),
    (&[64, 20], 40, 5),
    (&[17], 97, 8),
    (&[40], 37, 32),
];

#[test]
fn the_synthetic_model_folds_exactly_only_on_the_prepared_route() {
    with_synthetic_model(|model| {
        {
            let _scope = PrefillScope::forced(true);
            assert!(
                model.prefill_fold_exact().expect("exactness"),
                "every shared product of the synthetic model takes the prepared kernel"
            );
        }
        let _scope = PrefillScope::forced(false);
        assert!(
            !model.prefill_fold_exact().expect("exactness"),
            "the stock matmul picks its kernel by row count"
        );
    });
}

#[test]
fn a_folded_prefill_leaves_exactly_what_the_two_forwards_leave() {
    with_synthetic_model(|model| {
        for capture in [true, false] {
            for (history, prefix, cue) in SHAPES {
                let two = prefill(
                    model,
                    Path::TwoForwards,
                    0,
                    history,
                    prefix,
                    cue,
                    capture,
                    0,
                );
                let folded = prefill(model, Path::Folded, 0, history, prefix, cue, capture, 0);
                let differences = two.differences(&folded);
                assert!(
                    differences.is_empty(),
                    "history {history:?}, {prefix} + {cue} rows, capture {capture}: the fold \
                     differs from the two forwards in {differences:?}"
                );
                assert_eq!(
                    two.captured.len(),
                    if capture { CAPTURE_LAYERS.len() } else { 0 }
                );
            }
        }
    });
}

/// The comparison is not vacuous: on the stock route, whose matmul picks
/// its kernel by row count, the same fold changes the cue rows. That is why
/// the fold is taken only where [`Qwen35Model::prefill_fold_exact`] holds.
#[test]
fn on_the_stock_route_the_same_fold_changes_bits() {
    with_synthetic_model(|model| {
        let two = prefill_on(model, false, Path::TwoForwards, 0, &[], 40, 5, true, 0);
        let folded = prefill_on(model, false, Path::Folded, 0, &[], 40, 5, true, 0);
        let differences = two.differences(&folded);
        assert!(
            differences.contains(&"last-row logits"),
            "the stock route folded exactly: {differences:?}"
        );
    });
}

#[test]
fn a_folded_prefill_is_repeatable_and_does_not_depend_on_the_other_slot() {
    with_synthetic_model(|model| {
        let run = |peer_rows| prefill(model, Path::Folded, 1, &[33], 40, 5, true, peer_rows);
        let alone = run(0);
        // Other rows through the same pooled scratch and arenas.
        prefill(model, Path::Folded, 0, &[], 64, 5, false, 0);
        prefill(model, Path::TwoForwards, 0, &[17], 31, 5, true, 0);
        let again = run(0);
        assert!(
            alone.differences(&again).is_empty(),
            "a repeated folded prefill changed {:?}",
            alone.differences(&again)
        );
        let beside = run(70);
        assert!(
            alone.differences(&beside).is_empty(),
            "slot 1's folded prefill depends on what slot 0 holds: {:?}",
            alone.differences(&beside)
        );
        let two = prefill(model, Path::TwoForwards, 1, &[33], 40, 5, true, 70);
        assert!(
            two.differences(&beside).is_empty(),
            "slot 1 beside a busy slot 0: {:?}",
            two.differences(&beside)
        );
    });
}

#[test]
fn a_fold_outside_the_served_shape_is_refused_before_the_slot_changes() {
    with_synthetic_model(|model| {
        let _scope = PrefillScope::forced(true);
        let mut cache = cache(model);
        for (prefix, cue) in [(40, 1), (1, 5), (40, 33)] {
            let len = prefix + cue;
            let refused = folded_forward(
                model,
                &tokens(0, 0, len),
                &positions_for_range(0, len),
                &mut cache,
                None,
                SlotId(0),
                prefix,
                prefix,
            );
            assert!(refused.is_err(), "{prefix} + {cue} rows folded");
            assert_eq!(
                cache.sequence_len_for_slot(SlotId(0)).expect("cursor"),
                0,
                "a refused fold moved the slot"
            );
        }
    });
}

/// Real-artifact gate, run explicitly on one served model:
/// `HF2Q_QWEN38_GGUF=… <test binary> qwen_prefill_fold_real_model_gate --ignored --nocapture --test-threads=1`
/// (or `HF2Q_QWEN36_GGUF=…`; add `HF2Q_ENCODER_SESSION=1` for the served
/// encoder sessions).
///
/// On a TQ cache it prefills the chunk sizes of the four-agent workload, each
/// followed by its five-row cue, along both paths and checks that the fold
/// leaves the same bits as the two forwards and the checkpoint between them.
/// It prints each pair's wall time (second pass of each, so weights,
/// pipelines and prepared planes are warm).
#[test]
#[ignore = "requires HF2Q_QWEN38_GGUF or HF2Q_QWEN36_GGUF and Apple Metal; run alone"]
fn qwen_prefill_fold_real_model_gate() {
    use std::time::Instant;

    use crate::serve::header::LoadProgress;
    use mlx_native::gguf::GgufFile;

    const PATTERN: [u32; 7] = [9707, 374, 279, 15, 1_782, 2_610, 13];
    /// Content rows of consecutive turns; each is followed by the cue and by
    /// untimed rows standing in for the generated reply.
    const TURNS: [usize; 6] = [720, 31, 157, 200, 96, 266];
    const CUE: usize = 5;
    const REPLY: usize = 150;
    const GATE_CAPACITY: u32 = 4096;

    let _gpu = crate::inference::hf2q_gpu_test_lock();
    let (path, capture_layers) = match (
        std::env::var_os("HF2Q_QWEN38_GGUF"),
        std::env::var_os("HF2Q_QWEN36_GGUF"),
    ) {
        (Some(path), None) => (path, vec![5, 19, 33, 47, 61]),
        (None, Some(path)) => (path, vec![1, 6, 11, 16, 22, 27, 32, 37]),
        _ => panic!("set exactly one of HF2Q_QWEN38_GGUF and HF2Q_QWEN36_GGUF"),
    };
    let path = std::path::PathBuf::from(path);
    let gguf = GgufFile::open(&path).expect("open GGUF");
    let mut progress = LoadProgress::new(false, 1, 0);
    let mut model = Qwen35Model::load_from_gguf(&gguf, &mut progress).expect("load Qwen model");
    let policy = super::routing_policy::resolve_qwen_ggml_routing_policy(&gguf, &model.cfg);
    model.bind_ggml_routing_policy(policy);
    let model = model
        .finalize_canonical_experts()
        .expect("finalize bound experts before folded prefill caches");
    model.ensure_gpu_cache_primed().expect("prime GPU cache");
    let hidden = model.cfg.hidden_size as usize;
    let chunk_tokens = |start: usize, len: usize| -> Vec<u32> {
        (start..start + len)
            .map(|index| PATTERN[index % PATTERN.len()])
            .collect()
    };
    let new_cache = || {
        model
            .with_gpu_cache_mut(|device, _registry| {
                HybridKvCache::new_with_options(&model.cfg, device, GATE_CAPACITY, 1, true)
            })
            .expect("allocate TQ cache")
    };
    let _scope = PrefillScope::forced(true);
    assert!(
        model.prefill_fold_exact().expect("exactness"),
        "the fold is not exact for this artifact on the prepared route"
    );

    // One turn along `path`: the checkpoint, both logit rows and the
    // captured rows as bits, and the walls of its parts (content forward,
    // snapshot, cue forward; or the folded forward alone).
    let turn = |cache: &mut HybridKvCache, path: Path, start: usize, prefix: usize| {
        let ms = |since: Instant| since.elapsed().as_secs_f64() * 1e3;
        match path {
            Path::TwoForwards => {
                let mut content =
                    DFlashCaptureSession::new(capture_layers.clone(), prefix, hidden, false);
                let started = Instant::now();
                let (boundary_logits, _) = model
                    .forward_gpu_last_logits_with_hidden_dflash(
                        &chunk_tokens(start, prefix),
                        &positions_for_range(start as i32, prefix),
                        cache,
                        Some(&mut content),
                        SlotId(0),
                    )
                    .expect("content chunk");
                let content_ms = ms(started);
                let started = Instant::now();
                let boundary = cache
                    .snapshot_slot_anchor(SlotId(0), start + prefix)
                    .expect("checkpoint");
                let snapshot_ms = ms(started);
                let mut cue = DFlashCaptureSession::new(capture_layers.clone(), CUE, hidden, false);
                let started = Instant::now();
                let (logits, _) = model
                    .forward_gpu_last_logits_with_hidden_dflash(
                        &chunk_tokens(start + prefix, CUE),
                        &positions_for_range((start + prefix) as i32, CUE),
                        cache,
                        Some(&mut cue),
                        SlotId(0),
                    )
                    .expect("cue chunk");
                let cue_ms = ms(started);
                let captured: Vec<Vec<u32>> = content
                    .hidden_output
                    .chunks(prefix * hidden)
                    .zip(cue.hidden_output.chunks(CUE * hidden))
                    .map(|(content, cue)| {
                        let mut rows = bits(content);
                        rows.extend(bits(cue));
                        rows
                    })
                    .collect();
                (
                    [content_ms, snapshot_ms, cue_ms],
                    (boundary_logits, boundary.test_words(), logits, captured),
                )
            }
            Path::Folded => {
                let len = prefix + CUE;
                let mut session =
                    DFlashCaptureSession::new(capture_layers.clone(), len, hidden, false);
                let started = Instant::now();
                let folded = folded_forward(
                    &model,
                    &chunk_tokens(start, len),
                    &positions_for_range(start as i32, len),
                    cache,
                    Some(&mut session),
                    SlotId(0),
                    prefix,
                    start + prefix,
                )
                .expect("folded chunk");
                let folded_ms = ms(started);
                let captured = session
                    .hidden_output
                    .chunks(len * hidden)
                    .map(bits)
                    .collect();
                (
                    [folded_ms, 0.0, 0.0],
                    (
                        folded.boundary_logits,
                        folded.boundary.expect("checkpoint").test_words(),
                        folded.logits,
                        captured,
                    ),
                )
            }
        }
    };
    // Two passes per path: the first warms weights, pipelines and planes.
    let mut walls = Vec::new();
    let mut outcomes = Vec::new();
    for path in [Path::TwoForwards, Path::Folded] {
        for pass in 0..2 {
            let mut cache = new_cache();
            let mut start = 0;
            let mut pass_walls = Vec::new();
            let mut pass_outcomes = Vec::new();
            for prefix in TURNS {
                let (wall, (boundary_logits, boundary, logits, captured)) =
                    turn(&mut cache, path, start, prefix);
                assert!(logits.iter().all(|value| value.is_finite()));
                pass_walls.push(wall);
                pass_outcomes.push((bits(&boundary_logits), boundary, bits(&logits), captured));
                start += prefix + CUE;
                model
                    .forward_gpu_last_logits(
                        &chunk_tokens(start, REPLY),
                        &positions_for_range(start as i32, REPLY),
                        &mut cache,
                        SlotId(0),
                    )
                    .expect("reply rows");
                start += REPLY;
            }
            let after = cache
                .snapshot_slot_anchor(SlotId(0), start)
                .expect("final slot state")
                .test_words();
            if pass == 1 {
                walls.push(pass_walls);
                outcomes.push((pass_outcomes, after));
            }
        }
    }
    // The same rows as one ordinary forward (no boundary, no checkpoint):
    // what a chunk of that many rows costs, for the fold's own overhead.
    let mut plain = Vec::new();
    for _pass in 0..2 {
        let mut cache = new_cache();
        let mut start = 0;
        plain.clear();
        for prefix in TURNS {
            for (rows, timed) in [(prefix + CUE, true), (REPLY, false)] {
                let mut session =
                    DFlashCaptureSession::new(capture_layers.clone(), rows, hidden, false);
                let started = Instant::now();
                model
                    .forward_gpu_last_logits_with_hidden_dflash(
                        &chunk_tokens(start, rows),
                        &positions_for_range(start as i32, rows),
                        &mut cache,
                        Some(&mut session),
                        SlotId(0),
                    )
                    .expect("plain chunk");
                if timed {
                    plain.push(started.elapsed().as_secs_f64() * 1e3);
                }
                start += rows;
            }
        }
    }
    for (index, prefix) in TURNS.into_iter().enumerate() {
        let two: [f64; 3] = walls[0][index];
        let folded = walls[1][index][0];
        eprintln!(
            "[PREFILL_FOLD] rows={prefix}+{CUE} content_ms={:.1} snapshot_ms={:.1} cue_ms={:.1} \
             two_forwards_ms={:.1} folded_ms={folded:.1} saved_ms={:.1} one_plain_forward_ms={:.1}",
            two[0],
            two[1],
            two[2],
            two.iter().sum::<f64>(),
            two.iter().sum::<f64>() - folded,
            plain[index]
        );
    }
    let (two, folded) = (&outcomes[0], &outcomes[1]);
    for (index, prefix) in TURNS.into_iter().enumerate() {
        let (a, b) = (&two.0[index], &folded.0[index]);
        assert!(a.0 == b.0, "{prefix} rows: boundary logits differ");
        assert!(a.1 == b.1, "{prefix} rows: boundary checkpoint differs");
        assert!(a.2 == b.2, "{prefix} rows: last-row logits differ");
        assert!(a.3 == b.3, "{prefix} rows: captured target layers differ");
    }
    assert!(two.1 == folded.1, "final slot state differs");
}

#[test]
fn a_request_no_forward_took_is_an_error_and_does_not_linger() {
    use super::prefill_joint::{take_request, with_request, JointRequest, LaneBoundary, LaneRequest};
    with_synthetic_model(|model| {
        let image = cache(model)
            .boundary_anchor_image()
            .expect("checkpoint image");
        let outcome = with_request(
            JointRequest {
                shared_rows: 0,
                lanes: vec![LaneRequest {
                    slot: SlotId(0),
                    rows: 9,
                    shares_prefix: false,
                    boundary: Some(LaneBoundary {
                        prefix_rows: 4,
                        image,
                    }),
                }],
            },
            || Ok(()),
        );
        assert!(
            outcome.is_err(),
            "nothing took the fold or published logits"
        );
        assert!(take_request(9, SLOTS).expect("no request left").is_none());
    });
}

/// The fold is not exact, so the scheduler keeps the two forwards, for each
/// forward route that has no folded form.
fn no_fold_under(name: &str, routes: super::prefill_fold::UnfoldedRoutes) {
    with_synthetic_model(|model| {
        let _scope = PrefillScope::forced(true);
        assert!(
            model
                .prefill_fold_exact_under(super::prefill_fold::UnfoldedRoutes::default())
                .expect("exactness"),
            "the fixture folds exactly without {name}"
        );
        assert!(routes.any());
        assert!(
            !model.prefill_fold_exact_under(routes).expect("exactness"),
            "the fold stayed exact under {name}"
        );
    });
}

#[test]
fn the_fold_is_not_taken_with_a_glp_vector_bound() {
    use std::collections::BTreeMap;

    use crate::inference::glp::bind::BoundGlp;
    use crate::inference::glp::reader::{GlpHookPoint, GlpMode, GlpVector};

    no_fold_under(
        "a bound GLP vector",
        super::prefill_fold::UnfoldedRoutes {
            glp_bound: true,
            ..Default::default()
        },
    );
    // The model reports its own binding: what `hf2q serve --glp` leaves.
    super::prefill_prepared_tests::with_synthetic_model_mut(|model| {
        let _scope = PrefillScope::forced(true);
        assert!(model.prefill_fold_exact().expect("exactness"));
        let width = model.cfg.hidden_size as usize;
        model.glp = Some(BoundGlp {
            vector: GlpVector {
                mode: GlpMode::Add,
                hook_point: GlpHookPoint::ResidualStreamPostLayer,
                derived_at: None,
                alpha_default: 1.0,
                rank: 1,
                layers: BTreeMap::from([(3_u32, vec![0.25_f32; width])]),
                width,
                content_sha256: None,
                method: None,
                base_model_name: None,
            },
            alpha: 1.0,
            device_directions: BTreeMap::new(),
        });
        assert!(model.unfolded_routes().glp_bound);
        assert!(
            !model.prefill_fold_exact().expect("exactness"),
            "a model with a bound GLP vector still folds"
        );
    });
}

/// `HF2Q_MM_ID_ROUTING_THRESHOLD` moves the row count at which mlx-native
/// changes the MoE FFN's kernel family, and with it the bits of segments
/// between the two values: a MoE model then admits neither the fold nor a
/// joint forward (each request keeps its own forwards, whose bits are its
/// own by definition); a dense-FFN model has no such kernel and keeps both.
#[test]
fn a_moe_model_admits_no_fold_under_a_mm_id_threshold_override() {
    let overridden = super::prefill_fold::UnfoldedRoutes {
        moe_route_override: true,
        ..Default::default()
    };
    assert!(!overridden.any(), "the override concerns MoE layers only");
    with_synthetic_moe_model(|model| {
        let _scope = PrefillScope::forced(true);
        assert!(
            model
                .prefill_fold_exact_under(super::prefill_fold::UnfoldedRoutes::default())
                .expect("exactness"),
            "the synthetic MoE model does not fold at the stock threshold"
        );
        assert!(
            !model.prefill_fold_exact_under(overridden).expect("exactness"),
            "a MoE model still folds under a threshold override"
        );
    });
    with_synthetic_model(|model| {
        let _scope = PrefillScope::forced(true);
        assert!(
            model.prefill_fold_exact_under(overridden).expect("exactness"),
            "a dense-FFN model stopped folding under a MoE threshold override"
        );
    });
    // This process runs without the override: the production predicate
    // reads the environment, never a default.
    assert_eq!(
        super::prefill_fold::moe_route_override(),
        std::env::var_os("HF2Q_MM_ID_ROUTING_THRESHOLD").is_some()
    );
}

/// The same refusal through the process environment, which is what a
/// served worker reads: this test runs [`moe_fold_refusal_in_a_child`] in a
/// fresh process of this test binary with `HF2Q_MM_ID_ROUTING_THRESHOLD=64`
/// (the predicate and mlx-native both read the variable once per process).
#[test]
fn a_process_with_a_mm_id_threshold_override_admits_no_moe_fold() {
    let output = std::process::Command::new(std::env::current_exe().expect("test binary"))
        .args([
            "inference::models::qwen35::prefill_fold_tests::moe_fold_refusal_in_a_child",
            "--exact",
            "--nocapture",
            "--test-threads=1",
        ])
        .env("HF2Q_MM_ID_ROUTING_THRESHOLD", "64")
        .env("P834_OVERRIDE_CHILD", "1")
        .output()
        .expect("run the child test process");
    let text = format!(
        "{}{}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
    assert!(
        output.status.success() && text.contains("[OVERRIDE_CHILD] refused") && text.contains("1 passed"),
        "the child process under the override did not refuse the MoE fold:\n{text}"
    );
}

/// The child of [`a_process_with_a_mm_id_threshold_override_admits_no_moe_fold`];
/// a no-op in an ordinary test run.
#[test]
fn moe_fold_refusal_in_a_child() {
    if std::env::var_os("P834_OVERRIDE_CHILD").is_none() {
        return;
    }
    assert!(
        super::prefill_fold::moe_route_override(),
        "the override is not visible to the predicate"
    );
    with_synthetic_moe_model(|model| {
        let _scope = PrefillScope::forced(true);
        assert!(model.unfolded_routes().moe_route_override);
        assert!(
            !model.prefill_fold_exact().expect("exactness"),
            "a MoE model folds in a process with the threshold override"
        );
    });
    with_synthetic_model(|model| {
        let _scope = PrefillScope::forced(true);
        assert!(
            model.prefill_fold_exact().expect("exactness"),
            "a dense-FFN model stopped folding in a process with the MoE threshold override"
        );
    });
    eprintln!("[OVERRIDE_CHILD] refused");
}

/// `HF2Q_DENSE_Q_ARENA_RESET=0` keeps a forward's pooled scratch out for the
/// whole forward. The fold and the joint forward pool per-segment scratch
/// and run under pool caps that rely on the reset at a layer's host wait, so
/// with the override neither is admitted (one predicate serves both); the
/// model reports the override itself.
#[test]
fn the_fold_is_not_taken_without_the_layer_scratch_reset() {
    no_fold_under(
        "HF2Q_DENSE_Q_ARENA_RESET=0",
        super::prefill_fold::UnfoldedRoutes {
            scratch_reset_off: true,
            ..Default::default()
        },
    );
    with_synthetic_model(|model| {
        let _scope = PrefillScope::forced(true);
        assert!(model.prefill_fold_exact().expect("exactness"));
        let _override = super::forward_gpu::NoScratchResetForTest::set();
        assert!(model.unfolded_routes().scratch_reset_off);
        assert!(
            !model.prefill_fold_exact().expect("exactness"),
            "a model without the layer scratch reset still folds"
        );
    });
}

#[test]
fn the_fold_is_not_taken_with_dump_bisect_enabled() {
    no_fold_under(
        "dump-bisect",
        super::prefill_fold::UnfoldedRoutes {
            dump_bisect: true,
            ..Default::default()
        },
    );
}

#[test]
fn the_fold_is_not_taken_under_the_dense_q_split_profile() {
    no_fold_under(
        "the dense-Q split profile",
        super::prefill_fold::UnfoldedRoutes {
            dense_q_split_profile: true,
            ..Default::default()
        },
    );
}

/// After the forward the slot holds both segments. A checkpoint that cannot
/// be assembled then is reported beside the logits, not instead of them: the
/// scheduler skips it as it skips a failed snapshot between two forwards.
#[test]
fn a_checkpoint_that_cannot_be_assembled_leaves_the_forward_and_the_slot_intact() {
    with_synthetic_model(|model| {
        let reference = prefill(model, Path::Folded, 0, &[], 40, 5, false, 0);
        let _scope = PrefillScope::forced(true);
        let mut cache = cache(model);
        // A boundary cursor at the chunk's end: no row lies past it.
        let folded = folded_forward(
            model,
            &tokens(0, 0, 45),
            &positions_for_range(0, 45),
            &mut cache,
            None,
            SlotId(0),
            40,
            45,
        )
        .expect("the forward itself");
        assert!(folded.boundary.is_err(), "a checkpoint behind the last row");
        assert!(bits(&folded.logits) == reference.logits);
        assert!(bits(&folded.boundary_logits) == reference.boundary_logits);
        let after = cache
            .snapshot_slot_anchor(SlotId(0), 45)
            .expect("slot state after the cue")
            .test_words();
        assert!(after == reference.after, "the slot differs after the cue");
    });
}

/// Shapes beside [`SHAPES`]: a cue that crosses a 32-row tile behind a
/// content chunk of more than 32 rows, a resumed chunk whose cue starts a
/// new tile, a content chunk that ends on a tile boundary behind a history,
/// and the short chunks the scheduler excludes (still exact at the model
/// level), fresh and resumed.
const SLOT_SHAPES: [(&[usize], usize, usize); 10] = [
    (&[], 40, 5),
    (&[], 60, 5),
    (&[50], 157, 5),
    (&[10], 96, 5),
    (&[], 30, 3),
    (&[100], 28, 5),
    (&[33], 20, 5),
    (&[64, 20], 40, 5),
    (&[17], 97, 8),
    (&[40], 37, 32),
];

/// The fold against the two forwards in the last slot of a one- to
/// four-slot cache, and in slot 1 beside a busy slot 0.
fn fold_at_one_to_four_slots(tq: bool) {
    with_synthetic_model(|model| {
        let mut failures = Vec::new();
        for slots in 1..=4_u32 {
            let _cache = CacheShape::set(slots, tq);
            let slot = slots - 1;
            for (history, prefix, cue) in SLOT_SHAPES {
                for capture in [true, false] {
                    if !capture && slots != 2 {
                        continue;
                    }
                    let two = prefill(
                        model,
                        Path::TwoForwards,
                        slot,
                        history,
                        prefix,
                        cue,
                        capture,
                        0,
                    );
                    let folded =
                        prefill(model, Path::Folded, slot, history, prefix, cue, capture, 0);
                    let differences = two.differences(&folded);
                    if !differences.is_empty() {
                        failures.push(format!(
                            "tq={tq} slots={slots} history={history:?} rows={prefix}+{cue} \
                             capture={capture}: {differences:?}"
                        ));
                    }
                }
            }
            if slots >= 2 {
                let two = prefill(model, Path::TwoForwards, 1, &[33], 40, 5, true, 70);
                let folded = prefill(model, Path::Folded, 1, &[33], 40, 5, true, 70);
                let differences = two.differences(&folded);
                if !differences.is_empty() {
                    failures.push(format!("tq={tq} slots={slots} busy peer: {differences:?}"));
                }
            }
        }
        assert!(failures.is_empty(), "{failures:#?}");
    });
}

#[test]
fn a_folded_prefill_is_exact_at_one_to_four_slots_on_a_tq_cache() {
    fold_at_one_to_four_slots(true);
}

#[test]
fn a_folded_prefill_is_exact_at_one_to_four_slots_on_an_f16_cache() {
    fold_at_one_to_four_slots(false);
}

type AnchorWords = (usize, u64, u32, Vec<u32>, Option<u32>, Vec<u32>);

/// What a request that reuses the boundary checkpoint and diverges behind it
/// sees: every logits row of the diverging rows, the slot state after them,
/// and every logits row of a chunk behind them. `path = None` is the
/// control: nothing ever ran past the boundary on that slot.
fn restore_and_diverge(
    model: &Qwen35Model,
    path: Option<Path>,
    slot: u32,
    history: &[usize],
    prefix: usize,
    cue: usize,
) -> (Vec<u32>, AnchorWords, Vec<u32>) {
    const REPLY: usize = 6;
    const SUFFIX: usize = 11;
    let _scope = PrefillScope::forced(true);
    let mut cache = cache(model);
    let mut start = 0;
    for &rows in history {
        forward(model, &mut cache, slot, start, rows, false);
        start += rows;
    }
    let boundary_len = start + prefix;
    match path {
        None => {
            forward(model, &mut cache, slot, start, prefix, false);
        }
        Some(path) => {
            let anchor = match path {
                Path::TwoForwards => {
                    forward(model, &mut cache, slot, start, prefix, false);
                    let anchor = cache
                        .snapshot_slot_anchor(SlotId(slot), boundary_len)
                        .expect("checkpoint at the boundary");
                    forward(model, &mut cache, slot, boundary_len, cue, false);
                    anchor
                }
                Path::Folded => {
                    let len = prefix + cue;
                    folded_forward(
                        model,
                        &tokens(slot, start, len),
                        &positions_for_range(start as i32, len),
                        &mut cache,
                        None,
                        SlotId(slot),
                        prefix,
                        boundary_len,
                    )
                    .expect("folded chunk")
                    .boundary
                    .expect("checkpoint")
                }
            };
            // The rows a reply appends behind the cue.
            forward(model, &mut cache, slot, boundary_len + cue, REPLY, false);
            let plan = cache
                .plan_slot_anchor_restore(SlotId(slot), &anchor)
                .expect("restore plan");
            cache.apply_slot_anchor_restore(plan).expect("restore");
            assert_eq!(
                cache.sequence_len_for_slot(SlotId(slot)).expect("cursor") as usize,
                boundary_len,
                "the restore did not rewind to the boundary"
            );
        }
    }
    let diverged = model
        .forward_gpu(
            &tokens(slot + 7, boundary_len, SUFFIX),
            &positions_for_range(boundary_len as i32, SUFFIX),
            &mut cache,
            SlotId(slot),
        )
        .expect("diverging rows");
    let end = boundary_len + SUFFIX;
    let after = cache
        .snapshot_slot_anchor(SlotId(slot), end)
        .expect("slot state after the diverging rows")
        .test_words();
    let follow_up = model
        .forward_gpu(
            &tokens(slot, end, FOLLOW_UP_ROWS),
            &positions_for_range(end as i32, FOLLOW_UP_ROWS),
            &mut cache,
            SlotId(slot),
        )
        .expect("follow-up chunk");
    (bits(&diverged), after, bits(&follow_up))
}

/// A later request restores the checkpoint a folded forward took and
/// diverges behind it. It sees what it sees after the two-forward
/// checkpoint, and both see the logits, DeltaNet state and follow-up rows of
/// a slot that never ran past the boundary: no cue or reply row leaks into
/// the restored prefix.
#[test]
fn a_request_diverging_behind_a_folded_checkpoint_sees_the_two_forward_bytes() {
    const CASES: [(&[usize], usize, usize); 6] = [
        (&[], 40, 5),
        (&[33], 20, 5),
        (&[64, 20], 40, 5),
        (&[], 60, 5),
        (&[17], 97, 8),
        (&[50], 157, 5),
    ];
    with_synthetic_model(|model| {
        let mut failures = Vec::new();
        for (slots, tq) in [(1_u32, true), (3, true), (2, false)] {
            let _cache = CacheShape::set(slots, tq);
            let slot = slots - 1;
            for (history, prefix, cue) in CASES {
                let run = |path| restore_and_diverge(model, path, slot, history, prefix, cue);
                let two = run(Some(Path::TwoForwards));
                let folded = run(Some(Path::Folded));
                let control = run(None);
                // The state generation and cursors (`.0`-`.2`, `.4`) follow
                // the slot's own history; the rows and the DeltaNet words
                // are what a request reads.
                let reads_the_control = |run: &(Vec<u32>, AnchorWords, Vec<u32>)| {
                    run.0 == control.0
                        && run.1 .3 == control.1 .3
                        && run.1 .5 == control.1 .5
                        && run.2 == control.2
                };
                if two != folded || !reads_the_control(&two) || !reads_the_control(&folded) {
                    failures.push(format!(
                        "tq={tq} slots={slots} history={history:?} {prefix}+{cue}: \
                         folded equals two forwards: {}, two forwards read the control: {}, \
                         folded reads the control: {}",
                        two == folded,
                        reads_the_control(&two),
                        reads_the_control(&folded)
                    ));
                }
            }
        }
        assert!(failures.is_empty(), "{failures:#?}");
    });
}

/// The synthetic model with every dense FFN replaced by a quantized MoE FFN
/// (four experts, two per row, a shared expert), so a prefill forward takes
/// the MoE arm the fold builds once per segment.
pub(super) fn with_synthetic_moe_model(run: impl FnOnce(&Qwen35Model)) {
    use mlx_native::ops::quantized_matmul_ggml::GgmlType;
    use mlx_native::MlxDevice;

    use super::joint_decode_test_support::{random_f32, synthetic_blocks};
    use super::joint_verify_tests::{padded_policy, synthetic_model};
    use super::model::Qwen35FfnWeights;
    use super::prefill_prepared_tests::{rescale_routed_weights, set_scale, OBSERVABLE_SCALE};
    use super::weight_loader::MoeFfnWeightsQ;
    use super::{Qwen35MoeConfig, Qwen35Variant};

    const EXPERTS: usize = 4;
    const EXPERT_ROWS: usize = 256;
    const SHARED_ROWS: usize = 256;

    let _gpu = crate::inference::hf2q_gpu_test_lock();
    let Ok(device) = MlxDevice::new() else {
        return;
    };
    let mut model = synthetic_model(&device, padded_policy(), false);
    let hidden = model.cfg.hidden_size as usize;
    let mut seed = 0x51CE_0E57_u32;
    let stack = |ty: GgmlType, rows: usize, cols: usize, seed: &mut u32| {
        let blocks = synthetic_blocks(&device, ty, (EXPERTS * rows) as u32, cols as u32, seed);
        set_scale(&blocks, ty, OBSERVABLE_SCALE);
        blocks
    };
    let small = |seed: &mut u32, n: usize| -> Vec<f32> {
        random_f32(seed, n)
            .into_iter()
            .map(|value| value * 0.05)
            .collect()
    };
    for layer in &mut model.layers {
        *layer.ffn_mut() = Qwen35FfnWeights::MoeQ(MoeFfnWeightsQ {
            router: random_f32(&mut seed, EXPERTS * hidden),
            experts: std::sync::Arc::new(super::expert_storage::MoeExpertStorage::from_ggml(
                stack(GgmlType::Q4_K, EXPERT_ROWS, hidden, &mut seed),
                stack(GgmlType::Q4_K, EXPERT_ROWS, hidden, &mut seed),
                stack(GgmlType::Q6_K, hidden, EXPERT_ROWS, &mut seed),
            )),
            ggml_type_gate_up: GgmlType::Q4_K,
            ggml_type_down: GgmlType::Q6_K,
            shared_gate_logit: random_f32(&mut seed, hidden),
            shared_gate: small(&mut seed, SHARED_ROWS * hidden),
            shared_up: small(&mut seed, SHARED_ROWS * hidden),
            shared_down: small(&mut seed, hidden * SHARED_ROWS),
            native_dense: None,
        });
    }
    model.cfg.variant = Qwen35Variant::Moe;
    model.cfg.moe = Some(Qwen35MoeConfig {
        moe_intermediate_size: EXPERT_ROWS as u32,
        num_experts: EXPERTS as u32,
        num_experts_per_tok: 2,
        shared_expert_intermediate_size: SHARED_ROWS as u32,
    });
    rescale_routed_weights(&model);
    model
        .ensure_gpu_cache_primed()
        .expect("prime the GPU cache for the MoE layers");
    run(&model);
}

/// The MoE FFN picks its router and shared-expert kernels by row count, so a
/// folded forward builds it once per segment. On a model whose FFN layers
/// are quantized MoE layers the fold leaves what the two forwards leave.
#[test]
fn a_folded_prefill_through_moe_layers_leaves_what_the_two_forwards_leave() {
    use mlx_native::ops::quantized_matmul_ggml::GgmlType;

    use super::model::Qwen35FfnWeights;
    use super::prefill_prepared_tests::xor_quants;

    with_synthetic_moe_model(|model| {
        {
            let _scope = PrefillScope::forced(true);
            assert!(
                model.prefill_fold_exact().expect("exactness"),
                "the MoE fixture does not fold exactly on the prepared route"
            );
        }
        // The expert stacks are live: the last layer's reaches the logits.
        let reference = prefill(model, Path::TwoForwards, 0, &[], 40, 5, false, 0);
        let Qwen35FfnWeights::MoeQ(last) = model.layers.last().expect("layers").ffn() else {
            panic!("the fixture's last layer has no MoE FFN");
        };
        let super::expert_storage::ExpertRoleRef::Ggml(down) =
            last.experts.role(super::expert_storage::ExpertRole::Down) else {
                panic!("the mutable synthetic fixture requires raw expert weights");
            };
        xor_quants(down, GgmlType::Q6_K);
        let flipped = prefill(model, Path::TwoForwards, 0, &[], 40, 5, false, 0);
        xor_quants(down, GgmlType::Q6_K);
        assert!(
            flipped.logits != reference.logits,
            "the MoE expert weights do not reach the logits"
        );
        let restored = prefill(model, Path::TwoForwards, 0, &[], 40, 5, false, 0);
        assert!(restored.differences(&reference).is_empty());
        const MOE_SHAPES: [(&[usize], usize, usize); 5] = [
            (&[], 40, 5),
            (&[], 64, 5),
            (&[33], 20, 5),
            (&[17], 97, 8),
            (&[50], 157, 5),
        ];
        for (history, prefix, cue) in MOE_SHAPES {
            for capture in [true, false] {
                let two = prefill(
                    model,
                    Path::TwoForwards,
                    0,
                    history,
                    prefix,
                    cue,
                    capture,
                    0,
                );
                let folded = prefill(model, Path::Folded, 0, history, prefix, cue, capture, 0);
                assert!(
                    two.differences(&folded).is_empty(),
                    "MoE layers, history {history:?}, {prefix} + {cue} rows, capture {capture}: \
                     {:?} differ",
                    two.differences(&folded)
                );
            }
        }
    });
}