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
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
//! Metal gates for the prepared prompt-prefill route
//! (`HF2Q_QWEN_PREFILL_PREPARED`, [`super::prefill_prepared`]).
//!
//! The route is not bitwise equal to the stock GGML matmul (Q1 Class B), so
//! these gates check what it must guarantee instead:
//! - every product stays within a K-scaled bound of an F64 reference;
//! - a run is repeatable bit for bit, also on recycled pool storage;
//! - a prefilled row does not depend on the rows after it, on the chunk's
//!   row count, or on what another slot holds.
//!
//! The model-level gates run the synthetic native K-quant model of the joint
//! verify tests (no artifact). `HF2Q_QWEN38_GGUF` runs the ignored
//! real-artifact gate at the end of this file.

use mlx_native::gguf::test_only_dequantize;
use mlx_native::ops::quantized_matmul_ggml::GgmlType;
use mlx_native::{KernelRegistry, MlxBuffer, MlxDevice};

use super::gpu_full_attn::{download_f32, upload_f32};
use super::joint_decode_test_support::{random_f32, synthetic_blocks};
use super::joint_verify_tests::{padded_policy, synthetic_model};
use super::kv_cache::HybridKvCache;
use super::model::Qwen35Model;
use super::prefill_prepared::PrefillScope;
use super::prepared_projection::{self, Planes};
use super::spec_decode::positions_for_range;
use crate::serve::multi_seq_kv::SlotId;

/// Relative rounding of a BF16 activation (8 significant bits, to nearest).
const BF16_UNIT: f64 = 1.0 / 256.0;
/// Relative rounding of an F16 weight or product (11 significant bits).
const F16_UNIT: f64 = 1.0 / 2048.0;
/// Relative rounding of one F32 accumulation step.
const F32_UNIT: f64 = 1.0 / 16_777_216.0;

/// `|y - Σ w·x| <= gamma(K) · Σ |w·x|`: one BF16 rounding of the activation,
/// F16 roundings of the weight and of the product, and K F32 accumulation
/// steps.
fn gamma(k: usize) -> f64 {
    BF16_UNIT + 2.0 * F16_UNIT + k as f64 * F32_UNIT
}

fn dequantize(weight: &MlxBuffer, ggml_type: GgmlType, n: usize, k: usize) -> Vec<f32> {
    let mut out = vec![0.0f32; n * k];
    test_only_dequantize(
        weight.as_slice::<u8>().expect("weight bytes"),
        ggml_type,
        &mut out,
    )
    .expect("dequantize reference weight");
    out
}

/// One product on the prefill route's exact helper sequence: BF16 copy of
/// the activation, barrier, prepared row-tile MM.
#[allow(clippy::too_many_arguments)]
fn prepared_product(
    device: &MlxDevice,
    registry: &mut KernelRegistry,
    weight: &MlxBuffer,
    ggml_type: GgmlType,
    planes: &Planes,
    x: &[f32],
    rows: usize,
    k: usize,
    n: usize,
) -> Vec<f32> {
    let input = upload_f32(x, device)
        .expect("upload activation")
        .with_shape(vec![rows, k])
        .expect("shape activation");
    let mut enc = device.command_encoder().expect("encoder");
    let x_bf16 =
        prepared_projection::bf16_input(&mut enc, registry, device, &input, rows as u32, k as u32)
            .expect("BF16 activation");
    enc.memory_barrier();
    let output = prepared_projection::project(
        &mut enc,
        registry,
        device,
        &x_bf16,
        weight,
        ggml_type,
        planes,
        rows as u32,
        k as u32,
        n as u32,
    )
    .expect("prepared product");
    enc.commit_and_wait_labeled("test.qwen.prefill_prepared.product")
        .expect("complete prepared product");
    download_f32(&output).expect("download product")[..rows * n].to_vec()
}

/// Statistical companion of [`gamma`]. The activation's BF16 rounding and the
/// weight's and product's F16 roundings are independent per term, so over
/// many outputs the squared error is at most a third of the squared unit
/// roundoff times `Σ (w·x)²` (a relative error bounded by `u` has variance at
/// most `u²/3`), plus `K` F32 accumulation steps on the running sum. Where
/// [`gamma`] grows with `Σ |w·x|` (about `K` terms), this grows with their
/// L2 norm (about `√K`), so it sits within a small factor of the error the
/// kernel really makes: a tenfold larger error fails it.
struct RmsCheck {
    squared_error: f64,
    squared_bound: f64,
}

impl RmsCheck {
    fn new() -> Self {
        Self {
            squared_error: 0.0,
            squared_bound: 0.0,
        }
    }

    fn add(&mut self, error: f64, squared_terms: f64, magnitude: f64, k: usize) {
        self.squared_error += error * error;
        self.squared_bound += (BF16_UNIT * BF16_UNIT + 2.0 * F16_UNIT * F16_UNIT) / 3.0
            * squared_terms
            + k as f64 * F32_UNIT * F32_UNIT / 3.0 * magnitude * magnitude;
    }

    /// RMS error over RMS bound.
    fn ratio(&self) -> f64 {
        (self.squared_error / self.squared_bound).sqrt()
    }

    /// The RMS error must stay below the modelled bound, and the kernel must
    /// round at all like the model says (otherwise the check has no power).
    fn assert_within(&self, what: &str) {
        let ratio = self.ratio();
        assert!(
            ratio <= RMS_SLACK,
            "{what}: RMS error is {ratio:.3} of the modelled RMS bound (limit {RMS_SLACK})"
        );
        assert!(
            ratio >= RMS_SLACK / 10.0,
            "{what}: RMS error is only {ratio:.4} of the modelled RMS bound; a tenfold \
             regression would pass this gate"
        );
    }
}

/// Headroom of the RMS check over the modelled bound: the uniform-rounding
/// model overstates a BF16 rounding's variance by about two, and a case
/// averages a few hundred outputs.
const RMS_SLACK: f64 = 1.5;

/// `Σ w·x`, `Σ |w·x|` and `Σ (w·x)²` in F64.
fn exact_product(w: &[f32], x: &[f32]) -> (f64, f64, f64) {
    w.iter().zip(x).fold(
        (0.0f64, 0.0f64, 0.0f64),
        |(exact, magnitude, squares), (&w, &x)| {
            let product = f64::from(w) * f64::from(x);
            (
                exact + product,
                magnitude + product.abs(),
                squares + product * product,
            )
        },
    )
}

