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
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
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
//! Qwen DFlash2 serving transaction.
//!
//! One step drafts anchor-plus-seven proposal paths for every active physical
//! slot, verifies all selected rows in one fixed width-32 target tile, commits
//! only each lane's accepted prefix, and queues semantic output for the same
//! in-turn drain used by joint MTP. DFlash2 is optional and fail-closed: a lane
//! joins only when its target cursor and persistent draft-ring cursor agree.

#[path = "dflash2_grammar_overlap.rs"]
mod grammar_overlap;

use std::sync::LazyLock;
use std::time::Instant;

use anyhow::{Context, Result};

use crate::inference::models::qwen35::joint_decode_routes::{JointDenseFfnRoute, JointRouteMode};
use crate::inference::models::qwen35::joint_step_options::JointStepOptions;
use crate::inference::models::qwen35::kv_cache::HybridKvCache;
use crate::inference::models::qwen35::verify_decision::DECISION_SLOTS;
use crate::inference::models::qwen35::{
    Qwen35JointOutput, Qwen35JointRowOutput, Qwen35JointVerify, Qwen35JointVerifyLane,
};
use crate::inference::spec_decode::dflash2::{
    adapters::{DFlash2TargetFamily, QwenDFlash2Adapter},
    route_profile,
    proposal_repetition::{DraftRepetitionPrior, HISTORY_CAPACITY},
    verify_budget::VerifyBudgetLane,
};
use crate::serve::api::engine_supervisor::EngineSupervisor;

use super::{
    plan_qwen35_verified_block, Qwen35CanonicalDecision, Qwen35DecisionSource, Qwen35DecodeState,
    PreparedDFlash2Grammar,
    Qwen35LoadedModel, Qwen35SpecSemanticState, Qwen35TickOutcome, Qwen35VerifiedBlockPlan,
    QWEN35_WORKER_TRANSACTION_TIMEOUT,
};

const DFLASH2_MAX_ROWS_PER_LANE: usize = 8;
const DFLASH2_PROPOSALS_PER_LANE: usize = DFLASH2_MAX_ROWS_PER_LANE - 1;
const _: () = assert!(HISTORY_CAPACITY == super::QWEN35_REPETITION_WINDOW);

/// Opt-in switch of the context-first commit order; exact value `1`.
pub(in crate::serve::api) const DFLASH2_CONTEXT_FIRST_ENV: &str = "HF2Q_DFLASH2_CONTEXT_FIRST";

/// Whether a step submits its draft-context commit before it encodes the
/// DeltaNet replay, as this model's routes resolved it.
pub(in crate::serve::api) fn context_first() -> bool {
    const SWITCH: route_profile::Switch = route_profile::switch(DFLASH2_CONTEXT_FIRST_ENV);
    route_profile::is_one(SWITCH)
}

/// Opt-in switch of the draft prefetch; exact value `1`.
pub(in crate::serve::api) const DFLASH2_DRAFT_PREFETCH_ENV: &str = "HF2Q_DFLASH2_DRAFT_PREFETCH";

/// Whether a step that settles acceptance on the host commits the next
/// step's draft command before it waits for its own replay, as this model's
/// routes resolved it.
pub(in crate::serve::api) fn draft_prefetch() -> bool {
    const SWITCH: route_profile::Switch = route_profile::switch(DFLASH2_DRAFT_PREFETCH_ENV);
    route_profile::is_one(SWITCH)
}

/// Switch of the GPU decision candidates of host-sampled verify rows:
/// `1` uses them, `check` computes them beside the host sampler's decision
/// over the full logits and compares the two plans (a diagnostic).
pub(in crate::serve::api) const DFLASH2_GPU_DECISIONS_ENV: &str = "HF2Q_DFLASH2_GPU_DECISIONS";

/// How a step decides the rows of its lanes that sample on the host.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub(in crate::serve::api) enum DecisionRoute {
    /// The host sampler over every row's logits.
    #[default]
    HostLogits,
    /// The decision candidates; a row they cannot decide takes the host
    /// sampler over that row's logits.
    Candidates,
    /// Both, compared; the host sampler's plan is the one used.
    Check,
}

impl DecisionRoute {
    pub(in crate::serve::api) fn label(self) -> &'static str {
        match self {
            Self::HostLogits => "host_logits",
            Self::Candidates => "candidates",
            Self::Check => "check",
        }
    }
}

fn parse_decision_route(raw: Option<&str>) -> DecisionRoute {
    match raw {
        Some("1") => DecisionRoute::Candidates,
        Some("check") => DecisionRoute::Check,
        _ => DecisionRoute::HostLogits,
    }
}

/// The decision route as this model's routes resolved it.
pub(in crate::serve::api) fn decision_route() -> DecisionRoute {
    const SWITCH: route_profile::Switch = route_profile::switch(DFLASH2_GPU_DECISIONS_ENV);
    route_profile::with_value(SWITCH, parse_decision_route)
}

/// Development diagnostic for measuring the two tail switches inside one
/// process: a positive block length `n` runs the switches that are on for
/// `n` cycles, then the stock route for `n` cycles, and so on, and marks
/// every cycle in the cycle timeline (`tail_block.on` / `tail_block.off`).
pub(in crate::serve::api) const DFLASH2_TAIL_BLOCKS_ENV: &str = "HF2Q_DFLASH2_TAIL_BLOCKS";

/// The arm of the next cycle under [`DFLASH2_TAIL_BLOCKS_ENV`]: `None` when
/// the diagnostic is off, else whether the tail switches apply.
pub(in crate::serve::api) fn tail_block_arm() -> Option<bool> {
    static BLOCK: LazyLock<Option<u64>> = LazyLock::new(|| {
        std::env::var(DFLASH2_TAIL_BLOCKS_ENV)
            .ok()
            .and_then(|value| value.parse::<u64>().ok())
            .filter(|&block| block > 0)
    });
    static CYCLE: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
    let block = (*BLOCK)?;
    let cycle = CYCLE.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
    Some((cycle / block) % 2 == 0)
}

/// Plans of a `check` run whose candidate plan was not the host sampler's.
static DECISION_MISMATCHES: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);

/// The draft call the step after this one will make, as far as this step
/// can tell: every lane that verified goes on from the last token of its
/// plan unless the plan ended it or it reached its length limit.
struct NextDraft {
    anchors: Vec<u32>,
    positions: Vec<u32>,
    maximum_rows: Vec<u32>,
    lane_order: Vec<u32>,
    gpu_target_tokens: bool,
    repetition: Option<DraftRepetitionPrior>,
}

#[derive(Clone, Copy, Debug)]
pub(in crate::serve::api) struct Qwen35DFlash2Forward {
    pub mode: JointRouteMode,
    pub dense_ffn: JointDenseFfnRoute,
    pub options: JointStepOptions,
    /// Commit the next step's draft before waiting for this step's replay
    /// ([`draft_prefetch`]).
    pub draft_prefetch: bool,
    /// How host-sampled rows are decided ([`decision_route`]).
    pub decisions: DecisionRoute,
    /// The cycle's arm under [`DFLASH2_TAIL_BLOCKS_ENV`], for its timeline
    /// mark; the two fields above already follow it.
    pub tail_block: Option<bool>,
}

struct Qwen35DFlash2Window {
    pending_token: u32,
    position: i32,
    greedy: bool,
    maximum_rows: usize,
    /// How the window's rows are decided; [`DecisionRoute::HostLogits`] for
    /// a greedy window (the GPU argmax decides it) and for a request the
    /// candidates do not represent.
    decisions: DecisionRoute,
}

impl Qwen35DFlash2Window {
    fn output(&self) -> Qwen35JointOutput {
        if self.greedy {
            Qwen35JointOutput::GreedyTokenAndHidden
        } else if self.decisions == DecisionRoute::Candidates {
            Qwen35JointOutput::Hidden
        } else {
            Qwen35JointOutput::LogitsAndHidden
        }
    }

