hf2q 0.1.30

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
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
//! Qwen joint B-row decode scheduling (ADR-063 S3, phase 1).
//!
//! One scheduler decode turn advances every eligible decoding slot with one
//! shared target forward instead of one forward per slot. Sampling, grammar
//! and tool-call constraints, thinking budgets, stop strings, streaming,
//! cancellation, retained-prefix publication and failure recovery stay per
//! slot: each joint lane settles through `settle_qwen35_decode_lane`, the
//! same tail the serial loop uses. Ineligible handles (closed clients, CPU-only
//! verified output, cursor/ledger mismatch) stay on the serial loop.
//!
//! Exact mode: a turn with fewer than two eligible lanes is serial, including
//! MTP K3 and history speculation, and lanes beyond `max_rows` stay serial;
//! both are bitwise equal to the joint row. Padded mode (#717): every eligible
//! lane runs in consecutive joint groups of at most `max_rows`, a lone
//! ordinary lane included, so an ordinary request's arithmetic does not depend
//! on which tenants share its turns; only a lone lane that would speculate
//! stays serial.

use std::sync::{
    atomic::{AtomicU64, Ordering},
    Once,
};

use crate::inference::models::qwen35::joint_decode_routes::{
    joint_decode_env_unavailable_reason, joint_ffn_shared_from_process, joint_mode_max_rows,
    select_dense_ffn_route, JointDenseFfnRoute, JointRouteMode, MAX_JOINT_DECODE_ROWS,
};
use crate::inference::models::qwen35::joint_projection::{
    select_joint_projection_kernel, JointProjectionKernel, QWEN_JOINT_MMA_ENV,
};
use crate::inference::models::qwen35::joint_step_options::{
    parse_switch, switch_from_process, JointStepOptions, QWEN_JOINT_MTP_ENV,
    QWEN_JOINT_POOLED_OUTPUTS_ENV,
};
use crate::inference::models::qwen35::model::Qwen35Model;
use crate::serve::api::engine_qwen35::{
    anchor_store::Qwen35AnchorStore, critical_path, Qwen35JointLane, Qwen35LaneRefusal,
    Qwen35TickOutcome,
};
use crate::serve::api::qwen35_speculation::QwenSpeculationRoute;
use crate::serve::api::registry::ModelRegistration;

use super::{
    is_fatal_command_buffer_error, settle_qwen35_decode_lane, EngineSupervisor,
    InflightBatchedScheduler, PromptCache, Qwen35FatalFailure, Qwen35FatalReply, Qwen35KvGuard,
    Qwen35RetainedPrefix, Qwen35Slot, Qwen35SlotWork, Qwen35WorkerFatalKind, SlotHandle,
};

/// The joint MTP verify turn (`HF2Q_QWEN_JOINT_MTP`).
#[path = "qwen35_joint_mtp.rs"]
mod joint_mtp;

/// Kill switch for A/B measurement: `1`/`on`/`true` selects joint decode,
/// `0`/`off`/`false` keeps the serial per-slot turn. Unset selects joint
/// decode on every Qwen artifact that has a proven joint route:
/// - dense (Qwen3.8): the official-artifact gates passed and the four-agent
///   ABBA A/B cut median makespan 116.4 → 90.1 s with 8/8 `CalibrationPass`;
/// - MoE (Qwen3.6): on the declared repair-turn workload (ADR-063
///   Q4-QWEN36) the paired 24-instance task campaign is `Pass` against serial
///   decode, and the six-block canonical n4 ABBA cut makespan by a ratio of
///   0.800 (95% CI 0.776–0.824) with 26/26 `CalibrationPass`.
const QWEN_JOINT_DECODE_ENV: &str = "HF2Q_QWEN_JOINT_DECODE";

/// Widest joint step selected by default. Exact mode accepts 5..=8 when
/// raised explicitly (no serving measurement); the padded `mul_mv_ext` mode
/// caps at 4 so its packed width stays fixed.
const QWEN_JOINT_DECODE_MAX_ROWS_ENV: &str = "HF2Q_QWEN_JOINT_DECODE_MAX_ROWS";
const DEFAULT_QWEN_JOINT_DECODE_ROWS: usize = 4;

/// Kill switch for the padded-mode solo route (#717). Unset or
/// `1`/`on`/`true`: every eligible ordinary lane, a lone one included, runs
/// the padded joint forward. `0`/`off`/`false`: a lone lane and lanes beyond
/// `max_rows` run the serial one-row forward (the phase-1 partition).
const QWEN_JOINT_SOLO_ENV: &str = "HF2Q_QWEN_JOINT_SOLO";

/// Joint decode configuration resolved once at worker start.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(in crate::serve::api) struct Qwen35JointDecodeConfig {
    pub max_rows: usize,
    pub mode: JointRouteMode,
    /// Dense FFN gate/up route; `HF2Q_QWEN_JOINT_FFN_SHARED=0` keeps the
    /// serial builder in the padded mode (exact mode always keeps it).
    pub dense_ffn: JointDenseFfnRoute,
    /// Padded mode on a multi-slot worker with the solo route on: every
    /// eligible lane joins a joint group, and a lone ordinary lane runs a
    /// one-lane joint step instead of the serial forward.
    pub solo: bool,
    /// Per-step options; their switches are read here, once.
    pub step: JointStepOptions,
    /// `HF2Q_QWEN_JOINT_MTP`: speculating lanes verify their MTP drafts as
    /// windows of a joint step (`forward_gpu_joint_verify`) instead of the
    /// serial K3 round. In padded mode the same switch selects the
    /// column-invariant projection unless its diagnostic override is explicit.
    /// Default on for dense artifacts with an MTP head; explicit false is the
    /// kill switch. MoE and artifacts without MTP remain off by default.
    pub joint_mtp: bool,
}

/// `Ok(None)` when unset, so the caller applies the default (on).
fn parse_qwen_joint_decode(raw: Option<&str>) -> Result<Option<bool>, String> {
    let Some(raw) = raw.map(str::trim).filter(|raw| !raw.is_empty()) else {
        return Ok(None);
    };
    match raw.to_ascii_lowercase().as_str() {
        "1" | "on" | "true" => Ok(Some(true)),
        "0" | "off" | "false" => Ok(Some(false)),
        _ => Err(format!(
            "{QWEN_JOINT_DECODE_ENV} must be 1/on/true or 0/off/false, got {raw:?}"
        )),
    }
}

fn parse_qwen_joint_decode_max_rows(raw: Option<&str>) -> Result<usize, String> {
    let Some(raw) = raw.map(str::trim).filter(|raw| !raw.is_empty()) else {
        return Ok(DEFAULT_QWEN_JOINT_DECODE_ROWS);
    };
    raw.parse::<usize>()
        .ok()
        .filter(|rows| (2..=MAX_JOINT_DECODE_ROWS).contains(rows))
        .ok_or_else(|| {
            format!(
                "{QWEN_JOINT_DECODE_MAX_ROWS_ENV} must be an integer in 2..={MAX_JOINT_DECODE_ROWS}, got {raw:?}"
            )
        })
}