#[test]
fn prepared_products_stay_within_the_k_scaled_bound_of_an_f64_reference() {
    let _gpu = crate::inference::hf2q_gpu_test_lock();
    let Ok(device) = MlxDevice::new() else {
        return;
    };
    let _scratch = super::prepared_scratch::ExecutionScratch::test_scope(&device);
    let mut registry = KernelRegistry::new();
    let mut seed = 0x9E37_79B9_u32;
    let mut worst = 0.0f64;
    // Small shapes at every tiling, then the served inner dimensions: the
    // Qwen3.8 FFN gate (K = 5120) and FFN down (K = 17408) at their real
    // output widths, whose columns are sampled for the reference.
    for (ggml_type, n, k, row_counts) in [
        (
            GgmlType::Q4_K,
            128usize,
            512usize,
            &[2usize, 5, 31, 32, 33, 157][..],
        ),
        (GgmlType::Q6_K, 128, 512, &[2, 5, 31, 32, 33, 157]),
        (GgmlType::Q4_K, 64, 1024, &[2, 5, 31, 32, 33, 157]),
        (GgmlType::Q6_K, 192, 768, &[2, 5, 31, 32, 33, 157]),
        (GgmlType::Q4_K, 17408, 5120, &[5, 33, 157]),
        (GgmlType::Q6_K, 5120, 17408, &[5, 33, 157]),
    ] {
        let weight = synthetic_blocks(&device, ggml_type, n as u32, k as u32, &mut seed);
        let w = dequantize(&weight, ggml_type, n, k);
        let planes = Planes::new();
        let columns: Vec<usize> = (0..n).step_by((n / 128).max(1)).collect();
        for &rows in row_counts {
            let x = random_f32(&mut seed, rows * k);
            let y = prepared_product(
                &device,
                &mut registry,
                &weight,
                ggml_type,
                &planes,
                &x,
                rows,
                k,
                n,
            );
            let mut rms = RmsCheck::new();
            for row in 0..rows {
                let xr = &x[row * k..(row + 1) * k];
                for &col in &columns {
                    let (exact, magnitude, squares) = exact_product(&w[col * k..(col + 1) * k], xr);
                    let got = f64::from(y[row * n + col]);
                    let error = (got - exact).abs();
                    let bound = gamma(k) * magnitude + 1e-30;
                    assert!(
                        error <= bound,
                        "{ggml_type:?} N={n} K={k} rows={rows}: row {row} col {col}: \
                         got {got}, F64 {exact}, |error| {error} > bound {bound}"
                    );
                    worst = worst.max(error / bound);
                    rms.add(error, squares, magnitude, k);
                }
            }
            eprintln!(
                "prepared prefill product {ggml_type:?} N={n} K={k} rows={rows}: RMS error / RMS bound = {:.3}",
                rms.ratio()
            );
            rms.assert_within(&format!("{ggml_type:?} N={n} K={k} rows={rows}"));
        }
    }
    eprintln!("prepared prefill products: worst |error| / bound = {worst:.4}");
    assert!(
        worst > 0.0,
        "the reference comparison saw no rounding at all"
    );
}

/// The fused alpha/beta product and its split against the F64 reference of
/// the two separate projections, at synthetic and at the served head counts
/// and inner dimensions (Qwen3.8: 48 heads, K = 5120; Qwen3.6: 32, 2048).
#[test]
fn fused_alpha_beta_stays_within_the_bounds_of_an_f64_reference() {
    use super::prepared_projection::DeltaNetPlanes;
    let _gpu = crate::inference::hf2q_gpu_test_lock();
    let Ok(device) = MlxDevice::new() else {
        return;
    };
    let _scratch = super::prepared_scratch::ExecutionScratch::test_scope(&device);
    let mut registry = KernelRegistry::new();
    let mut seed = 0x8230_0004_u32;
    for (ggml_type, heads, k) in [
        (GgmlType::Q4_K, 4usize, 256usize),
        (GgmlType::Q4_K, 48, 5120),
        (GgmlType::Q4_K, 32, 2048),
        (GgmlType::Q6_K, 16, 2048),
    ] {
        let alpha = synthetic_blocks(&device, ggml_type, heads as u32, k as u32, &mut seed);
        let beta = synthetic_blocks(&device, ggml_type, heads as u32, k as u32, &mut seed);
        let wa = dequantize(&alpha, ggml_type, heads, k);
        let wb = dequantize(&beta, ggml_type, heads, k);
        let planes = DeltaNetPlanes::default();
        let mut rms = RmsCheck::new();
        for rows in [2usize, 5, 33, 157] {
            let x = random_f32(&mut seed, rows * k);
            let input = upload_f32(&x, &device)
                .expect("upload activation")
                .with_shape(vec![rows, k])
                .expect("shape activation");
            let mut enc = device.command_encoder().expect("encoder");
            let x_bf16 = prepared_projection::bf16_input(
                &mut enc,
                &mut registry,
                &device,
                &input,
                rows as u32,
                k as u32,
            )
            .expect("BF16 activation");
            enc.memory_barrier();
            let fused = prepared_projection::project_alpha_beta(
                &mut enc,
                &mut registry,
                &device,
                &x_bf16,
                &alpha,
                &beta,
                ggml_type,
                &planes,
                rows as u32,
                k as u32,
                heads as u32,
            )
            .expect("fused alpha/beta");
            enc.memory_barrier();
            let (a, b) = prepared_projection::split_alpha_beta(
                &mut enc,
                &mut registry,
                &device,
                &fused,
                rows as u32,
                heads as u32,
            )
            .expect("split alpha/beta");
            enc.commit_and_wait_labeled("test.qwen.prefill_prepared.alpha_beta")
                .expect("complete fused alpha/beta");
            let got_a = download_f32(&a).expect("alpha logits");
            let got_b = download_f32(&b).expect("beta logits");
            for (got, w, name) in [(&got_a, &wa, "alpha"), (&got_b, &wb, "beta")] {
                for row in 0..rows {
                    let xr = &x[row * k..(row + 1) * k];
                    for head in 0..heads {
                        let (exact, magnitude, squares) =
                            exact_product(&w[head * k..(head + 1) * k], xr);
                        let error = (f64::from(got[row * heads + head]) - exact).abs();
                        assert!(
                            error <= gamma(k) * magnitude + 1e-30,
                            "{ggml_type:?} heads={heads} K={k} rows={rows}: {name} row {row} \
                             head {head}: got {}, F64 {exact}",
                            got[row * heads + head]
                        );
                        rms.add(error, squares, magnitude, k);
                    }
                }
            }
        }
        eprintln!(
            "prepared fused alpha/beta {ggml_type:?} heads={heads} K={k}: RMS error / RMS bound = {:.3}",
            rms.ratio()
        );
        rms.assert_within(&format!(
            "fused alpha/beta {ggml_type:?} heads={heads} K={k}"
        ));
    }
}