    fn tokens(&self, proposals: &[u32], verify_rows: usize) -> Result<Vec<u32>> {
        anyhow::ensure!(
            (1..=self.maximum_rows).contains(&verify_rows),
            "Qwen DFlash2 selected {verify_rows} rows outside 1..={}",
            self.maximum_rows
        );
        anyhow::ensure!(
            verify_rows - 1 <= proposals.len(),
            "Qwen DFlash2 selected {verify_rows} rows from {} proposals",
            proposals.len()
        );
        let mut tokens = Vec::with_capacity(verify_rows);
        tokens.push(self.pending_token);
        tokens.extend_from_slice(&proposals[..verify_rows - 1]);
        Ok(tokens)
    }
}

struct Qwen35DFlash2Plan {
    plan: Qwen35VerifiedBlockPlan,
    /// Rows the decision candidates decided, and rows that had candidates
    /// but took the host sampler over their own logits.
    candidate_rows: usize,
    fallback_rows: usize,
    grammar_work: Option<super::dflash2_grammar::DFlash2GrammarWork>,
    grammar_ordinary_checked_rows: Option<usize>,
}

impl Qwen35DFlash2Plan {
    fn accepted(&self) -> usize {
        self.plan.valid_input_tokens
    }
}

enum Verified {
    Window {
        plan: Qwen35DFlash2Plan,
        tokens: Vec<u32>,
    },
    Failed(anyhow::Error),
}

impl Qwen35DecodeState {
    fn prepare_dflash2_window(
        &self,
        kv_cache: &HybridKvCache,
        draft_context: usize,
    ) -> Result<Qwen35DFlash2Window> {
        self.joint_decode_readiness(kv_cache)
            .map_err(|reason| anyhow::anyhow!("Qwen DFlash2 lane refused: {}", reason.label()))?;
        let pending_token = *self
            .generated_tokens
            .last()
            .context("Qwen DFlash2 window needs a seeded output token")?;
        let position = (self.decode_position_base + self.generated_tokens.len() - 1) as i32;
        let target_cursor = kv_cache.sequence_len_for_slot(self.slot_id)? as usize;
        anyhow::ensure!(
            draft_context == target_cursor,
            "Qwen DFlash2 slot {} draft cursor {draft_context} != target cursor {target_cursor}",
            self.slot_id.0
        );
        let remaining = self.max_tokens.saturating_sub(self.generated_tokens.len());
        anyhow::ensure!(remaining > 0, "Qwen DFlash2 window past the length limit");
        Ok(Qwen35DFlash2Window {
            pending_token,
            position,
            greedy: self.is_greedy && !self.want_logprobs,
            maximum_rows: remaining.min(DFLASH2_MAX_ROWS_PER_LANE),
            decisions: DecisionRoute::HostLogits,
        })
    }

    /// Whether the decision candidates represent this request's sampler: a
    /// host-sampled window at temperature zero with nothing between the
    /// logits and the argmax but the repetition penalty (no logit bias,
    /// top-k, top-p, min-p, seed or logprobs), whose repetition window fits
    /// the candidates' history. A grammar is admitted; the rows it
    /// constrains fall back to the host sampler one by one.
    fn decision_candidates_eligible(&self, window: &Qwen35DFlash2Window) -> bool {
        !window.greedy
            && !self.want_logprobs
            && super::is_qwen_server_speculation_exact_eligible(&self.params)
            && f64::from(self.params.temperature) < crate::serve::sampler_pure::SAMPLING_EPS
            && self.sampling_history.len() <= DECISION_SLOTS
    }

    fn gpu_greedy_acceptance_eligible(&self, window: &Qwen35DFlash2Window) -> bool {
        window.greedy
            && super::shadow_draft::verify_transforms_clear(self)
            && super::is_qwen_server_speculation_exact_eligible(&self.params)
    }

    fn plan_dflash2_window(
        &self,
        qwen: &Qwen35LoadedModel,
        window: &Qwen35DFlash2Window,
        drafts: &[u32],
        mut rows: Vec<Qwen35JointRowOutput>,
        mut prepared: Option<PreparedDFlash2Grammar>,
    ) -> Result<Qwen35DFlash2Plan> {
        self.plan_dflash2_rows(
            qwen, window, drafts, &mut rows, window.decisions, prepared.as_mut(),
        )
    }

    /// [`Self::plan_dflash2_window`] over borrowed rows with an explicit
    /// decision route (`check` plans the same rows twice).
    fn plan_dflash2_rows(
        &self,
        qwen: &Qwen35LoadedModel,
        window: &Qwen35DFlash2Window,
        drafts: &[u32],
        rows: &mut [Qwen35JointRowOutput],
        decisions: DecisionRoute,
        mut prepared: Option<&mut PreparedDFlash2Grammar>,
    ) -> Result<Qwen35DFlash2Plan> {
        anyhow::ensure!(
            rows.len() == drafts.len() + 1,
            "Qwen DFlash2 window verified {} rows for {} drafts",
            rows.len(),
            drafts.len()
        );
        let mut semantic = Qwen35SpecSemanticState::from_decode(self);
        let mut ordinary = super::dflash2_grammar::OrdinaryDFlash2Check::begin(
            prepared.as_deref(), &semantic, &self.params,
        );
        let penalty = super::effective_repetition_penalty(&self.params);
        let vocab = qwen.vocab_size;
        let (mut candidate_rows, mut fallback_rows) = (0, 0);
        let mut canonical_at = |row: usize| {
            let output = &mut rows[row];
            if window.greedy {
                let token = output
                    .greedy_token
                    .context("Qwen DFlash2 greedy row returned no argmax")?;
                return semantic.select_and_observe_with_ordinary_check(
                    qwen,
                    &self.params,
                    Qwen35DecisionSource::Argmax(token),
                    prepared.as_deref_mut(),
                    ordinary.as_mut(),
                );
            }
            let candidates = match decisions {
                DecisionRoute::HostLogits => None,
                DecisionRoute::Candidates | DecisionRoute::Check => output.decision.as_ref(),
            };
            if let Some(candidates) = candidates {
                // The sampler is a penalised argmax exactly when no grammar
                // constrains this row: none at all (and then no token table,
                // which the sampler refuses), or one still waiting for its
                // trigger (with the table the sampler checks first).
                let unconstrained = match (
                    semantic.grammar_runtime.as_ref(),
                    self.params.token_bytes.as_deref(),
                ) {
                    (None, None) => true,
                    (Some(grammar), Some(table)) => {
                        grammar.is_awaiting_trigger() && table.len() == vocab
                    }
                    _ => false,
                };
                let token = unconstrained
                    .then(|| candidates.greedy_token(&semantic.sampling_history, penalty))
                    .flatten();
                if let Some(token) = token {
                    candidate_rows += 1;
                    return semantic.select_and_observe_with_ordinary_check(
                        qwen,
                        &self.params,
                        Qwen35DecisionSource::Argmax(token),
                        prepared.as_deref_mut(),
                        ordinary.as_mut(),
                    );
                }
                fallback_rows += 1;
                let mut logits = candidates
                    .logits()
                    .context("Qwen DFlash2 decision fallback logits")?;
                return semantic.select_and_observe_with_ordinary_check(
                    qwen,
                    &self.params,
                    Qwen35DecisionSource::Logits(&mut logits),
                    prepared.as_deref_mut(),
                    ordinary.as_mut(),
                );
            }
            // The host sampler changes the logits it is given (penalty,
            // grammar mask): sample from a copy when the row is planned
            // twice.
            let mut copy;
            let logits = output
                .logits
                .as_deref_mut()
                .context("Qwen DFlash2 row returned no logits")?;
            let logits = if window.decisions == DecisionRoute::Check {
                copy = logits.to_vec();
                copy.as_mut_slice()
            } else {
                logits
            };
            semantic.select_and_observe_with_ordinary_check(
                qwen,
                &self.params,
                Qwen35DecisionSource::Logits(logits),
                prepared.as_deref_mut(),
                ordinary.as_mut(),
            )
        };
        let plan = if drafts.is_empty() {
            let decision: Qwen35CanonicalDecision = canonical_at(0)?;
            Qwen35VerifiedBlockPlan {
                output: if decision.terminal {
                    Default::default()
                } else {
                    [decision.token].into()
                },
                terminal_after_pending: decision.terminal,
                matched_drafts: 0,
                rejected_drafts: 0,
                valid_input_tokens: 1,
                carry_hidden_row: 0,
            }
        } else {
            plan_qwen35_verified_block(drafts, &mut canonical_at)?
        };
        drop(canonical_at);
        Ok(Qwen35DFlash2Plan {
            plan,
            candidate_rows,
            fallback_rows,
            grammar_work: prepared.as_deref().and_then(PreparedDFlash2Grammar::work),
            grammar_ordinary_checked_rows: ordinary.as_ref().map(|check| check.rows()),
        })
    }