/// Unset selects the solo route (`true`).
fn parse_qwen_joint_solo(raw: Option<&str>) -> Result<bool, String> {
    let Some(raw) = raw.map(str::trim).filter(|raw| !raw.is_empty()) else {
        return Ok(true);
    };
    match raw.to_ascii_lowercase().as_str() {
        "1" | "on" | "true" => Ok(true),
        "0" | "off" | "false" => Ok(false),
        _ => Err(format!(
            "{QWEN_JOINT_SOLO_ENV} must be 1/on/true or 0/off/false, got {raw:?}"
        )),
    }
}

/// The measured joint MTP+MMA candidate is the dense-MTP default. An explicit
/// value remains the production kill switch.
fn parse_qwen_joint_mtp(raw: Option<&str>, default: bool) -> Result<bool, String> {
    if raw.map(str::trim).filter(|raw| !raw.is_empty()).is_none() {
        return Ok(default);
    }
    parse_switch(QWEN_JOINT_MTP_ENV, raw)
}

/// Joint MTP and the column-invariant projection form one measured candidate.
/// An explicit MMA value remains a diagnostic override; otherwise MTP selects
/// MMA in padded mode and the route selector keeps exact mode canonical.
fn parse_qwen_joint_mma(raw: Option<&str>, joint_mtp: bool) -> Result<bool, String> {
    if raw.map(str::trim).filter(|raw| !raw.is_empty()).is_none() {
        return Ok(joint_mtp);
    }
    parse_switch(QWEN_JOINT_MMA_ENV, raw)
}

/// `solo` applies only to the padded mode on a worker with at least two
/// slots: exact-mode joint rows equal serial rows bitwise, and a one-slot
/// worker (N1) never shares a turn. `requested_step` holds the switches;
/// the column-invariant MMA projection applies only to the padded mode.
/// `joint_mtp` needs a lone lane to be able to run as a one-lane joint step
/// whose row equals the spec-off row: exact mode (joint ≡ serial), or the
/// padded mode with the solo route. "Joint" here is plain joint decode and
/// joint MTP verify; a DFlash2 target verify step is a route of its own
/// (Class B against serial decode at every tile width once the prepared
/// routes serve it), so a lane that leaves DFlash2 for these steps changes
/// route.
#[allow(clippy::too_many_arguments)]
fn select_qwen_joint_decode(
    requested: bool,
    max_rows: usize,
    mode: Result<JointRouteMode, String>,
    ffn_shared: bool,
    solo_requested: bool,
    n_slots: usize,
    requested_step: JointStepOptions,
    joint_mtp: bool,
) -> Option<Qwen35JointDecodeConfig> {
    match (requested, mode) {
        (true, Ok(mode)) => {
            let solo = solo_requested && mode == JointRouteMode::PaddedMvExt && n_slots >= 2;
            Some(Qwen35JointDecodeConfig {
                max_rows: joint_mode_max_rows(mode, max_rows),
                mode,
                dense_ffn: select_dense_ffn_route(mode, ffn_shared),
                solo,
                step: JointStepOptions {
                    projection: select_joint_projection_kernel(
                        mode,
                        requested_step.projection == JointProjectionKernel::ColumnInvariantMma,
                    ),
                    pooled_outputs: requested_step.pooled_outputs,
                    verify_attention_tile: false,
                    verify_mixer_fused: false,
                    dflash2: crate::inference::models::qwen35::joint_step_options::DFlash2VerifyOptions::OFF,
                },
                joint_mtp: joint_mtp && (mode == JointRouteMode::Exact || solo),
            })
        }
        _ => None,
    }
}

/// Resolve joint decode once when the Qwen worker starts and log the
/// decision, including why an artifact or configuration keeps serial decode.
pub(in crate::serve::api) fn qwen35_joint_decode_config(
    model: &Qwen35Model,
    n_slots: usize,
) -> Option<Qwen35JointDecodeConfig> {
    let moe = model.cfg.moe.is_some();
    let default = true;
    let raw = std::env::var(QWEN_JOINT_DECODE_ENV).ok();
    let explicit = parse_qwen_joint_decode(raw.as_deref()).unwrap_or_else(|error| {
        tracing::warn!(
            error,
            fallback = default,
            "invalid Qwen joint decode switch"
        );
        None
    });
    let requested = explicit.unwrap_or(default);
    let raw_rows = std::env::var(QWEN_JOINT_DECODE_MAX_ROWS_ENV).ok();
    let max_rows = parse_qwen_joint_decode_max_rows(raw_rows.as_deref()).unwrap_or_else(|error| {
        tracing::warn!(
            error,
            fallback = DEFAULT_QWEN_JOINT_DECODE_ROWS,
            "invalid Qwen joint decode width"
        );
        DEFAULT_QWEN_JOINT_DECODE_ROWS
    });
    let mode = if requested {
        model.joint_decode_mode()
    } else {
        Err("disabled by HF2Q_QWEN_JOINT_DECODE".into())
    };
    let unavailable = mode.as_ref().err().cloned();
    let ffn_shared = joint_ffn_shared_from_process().unwrap_or_else(|error| {
        tracing::warn!(
            error,
            fallback = true,
            "invalid Qwen joint dense FFN switch"
        );
        true
    });
    let raw_solo = std::env::var(QWEN_JOINT_SOLO_ENV).ok();
    let solo_requested = parse_qwen_joint_solo(raw_solo.as_deref()).unwrap_or_else(|error| {
        tracing::warn!(error, fallback = true, "invalid Qwen joint solo switch");
        true
    });
    let switch = |name: &str, label: &str| {
        switch_from_process(name).unwrap_or_else(|error| {
            tracing::warn!(error, fallback = false, "invalid Qwen joint {label} switch");
            false
        })
    };
    let joint_mtp_default = !moe && model.mtp.is_some();
    let raw_mtp = std::env::var(QWEN_JOINT_MTP_ENV).ok();
    let joint_mtp =
        parse_qwen_joint_mtp(raw_mtp.as_deref(), joint_mtp_default).unwrap_or_else(|error| {
            tracing::warn!(
                error,
                fallback = joint_mtp_default,
                "invalid Qwen joint MTP verify switch"
            );
            joint_mtp_default
        });
    let raw_mma = std::env::var(QWEN_JOINT_MMA_ENV).ok();
    let mma = parse_qwen_joint_mma(raw_mma.as_deref(), joint_mtp).unwrap_or_else(|error| {
        tracing::warn!(error, fallback = joint_mtp, "invalid Qwen joint MMA switch");
        joint_mtp
    });
    let requested_step = JointStepOptions {
        projection: if mma {
            JointProjectionKernel::ColumnInvariantMma
        } else {
            JointProjectionKernel::Canonical
        },
        pooled_outputs: switch(QWEN_JOINT_POOLED_OUTPUTS_ENV, "pooled outputs"),
        verify_attention_tile: false,
        verify_mixer_fused: false,
        dflash2: crate::inference::models::qwen35::joint_step_options::DFlash2VerifyOptions::OFF,
    };
    let selected = select_qwen_joint_decode(
        requested,
        max_rows,
        mode,
        ffn_shared,
        solo_requested,
        n_slots,
        requested_step,
        joint_mtp,
    );
    tracing::info!(
        target: "hf2q::serve::api::engine_qwen35::progress",
        requested,
        explicit = explicit.is_some(),
        moe,
        dense_decode_mvn = model.ggml_routing_policy().dense_decode_mvn,
        dense_decode_mv_ext = model.ggml_routing_policy().dense_decode_mv_ext,
        moe_ffn = model.moe_ffn_decode_route(),
        moe_experts = model.moe_expert_decode_route(),
        max_rows = selected.map_or(max_rows, |config| config.max_rows),
        selected = selected.is_some(),
        mode = selected.map_or("serial", |config| config.mode.label()),
        dense_ffn = selected.map_or("serial", |config| config.dense_ffn.label()),
        solo = selected.is_some_and(|config| config.solo),
        column_invariant_mma = selected.is_some_and(|config| {
            config.step.projection == JointProjectionKernel::ColumnInvariantMma
        }),
        pooled_outputs = selected.is_some_and(|config| config.step.pooled_outputs),
        joint_mtp = selected.is_some_and(|config| config.joint_mtp),
        verify_replay_reuse = crate::inference::models::qwen35::replay_arena::enabled(),
        dflash2_context_first = crate::serve::api::engine_qwen35::dflash2_context_first(),
        dflash2_draft_prefetch = crate::serve::api::engine_qwen35::dflash2_draft_prefetch(),
        dflash2_decisions = crate::serve::api::engine_qwen35::dflash2_decision_route().label(),
        unavailable = unavailable.as_deref().unwrap_or("none"),
        "Qwen joint decode configuration"
    );
    selected
}