const SLOTS: u32 = 2;
const CAPACITY: u32 = 1024;
/// Chunk sizes of a fresh slot: the served cue and content sizes (5, 31,
/// 157, 720), one row either side of a 32-row tile boundary, whole tiles,
/// several tiles with a partial last one.
const FRESH_ROWS: [usize; 8] = [5, 17, 31, 33, 64, 97, 157, 720];

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 cache(model: &Qwen35Model) -> HybridKvCache {
    model
        .with_gpu_cache_mut(|device, _registry| {
            HybridKvCache::new_with_options(&model.cfg, device, CAPACITY, SLOTS, true)
        })
        .expect("allocate TQ cache")
}

/// Every row's logits of one chunk on `slot`, as bits.
fn chunk_rows(
    model: &Qwen35Model,
    cache: &mut HybridKvCache,
    slot: u32,
    start: usize,
    len: usize,
    prepared: bool,
) -> Vec<u32> {
    let _scope = PrefillScope::forced(prepared);
    let logits = model
        .forward_gpu(
            &tokens(slot, start, len),
            &positions_for_range(start as i32, len),
            cache,
            SlotId(slot),
        )
        .unwrap_or_else(|error| panic!("slot {slot} chunk start={start} n={len}: {error:#}"));
    assert_eq!(logits.len(), len * model.cfg.vocab_size as usize);
    assert!(
        logits.iter().all(|value| value.is_finite()),
        "slot {slot} chunk start={start} n={len}: non-finite logits"
    );
    logits.iter().map(|value| value.to_bits()).collect()
}

/// Scale fields of the fixture's K-quant blocks. The joint-verify fixture's
/// own scale (0x211F) drives the residual stream to about 6e6 after the
/// first layer, where no attention product and no later layer can move a
/// logit; at 1/64 of it every routed product of every layer does
/// (`the_fixture_observes_every_routed_product_in_every_layer`).
pub(super) const OBSERVABLE_SCALE: u16 = 0x091F;

/// `(block bytes, quant bytes, scale field offsets)` of a K-quant block.
fn block_layout(ty: GgmlType) -> (usize, std::ops::Range<usize>, &'static [usize]) {
    match ty {
        GgmlType::Q4_K => (144, 16..144, &[0, 2]),
        GgmlType::Q6_K => (210, 0..128, &[208]),
        other => panic!("no synthetic layout for {other:?}"),
    }
}

/// Flip quant bits of every block in place; twice restores the weight.
pub(super) fn xor_quants(buffer: &MlxBuffer, ty: GgmlType) {
    let (block, quants, _) = block_layout(ty);
    let mut handle = buffer.clone();
    for chunk in handle
        .as_mut_slice::<u8>()
        .expect("map weight")
        .chunks_exact_mut(block)
    {
        for byte in &mut chunk[quants.clone()] {
            *byte ^= 0x5A;
        }
    }
}

pub(super) fn set_scale(buffer: &MlxBuffer, ty: GgmlType, scale: u16) {
    let (block, _, offsets) = block_layout(ty);
    let mut handle = buffer.clone();
    for chunk in handle
        .as_mut_slice::<u8>()
        .expect("map weight")
        .chunks_exact_mut(block)
    {
        for &offset in offsets {
            chunk[offset..offset + 2].copy_from_slice(&scale.to_le_bytes());
        }
    }
}

/// Every weight the prepared prefill route serves: `(layer, product, handle,
/// type)`.
fn routed_weights(model: &Qwen35Model) -> Vec<(usize, &'static str, MlxBuffer, GgmlType)> {
    use super::gpu_full_attn::FullAttnQGateWeightsGpu;
    use super::model::{Qwen35FfnWeights, Qwen35LayerWeights};
    let mut out = Vec::new();
    for (index, layer) in model.layers.iter().enumerate() {
        let ffn = match layer {
            Qwen35LayerWeights::NativeLinearAttn { attn, ffn } => {
                out.push((
                    index,
                    "gdn.qkv",
                    attn.attn_qkv.clone(),
                    attn.attn_qkv_ggml_type,
                ));
                out.push((
                    index,
                    "gdn.gate",
                    attn.attn_gate.clone(),
                    attn.attn_gate_ggml_type,
                ));
                out.push((
                    index,
                    "gdn.alpha",
                    attn.ssm_alpha.clone(),
                    attn.ssm_alpha_ggml_type,
                ));
                out.push((
                    index,
                    "gdn.beta",
                    attn.ssm_beta.clone(),
                    attn.ssm_beta_ggml_type,
                ));
                out.push((
                    index,
                    "gdn.out",
                    attn.ssm_out.clone(),
                    attn.ssm_out_ggml_type,
                ));
                ffn
            }
            Qwen35LayerWeights::NativeFullAttn { attn, ffn } => {
                if let FullAttnQGateWeightsGpu::Fused { weight, ggml_type } = &attn.q_gate {
                    out.push((index, "attn.q_gate", weight.clone(), *ggml_type));
                }
                out.push((index, "attn.k", attn.wk.clone(), attn.wk_ggml_type));
                out.push((index, "attn.v", attn.wv.clone(), attn.wv_ggml_type));
                out.push((index, "attn.out", attn.wo.clone(), attn.wo_ggml_type));
                ffn
            }
            _ => panic!("the synthetic model holds a float layer"),
        };
        if let Qwen35FfnWeights::DenseQ(weights) = ffn {
            out.push((
                index,
                "ffn.gate",
                weights.gate_q.clone(),
                weights.ggml_type_gate_up,
            ));
            out.push((
                index,
                "ffn.up",
                weights.up_q.clone(),
                weights.ggml_type_gate_up,
            ));
            out.push((
                index,
                "ffn.down",
                weights.down_q.clone(),
                weights.ggml_type_down,
            ));
        }
    }
    out
}

/// Rescale every weight the prepared prefill route serves to
/// [`OBSERVABLE_SCALE`].
pub(super) fn rescale_routed_weights(model: &Qwen35Model) {
    for (_, _, buffer, ty) in &routed_weights(model) {
        set_scale(buffer, *ty, OBSERVABLE_SCALE);
    }
}