    fn plan_selected_dflash2_window(
        &self,
        qwen: &Qwen35LoadedModel,
        window: &Qwen35DFlash2Window,
        tokens: &[u32],
        mut rows: Vec<Qwen35JointRowOutput>,
        mut prepared: Option<PreparedDFlash2Grammar>,
    ) -> Result<Qwen35DFlash2Plan> {
        anyhow::ensure!(!tokens.is_empty(), "Qwen DFlash2 selected no target rows");
        rows.truncate(tokens.len());
        if window.decisions != DecisionRoute::Check {
            return self.plan_dflash2_window(qwen, window, &tokens[1..], rows, prepared);
        }
        // Diagnostic: the candidates' plan beside the host sampler's over
        // the full logits. The host sampler's is the one used.
        let drafts = &tokens[1..];
        let mut candidate_prepared = prepared.clone();
        if let Some(candidate) = candidate_prepared.as_mut() {
            // Only the authoritative host plan supplies ordinary-check evidence
            // or errors; this diagnostic clone's failures are otherwise tolerated.
            candidate.omit_work_recording();
        }
        let candidate = {
            #[cfg(test)]
            let _comparison = super::anchor_exactness_tests::trace::Comparison::begin();
            self.plan_dflash2_rows(
                qwen, window, drafts, &mut rows, DecisionRoute::Candidates,
                candidate_prepared.as_mut(),
            )
        };
        let mut host = self.plan_dflash2_rows(
            qwen, window, drafts, &mut rows, DecisionRoute::HostLogits, prepared.as_mut(),
        )?;
        match candidate {
            Ok(candidate) if candidate.plan == host.plan => {
                host.candidate_rows = candidate.candidate_rows;
                host.fallback_rows = candidate.fallback_rows;
            }
            other => {
                let total =
                    DECISION_MISMATCHES.fetch_add(1, std::sync::atomic::Ordering::Relaxed) + 1;
                tracing::error!(
                    target: "hf2q::serve::api::engine_qwen35::progress",
                    slot = self.slot_id.0,
                    mismatches = total,
                    host = ?host.plan,
                    candidate = ?other.map(|plan| plan.plan),
                    "Qwen DFlash2 decision candidates disagree with the host sampler"
                );
            }
        }
        Ok(host)
    }

    fn settle_dflash2_window(&mut self, plan: Qwen35DFlash2Plan) {
        let Qwen35DFlash2Plan { plan, .. } = plan;
        self.route_evidence.record_work(
            super::decode_route_evidence::DecodeWorkRoute::DFlash2,
            plan.valid_input_tokens,
        );
        self.mtp = None;
        self.history_lookup = None;
        self.pending_speculation_output = plan.output;
        self.terminal_after_pending = plan.terminal_after_pending;
    }
}

impl Qwen35LoadedModel {
    /// Run one DFlash2 draft/verify/commit transaction over distinct physical
    /// scheduler slots. The optional runtime is always restored before return.
    pub(in crate::serve::api) fn run_dflash2_step(
        &mut self,
        kv_cache: &mut HybridKvCache,
        states: &mut [&mut Qwen35DecodeState],
        forward: Qwen35DFlash2Forward,
        supervisor: &EngineSupervisor,
        is_fatal: &dyn Fn(&anyhow::Error) -> bool,
    ) -> Result<Vec<Result<Qwen35TickOutcome>>> {
        let mut runtime = self
            .dflash2
            .take()
            .context("Qwen DFlash2 runtime is not provisioned")?;
        let result = self.run_dflash2_step_inner(
            &mut runtime,
            kv_cache,
            states,
            forward,
            supervisor,
            is_fatal,
        );
        self.dflash2 = Some(runtime);
        #[cfg(test)]
        if let Ok(outcomes) = &result {
            for (state, outcome) in states.iter().zip(outcomes) {
                if outcome.is_ok() {
                    super::anchor_exactness_tests::trace::route(state.slot_id, "DFlash2-actual-runner-completed");
                }
            }
        }
        result
    }