/// Re-check the serial-graph diagnostics every turn. The worker's mode came
/// from the model's routing policy, which is frozen into its kernel registry
/// at load (#729) and cannot change mid-run, so only the live diagnostic
/// switches (`HF2Q_LEGACY_PER_LAYER_CB`, `MLX_UNRETAINED_REFS`) can revoke
/// it: such a turn runs serially (logged once), and a later turn that
/// satisfies them again runs jointly.
pub(in crate::serve::api) fn qwen35_joint_decode_turn_config(
    worker: Option<Qwen35JointDecodeConfig>,
) -> Option<Qwen35JointDecodeConfig> {
    let config = worker?;
    let Some(reason) = joint_decode_env_unavailable_reason() else {
        return Some(config);
    };
    static REVOKED: Once = Once::new();
    REVOKED.call_once(|| {
        tracing::warn!(
            target: "hf2q::serve::api::engine_qwen35::progress",
            reason = %reason,
            "Qwen joint decode revoked after worker start; decoding serially"
        );
    });
    None
}

/// One decode turn's joint groups and serial leftovers.
#[derive(Debug, PartialEq, Eq)]
struct JointPartition {
    /// Joint steps, run in order; each is sorted by slot.
    groups: Vec<Vec<SlotHandle>>,
    /// Handles for the serial loop, in scheduler order.
    serial: Vec<SlotHandle>,
}

/// Split one decode turn into joint groups and serial leftovers.
/// `classify` returns `None` for a lane that cannot join a joint step and
/// `Some(ordinary)` otherwise, where `ordinary` means its serial tick would
/// be the ordinary target forward (no MTP or history speculation state).
///
/// - Without `solo` (exact mode, one-slot workers, or the kill switch): one
///   group of the first `max_rows` eligible lanes, only when at least two are
///   eligible.
/// - With `solo` (padded mode, #717): every eligible lane, in consecutive
///   groups of at most `max_rows` in scheduler order. A group of one joins
///   only when its lane is ordinary, so a lone speculating lane keeps its
///   serial round.
///
/// `None` when handles repeat a slot or no group forms: the whole turn is
/// serial.
fn partition_qwen35_joint_lanes(
    handles: &[SlotHandle],
    max_rows: usize,
    solo: bool,
    mut classify: impl FnMut(SlotHandle) -> Option<bool>,
) -> Option<JointPartition> {
    let unique = handles.iter().enumerate().all(|(index, handle)| {
        handles[..index]
            .iter()
            .all(|earlier| earlier.slot_id != handle.slot_id)
    });
    if !unique || max_rows < 2 {
        return None;
    }
    let mut joined = vec![false; handles.len()];
    let mut groups: Vec<Vec<SlotHandle>> = Vec::new();
    if solo {
        let eligible = handles
            .iter()
            .enumerate()
            .filter_map(|(index, &handle)| classify(handle).map(|ordinary| (index, ordinary)))
            .collect::<Vec<_>>();
        for chunk in eligible.chunks(max_rows) {
            if let [(_, false)] = chunk {
                continue;
            }
            for &(index, _) in chunk {
                joined[index] = true;
            }
            groups.push(chunk.iter().map(|&(index, _)| handles[index]).collect());
        }
    } else {
        let mut group = Vec::with_capacity(handles.len().min(max_rows));
        for (index, &handle) in handles.iter().enumerate() {
            if group.len() < max_rows && classify(handle).is_some() {
                group.push(index);
            }
        }
        if group.len() >= 2 {
            for &index in &group {
                joined[index] = true;
            }
            groups.push(group.iter().map(|&index| handles[index]).collect());
        }
    }
    if groups.is_empty() {
        return None;
    }
    for group in &mut groups {
        group.sort_by_key(|handle| handle.slot_id);
    }
    let serial = handles
        .iter()
        .zip(&joined)
        .filter(|(_, &joined)| !joined)
        .map(|(&handle, _)| handle)
        .collect();
    Some(JointPartition { groups, serial })
}

/// A failed shared transaction fails exactly the rows that submitted it:
/// the first keeps the original error, the rest get its rendering.
fn distribute_step_error<T>(
    error: anyhow::Error,
    rows: usize,
) -> impl Iterator<Item = anyhow::Result<T>> {
    let message = format!("{error:#}");
    let mut first = Some(error);
    (0..rows).map(move |_| Err(first.take().unwrap_or_else(|| anyhow::anyhow!("{message}"))))
}