/// The synthetic model at [`OBSERVABLE_SCALE`]. `synthetic_model` primed
/// the thread's GPU cache for an address the model no longer has: the
/// caller primes it again where the model finally lives.
pub(crate) fn observable_synthetic_model(device: &MlxDevice) -> Qwen35Model {
    let model = synthetic_model(device, padded_policy(), false);
    rescale_routed_weights(&model);
    model
}

/// The same observable weights with the context declared before cache construction.
pub(crate) fn observable_synthetic_model_with_context_length(
    device: &MlxDevice,
    context_length: u32,
) -> Qwen35Model {
    let mut cfg = super::joint_verify_tests::synthetic_cfg();
    cfg.max_position_embeddings = context_length;
    let model =
        super::joint_verify_tests::synthetic_model_with_cfg(device, padded_policy(), false, cfg);
    rescale_routed_weights(&model);
    model
}

/// [`observable_synthetic_model`] with its GPU cache primed, mutable.
pub(super) fn with_synthetic_model_mut(run: impl FnOnce(&mut Qwen35Model)) {
    let _gpu = crate::inference::hf2q_gpu_test_lock();
    let Ok(device) = MlxDevice::new() else {
        return;
    };
    let mut model = observable_synthetic_model(&device);
    model
        .ensure_gpu_cache_primed()
        .expect("prime the GPU cache at the model's final address");
    run(&mut model);
}

/// [`observable_synthetic_model`] with its GPU cache primed.
pub(super) fn with_synthetic_model(run: impl FnOnce(&Qwen35Model)) {
    with_synthetic_model_mut(|model| run(model));
}

/// A schedule that reaches every attention route: a fresh chunk, a short
/// resumed chunk (direct TQ attention), a long resumed chunk (dequantized
/// history) and a one-row forward. Every row's logits per chunk, as bits.
const OBSERVED_CHUNKS: [usize; 4] = [33, 5, 40, 1];

fn observed_schedule(model: &Qwen35Model, prepared: bool) -> Vec<Vec<u32>> {
    let mut cache = cache(model);
    let mut start = 0usize;
    OBSERVED_CHUNKS
        .iter()
        .map(|&rows| {
            let logits = chunk_rows(model, &mut cache, 0, start, rows, prepared);
            start += rows;
            logits
        })
        .collect()
}

/// The gates below are only as strong as the fixture: a product whose bits
/// cannot move a logit is not tested by them. Flipping the quant bits of any
/// one routed weight, in any layer, must change the schedule's logits. (The
/// stock route is mutated: the prepared planes are prepared once per model.)
#[test]
fn the_fixture_observes_every_routed_product_in_every_layer() {
    with_synthetic_model(|model| {
        let baseline = observed_schedule(model, false);
        let weights = routed_weights(model);
        let mut products = std::collections::BTreeSet::new();
        for (layer, product, buffer, ty) in &weights {
            xor_quants(buffer, *ty);
            let mutated = observed_schedule(model, false);
            xor_quants(buffer, *ty);
            assert!(
                mutated != baseline,
                "layer {layer} {product}: no logit of the schedule depends on this weight"
            );
            products.insert(*product);
        }
        assert!(
            observed_schedule(model, false) == baseline,
            "the weights were not restored"
        );
        let expected: std::collections::BTreeSet<&str> = [
            "attn.k",
            "attn.out",
            "attn.q_gate",
            "attn.v",
            "ffn.down",
            "ffn.gate",
            "ffn.up",
            "gdn.alpha",
            "gdn.beta",
            "gdn.gate",
            "gdn.out",
            "gdn.qkv",
        ]
        .into_iter()
        .collect();
        assert_eq!(products, expected, "routed products of the fixture");
        // The prepared route computes the same function to within rounding
        // and differs in bits in every chunk of the schedule.
        let prepared = observed_schedule(model, true);
        for (chunk, (stock, prepared)) in baseline.iter().zip(&prepared).enumerate() {
            assert!(
                stock != prepared,
                "chunk {chunk} ({} rows): the prepared route left the stock bits",
                OBSERVED_CHUNKS[chunk]
            );
        }
    });
}

/// Every layer's output rows of a fresh prepared chunk, as bits
/// (`[layer][row][hidden]`).
fn fresh_layer_rows(model: &Qwen35Model, rows: usize) -> Vec<u32> {
    use crate::inference::spec_decode::dflash::hidden_capture::DFlashCaptureSession;
    let _scope = PrefillScope::forced(true);
    let hidden = model.cfg.hidden_size as usize;
    let layers = model.layers.len();
    let mut session = DFlashCaptureSession::new((0..layers).collect(), rows, hidden, false);
    model
        .forward_gpu_last_logits_with_hidden_dflash(
            &tokens(0, 0, rows),
            &positions_for_range(0, rows),
            &mut cache(model),
            Some(&mut session),
            SlotId(0),
        )
        .unwrap_or_else(|error| panic!("fresh {rows}-row chunk: {error:#}"));
    session.hidden_output.iter().map(|v| v.to_bits()).collect()
}

/// A prefilled row's residual stream after every layer does not depend on
/// the rows after it or on the chunk's size. The logits are compared too
/// where the chunks share the output head's kernel (the head is not a
/// routed product: the stock matmul serves it and picks its kernel by row
/// count, at most eight rows on the mat-vec family).
#[test]
fn prepared_prefill_rows_do_not_depend_on_later_rows_or_the_chunk_size() {
    with_synthetic_model(|model| {
        let vocab = model.cfg.vocab_size as usize;
        let hidden = model.cfg.hidden_size as usize;
        let layers = model.layers.len();
        let longest = *FRESH_ROWS.iter().max().expect("sizes");
        let full_layers = fresh_layer_rows(model, longest);
        let full = chunk_rows(model, &mut cache(model), 0, 0, longest, true);
        for rows in FRESH_ROWS {
            let part = fresh_layer_rows(model, rows);
            for layer in 0..layers {
                for row in 0..rows {
                    let at = |total: usize| {
                        let start = (layer * total + row) * hidden;
                        start..start + hidden
                    };
                    assert!(
                        part[at(rows)] == full_layers[at(longest)],
                        "layer {layer} row {row} of a {rows}-row chunk differs from the same \
                         row of a {longest}-row chunk"
                    );
                }
            }
            if rows <= 8 {
                continue;
            }
            let prefix = chunk_rows(model, &mut cache(model), 0, 0, rows, true);
            for row in 0..rows {
                assert!(
                    prefix[row * vocab..(row + 1) * vocab] == full[row * vocab..(row + 1) * vocab],
                    "logits row {row} of a {rows}-row chunk differ from the same row of a \
                     {longest}-row chunk"
                );
            }
        }
    });
}