    fn run_dflash2_step_inner(
        &self,
        runtime: &mut crate::inference::spec_decode::dflash2::runtime::DFlash2Runtime,
        kv_cache: &mut HybridKvCache,
        states: &mut [&mut Qwen35DecodeState],
        forward: Qwen35DFlash2Forward,
        supervisor: &EngineSupervisor,
        is_fatal: &dyn Fn(&anyhow::Error) -> bool,
    ) -> Result<Vec<Result<Qwen35TickOutcome>>> {
        anyhow::ensure!(!states.is_empty(), "Qwen DFlash2 step requires a lane");
        anyhow::ensure!(
            states.len() <= runtime.workspace.max_lanes,
            "Qwen DFlash2 step has {} lanes, runtime supports {}",
            states.len(),
            runtime.workspace.max_lanes
        );
        let _stage_round =
            crate::inference::stage_profile::Round::begin("qwen35", "dflash2", states.len());
        let lanes = states.len();
        let mut ticks: Vec<Option<Result<Qwen35TickOutcome>>> = (0..lanes).map(|_| None).collect();
        let mut windows: Vec<Option<Qwen35DFlash2Window>> = Vec::with_capacity(lanes);
        for (index, state) in states.iter_mut().enumerate() {
            state.consume_planned_tick();
            let slot = state.slot_id.0 as usize;
            let prepared = runtime
                .lane_lengths()
                .get(slot)
                .context("Qwen DFlash2 slot exceeds runtime lanes")
                .and_then(|&draft_context| state.prepare_dflash2_window(kv_cache, draft_context));
            match prepared {
                Ok(mut window) => {
                    if forward.decisions != DecisionRoute::HostLogits
                        && state.decision_candidates_eligible(&window)
                    {
                        window.decisions = forward.decisions;
                    }
                    windows.push(Some(window));
                }
                Err(error) => {
                    ticks[index] = Some(Err(error));
                    windows.push(None);
                }
            }
        }
        let active = (0..lanes)
            .filter(|&index| windows[index].is_some())
            .collect::<Vec<_>>();
        if active.is_empty() {
            return Ok(finish_ticks(ticks));
        }
        let trace_timing = tracing::enabled!(
            target: "hf2q::serve::api::engine_qwen35::progress",
            tracing::Level::DEBUG
        );
        let cycle_started = trace_timing.then(Instant::now);
        let mut timeline = super::dflash2_timeline::Cycle::begin();
        let timeline_on = timeline.is_some();
        let timeline_mark = mlx_native::command_timeline::mark;
        match forward.tail_block {
            Some(true) => timeline_mark("tail_block.on"),
            Some(false) => timeline_mark("tail_block.off"),
            None => {}
        }
        // Native encode counters and this worker thread's CPU time at cycle
        // entry; the DEBUG record reports the dispatches, command buffers,
        // host waits, barriers and host CPU one cycle costs. CPU time close
        // to the cycle's wall time means the host, not the GPU, bounds it.
        let native_counters = || {
            let mut cpu = libc::timespec {
                tv_sec: 0,
                tv_nsec: 0,
            };
            // SAFETY: `cpu` is a valid, exclusively borrowed timespec.
            let cpu_us =
                if unsafe { libc::clock_gettime(libc::CLOCK_THREAD_CPUTIME_ID, &mut cpu) } == 0 {
                    cpu.tv_sec as u64 * 1_000_000 + cpu.tv_nsec as u64 / 1_000
                } else {
                    0
                };
            (
                mlx_native::dispatch_count(),
                mlx_native::commit_count(),
                mlx_native::sync_count(),
                mlx_native::barrier_count(),
                cpu_us,
            )
        };
        let counters_at_entry = trace_timing.then(native_counters);

        let physical_lanes = runtime.workspace.max_lanes;
        let mut anchors = vec![0_u32; physical_lanes];
        let mut repetition = runtime
            .proposal_repetition_enabled()
            .then(DraftRepetitionPrior::default);
        let mut temperatures = vec![0.0_f32; physical_lanes];
        // The native selector validates every physical lane before dispatch,
        // including lanes omitted from this scheduler step. Give inactive
        // lanes the smallest valid bound; their draft/verify output is ignored.
        let mut maximum_rows = vec![1_u32; physical_lanes];
        for &index in &active {
            let state = &states[index];
            let slot = state.slot_id.0 as usize;
            let window = windows[index].as_ref().expect("active window");
            anchors[slot] = window.pending_token;
            temperatures[slot] = 0.0;
            maximum_rows[slot] = u32::try_from(window.maximum_rows)?;
            if let Some(prior) = repetition.as_mut() {
                prior.set_lane(
                    slot,
                    super::effective_repetition_penalty(&state.params) as f32,
                    state.sampling_history.iter().copied(),
                )?;
            }
        }
        let positions = runtime
            .lane_lengths()
            .iter()
            .map(|&position| u32::try_from(position).context("Qwen DFlash2 position exceeds u32"))
            .collect::<Result<Vec<_>>>()?;
        let uniforms = vec![0.5_f32; physical_lanes * DFLASH2_PROPOSALS_PER_LANE];
        let gpu_target_tokens = runtime.gpu_target_tokens()
            && active
                .iter()
                .all(|&index| maximum_rows[states[index].slot_id.0 as usize] == 8);
        let target_lane_order = active
            .iter()
            .map(|&index| states[index].slot_id.0)
            .collect::<Vec<_>>();
        let adapter_config = runtime.config.clone();
        let adapter = QwenDFlash2Adapter::new(&self.model, &adapter_config)?;
        timeline_mark("draft_begin");
        let draft_started = trace_timing.then(Instant::now);
        let lease = supervisor.arm("Qwen35 DFlash2 draft", QWEN35_WORKER_TRANSACTION_TIMEOUT)?;
        let drafted = self.model.with_gpu_cache_mut(|device, registry| {
            if gpu_target_tokens {
                runtime.run_draft_for_gpu_target(
                    device,
                    registry,
                    &adapter,
                    &anchors,
                    &positions,
                    &temperatures,
                    0,
                    &maximum_rows,
                    &target_lane_order,
                    &uniforms,
                    repetition.as_ref(),
                )
            } else {
                runtime.run_draft(
                    device,
                    registry,
                    &adapter,
                    &anchors,
                    &positions,
                    &temperatures,
                    0,
                    &maximum_rows,
                    &uniforms,
                    repetition.as_ref(),
                )
            }
        });
        let drafted = lease.finish().and(drafted);
        let draft_micros = draft_started.map(|started| started.elapsed().as_micros());
        timeline_mark("draft_end");
        if let Err(error) = drafted {
            if is_fatal(&error) {
                return Err(error);
            }
            distribute_error(&mut ticks, &active, error);
            return Ok(finish_ticks(ticks));
        }
        if !gpu_target_tokens {
            let mut active_lanes = [0; 4];
            for (lane, &index) in active.iter().enumerate() {
                active_lanes[lane] = states[index].slot_id.0 as usize;
            }
            runtime.select_verify_rows(&active_lanes[..active.len()], &maximum_rows)?;
        }
        let mut tokens: Vec<Option<Vec<u32>>> = (0..lanes).map(|_| None).collect();
        if gpu_target_tokens {
            for &index in &active {
                let window = windows[index].as_ref().expect("active window");
                tokens[index] = Some(vec![window.pending_token; window.maximum_rows]);
            }
        } else {
            let proposals = runtime.proposals()?;
            let verify_rows = runtime.verify_rows()?;
            for &index in &active {
                let slot = states[index].slot_id.0 as usize;
                let proposal_start = slot * DFLASH2_PROPOSALS_PER_LANE;
                let proposal_end = proposal_start + DFLASH2_PROPOSALS_PER_LANE;
                match windows[index].as_ref().expect("active window").tokens(
                    &proposals[proposal_start..proposal_end],
                    verify_rows[slot] as usize,
                ) {
                    Ok(lane_tokens) => tokens[index] = Some(lane_tokens),
                    Err(error) => ticks[index] = Some(Err(error)),
                }
            }
        }
        let verifying = active
            .iter()
            .copied()
            .filter(|&index| tokens[index].is_some())
            .collect::<Vec<_>>();
        if verifying.is_empty() {
            return Ok(finish_ticks(ticks));
        }
        let lane_indices = verifying
            .iter()
            .map(|&index| states[index].slot_id.0 as usize)
            .collect::<Vec<_>>();
        let gpu_acceptance = gpu_target_tokens
            && verifying.iter().all(|&index| {
                states[index].gpu_greedy_acceptance_eligible(
                    windows[index].as_ref().expect("verified window"),
                )
            });
        let verify_lanes = verifying
            .iter()
            .map(|&index| {
                let state = &states[index];
                let window = windows[index].as_ref().expect("verifying window");
                Qwen35JointVerifyLane {
                    slot_id: state.slot_id,
                    position: window.position,
                    tokens: tokens[index].as_deref().expect("verifying tokens"),
                    output: window.output(),
                }
            })
            .collect::<Vec<_>>();
        let selected_rows = (trace_timing || timeline_on).then(|| {
            verify_lanes
                .iter()
                .map(|lane| lane.tokens.len())
                .sum::<usize>()
        });
        let adapter = QwenDFlash2Adapter::new(&self.model, &runtime.config)?;
        let capture = adapter.target_capture(&runtime.config, &runtime.workspace, &lane_indices)?;
        timeline_mark("verify_begin");
        let verify_started = Instant::now();
        let lease = supervisor.arm("Qwen35 DFlash2 verify", QWEN35_WORKER_TRANSACTION_TIMEOUT)?;
        // Base repetition windows of the lanes whose rows are decided from
        // candidates (none when the route is off or no lane is eligible).
        let decision_histories = verifying
            .iter()
            .map(|&index| {
                let window = windows[index].as_ref().expect("verifying window");
                (window.decisions != DecisionRoute::HostLogits)
                    .then_some(states[index].sampling_history.as_slice())
            })
            .collect::<Vec<_>>();
        let prepare_needed = grammar_overlap::needed(states, &verifying, &tokens, timeline_on);
        let mut prepared_grammars: Option<grammar_overlap::PreparedBySlot> =
            prepare_needed.then(|| std::array::from_fn(|_| Ok(None)));
        let mut prepare_result = None;
        let mut grammar_work = timeline_on.then(|| [None; 4]);
        let mut grammar_ordinary_checked_rows = timeline_on.then(|| [None; 4]);
        let mut prepare_grammar = || {
            prepare_result = Some(grammar_overlap::prepare(
                self,
                runtime,
                states,
                &verifying,
                &windows,
                &tokens,
                gpu_target_tokens,
                prepared_grammars.as_mut().expect("enabled grammar preparation"),
                timeline_on,
            ));
        };
        let host_work = if prepare_needed {
            Some(&mut prepare_grammar as &mut dyn FnMut())
        } else {
            None
        };
        let verify = if decision_histories.iter().any(Option::is_some) {
            // Never together with GPU acceptance, which needs every lane
            // greedy; candidates are for lanes that are not.
            self.model.forward_gpu_dflash2_verify_with_decisions(
                &verify_lanes,
                gpu_target_tokens.then_some(&runtime.workspace.target_tokens),
                kv_cache,
                forward.mode,
                forward.dense_ffn,
                forward.options,
                Some(capture),
                &decision_histories,
                host_work,
            )
        } else if gpu_target_tokens {
            let mut output_tail =
                |encoder: &mut mlx_native::CommandEncoder,
                 registry: &mut mlx_native::KernelRegistry,
                 device: &mlx_native::MlxDevice,
                 target_tokens: &mlx_native::MlxBuffer| {
                    runtime.encode_gpu_acceptance_and_context(
                        encoder,
                        registry,
                        device,
                        target_tokens,
                        &target_lane_order,
                        &maximum_rows,
                        &self.eos_token_ids,
                    )
                };
            self.model.forward_gpu_dflash2_verify_from_gpu_tokens(
                &verify_lanes,
                &runtime.workspace.target_tokens,
                kv_cache,
                forward.mode,
                forward.dense_ffn,
                forward.options,
                Some(capture),
                gpu_acceptance.then_some(&mut output_tail),
                host_work,
            )
        } else {
            self.model.forward_gpu_dflash2_verify(
                &verify_lanes,
                kv_cache,
                forward.mode,
                forward.dense_ffn,
                forward.options,
                Some(capture),
                host_work,
            )
        };
        drop(prepare_grammar);
        drop(decision_histories);
        let verify = lease.finish().and(verify);
        if let Some(result) = prepare_result {
            if let Err(error) = result {
                return Err(match verify {
                    Ok(_) => error.context("prepare grammar alongside DFlash2 target"),
                    Err(target) => error.context(format!(
                        "prepare grammar alongside DFlash2 target; target completion also failed: {target:#}"
                    )),
                });
            }
        } else {
            anyhow::ensure!(
                !prepare_needed || verify.is_err(),
                "DFlash2 target returned without requested grammar preparation"
            );
        }
        let verify_elapsed = verify_started.elapsed();
        let verify_micros = trace_timing.then(|| verify_elapsed.as_micros());
        timeline_mark("verify_end");
        if timeline_on {
            // Every command committed before the output head has completed:
            // a previous cycle that deferred its line can write it now.
            super::dflash2_timeline::settle_deferred();
        }
        // GPU acceptance has already committed the draft context when verify
        // returns. Any later failure must stop the worker rather than continue
        // with host and draft cursors that may differ.
        let Qwen35JointVerify {
            rows,
            hidden: _,
            pending,
        } = match verify {
            Ok(verify) => verify,
            Err(error) if gpu_acceptance || is_fatal(&error) => return Err(error),
            Err(error) => {
                distribute_error(&mut ticks, &verifying, error);
                return Ok(finish_ticks(ticks));
            }
        };
        drop(verify_lanes);
        if gpu_target_tokens {
            let proposals = runtime.proposals()?;
            let verify_rows = runtime.verify_rows()?;
            for &index in &verifying {
                let slot = states[index].slot_id.0 as usize;
                let proposal_start = slot * DFLASH2_PROPOSALS_PER_LANE;
                let proposal_end = proposal_start + DFLASH2_PROPOSALS_PER_LANE;
                let window = windows[index].as_ref().expect("verified window");
                tokens[index] = Some(
                    window
                        .tokens(
                            &proposals[proposal_start..proposal_end],
                            verify_rows[slot] as usize,
                        )
                        .with_context(|| {
                            format!("bind GPU-produced DFlash2 target tokens for slot {slot}")
                        })?,
                );
            }
        }
        let plan_started = trace_timing.then(Instant::now);
        timeline_mark("plan_begin");

        let mut accepted = Vec::with_capacity(verifying.len());
        let mut verified = Vec::with_capacity(verifying.len());
        let (mut candidate_rows, mut fallback_rows) = (0_usize, 0_usize);
        for ((&index, rows), lane_tokens) in verifying.iter().zip(rows).zip(
            verifying
                .iter()
                .map(|&index| tokens[index].take().expect("verified tokens")),
        ) {
            let slot = states[index].slot_id.0 as usize;
            let preparation = match prepared_grammars.as_mut() {
                Some(prepared) => std::mem::replace(&mut prepared[slot], Ok(None)),
                None => Ok(None),
            };
            let step = preparation
                .and_then(|prepared| {
                    states[index].plan_selected_dflash2_window(
                        self,
                        windows[index].as_ref().expect("verified window"),
                        &lane_tokens,
                        rows,
                        prepared,
                    )
                })
                .map(|mut plan| {
                    if let Some(work) = grammar_work.as_mut() {
                        work[slot] = plan.grammar_work.take();
                        grammar_ordinary_checked_rows.as_mut()
                            .expect("enabled grammar timeline")[slot] =
                            plan.grammar_ordinary_checked_rows;
                    }
                    Verified::Window {
                        plan,
                        tokens: lane_tokens,
                    }
                })
                .unwrap_or_else(Verified::Failed);
            accepted.push(match &step {
                Verified::Window { plan, .. } => {
                    candidate_rows += plan.candidate_rows;
                    fallback_rows += plan.fallback_rows;
                    plan.accepted()
                }
                Verified::Failed(_) => 0,
            });
            verified.push(step);
        }
        if gpu_acceptance {
            let (gpu_retained, gpu_accepted) = runtime.gpu_acceptance_counts()?;
            for (&index, step) in verifying.iter().zip(&verified) {
                let slot = states[index].slot_id.0 as usize;
                let (host_retained, host_accepted) = match step {
                    Verified::Window { plan, .. } => (plan.accepted(), plan.plan.matched_drafts),
                    Verified::Failed(_) => (0, 0),
                };
                anyhow::ensure!(
                    gpu_retained[slot] as usize == host_retained
                        && gpu_accepted[slot] as usize == host_accepted,
                    "Qwen DFlash2 GPU acceptance mismatch for slot {slot}: \
                     GPU retained/accepted={}/{}, host={host_retained}/{host_accepted}",
                    gpu_retained[slot],
                    gpu_accepted[slot]
                );
            }
        }
        let plan_micros = plan_started.map(|started| started.elapsed().as_micros());

        let mut retained = vec![0_u32; physical_lanes];
        for (&index, &count) in verifying.iter().zip(&accepted) {
            retained[states[index].slot_id.0 as usize] = u32::try_from(count)?;
        }
        // Context first: the draft-context commit needs only the retained
        // counts, so it goes to the GPU before the replay is encoded. The
        // GPU, idle since the output head, starts it while the host encodes
        // the replay. The commit's host checks run first, so only a failing
        // replay command can leave a submitted context without its replay.
        let commit_valid = !gpu_acceptance
            && self
                .model
                .validate_joint_verify_commit(kv_cache, &pending, &accepted)
                .is_ok();
        let prefetch = commit_valid && forward.draft_prefetch;
        let context_first = commit_valid && !prefetch && context_first();
        // The verify-budget observation reads this step's draft confidences,
        // which a prefetched draft overwrites: it runs first when the step
        // prefetches, and after both commits otherwise.
        let mut budget_observed = false;
        if prefetch {
            self.observe_dflash2_verify_budget(
                runtime,
                states,
                &verifying,
                &verified,
                verify_elapsed,
            );
            budget_observed = true;
        }
        let next_draft = if prefetch {
            next_draft(runtime, states, &verifying, &verified, &retained)
        } else {
            None
        };
        timeline_mark("target_commit_begin");
        let target_commit_started = trace_timing.then(Instant::now);
        let lease = supervisor.arm("Qwen35 DFlash2 commit", QWEN35_WORKER_TRANSACTION_TIMEOUT)?;
        // Waits this step makes that mlx-native's sync counter does not see
        // (it counts `commit_and_wait` only).
        let mut uncounted_waits = 0_u64;
        let (committed, pipelined_context) = if prefetch {
            // Prefetch: the context commit goes to the GPU first, reading
            // its own copy of the step's positions; the replay is committed
            // behind it (they share no resource, so they overlap); then the
            // next step's draft, whose ring reads Metal orders behind the
            // context commit's ring writes because the rings are tracked
            // buffers bound directly to both commands
            // (`submit_target_context_detached`). The step waits for its
            // replay and its context commit while the draft runs on. The
            // draft is committed last because a wait on a command returns
            // only once every command committed before it has completed:
            // behind the draft, the replay's wait would last until the
            // draft's end.
            let submitted = self.model.with_gpu_cache_mut(|device, registry| {
                runtime.submit_target_context_detached(device, registry, &retained)
            });
            timeline_mark("context_submitted");
            let step_lanes = verifying
                .iter()
                .map(|&index| states[index].slot_id.0 as usize)
                .collect::<Vec<_>>();
            // No early return from here to `settle_submitted_step`, which
            // follows the waits on both commands.
            let replay = match &submitted {
                Ok(_) => self
                    .model
                    .submit_joint_verify_commit(kv_cache, pending, &accepted),
                Err(_) => Err(anyhow::anyhow!(
                    "Qwen DFlash2 replay not submitted: the context commit was not"
                )),
            };
            uncounted_waits += u64::from(submitted.is_ok()) + u64::from(replay.is_ok());
            if let (Ok(_), Ok(_), Some(next)) = (&submitted, &replay, &next_draft) {
                let prefetched =
                    QwenDFlash2Adapter::new(&self.model, &runtime.config).and_then(|adapter| {
                        let temperatures = vec![0.0_f32; physical_lanes];
                        self.model.with_gpu_cache_mut(|device, registry| {
                            runtime.prefetch_draft(
                                device,
                                registry,
                                &adapter,
                                &next.anchors,
                                &next.positions,
                                &temperatures,
                                0,
                                &next.maximum_rows,
                                &next.lane_order,
                                &uniforms,
                                !next.gpu_target_tokens,
                                next.repetition.as_ref(),
                            )
                        })
                    });
                timeline_mark("draft.prefetch_committed");
                if let Err(error) = prefetched {
                    // Not a failure of this step: the next one drafts itself.
                    tracing::warn!(
                        target: "hf2q::serve::api::engine_qwen35::progress",
                        error = %format!("{error:#}"),
                        "Qwen DFlash2 draft prefetch failed"
                    );
                }
            }
            let committed = replay
                .and_then(|submitted| self.model.finish_joint_verify_commit(kv_cache, submitted));
            let context = submitted
                .and_then(|submitted| runtime.finish_target_context(submitted, &retained, false));
            let context = runtime.settle_submitted_step(committed.is_ok(), context, &step_lanes);
            (committed, Some(context))
        } else if context_first {
            let submitted = self.model.with_gpu_cache_mut(|device, registry| {
                runtime.submit_target_context(device, registry, &retained)
            });
            timeline_mark("context_submitted");
            uncounted_waits += u64::from(submitted.is_ok());
            let step_lanes = verifying
                .iter()
                .map(|&index| states[index].slot_id.0 as usize)
                .collect::<Vec<_>>();
            // The replay and the context commit overlap on the GPU; they
            // share no resource, and `settle_context_first` waits for the
            // context command itself after the replay's wait, also when the
            // replay failed. No early return may come between the
            // submission above and that call.
            let (committed, context) =
                runtime.settle_context_first(submitted, &retained, &step_lanes, || {
                    self.model.commit_joint_verify(kv_cache, pending, &accepted)
                });
            (committed, Some(context))
        } else {
            (
                self.model.commit_joint_verify(kv_cache, pending, &accepted),
                None,
            )
        };
        let committed = lease.finish().and(committed);
        let target_commit_micros =
            target_commit_started.map(|started| started.elapsed().as_micros());
        timeline_mark("target_commit_end");
        timeline_mark("draft_commit_begin");
        let draft_commit_started = trace_timing.then(Instant::now);
        if let Err(error) = committed {
            if let Some(Err(cleanup)) = &pipelined_context {
                // The step failed after its context commit was submitted and
                // the draft rings could not be waited for or cleared: stop
                // the worker rather than draft from them.
                return Err(error.context(format!(
                    "Qwen DFlash2 draft rings left unsettled by a failed step: {cleanup:#}"
                )));
            }
            if gpu_acceptance || is_fatal(&error) {
                return Err(error);
            }
            distribute_error(&mut ticks, &verifying, error);
            return Ok(finish_ticks(ticks));
        }
        let context_committed = match pipelined_context {
            Some(result) => result,
            None => {
                let lease = supervisor.arm(
                    "Qwen35 DFlash2 target-context commit",
                    QWEN35_WORKER_TRANSACTION_TIMEOUT,
                )?;
                let context_committed = if gpu_acceptance {
                    runtime.settle_gpu_context_lengths(&retained)
                } else {
                    self.model.with_gpu_cache_mut(|device, registry| {
                        runtime.commit_target_context(device, registry, &retained)
                    })
                };
                lease.finish().and(context_committed)
            }
        };
        let draft_commit_micros = draft_commit_started.map(|started| started.elapsed().as_micros());
        timeline_mark("draft_commit_end");
        if let Err(error) = context_committed {
            if gpu_acceptance || is_fatal(&error) {
                return Err(error);
            }
            distribute_error(&mut ticks, &verifying, error);
            return Ok(finish_ticks(ticks));
        }
        if !budget_observed {
            self.observe_dflash2_verify_budget(
                runtime,
                states,
                &verifying,
                &verified,
                verify_elapsed,
            );
        }
        for ((&index, step), &count) in verifying.iter().zip(&verified).zip(&accepted) {
            if let Verified::Window { tokens, .. } = step {
                let slot = states[index].slot_id.0 as usize;
                runtime
                    .record_lane_tokens(slot, &tokens[..count])
                    .with_context(|| format!("bind DFlash2 slot {slot} retained token prefix"))?;
            }
        }
        if let (Some(started), Some(entry)) = (cycle_started, counters_at_entry) {
            let now = native_counters();
            tracing::debug!(
                target: "hf2q::serve::api::engine_qwen35::progress",
                active_lanes = verifying.len(),
                selected_rows = selected_rows.unwrap_or_default(),
                accepted_rows = accepted.iter().sum::<usize>(),
                gpu_acceptance,
                draft_us = draft_micros.unwrap_or_default(),
                target_verify_us = verify_micros.unwrap_or_default(),
                host_plan_us = plan_micros.unwrap_or_default(),
                target_commit_us = target_commit_micros.unwrap_or_default(),
                draft_commit_us = draft_commit_micros.unwrap_or_default(),
                total_us = started.elapsed().as_micros(),
                dispatches = now.0.wrapping_sub(entry.0),
                command_buffers = now.1.wrapping_sub(entry.1),
                host_waits = now.2.wrapping_sub(entry.2) + uncounted_waits,
                barriers = now.3.wrapping_sub(entry.3),
                host_cpu_us = now.4.wrapping_sub(entry.4),
                candidate_rows,
                fallback_rows,
                prefetched_drafts = runtime.prefetch_counts().prefetched,
                adopted_drafts = runtime.prefetch_counts().adopted,
                discarded_drafts = runtime.prefetch_counts().discarded,
                "Qwen DFlash2 stage timing"
            );
        }
        if let Some(mut timeline) = timeline.take() {
            if runtime.prefetch_outstanding() {
                timeline.defer_line();
            }
            timeline.finish(super::dflash2_timeline::CycleSummary {
                active_lanes: verifying.len(),
                selected_rows: selected_rows.unwrap_or_default(),
                accepted_rows: accepted.iter().sum::<usize>(),
                gpu_acceptance,
                grammar_work: grammar_work.expect("enabled grammar timeline"),
                grammar_ordinary_checked_rows:
                    grammar_ordinary_checked_rows.expect("enabled grammar timeline"),
            });
        }

        for (&index, step) in verifying.iter().zip(verified) {
            ticks[index] = Some(match step {
                Verified::Window { plan, tokens } => {
                    let drafted = tokens.len().saturating_sub(1);
                    tracing::debug!(
                        target: "hf2q::serve::api::engine_qwen35::progress",
                        slot = states[index].slot_id.0,
                        drafted,
                        accepted = plan.accepted(),
                        "Qwen DFlash2 verified window"
                    );
                    states[index].settle_dflash2_window(plan);
                    states[index].joint_mtp_drain_tick(self)
                }
                Verified::Failed(error) => Err(error),
            });
        }
        Ok(finish_ticks(ticks))
    }
}