pub(in crate::serve::api) struct Qwen35JointTelemetry {
    pub capture_active: AtomicU64,
    pub duplicate_slots: AtomicU64,
    pub max_rows: AtomicU64,
    pub too_few_ready: AtomicU64,
    pub anchor_capture: AtomicU64,
    pub anchor_restore: AtomicU64,
    pub lane_anchor_capture: AtomicU64,
    pub lane_anchor_restore: AtomicU64,
    pub c2_joint_lane_steps: AtomicU64,
    pub c2_serial_lane_steps: AtomicU64,
    pub lane_step_bound: AtomicU64,
    pub lane_pending_speculation_output: AtomicU64,
    pub lane_no_seed: AtomicU64,
    pub lane_target_cursor: AtomicU64,
    pub lane_mtp_cursor: AtomicU64,
    pub lane_speculation_route: AtomicU64,
    pub lane_client_closed: AtomicU64,
    pub lane_handle_mismatch: AtomicU64,
}

pub(in crate::serve::api) static JOINT_TELEMETRY: Qwen35JointTelemetry = Qwen35JointTelemetry {
    capture_active: AtomicU64::new(0),
    duplicate_slots: AtomicU64::new(0),
    max_rows: AtomicU64::new(0),
    too_few_ready: AtomicU64::new(0),
    anchor_capture: AtomicU64::new(0),
    anchor_restore: AtomicU64::new(0),
    lane_anchor_capture: AtomicU64::new(0),
    lane_anchor_restore: AtomicU64::new(0),
    c2_joint_lane_steps: AtomicU64::new(0),
    c2_serial_lane_steps: AtomicU64::new(0),
    lane_step_bound: AtomicU64::new(0),
    lane_pending_speculation_output: AtomicU64::new(0),
    lane_no_seed: AtomicU64::new(0),
    lane_target_cursor: AtomicU64::new(0),
    lane_mtp_cursor: AtomicU64::new(0),
    lane_speculation_route: AtomicU64::new(0),
    lane_client_closed: AtomicU64::new(0),
    lane_handle_mismatch: AtomicU64::new(0),
};

pub(in crate::serve::api) fn record_joint_ineligible(reason: &Qwen35JointIneligible) {
    let counter = match reason {
        Qwen35JointIneligible::CaptureActive => &JOINT_TELEMETRY.capture_active,
        Qwen35JointIneligible::DuplicateSlots => &JOINT_TELEMETRY.duplicate_slots,
        Qwen35JointIneligible::MaxRowsBelowTwo => &JOINT_TELEMETRY.max_rows,
        Qwen35JointIneligible::AnchorTransaction { kind, lanes } => {
            use crate::serve::api::engine_qwen35::anchor_store::AnchorTransaction;
            let (turn, lane) = match kind {
                AnchorTransaction::Capture => (&JOINT_TELEMETRY.anchor_capture, &JOINT_TELEMETRY.lane_anchor_capture),
                AnchorTransaction::Restore => (&JOINT_TELEMETRY.anchor_restore, &JOINT_TELEMETRY.lane_anchor_restore),
            };
            lane.fetch_add(*lanes as u64, Ordering::Relaxed);
            turn
        }
        Qwen35JointIneligible::TooFewReady { refusals, .. } => {
            for refusal in refusals {
                let lane = match refusal {
                    Qwen35LaneRefusal::StepBound => &JOINT_TELEMETRY.lane_step_bound,
                    Qwen35LaneRefusal::PendingSpeculationOutput => {
                        &JOINT_TELEMETRY.lane_pending_speculation_output
                    }
                    Qwen35LaneRefusal::NoSeed => &JOINT_TELEMETRY.lane_no_seed,
                    Qwen35LaneRefusal::TargetCursor => &JOINT_TELEMETRY.lane_target_cursor,
                    Qwen35LaneRefusal::MtpCursor => &JOINT_TELEMETRY.lane_mtp_cursor,
                    Qwen35LaneRefusal::SpeculationRoute => &JOINT_TELEMETRY.lane_speculation_route,
                    Qwen35LaneRefusal::ClientClosed => &JOINT_TELEMETRY.lane_client_closed,
                    Qwen35LaneRefusal::HandleMismatch => &JOINT_TELEMETRY.lane_handle_mismatch,
                    Qwen35LaneRefusal::AnchorCapture => &JOINT_TELEMETRY.lane_anchor_capture,
                    Qwen35LaneRefusal::AnchorRestore => &JOINT_TELEMETRY.lane_anchor_restore,
                };
                lane.fetch_add(1, Ordering::Relaxed);
            }
            if refusals.contains(&Qwen35LaneRefusal::AnchorCapture) {
                &JOINT_TELEMETRY.anchor_capture
            } else if refusals.contains(&Qwen35LaneRefusal::AnchorRestore) {
                &JOINT_TELEMETRY.anchor_restore
            } else {
                &JOINT_TELEMETRY.too_few_ready
            }
        }
    };
    counter.fetch_add(1, Ordering::Relaxed);
}

/// A lane-step is successful native dispatch participation, not an accepted
/// speculative token, a CPU drain or a sampled group width.
pub(in crate::serve::api) fn record_c2_decode_lane_steps(
    decode_ready_lanes: usize, joint_lane_steps: usize, serial_lane_steps: usize,
    route: &'static str, path: &'static str,
) {
    if decode_ready_lanes < 2 || joint_lane_steps + serial_lane_steps == 0 {
        return;
    }
    if joint_lane_steps != 0 {
        JOINT_TELEMETRY.c2_joint_lane_steps.fetch_add(joint_lane_steps as u64, Ordering::Relaxed);
    }
    if serial_lane_steps != 0 {
        JOINT_TELEMETRY.c2_serial_lane_steps.fetch_add(serial_lane_steps as u64, Ordering::Relaxed);
    }
    tracing::debug!(
        target: "hf2q::serve::api::engine_qwen35::progress",
        decode_ready_lanes, joint_lane_steps, serial_lane_steps, route, path,
        "Qwen C2 decode lane steps"
    );
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub(in crate::serve::api) enum Qwen35JointIneligible {
    CaptureActive,
    DuplicateSlots,
    MaxRowsBelowTwo,
    AnchorTransaction {
        kind: crate::serve::api::engine_qwen35::anchor_store::AnchorTransaction,
        lanes: usize,
    },
    TooFewReady {
        ready: usize,
        refusals: Vec<Qwen35LaneRefusal>,
    },
}

impl Qwen35JointIneligible {
    pub fn label(&self) -> &'static str {
        match self {
            Self::CaptureActive => "capture_active",
            Self::DuplicateSlots => "duplicate_slots",
            Self::MaxRowsBelowTwo => "max_rows",
            Self::AnchorTransaction { kind, .. } => match kind {
                crate::serve::api::engine_qwen35::anchor_store::AnchorTransaction::Capture => "anchor_capture",
                crate::serve::api::engine_qwen35::anchor_store::AnchorTransaction::Restore => "anchor_restore",
            },
            Self::TooFewReady { refusals, .. } => {
                if refusals.contains(&Qwen35LaneRefusal::AnchorCapture) {
                    "anchor_capture"
                } else if refusals.contains(&Qwen35LaneRefusal::AnchorRestore) {
                    "anchor_restore"
                } else {
                    "too_few_ready"
                }
            }
        }
    }
}