#[test]
fn prepared_prefill_is_repeatable_on_recycled_storage_and_across_resumed_chunks() {
    with_synthetic_model(|model| {
        let run = |model: &Qwen35Model| {
            let mut cache = cache(model);
            let mut rows = chunk_rows(model, &mut cache, 0, 0, 97, true);
            // Resumed chunks: the direct TQ route (at most 32 queries) and
            // the dequantized-history route.
            rows.extend(chunk_rows(model, &mut cache, 0, 97, 5, true));
            rows.extend(chunk_rows(model, &mut cache, 0, 102, 40, true));
            rows
        };
        let first = run(model);
        // Different rows through the same pooled scratch and arenas.
        chunk_rows(model, &mut cache(model), 1, 0, 64, true);
        chunk_rows(model, &mut cache(model), 1, 0, 33, false);
        let second = run(model);
        assert!(first == second, "a repeated prepared prefill changed bits");
    });
}

#[test]
fn prepared_prefill_of_a_slot_does_not_depend_on_another_slot() {
    with_synthetic_model(|model| {
        let alone = chunk_rows(model, &mut cache(model), 1, 0, 33, true);
        let mut shared = cache(model);
        chunk_rows(model, &mut shared, 0, 0, 64, true);
        chunk_rows(model, &mut shared, 0, 64, 20, true);
        let beside = chunk_rows(model, &mut shared, 1, 0, 33, true);
        assert!(
            alone == beside,
            "slot 1's prefill depends on what slot 0 holds"
        );
    });
}

#[test]
fn the_scope_selects_the_route_and_a_closed_scope_keeps_the_stock_kernels() {
    with_synthetic_model(|model| {
        let stock = chunk_rows(model, &mut cache(model), 0, 0, 33, false);
        let unscoped = {
            let logits = model
                .forward_gpu(
                    &tokens(0, 0, 33),
                    &positions_for_range(0, 33),
                    &mut cache(model),
                    SlotId(0),
                )
                .expect("unscoped forward");
            logits
                .iter()
                .map(|value| value.to_bits())
                .collect::<Vec<_>>()
        };
        assert!(
            stock == unscoped,
            "a scope with the route off changed the stock forward"
        );
        let prepared = chunk_rows(model, &mut cache(model), 0, 0, 33, true);
        assert!(
            prepared != stock,
            "the routed scope did not change any product"
        );
    });
}

/// What decode reads after a prepared prefill (full-attention K/V, DeltaNet
/// state) is deterministic, and the decoded logits stay close to those after
/// a stock prefill of the same rows: three serial steps and one four-row
/// step. The greedy token must agree wherever the stock margin exceeds twice
/// the difference.
#[test]
fn decode_after_a_prepared_prefill_is_repeatable_and_close_to_the_stock_prefill() {
    /// Largest |prepared - stock| decode logit over the largest |logit|.
    /// The fixture measures about 5e-4; ten times that fails.
    const LIMIT: f32 = 2.5e-3;
    const HISTORY: usize = 40;
    with_synthetic_model(|model| {
        let vocab = model.cfg.vocab_size as usize;
        let decode = |prepared: bool| -> Vec<f32> {
            let mut cache = cache(model);
            {
                let _scope = PrefillScope::forced(prepared);
                model
                    .forward_gpu_last_logits(
                        &tokens(0, 0, HISTORY),
                        &positions_for_range(0, HISTORY),
                        &mut cache,
                        SlotId(0),
                    )
                    .expect("prefill");
            }
            let mut out = Vec::new();
            for (start, rows) in [
                (HISTORY, 1),
                (HISTORY + 1, 1),
                (HISTORY + 2, 1),
                (HISTORY + 3, 4),
            ] {
                out.extend(
                    model
                        .forward_gpu(
                            &tokens(0, start, rows),
                            &positions_for_range(start as i32, rows),
                            &mut cache,
                            SlotId(0),
                        )
                        .expect("decode step"),
                );
            }
            out
        };
        let first = decode(true);
        let second = decode(true);
        let stock = decode(false);
        assert!(first.iter().all(|value| value.is_finite()));
        assert!(
            first
                .iter()
                .zip(&second)
                .all(|(a, b)| a.to_bits() == b.to_bits()),
            "decode after a prepared prefill is not repeatable"
        );
        assert!(
            first
                .iter()
                .zip(&stock)
                .any(|(a, b)| a.to_bits() != b.to_bits()),
            "decode does not see which route prefilled its history"
        );
        let scale = stock
            .iter()
            .fold(0.0f32, |scale, value| scale.max(value.abs()));
        let worst = first
            .iter()
            .zip(&stock)
            .fold(0.0f32, |worst, (a, b)| worst.max((a - b).abs()));
        eprintln!(
            "decode after prefill: max |prepared - stock| = {worst:.5} of max |logit| {scale:.3} ({:.2e})",
            worst / scale
        );
        assert!(
            worst <= LIMIT * scale,
            "decode after a prepared prefill differs from the stock prefill by {worst} of {scale}"
        );
        for row in 0..7 {
            let top = |logits: &[f32]| {
                let mut order: Vec<usize> = (0..vocab).collect();
                order.sort_by(|a, b| logits[*b].total_cmp(&logits[*a]));
                (order[0], logits[order[0]] - logits[order[1]])
            };
            let (stock_token, margin) = top(&stock[row * vocab..(row + 1) * vocab]);
            let (token, _) = top(&first[row * vocab..(row + 1) * vocab]);
            assert!(
                token == stock_token || margin <= 2.0 * worst,
                "decode row {row}: greedy token {token} against {stock_token} at margin {margin}"
            );
        }
    });
}