impl Qwen35LoadedModel {
    /// Feed a verified step to the MoE target's verify-row budget. Reads the
    /// step's draft confidences from the runtime's workspace.
    fn observe_dflash2_verify_budget(
        &self,
        runtime: &mut crate::inference::spec_decode::dflash2::runtime::DFlash2Runtime,
        states: &[&mut Qwen35DecodeState],
        verifying: &[usize],
        verified: &[Verified],
        verify_elapsed: std::time::Duration,
    ) {
        if self.model.cfg.moe.is_none() {
            return;
        }
        let mut observations = [VerifyBudgetLane::default(); 4];
        let mut observed = 0;
        for (&index, step) in verifying.iter().zip(verified) {
            if let Verified::Window { plan, tokens } = step {
                observations[observed] = VerifyBudgetLane {
                    lane_index: states[index].slot_id.0 as usize,
                    verify_rows: tokens.len(),
                    accepted: plan.plan.matched_drafts,
                    labels: true,
                };
                observed += 1;
            }
        }
        if observed == verifying.len() {
            if let Err(error) = runtime.observe_verify_budget(
                &observations[..observed],
                verify_elapsed.as_secs_f64() * 1000.0,
            ) {
                tracing::warn!(
                    target: "hf2q::serve::api::engine_qwen35::progress",
                    error = %error,
                    "Qwen DFlash2 verify-budget observation discarded"
                );
            }
        }
    }
}