pub(super) enum Qwen35JointRun {
    /// No joint group forms; the caller runs the whole turn serially.
    NotEligible(Qwen35JointIneligible),
    /// Joint lanes are settled; `serial` handles still need the serial loop.
    Ran {
        serial: Vec<SlotHandle>,
    },
    Fatal(Qwen35FatalFailure),
}

struct JointLane {
    handle: SlotHandle,
    reply: super::SlotReply,
    state: crate::serve::api::engine_qwen35::Qwen35DecodeState,
    tick: Option<anyhow::Result<Qwen35TickOutcome>>,
}

fn reinstall_lanes(slots: &mut [Option<Qwen35Slot>], lanes: impl IntoIterator<Item = JointLane>) {
    for lane in lanes {
        let index = lane.handle.slot_id.0 as usize;
        debug_assert!(slots[index].is_none());
        slots[index] = Some((Qwen35SlotWork::Decode(lane.state), lane.reply, lane.handle));
    }
}


/// The real admission guard: pending publication is deliberately not examined.
pub(in crate::serve::api) fn joint_anchor_admission(
    handles: &[SlotHandle],
    stores: &[Qwen35AnchorStore],
) -> Option<Qwen35JointIneligible> {
    let kind = handles.iter().find_map(|handle|
        stores.get(handle.slot_id.0 as usize).and_then(Qwen35AnchorStore::open_transaction))?;
    let lanes = handles.iter().filter(|handle|
        stores.get(handle.slot_id.0 as usize).and_then(Qwen35AnchorStore::open_transaction) == Some(kind)).count();
    Some(Qwen35JointIneligible::AnchorTransaction { kind, lanes })
}
/// Run the turn's joint groups one after another, then hand the serial
/// leftovers back to the caller. Each group is one joint step whose lanes
/// settle with the serial path's slot semantics.
#[allow(clippy::too_many_arguments)]
pub(super) fn run_qwen35_joint_decode(
    guard: &mut Qwen35KvGuard<'_>,
    scheduler: &mut InflightBatchedScheduler,
    slots: &mut [Option<Qwen35Slot>],
    retained_tokens: &mut [Qwen35RetainedPrefix],
    prompt_caches: &mut [PromptCache],
    prompt_anchors: &mut [Qwen35AnchorStore],
    registration: Option<&ModelRegistration>,
    handles: &[SlotHandle],
    decode_ready_lanes: usize,
    kv_bytes_per_token: u64,
    config: Qwen35JointDecodeConfig,
    supervisor: &EngineSupervisor,
) -> Qwen35JointRun {
    if let Some(reason) = joint_anchor_admission(handles, prompt_anchors) {
        return Qwen35JointRun::NotEligible(reason);
    }
    if guard
        .kv
        .as_ref()
        .expect("kv Some during loop")
        .la_capture_active()
    {
        // An active DeltaNet capture would make the joint transaction refuse
        // after lanes were prepared; keep such a turn serial up front.
        return Qwen35JointRun::NotEligible(Qwen35JointIneligible::CaptureActive);
    }
    if config.max_rows < 2 {
        return Qwen35JointRun::NotEligible(Qwen35JointIneligible::MaxRowsBelowTwo);
    }
    if handles.iter().enumerate().any(|(index, handle)| {
        handles[..index]
            .iter()
            .any(|earlier| earlier.slot_id == handle.slot_id)
    }) {
        return Qwen35JointRun::NotEligible(Qwen35JointIneligible::DuplicateSlots);
    }
    // Repeatable route: plan every lane's tick from its own state before the
    // partition, so a lane that speculates or drains verified output never
    // joins a joint step and the serial loop runs the same decision. A plan
    // error leaves the lane unplanned; its serial tick reports the error.
    for &handle in handles {
        if let Some(Some((Qwen35SlotWork::Decode(state), _, installed))) =
            slots.get_mut(handle.slot_id.0 as usize)
        {
            if *installed == handle && state.speculation_route() == QwenSpeculationRoute::Repeatable
            {
                let _ = state.plan_tick();
            }
        }
    }
    let mut refusals = Vec::new();
    let mut ready = 0usize;
    let Some(JointPartition { groups, mut serial }) = ({
        let kv = guard.kv.as_ref().expect("kv Some during loop");
        // Joint MTP: every joint-ready lane joins (a speculating lane as a
        // window), a lone one included, so no request ever runs a serial
        // speculation round.
        let solo = config.solo || config.joint_mtp;
        partition_qwen35_joint_lanes(handles, config.max_rows, solo, |handle| {
            let Some((Qwen35SlotWork::Decode(state), reply, installed)) = slots
                .get(handle.slot_id.0 as usize)
                .and_then(Option::as_ref)
            else {
                refusals.push(Qwen35LaneRefusal::HandleMismatch);
                return None;
            };
            if *installed != handle {
                refusals.push(Qwen35LaneRefusal::HandleMismatch);
                return None;
            }
            if reply.client_closed() {
                refusals.push(Qwen35LaneRefusal::ClientClosed);
                return None;
            }
            if let Err(reason) = state.joint_decode_readiness(kv) {
                refusals.push(reason);
                return None;
            }
            let class = if config.joint_mtp {
                Some(true)
            } else {
                state.joint_lane_class()
            };
            if class.is_some() {
                ready += 1;
            } else {
                refusals.push(Qwen35LaneRefusal::SpeculationRoute);
            }
            class
        })
    }) else {
        return Qwen35JointRun::NotEligible(Qwen35JointIneligible::TooFewReady { ready, refusals });
    };

    let mut groups = groups.into_iter();
    while let Some(group) = groups.next() {
        // A capture opened by an earlier group's settlement would make this
        // group's transaction refuse; its lanes and the rest decode serially.
        if guard
            .kv
            .as_ref()
            .expect("kv Some during loop")
            .la_capture_active()
        {
            serial.extend(group);
            serial.extend(groups.by_ref().flatten());
            break;
        }
        let run = if config.joint_mtp {
            joint_mtp::run_qwen35_joint_mtp_group
        } else {
            run_qwen35_joint_group
        };
        if let Some(fatal) = run(
            guard,
            scheduler,
            slots,
            retained_tokens,
            prompt_caches,
            prompt_anchors,
            registration,
            group,
            serial.len(),
            handles.len(),
            decode_ready_lanes,
            kv_bytes_per_token,
            config,
            supervisor,
        ) {
            return Qwen35JointRun::Fatal(fatal);
        }
    }
    Qwen35JointRun::Ran { serial }
}