/// The DFlash target-layer capture of a prepared chunk does not depend on
/// where the host waits: short chunks at the default eight-layer batch
/// against a wait after every layer, fresh and resumed, and the captured
/// layers are distinct residual streams (so an earlier layer's rows cannot
/// pass for a later one's).
#[test]
fn a_prepared_chunk_capture_equals_the_wait_every_layer_capture() {
    use crate::inference::spec_decode::dflash::hidden_capture::DFlashCaptureSession;
    const K_BATCH_ENV: &str = "HF2Q_FFN_TERMINAL_K_BATCH";
    struct WaitEveryLayer;
    impl WaitEveryLayer {
        fn set() -> Self {
            std::env::set_var(K_BATCH_ENV, "1");
            Self
        }
    }
    impl Drop for WaitEveryLayer {
        fn drop(&mut self) {
            std::env::remove_var(K_BATCH_ENV);
        }
    }
    with_synthetic_model(|model| {
        assert!(
            std::env::var_os(K_BATCH_ENV).is_none(),
            "{K_BATCH_ENV} must be unset: this gate compares the default batch against 1"
        );
        let hidden = model.cfg.hidden_size as usize;
        let capture = |history: usize, rows: usize| -> (Vec<u32>, Vec<u32>) {
            let _scope = PrefillScope::forced(true);
            let mut cache = cache(model);
            if history > 0 {
                model
                    .forward_gpu_last_logits(
                        &tokens(0, 0, history),
                        &positions_for_range(0, history),
                        &mut cache,
                        SlotId(0),
                    )
                    .expect("history prefill");
            }
            let mut session = DFlashCaptureSession::new(vec![1, 4, 6, 7], rows, hidden, false);
            let (logits, _hidden) = model
                .forward_gpu_last_logits_with_hidden_dflash(
                    &tokens(0, history, rows),
                    &positions_for_range(history as i32, rows),
                    &mut cache,
                    Some(&mut session),
                    SlotId(0),
                )
                .expect("captured chunk");
            (
                session.hidden_output.iter().map(|v| v.to_bits()).collect(),
                logits.iter().map(|v| v.to_bits()).collect(),
            )
        };
        for (history, rows) in [
            (0usize, 20usize),
            (20, 5),
            (20, 31),
            (40, 2),
            (17, 32),
            (0, 97),
            (97, 40),
        ] {
            let batched = capture(history, rows);
            let waited = {
                let _wait = WaitEveryLayer::set();
                capture(history, rows)
            };
            assert!(
                batched.1 == waited.1,
                "history {history}, {rows} rows: logits differ"
            );
            assert!(
                batched.0 == waited.0,
                "history {history}, {rows} rows: the capture differs from the wait-every-layer capture"
            );
            let layers: Vec<&[u32]> = batched.0.chunks(rows * hidden).collect();
            assert!(
                layers.windows(2).all(|pair| pair[0] != pair[1]),
                "history {history}, {rows} rows: captured layers are not distinct"
            );
        }
    });
}

/// `HF2Q_QWEN_PREFILL_WAIT_LAYERS` changes when the host waits and when a
/// layer's command buffer is submitted, not what the GPU computes: a large
/// fresh chunk and resumed ones, with target layers the host copies on and
/// off a wait boundary, give the same logits, the same captured rows and the
/// same slot at every setting.
#[test]
fn waiting_every_few_layers_leaves_the_bits_of_waiting_every_layer() {
    use super::prefill_prepared::ForcedWaitLayers;
    use crate::inference::spec_decode::dflash::hidden_capture::DFlashCaptureSession;
    with_synthetic_model(|model| {
        let hidden = model.cfg.hidden_size as usize;
        let run = |wait_layers: usize, captured_layers: &[usize]| {
            let _cadence = ForcedWaitLayers::set(wait_layers);
            let _scope = PrefillScope::forced(true);
            assert_eq!(super::prefill_prepared::wait_layers(), wait_layers);
            let mut cache = cache(model);
            let mut bits = Vec::new();
            let mut start = 0usize;
            for len in [97usize, 40, 33, 157, 5] {
                let mut session =
                    DFlashCaptureSession::new(captured_layers.to_vec(), len, hidden, false);
                let (logits, _hidden) = model
                    .forward_gpu_last_logits_with_hidden_dflash(
                        &tokens(0, start, len),
                        &positions_for_range(start as i32, len),
                        &mut cache,
                        Some(&mut session),
                        SlotId(0),
                    )
                    .unwrap_or_else(|error| panic!("chunk start={start} n={len}: {error:#}"));
                bits.extend(logits.iter().map(|value| value.to_bits()));
                bits.extend(session.hidden_output.iter().map(|value| value.to_bits()));
                start += len;
            }
            // Every row of a chunk without a capture reads the slot back.
            let read_back = model
                .forward_gpu(
                    &tokens(0, start, 41),
                    &positions_for_range(start as i32, 41),
                    &mut cache,
                    SlotId(0),
                )
                .expect("read-back chunk");
            bits.extend(read_back.iter().map(|value| value.to_bits()));
            bits
        };
        for captured_layers in [&[1usize, 4, 6, 7][..], &[0, 3][..], &[2][..]] {
            let every_layer = run(1, captured_layers);
            for wait_layers in [2, 3, 4, 8] {
                assert!(
                    run(wait_layers, captured_layers) == every_layer,
                    "waiting every {wait_layers} layers changed bits (captured layers \
                     {captured_layers:?})"
                );
            }
        }
    });
}