/// The draft call of the step after one whose lanes `verifying` planned
/// `verified` and retain `retained` rows; `None` when a lane failed or no
/// lane goes on. A lane goes on from the last token of its plan unless the
/// plan ended on a terminal decision or the lane reached its length limit;
/// anything else that ends or changes a lane before the next step (a stop
/// string, a closed client, a new lane) makes that step's inputs differ
/// from these, and the runtime then drops the prefetched command.
fn next_draft(
    runtime: &crate::inference::spec_decode::dflash2::runtime::DFlash2Runtime,
    states: &[&mut Qwen35DecodeState],
    verifying: &[usize],
    verified: &[Verified],
    retained: &[u32],
) -> Option<NextDraft> {
    let physical_lanes = runtime.workspace.max_lanes;
    let mut next = NextDraft {
        anchors: vec![0; physical_lanes],
        positions: Vec::with_capacity(physical_lanes),
        maximum_rows: vec![1; physical_lanes],
        lane_order: Vec::with_capacity(verifying.len()),
        gpu_target_tokens: false,
        repetition: runtime
            .proposal_repetition_enabled()
            .then(DraftRepetitionPrior::default),
    };
    for (&length, &retained) in runtime.lane_lengths().iter().zip(retained) {
        next.positions
            .push(u32::try_from(length.checked_add(retained as usize)?).ok()?);
    }
    for (&index, step) in verifying.iter().zip(verified) {
        let Verified::Window { plan, .. } = step else {
            return None;
        };
        let state = &states[index];
        let generated = state.generated_tokens.len() + plan.plan.output.len();
        let remaining = state.max_tokens.saturating_sub(generated);
        let (false, Some(&anchor), true) = (
            plan.plan.terminal_after_pending,
            plan.plan.output.back(),
            remaining > 0,
        ) else {
            continue;
        };
        let slot = state.slot_id.0;
        {
            if let Some(prior) = next.repetition.as_mut() {
                prior
                    .set_lane(
                        slot as usize,
                        super::effective_repetition_penalty(&state.params) as f32,
                        state
                            .sampling_history
                            .iter()
                            .chain(plan.plan.output.iter())
                            .copied(),
                    )
                    .ok()?;
            }
            next.anchors[slot as usize] = anchor;
        };
        next.maximum_rows[slot as usize] = remaining.min(DFLASH2_MAX_ROWS_PER_LANE) as u32;
        next.lane_order.push(slot);
    }
    if next.lane_order.is_empty() {
        return None;
    }
    next.gpu_target_tokens = runtime.gpu_target_tokens()
        && next
            .lane_order
            .iter()
            .all(|&slot| next.maximum_rows[slot as usize] == 8);
    Some(next)
}