/// Take `group`'s decode lanes out of `slots` (every lane installed with its
/// scheduled handle), or put everything back and fail.
fn take_joint_lanes(
    slots: &mut [Option<Qwen35Slot>],
    group: Vec<SlotHandle>,
) -> Result<Vec<JointLane>, Box<Qwen35FatalFailure>> {
    let mut lanes = Vec::with_capacity(group.len());
    for handle in group {
        let index = handle.slot_id.0 as usize;
        match slots[index].take() {
            Some((Qwen35SlotWork::Decode(state), reply, installed)) if installed == handle => {
                lanes.push(JointLane {
                    handle,
                    reply,
                    state,
                    tick: None,
                });
            }
            other => {
                slots[index] = other;
                reinstall_lanes(slots, lanes);
                return Err(Box::new(Qwen35FatalFailure {
                    handle: Some(handle),
                    reply: Qwen35FatalReply::None,
                    error: anyhow::anyhow!("Qwen joint decode lane changed before execution"),
                    kind: Qwen35WorkerFatalKind::Invariant,
                }));
            }
        }
    }
    Ok(lanes)
}

/// One joint step over `group` (sorted by slot, 1..=`max_rows` lanes), then
/// settle each lane with the serial path's slot semantics.
#[allow(clippy::too_many_arguments)]
fn run_qwen35_joint_group(
    guard: &mut Qwen35KvGuard<'_>,
    scheduler: &mut InflightBatchedScheduler,
    slots: &mut [Option<Qwen35Slot>],
    retained_tokens: &mut [Qwen35RetainedPrefix],
    prompt_caches: &mut [PromptCache],
    prompt_anchors: &mut [Qwen35AnchorStore],
    registration: Option<&ModelRegistration>,
    group: Vec<SlotHandle>,
    serial_lanes: usize,
    turn_lanes: usize,
    decode_ready_lanes: usize,
    kv_bytes_per_token: u64,
    config: Qwen35JointDecodeConfig,
    supervisor: &EngineSupervisor,
) -> Option<Qwen35FatalFailure> {
    let mut lanes = match take_joint_lanes(slots, group) {
        Ok(lanes) => lanes,
        Err(fatal) => return Some(*fatal),
    };

    let _stage_round =
        crate::inference::stage_profile::Round::begin("qwen35", "joint", lanes.len());
    let _critical_path_span = if critical_path::enabled() {
        critical_path::Span::decode(
            "joint",
            lanes
                .iter()
                .map(|lane| (lane.handle.slot_id, lane.state.joint_verify_eligibility())),
        )
    } else {
        None
    };

    for lane in &mut lanes {
        // The joint row serves the lane's ordinary tick; drop its plan.
        lane.state.consume_planned_tick();
        tracing::debug!(
            target: "hf2q::serve::api::engine_qwen35::progress",
            slot = lane.handle.slot_id.0,
            kind = "ordinary",
            path = "joint",
            turn_lanes,
            "Qwen decode tick route"
        );
    }

    static FIRST_JOINT_DECODE: Once = Once::new();
    FIRST_JOINT_DECODE.call_once(|| {
        tracing::info!(
            target: "hf2q::serve::api::engine_qwen35::progress",
            lanes = lanes.len(),
            mode = config.mode.label(),
            dense_ffn = config.dense_ffn.label(),
            "Qwen joint decode engaged"
        );
    });

    // E3 diagnostic: draft only after the scheduler has formed a real joint
    // group, then discard proposer state before the ordinary target forward.
    if lanes.len() >= 2 {
        for lane in &mut lanes {
            if let Err(error) = lane.state.shadow_draft_before_joint(
                guard.model,
                guard.kv.as_mut().expect("kv Some during loop"),
                supervisor,
            ) {
                lane.tick = Some(Err(error));
            }
        }
    }

    // Prepare each lane exactly as its serial tick would before the forward.
    let mut prepared: Vec<(usize, Qwen35JointLane)> = Vec::with_capacity(lanes.len());
    {
        let kv = guard.kv.as_ref().expect("kv Some during loop");
        for (index, lane) in lanes.iter_mut().enumerate() {
            if lane.tick.is_some() {
                continue;
            }
            match lane.state.prepare_joint_tick(kv) {
                Ok(joint_lane) => prepared.push((index, joint_lane)),
                Err(error) => lane.tick = Some(Err(error)),
            }
        }
    }
    tracing::debug!(
        target: "hf2q::serve::api::engine_qwen35::progress",
        lanes = lanes.len(),
        rows = prepared.len(),
        serial = serial_lanes,
        path = "joint",
        turn_lanes,
        "Qwen joint decode turn"
    );

    if !prepared.is_empty() {
        let rows = prepared
            .iter()
            .map(|(_, lane)| lane.row)
            .collect::<Vec<_>>();
        let _native_invocation = scheduler.trace_transaction(
            "qwen35",
            "joint_ordinary_decode",
            &prepared
                .iter()
                .map(|&(index, _)| lanes[index].handle)
                .collect::<Vec<_>>(),
        );
        let forward = guard.model.forward_joint_decode(
            &rows,
            guard.kv.as_mut().expect("kv Some during loop"),
            config.mode,
            config.dense_ffn,
            config.step,
            supervisor,
        );
        match forward {
            Ok(outputs) => {
                record_c2_decode_lane_steps(decode_ready_lanes, prepared.len(), 0, "ordinary", "joint");
                for ((index, joint_lane), output) in prepared.into_iter().zip(outputs) {
                    let lane = &mut lanes[index];
                    lane.tick = Some(lane.state.finish_joint_tick(
                        guard.model,
                        guard.kv.as_mut().expect("kv Some during loop"),
                        supervisor,
                        joint_lane.target,
                        output,
                    ));
                }
            }
            Err(error) if !supervisor.is_healthy() || is_fatal_command_buffer_error(&error) => {
                let mut owned = lanes.into_iter();
                let first = owned.next().expect("a joint step has at least one lane");
                reinstall_lanes(slots, owned);
                return Some(Qwen35FatalFailure {
                    handle: Some(first.handle),
                    reply: Qwen35FatalReply::Slot(first.reply),
                    error,
                    kind: Qwen35WorkerFatalKind::Gpu,
                });
            }
            Err(error) => {
                // Every prepared row shared the failed transaction, so each
                // fails exactly as its own serial forward failure would;
                // rows outside the step are untouched.
                let indices = prepared.iter().map(|&(index, _)| index).collect::<Vec<_>>();
                for (index, tick) in indices
                    .iter()
                    .zip(distribute_step_error(error, indices.len()))
                {
                    lanes[*index].tick = Some(tick);
                }
            }
        }
    }

    let mut owned = lanes.into_iter();
    while let Some(lane) = owned.next() {
        let tick = lane
            .tick
            .unwrap_or_else(|| Err(anyhow::anyhow!("Qwen joint lane produced no tick")));
        if let Some(fatal) = settle_qwen35_decode_lane(
            guard,
            scheduler,
            slots,
            retained_tokens,
            prompt_caches,
            prompt_anchors,
            registration,
            lane.handle,
            lane.reply,
            lane.state,
            tick,
            kv_bytes_per_token,
            supervisor,
        ) {
            reinstall_lanes(slots, owned);
            return Some(fatal);
        }
    }
    None
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::serve::multi_seq_kv::SlotId;

    fn handle(slot: u32, generation: u64) -> SlotHandle {
        SlotHandle {
            slot_id: SlotId(slot),
            generation,
        }
    }

    #[test]
    fn joint_decode_switch_parses_explicit_arms() {
        assert_eq!(parse_qwen_joint_decode(None), Ok(None));
        assert_eq!(parse_qwen_joint_decode(Some("  ")), Ok(None));
        for raw in ["1", "on", "TRUE"] {
            assert_eq!(parse_qwen_joint_decode(Some(raw)), Ok(Some(true)));
        }
        for raw in ["0", "Off", "false"] {
            assert_eq!(parse_qwen_joint_decode(Some(raw)), Ok(Some(false)));
        }
        assert!(parse_qwen_joint_decode(Some("joint")).is_err());
    }

    #[test]
    fn joint_solo_switch_defaults_on_and_parses_explicit_arms() {
        assert_eq!(parse_qwen_joint_solo(None), Ok(true));
        assert_eq!(parse_qwen_joint_solo(Some(" ")), Ok(true));
        for raw in ["1", "On", "true"] {
            assert_eq!(parse_qwen_joint_solo(Some(raw)), Ok(true));
        }
        for raw in ["0", "OFF", "false"] {
            assert_eq!(parse_qwen_joint_solo(Some(raw)), Ok(false));
        }
        assert!(parse_qwen_joint_solo(Some("solo")).is_err());
    }

    #[test]
    fn joint_mtp_defaults_only_for_eligible_dense_models() {
        assert_eq!(parse_qwen_joint_mtp(None, true), Ok(true));
        assert_eq!(parse_qwen_joint_mtp(Some(" "), true), Ok(true));
        assert_eq!(parse_qwen_joint_mtp(None, false), Ok(false));
        assert_eq!(parse_qwen_joint_mtp(Some("0"), true), Ok(false));
        assert_eq!(parse_qwen_joint_mtp(Some("1"), false), Ok(true));
        assert!(parse_qwen_joint_mtp(Some("mtp"), true).is_err());
    }

    #[test]
    fn joint_mtp_selects_mma_unless_explicitly_overridden() {
        assert_eq!(parse_qwen_joint_mma(None, false), Ok(false));
        assert_eq!(parse_qwen_joint_mma(Some(" "), false), Ok(false));
        assert_eq!(parse_qwen_joint_mma(None, true), Ok(true));
        assert_eq!(parse_qwen_joint_mma(Some(" "), true), Ok(true));
        assert_eq!(parse_qwen_joint_mma(Some("0"), true), Ok(false));
        assert_eq!(parse_qwen_joint_mma(Some("1"), false), Ok(true));
        assert!(parse_qwen_joint_mma(Some("mma"), true).is_err());
    }

    #[test]
    fn joint_width_defaults_to_four_and_accepts_two_through_eight() {
        assert_eq!(parse_qwen_joint_decode_max_rows(None), Ok(4));
        assert_eq!(parse_qwen_joint_decode_max_rows(Some(" ")), Ok(4));
        for rows in 2..=MAX_JOINT_DECODE_ROWS {
            assert_eq!(
                parse_qwen_joint_decode_max_rows(Some(&rows.to_string())),
                Ok(rows)
            );
        }
        for raw in ["0", "1", "9", "four"] {
            assert!(parse_qwen_joint_decode_max_rows(Some(raw)).is_err());
        }
    }

    #[test]
    fn refused_artifacts_and_disabled_switch_keep_serial_decode() {
        let pooled = JointStepOptions {
            pooled_outputs: true,
            ..JointStepOptions::default()
        };
        assert_eq!(
            select_qwen_joint_decode(
                true,
                4,
                Ok(JointRouteMode::Exact),
                true,
                true,
                4,
                pooled,
                true
            ),
            Some(Qwen35JointDecodeConfig {
                max_rows: 4,
                mode: JointRouteMode::Exact,
                dense_ffn: JointDenseFfnRoute::SerialBuilder,
                solo: false,
                step: pooled,
                joint_mtp: true,
            })
        );
        assert_eq!(
            select_qwen_joint_decode(
                true,
                4,
                Err("layer 3: attn_qkv is IQ4_NL, which has no proven joint decode route".into()),
                true,
                true,
                4,
                JointStepOptions::default(),
                true
            ),
            None
        );
        assert_eq!(
            select_qwen_joint_decode(
                false,
                4,
                Ok(JointRouteMode::PaddedMvExt),
                true,
                true,
                4,
                JointStepOptions::default(),
                true
            ),
            None
        );
    }

    #[test]
    fn mma_projection_is_padded_mode_only() {
        let mma = JointStepOptions {
            projection: JointProjectionKernel::ColumnInvariantMma,
            ..JointStepOptions::default()
        };
        let projection = |mode| {
            select_qwen_joint_decode(true, 4, Ok(mode), true, true, 4, mma, false)
                .expect("admitted mode")
                .step
                .projection
        };
        assert_eq!(
            projection(JointRouteMode::PaddedMvExt),
            JointProjectionKernel::ColumnInvariantMma
        );
        assert_eq!(
            projection(JointRouteMode::Exact),
            JointProjectionKernel::Canonical
        );
    }

    #[test]
    fn padded_mode_caps_joint_width_at_four() {
        for requested in 2..=MAX_JOINT_DECODE_ROWS {
            let padded = select_qwen_joint_decode(
                true,
                requested,
                Ok(JointRouteMode::PaddedMvExt),
                true,
                true,
                4,
                JointStepOptions::default(),
                false,
            )
            .expect("admitted padded mode");
            assert_eq!(padded.max_rows, requested.min(4));
            let exact = select_qwen_joint_decode(
                true,
                requested,
                Ok(JointRouteMode::Exact),
                true,
                true,
                4,
                JointStepOptions::default(),
                false,
            )
            .expect("admitted exact mode");
            assert_eq!(exact.max_rows, requested);
        }
    }

    /// The joint MTP route needs a lone lane to run as a joint step: exact
    /// mode, or the padded mode with the solo route.
    #[test]
    fn joint_mtp_requires_a_lone_lane_joint_route() {
        let joint_mtp = |mode, solo_requested, n_slots, requested| {
            select_qwen_joint_decode(
                true,
                4,
                Ok(mode),
                true,
                solo_requested,
                n_slots,
                JointStepOptions::default(),
                requested,
            )
            .expect("admitted mode")
            .joint_mtp
        };
        assert!(joint_mtp(JointRouteMode::PaddedMvExt, true, 4, true));
        assert!(joint_mtp(JointRouteMode::Exact, false, 4, true));
        assert!(!joint_mtp(JointRouteMode::PaddedMvExt, false, 4, true));
        assert!(!joint_mtp(JointRouteMode::PaddedMvExt, true, 1, true));
        assert!(!joint_mtp(JointRouteMode::PaddedMvExt, true, 4, false));
        assert!(!joint_mtp(JointRouteMode::Exact, true, 4, false));
    }

    #[test]
    fn solo_route_is_padded_mode_on_multi_slot_workers_only() {
        let solo = |mode, solo_requested, n_slots| {
            select_qwen_joint_decode(
                true,
                4,
                Ok(mode),
                true,
                solo_requested,
                n_slots,
                JointStepOptions::default(),
                false,
            )
            .expect("admitted mode")
            .solo
        };
        for n_slots in 2..=8 {
            assert!(solo(JointRouteMode::PaddedMvExt, true, n_slots));
            assert!(!solo(JointRouteMode::PaddedMvExt, false, n_slots));
            assert!(!solo(JointRouteMode::Exact, true, n_slots));
        }
        // N1: a one-slot worker never shares a turn and keeps serial decode.
        assert!(!solo(JointRouteMode::PaddedMvExt, true, 1));
    }

    /// Every handle eligible; `ordinary` lists the slots whose serial tick is
    /// the ordinary target forward.
    fn classify_by(eligible: &[u32], ordinary: &[u32]) -> impl Fn(SlotHandle) -> Option<bool> {
        let eligible = eligible.to_vec();
        let ordinary = ordinary.to_vec();
        move |handle| {
            eligible
                .contains(&handle.slot_id.0)
                .then(|| ordinary.contains(&handle.slot_id.0))
        }
    }

    #[test]
    fn ragged_eligible_lanes_join_sorted_and_the_rest_stay_serial() {
        let handles = [handle(3, 9), handle(0, 1), handle(2, 4), handle(1, 7)];
        for solo in [false, true] {
            let partition = partition_qwen35_joint_lanes(
                &handles,
                4,
                solo,
                classify_by(&[0, 1, 3], &[0, 1, 3]),
            )
            .expect("three eligible lanes form a joint step");
            assert_eq!(
                partition,
                JointPartition {
                    groups: vec![vec![handle(0, 1), handle(1, 7), handle(3, 9)]],
                    serial: vec![handle(2, 4)],
                }
            );
        }
    }

    #[test]
    fn phase_one_partition_keeps_fewer_than_two_eligible_lanes_serial() {
        let handles = [handle(0, 1), handle(1, 1)];
        assert!(
            partition_qwen35_joint_lanes(&handles, 4, false, classify_by(&[0], &[0])).is_none()
        );
        assert!(partition_qwen35_joint_lanes(&[], 4, false, |_| Some(true)).is_none());
        assert!(partition_qwen35_joint_lanes(&handles, 1, false, |_| Some(true)).is_none());
    }

    #[test]
    fn solo_partition_runs_a_lone_ordinary_lane_jointly() {
        let handles = [handle(0, 1), handle(1, 1)];
        assert_eq!(
            partition_qwen35_joint_lanes(&handles, 4, true, classify_by(&[1], &[1])),
            Some(JointPartition {
                groups: vec![vec![handle(1, 1)]],
                serial: vec![handle(0, 1)],
            })
        );
        // A lone lane that would speculate keeps its serial round.
        assert!(partition_qwen35_joint_lanes(&handles, 4, true, classify_by(&[1], &[])).is_none());
        // Two lanes join whatever their speculation state, as in phase 1.
        assert_eq!(
            partition_qwen35_joint_lanes(&handles, 4, true, classify_by(&[0, 1], &[])),
            Some(JointPartition {
                groups: vec![vec![handle(0, 1), handle(1, 1)]],
                serial: vec![],
            })
        );
        assert!(partition_qwen35_joint_lanes(&[], 4, true, |_| Some(true)).is_none());
    }

    #[test]
    fn duplicate_slots_never_form_a_joint_step() {
        let handles = [handle(0, 1), handle(1, 1), handle(0, 2)];
        for solo in [false, true] {
            assert!(partition_qwen35_joint_lanes(&handles, 4, solo, |_| Some(true)).is_none());
        }
    }

    #[test]
    fn phase_one_partition_caps_the_joint_width_and_overflow_lanes_stay_serial() {
        let handles = [handle(4, 1), handle(3, 1), handle(2, 1), handle(1, 1)];
        let partition = partition_qwen35_joint_lanes(&handles, 3, false, |_| Some(true))
            .expect("capped joint step");
        assert_eq!(
            partition,
            JointPartition {
                groups: vec![vec![handle(2, 1), handle(3, 1), handle(4, 1)]],
                serial: vec![handle(1, 1)],
            }
        );
    }

    #[test]
    fn solo_partition_puts_every_eligible_lane_in_a_group_at_any_width() {
        let handles = (0..8).rev().map(|slot| handle(slot, 1)).collect::<Vec<_>>();
        for max_rows in 2..=4 {
            for lanes in 1..=handles.len() {
                let partition =
                    partition_qwen35_joint_lanes(&handles[..lanes], max_rows, true, |_| Some(true))
                        .expect("ordinary lanes always form groups");
                assert!(
                    partition.serial.is_empty(),
                    "max_rows={max_rows} lanes={lanes}"
                );
                assert_eq!(partition.groups.len(), lanes.div_ceil(max_rows));
                let mut joined = partition.groups.concat();
                assert!(partition.groups.iter().all(|group| {
                    (1..=max_rows).contains(&group.len())
                        && group
                            .windows(2)
                            .all(|pair| pair[0].slot_id < pair[1].slot_id)
                }));
                joined.sort_by_key(|handle| handle.slot_id);
                let mut expected = handles[..lanes].to_vec();
                expected.sort_by_key(|handle| handle.slot_id);
                assert_eq!(joined, expected);
            }
        }
        // Five lanes at width 4: the lone leftover speculates, so it alone is
        // serial; groups follow scheduler order.
        let partition = partition_qwen35_joint_lanes(
            &handles[..5],
            4,
            true,
            classify_by(&[3, 4, 5, 6, 7], &[4, 5, 6, 7]),
        )
        .expect("one full group");
        assert_eq!(
            partition,
            JointPartition {
                groups: vec![vec![handle(4, 1), handle(5, 1), handle(6, 1), handle(7, 1)]],
                serial: vec![handle(3, 1)],
            }
        );
    }

    #[test]
    fn a_failed_step_fails_exactly_its_rows_with_the_original_error_first() {
        let errors = distribute_step_error::<()>(
            anyhow::anyhow!("command buffer failed").context("Qwen joint decode"),
            3,
        )
        .collect::<Vec<_>>();
        assert_eq!(errors.len(), 3);
        for error in &errors {
            let rendered = format!("{:#}", error.as_ref().expect_err("every row fails"));
            assert!(rendered.contains("command buffer failed"), "{rendered}");
            assert!(rendered.contains("Qwen joint decode"), "{rendered}");
        }
        assert_eq!(
            distribute_step_error::<()>(anyhow::anyhow!("x"), 0).count(),
            0
        );
    }
}