/// Real-artifact gate, run explicitly:
/// `HF2Q_QWEN38_GGUF=… <test binary> qwen38_prepared_prefill_real_model_gate --ignored --nocapture --test-threads=1`
///
/// On the served cache shape it checks, for the chunk sizes of the
/// four-agent workload, that the prepared route is repeatable bit for bit
/// (fresh and resumed chunks), and reports how far its last-row logits sit
/// from the stock route's, whether the greedy token agrees, and the wall
/// time of each chunk on both routes (second pass of each, so weights,
/// pipelines and prepared planes are warm).
#[test]
#[ignore = "requires HF2Q_QWEN38_GGUF (a Qwen3.8-named path) and Apple Metal; run alone"]
fn qwen38_prepared_prefill_real_model_gate() {
    use super::real_artifact_joint_decode_tests::{argmax, load, GateRouting};
    const PATTERN: [u32; 7] = [9707, 374, 279, 15, 1_782, 2_610, 13];
    /// Fresh content chunk, cue, then resumed content chunks and cues.
    const SCHEDULE: [usize; 7] = [720, 5, 157, 5, 31, 200, 5];
    let _gpu = crate::inference::hf2q_gpu_test_lock();
    let model = load("HF2Q_QWEN38_GGUF", GateRouting::Serve);
    let run = |prepared: bool| -> Vec<(Vec<f32>, f64)> {
        model
            .ensure_gpu_cache_primed()
            .expect("prime the GPU cache at the model's final address");
        let mut cache = model
            .with_gpu_cache_mut(|device, _registry| {
                HybridKvCache::new_with_options(&model.cfg, device, 4096, 1, true)
            })
            .expect("allocate TQ cache");
        let _scope = PrefillScope::forced(prepared);
        let mut start = 0usize;
        SCHEDULE
            .iter()
            .map(|&len| {
                let chunk: Vec<u32> = (start..start + len)
                    .map(|index| PATTERN[index % PATTERN.len()])
                    .collect();
                let started = std::time::Instant::now();
                let logits = model
                    .forward_gpu_last_logits(
                        &chunk,
                        &positions_for_range(start as i32, len),
                        &mut cache,
                        SlotId(0),
                    )
                    .unwrap_or_else(|error| panic!("chunk start={start} n={len}: {error:#}"));
                let elapsed_ms = started.elapsed().as_secs_f64() * 1e3;
                start += len;
                (logits, elapsed_ms)
            })
            .collect()
    };
    let _cold_stock = run(false);
    let stock = run(false);
    let prepared = run(true);
    let repeated = run(true);
    for (index, &len) in SCHEDULE.iter().enumerate() {
        let (stock_logits, stock_ms) = &stock[index];
        let (prepared_logits, _) = &prepared[index];
        let (repeated_logits, prepared_ms) = &repeated[index];
        assert!(
            prepared_logits
                .iter()
                .zip(repeated_logits)
                .all(|(a, b)| a.to_bits() == b.to_bits()),
            "chunk {index} (n={len}): the prepared route is not repeatable"
        );
        assert!(
            prepared_logits.iter().all(|value| value.is_finite()),
            "chunk {index} (n={len}): non-finite prepared logits"
        );
        let scale = stock_logits
            .iter()
            .fold(0.0f32, |scale, value| scale.max(value.abs()));
        let worst = stock_logits
            .iter()
            .zip(prepared_logits)
            .fold(0.0f32, |worst, (a, b)| worst.max((a - b).abs()));
        eprintln!(
            "[PREPARED_PREFILL] chunk {index} n={len}: stock {stock_ms:.1} ms, prepared \
             {prepared_ms:.1} ms; max |prepared - stock| = {worst:.4} (stock max |logit| \
             {scale:.2}), greedy token stock {} prepared {}",
            argmax(stock_logits),
            argmax(prepared_logits)
        );
    }
}

fn rms(values: &[f32]) -> f64 {
    (values
        .iter()
        .map(|v| f64::from(*v) * f64::from(*v))
        .sum::<f64>()
        / values.len() as f64)
        .sqrt()
}

fn rms_diff(a: &[f32], b: &[f32]) -> f64 {
    (a.iter()
        .zip(b)
        .map(|(a, b)| {
            let d = f64::from(*a) - f64::from(*b);
            d * d
        })
        .sum::<f64>()
        / a.len() as f64)
        .sqrt()
}

/// Real-artifact measurement of the Class B perturbation, run explicitly on
/// one served model:
/// `HF2Q_QWEN38_GGUF=… HF2Q_PREFILL_PROMPTS=prompts.json <test binary> qwen_prepared_vs_stock_on_served_prompts --ignored --nocapture --test-threads=1`
/// (or `HF2Q_QWEN36_GGUF=…`).
///
/// `HF2Q_PREFILL_PROMPTS` names a JSON list of `{"agent", "tokens", "spans"}`
/// objects: a conversation's final prompt and the row spans `[start, end,
/// prefill]` the server ran, `prefill = false` for rows it decoded. Each
/// conversation is replayed twice on its own cache, once with the stock and
/// once with the prepared prefill route (decoded rows run the stock prefill
/// kernels in both replays, outside the scope). For every prefilled span it
/// prints, per layer, the RMS difference of the two residual streams over
/// the stock stream's RMS (whole chunk and worst row), and for the span's
/// last row the largest logit difference, the stock top-1 margin and whether
/// the greedy token agrees. Nothing is asserted beyond finiteness: this
/// reports the distribution a quality decision needs.
#[test]
#[ignore = "requires HF2Q_QWEN38_GGUF or HF2Q_QWEN36_GGUF, HF2Q_PREFILL_PROMPTS and Apple Metal; run alone"]
fn qwen_prepared_vs_stock_on_served_prompts() {
    use crate::inference::spec_decode::dflash::hidden_capture::DFlashCaptureSession;
    use crate::serve::header::LoadProgress;
    use mlx_native::gguf::GgufFile;

    let _gpu = crate::inference::hf2q_gpu_test_lock();
    let path = match (
        std::env::var_os("HF2Q_QWEN38_GGUF"),
        std::env::var_os("HF2Q_QWEN36_GGUF"),
    ) {
        (Some(path), None) | (None, Some(path)) => std::path::PathBuf::from(path),
        _ => panic!("set exactly one of HF2Q_QWEN38_GGUF and HF2Q_QWEN36_GGUF"),
    };
    let prompts: serde_json::Value = serde_json::from_slice(
        &std::fs::read(std::env::var_os("HF2Q_PREFILL_PROMPTS").expect("HF2Q_PREFILL_PROMPTS"))
            .expect("read the prompt file"),
    )
    .expect("parse the prompt file");
    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 prepared prefill caches");
    model.ensure_gpu_cache_primed().expect("prime GPU cache");
    let hidden = model.cfg.hidden_size as usize;
    let layers = model.layers.len();
    let list = |values: &[f64]| {
        values
            .iter()
            .map(|value| format!("{value:.3e}"))
            .collect::<Vec<_>>()
            .join(",")
    };
    for prompt in prompts.as_array().expect("a list of prompts") {
        let agent = prompt["agent"].as_u64().expect("agent");
        let prompt_tokens: Vec<u32> = prompt["tokens"]
            .as_array()
            .expect("tokens")
            .iter()
            .map(|token| token.as_u64().expect("token id") as u32)
            .collect();
        let capacity = ((prompt_tokens.len() + 64).next_multiple_of(256)) as u32;
        let new_cache = || {
            model
                .with_gpu_cache_mut(|device, _registry| {
                    HybridKvCache::new_with_options(&model.cfg, device, capacity, 1, true)
                })
                .expect("allocate TQ cache")
        };
        // [stock replay, prepared replay]
        let mut caches = [new_cache(), new_cache()];
        for span in prompt["spans"].as_array().expect("spans") {
            let start = span[0].as_u64().expect("span start") as usize;
            let end = span[1].as_u64().expect("span end") as usize;
            let prefilled = span[2].as_bool().expect("span kind");
            let rows = end - start;
            let chunk = &prompt_tokens[start..end];
            let positions = positions_for_range(start as i32, rows);
            if !prefilled {
                for cache in &mut caches {
                    model
                        .forward_gpu_last_logits(chunk, &positions, cache, SlotId(0))
                        .expect("decoded rows");
                }
                continue;
            }
            let mut runs = Vec::with_capacity(2);
            for (arm, cache) in caches.iter_mut().enumerate() {
                let _scope = PrefillScope::forced(arm == 1);
                let mut session =
                    DFlashCaptureSession::new((0..layers).collect(), rows, hidden, false);
                let (logits, _hidden) = model
                    .forward_gpu_last_logits_with_hidden_dflash(
                        chunk,
                        &positions,
                        cache,
                        Some(&mut session),
                        SlotId(0),
                    )
                    .expect("prefilled span");
                assert!(logits.iter().all(|value| value.is_finite()));
                runs.push((logits, session.hidden_output));
            }
            let (stock, prepared) = (&runs[0], &runs[1]);
            let mut layer_rel = Vec::with_capacity(layers);
            let mut layer_worst_row = Vec::with_capacity(layers);
            for layer in 0..layers {
                let range = layer * rows * hidden..(layer + 1) * rows * hidden;
                let (s, p) = (&stock.1[range.clone()], &prepared.1[range]);
                layer_rel.push(rms_diff(s, p) / rms(s));
                layer_worst_row.push((0..rows).fold(0.0f64, |worst, row| {
                    let r = row * hidden..(row + 1) * hidden;
                    worst.max(rms_diff(&s[r.clone()], &p[r.clone()]) / rms(&s[r]))
                }));
            }
            let top = |logits: &[f32]| {
                let (mut best, mut second) = ((0usize, f32::NEG_INFINITY), f32::NEG_INFINITY);
                for (index, &value) in logits.iter().enumerate() {
                    if value > best.1 {
                        second = best.1;
                        best = (index, value);
                    } else if value > second {
                        second = value;
                    }
                }
                (best.0, best.1 - second)
            };
            let (stock_token, margin) = top(&stock.0);
            let (prepared_token, _) = top(&prepared.0);
            let logit_diff = stock
                .0
                .iter()
                .zip(&prepared.0)
                .fold(0.0f32, |worst, (a, b)| worst.max((a - b).abs()));
            let logit_scale = stock.0.iter().fold(0.0f32, |scale, v| scale.max(v.abs()));
            eprintln!(
                "[PREP_VS_STOCK] agent={agent} start={start} rows={rows} logit_max_abs_diff={logit_diff:.5} \
                 logit_scale={logit_scale:.3} stock_margin={margin:.4} greedy_agrees={} layer_rel={} layer_worst_row_rel={}",
                stock_token == prepared_token,
                list(&layer_rel),
                list(&layer_worst_row)
            );
        }
    }
}