fn distribute_error(
    ticks: &mut [Option<Result<Qwen35TickOutcome>>],
    indices: &[usize],
    error: anyhow::Error,
) {
    let message = format!("{error:#}");
    let mut first = Some(error);
    for &index in indices {
        ticks[index] = Some(Err(first
            .take()
            .unwrap_or_else(|| anyhow::anyhow!("{message}"))));
    }
}

fn finish_ticks(ticks: Vec<Option<Result<Qwen35TickOutcome>>>) -> Vec<Result<Qwen35TickOutcome>> {
    ticks
        .into_iter()
        .map(|tick| {
            tick.unwrap_or_else(|| Err(anyhow::anyhow!("Qwen DFlash2 lane produced no tick")))
        })
        .collect()
}

#[cfg(test)]
#[path = "dflash2_decision_tests.rs"]
mod decision_tests;

#[cfg(test)]
mod tests {
    use std::time::Duration;

    use super::super::speculation_qualification_tests::loaded_model;
    use super::*;
    use crate::serve::api::engine::SamplingParams;
    use crate::serve::api::qwen35_speculation::QwenSpeculationPolicy;
    use crate::serve::multi_seq_kv::SlotId;

    fn decode_state(qwen: &Qwen35LoadedModel, max_tokens: usize) -> Qwen35DecodeState {
        let prompt = vec![1, 2, 3, 4];
        let mut logits = vec![0.0_f32; qwen.vocab_size];
        logits[8] = 10.0;
        let mut state = Qwen35DecodeState::from_prefill_logits(
            qwen,
            prompt.clone(),
            SamplingParams {
                max_tokens,
                ..SamplingParams::default()
            },
            None,
            SlotId(0),
            0,
            &logits,
            Duration::ZERO,
            prompt.len(),
            None,
        )
        .expect("seed decode state");
        state.history_lookup = None;
        state
    }

    #[test]
    fn anchor_only_grammar_window_is_observed_without_enabling_ordinary_preparation() {
        let qwen = loaded_model(QwenSpeculationPolicy::Off);
        let mut state = decode_state(&qwen, 2);
        let grammar = crate::serve::api::grammar::parse("root ::= \"a\"\n").unwrap();
        let root = grammar.rule_id("root").unwrap();
        state.grammar_runtime =
            Some(crate::serve::api::grammar::GrammarRuntime::new(grammar, root).unwrap());
        state.params.token_bytes = Some(std::sync::Arc::new(
            crate::serve::api::token_text::token_bytes_table(&qwen.tokenizer, qwen.vocab_size),
        ));
        let states = [&mut state];
        let anchor_only = [Some(vec![8])];
        assert!(!grammar_overlap::needed(&states, &[0], &anchor_only, false));
        assert!(grammar_overlap::needed(&states, &[0], &anchor_only, true));
        let work = states[0].prepare_dflash2_grammar(&qwen, &[], true)
            .unwrap().unwrap().work().unwrap();
        assert_eq!(work.actual_proposals, 0);
        assert!(work.attempts.iter().all(Option::is_none));
        assert!(grammar_overlap::needed(&states, &[0], &[Some(vec![8, 9])], false));
        states[0].grammar_runtime = None;
        for collect_work in [false, true] {
            assert!(!grammar_overlap::needed(&states, &[0], &anchor_only, collect_work));
        }
    }