/// Every row's logits of one chunk through the scope the engine opens: the
/// route is whatever this thread's resolved switches say.
fn chunk_rows_as_served(model: &Qwen35Model, len: usize) -> Vec<u32> {
    let _scope = PrefillScope::enter();
    let logits = model
        .forward_gpu(
            &tokens(0, 0, len),
            &positions_for_range(0, len),
            &mut cache(model),
            SlotId(0),
        )
        .expect("prefill chunk");
    logits.iter().map(|value| value.to_bits()).collect()
}

/// ADR-067 D6: a load on which the DFlash2 policy selects nothing (the kill
/// switch, or an artifact that is not a qualified pair) prefills with the
/// kernels it had before the route profile existed, bit for bit; only a
/// selected profile, or an explicit switch, moves it to the prepared route.
#[test]
fn prefill_bits_change_only_when_the_route_profile_is_selected() {
    use crate::inference::spec_decode::dflash2::route_profile::{
        bind, ResolvedRoutes, RouteProfile,
    };
    use std::sync::Arc;
    let unset = |_: &str| None;
    let routes = |profile, env: &dyn Fn(&str) -> Option<String>| {
        Arc::new(ResolvedRoutes::resolve(profile, &|_| None, env))
    };
    with_synthetic_model(|model| {
        let stock = chunk_rows(model, &mut cache(model), 0, 0, 33, false);
        let prepared = chunk_rows(model, &mut cache(model), 0, 0, 33, true);
        assert!(stock != prepared, "the two kernels must be told apart");
        {
            let _prior_route = bind(routes(None, &unset));
            assert!(
                chunk_rows_as_served(model, 33) == stock,
                "without a selected profile the prefill left the stock kernels"
            );
        }
        for profile in [RouteProfile::Qwen38, RouteProfile::Qwen36] {
            let _selected = bind(routes(Some(profile), &unset));
            assert!(
                chunk_rows_as_served(model, 33) == prepared,
                "{profile:?} did not select the prepared prefill"
            );
        }
        {
            let off = |name: &str| {
                (name == "HF2Q_QWEN_PREFILL_PREPARED").then(|| "0".to_owned())
            };
            let _escape = bind(routes(Some(RouteProfile::Qwen38), &off));
            assert!(
                chunk_rows_as_served(model, 33) == stock,
                "an explicit 0 did not override the profile"
            );
        }
        {
            let on = |name: &str| {
                (name == "HF2Q_QWEN_PREFILL_PREPARED").then(|| "1".to_owned())
            };
            let _explicit = bind(routes(None, &on));
            assert!(
                chunk_rows_as_served(model, 33) == prepared,
                "an explicit 1 did not select the prepared prefill"
            );
        }
    });
}

/// A thread that primed the model's GPU cache only to provision something
/// releases it: the cache is gone from that thread, releasing again is a
/// no-op, and the model primes and computes the same bits afterwards.
#[test]
fn a_released_gpu_cache_is_gone_and_the_model_primes_again() {
    with_synthetic_model(|model| {
        let before = chunk_rows(model, &mut cache(model), 0, 0, 33, false);
        model.release_gpu_cache().expect("release");
        assert!(
            model.with_gpu_cache_mut(|_device, _registry| Ok(())).is_err(),
            "the released cache is still bound to this thread"
        );
        model.release_gpu_cache().expect("releasing nothing is a no-op");
        model.ensure_gpu_cache_primed().expect("prime again");
        let after = chunk_rows(model, &mut cache(model), 0, 0, 33, false);
        assert!(before == after, "a re-primed cache changed bits");
    });
}