    fn greedy(token: u32) -> Qwen35JointRowOutput {
        Qwen35JointRowOutput {
            greedy_token: Some(token),
            logits: None,
            nextn_hidden: None,
            decision: None,
        }
    }

    fn logits(token: usize, vocab: usize) -> Qwen35JointRowOutput {
        let mut values = vec![-10.0_f32; vocab];
        values[token] = 10.0;
        Qwen35JointRowOutput {
            greedy_token: None,
            logits: Some(values),
            nextn_hidden: None,
            decision: None,
        }
    }

    #[test]
    fn target_rejection_commits_matching_prefix_and_canonical_replacement() {
        let qwen = loaded_model(QwenSpeculationPolicy::Off);
        let state = decode_state(&qwen, 8);
        let window = Qwen35DFlash2Window {
            pending_token: 8,
            position: 4,
            greedy: true,
            maximum_rows: 4,
            decisions: DecisionRoute::HostLogits,
        };
        let plan = state
            .plan_dflash2_window(
                &qwen,
                &window,
                &[9, 10, 55],
                vec![greedy(9), greedy(10), greedy(11), greedy(12)],
                None,
            )
            .expect("plan DFlash2 rejection");
        assert_eq!(
            plan.plan.output.into_iter().collect::<Vec<_>>(),
            [9, 10, 11]
        );
        assert_eq!(plan.plan.matched_drafts, 2);
        assert_eq!(plan.plan.rejected_drafts, 1);
        assert_eq!(plan.plan.valid_input_tokens, 3);
    }

    #[test]
    fn full_acceptance_includes_target_bonus_and_all_input_rows() {
        let qwen = loaded_model(QwenSpeculationPolicy::Off);
        let state = decode_state(&qwen, 8);
        let window = Qwen35DFlash2Window {
            pending_token: 8,
            position: 4,
            greedy: true,
            maximum_rows: 4,
            decisions: DecisionRoute::HostLogits,
        };
        let plan = state
            .plan_dflash2_window(
                &qwen,
                &window,
                &[9, 10, 11],
                vec![greedy(9), greedy(10), greedy(11), greedy(12)],
                None,
            )
            .expect("plan DFlash2 full acceptance");
        assert_eq!(
            plan.plan.output.into_iter().collect::<Vec<_>>(),
            [9, 10, 11, 12]
        );
        assert_eq!(plan.plan.matched_drafts, 3);
        assert_eq!(plan.plan.rejected_drafts, 0);
        assert_eq!(plan.plan.valid_input_tokens, 4);
    }
    #[test]
    fn selected_gpu_budget_excludes_unverified_target_rows() {
        let qwen = loaded_model(QwenSpeculationPolicy::Off);
        let state = decode_state(&qwen, 8);
        let window = Qwen35DFlash2Window {
            pending_token: 8,
            position: 4,
            greedy: true,
            maximum_rows: 8,
            decisions: DecisionRoute::HostLogits,
        };
        let plan = state
            .plan_selected_dflash2_window(
                &qwen,
                &window,
                &[8, 9, 10],
                vec![
                    greedy(9),
                    greedy(10),
                    greedy(11),
                    greedy(99),
                    greedy(99),
                    greedy(99),
                    greedy(99),
                    greedy(99),
                ],
                None,
            )
            .expect("plan selected GPU DFlash2 rows");
        assert_eq!(
            plan.plan.output.into_iter().collect::<Vec<_>>(),
            [9, 10, 11]
        );
        assert_eq!(plan.plan.valid_input_tokens, 3);
    }

    #[test]
    fn gpu_acceptance_requires_transform_free_exact_greedy_semantics() {
        let qwen = loaded_model(QwenSpeculationPolicy::Off);
        let mut state = decode_state(&qwen, 8);
        let window = Qwen35DFlash2Window {
            pending_token: 8,
            position: 4,
            greedy: true,
            maximum_rows: 8,
            decisions: DecisionRoute::HostLogits,
        };
        assert!(state.gpu_greedy_acceptance_eligible(&window));

        state.params.repetition_penalty = 1.05;
        assert!(!state.gpu_greedy_acceptance_eligible(&window));

        state.params.repetition_penalty = 1.0;
        state.params.stop_strings.push("stop".to_owned());
        assert!(!state.gpu_greedy_acceptance_eligible(&window));
    }

    #[test]
    fn terminal_draft_is_not_streamed_or_retained() {
        let qwen = loaded_model(QwenSpeculationPolicy::Off);
        let state = decode_state(&qwen, 8);
        let window = Qwen35DFlash2Window {
            pending_token: 8,
            position: 4,
            greedy: true,
            maximum_rows: 4,
            decisions: DecisionRoute::HostLogits,
        };
        let plan = state
            .plan_dflash2_window(
                &qwen,
                &window,
                &[9, 63, 11],
                vec![greedy(9), greedy(63), greedy(11), greedy(12)],
                None,
            )
            .expect("plan DFlash2 terminal draft");
        assert_eq!(plan.plan.output.into_iter().collect::<Vec<_>>(), [9]);
        assert_eq!(plan.plan.matched_drafts, 2);
        assert_eq!(plan.plan.rejected_drafts, 0);
        assert_eq!(plan.plan.valid_input_tokens, 2);
        assert!(plan.plan.terminal_after_pending);
    }

    #[test]
    fn logprob_request_uses_full_logits_for_each_canonical_decision() {
        let qwen = loaded_model(QwenSpeculationPolicy::Off);
        let mut state = decode_state(&qwen, 8);
        state.want_logprobs = true;
        let window = Qwen35DFlash2Window {
            pending_token: 8,
            position: 4,
            greedy: false,
            maximum_rows: 4,
            decisions: DecisionRoute::HostLogits,
        };
        let plan = state
            .plan_dflash2_window(
                &qwen,
                &window,
                &[9, 10, 55],
                vec![
                    logits(9, qwen.vocab_size),
                    logits(10, qwen.vocab_size),
                    logits(11, qwen.vocab_size),
                    logits(12, qwen.vocab_size),
                ],
                None,
            )
            .expect("plan DFlash2 logprob rejection");
        assert_eq!(
            plan.plan.output.into_iter().collect::<Vec<_>>(),
            [9, 10, 11]
        );
        assert_eq!(plan.plan.valid_input_tokens, 3);
    }

    #[test]
    fn ordinary_plan_check_requires_recording_and_deterministic_sampling() {
        let qwen = loaded_model(QwenSpeculationPolicy::Off);
        let eos = qwen.eos_token_ids[0];
        for (record, temperature, expected_rows) in [
            (true, 0.0, Some(1)), (false, 0.0, None), (true, 0.6, None),
        ] {
            let mut state = decode_state(&qwen, 2);
            state.params.temperature = temperature;
            let grammar = crate::serve::api::grammar::parse("root ::= \"a\"*\n").unwrap();
            let root = grammar.rule_id("root").unwrap();
            state.params.grammar = Some(grammar.clone());
            state.grammar_runtime =
                Some(crate::serve::api::grammar::GrammarRuntime::new(grammar, root).unwrap());
            state.params.token_bytes = Some(std::sync::Arc::new(
                crate::serve::api::token_text::token_bytes_table(&qwen.tokenizer, qwen.vocab_size),
            ));
            let prepared = state.prepare_dflash2_grammar(&qwen, &[], record)
                .unwrap();
            let window = Qwen35DFlash2Window {
                pending_token: 8, position: 4, greedy: false,
                maximum_rows: 1, decisions: DecisionRoute::HostLogits,
            };
            let mut values = vec![f32::NEG_INFINITY; qwen.vocab_size];
            values[eos as usize] = 0.0;
            let row = Qwen35JointRowOutput {
                greedy_token: None, logits: Some(values), nextn_hidden: None, decision: None,
            };
            let plan = state.plan_selected_dflash2_window(
                &qwen, &window, &[8], vec![row], prepared,
            ).unwrap();
            assert!(plan.plan.terminal_after_pending);
            assert_eq!(plan.grammar_ordinary_checked_rows, expected_rows);
        }
    }
}