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
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
//! Multi-Token Prediction (MTP) draft block for Qwen3.5.
//!
//! Qwen3.5 stores the single NextN/MTP block at `blk.{num_hidden_layers}`.
//! Wrapper tensors live under `blk.N.nextn.*`; the inner block itself uses
//! normal full-attention/dense-FFN tensor names at `blk.N.*`. The main verifier
//! stack never executes this block directly: speculative decoding calls
//! [`MtpWeights::forward_draft`] with the verifier hidden state and the
//! embedding of the just-accepted token.

use anyhow::{anyhow, ensure, Context, Result};
use mlx_native::ops::argmax::dispatch_argmax_f32;
use mlx_native::ops::copy::dispatch_copy_f32;
use mlx_native::ops::quantized_matmul_ggml::GgmlType;
use mlx_native::ops::rms_norm;
use mlx_native::{DType, KernelRegistry, MlxBuffer, MlxDevice};

use super::ffn::{DenseFfnShape, MoeFfnShape};
use super::gpu_ffn::{
    build_dense_ffn_layer_gpu, build_dense_ffn_layer_gpu_q, build_moe_ffn_layer_gpu_q_raw_into,
    DenseFfnWeightsGpu, DenseFfnWeightsGpuQ, MoeFfnWeightsGpuQ,
};
use super::gpu_full_attn::{
    append_kv_to_cache_without_attention, apply_imrope, apply_linear_projection_f32_with_ggml_type,
    apply_q_gate_projection_f32, apply_q_or_k_per_head_rms_norm, apply_sdpa_with_kv_cache,
    apply_sdpa_with_kv_cache_decode_into, apply_sigmoid_gate_multiply, FullAttnQGateWeightsGpu,
};
use super::joint_decode_core::row_view;
use super::joint_projection::{JointProjectionKernel, JointProjectionScope};
use super::kv_cache::HybridKvCache;
use super::Qwen35Config;
use crate::serve::forward_mlx_shared::MlxQWeight;
use mlx_native::ops::fused_norm_add::dispatch_fused_residual_norm_f32;
#[cfg(test)]
pub(crate) use super::encoder_stage::FailSubmitted;

pub use super::mtp_weights_load::{
    load_mtp_weights_if_present, load_mtp_weights_if_present_with_shared_head,
};

/// Fully-loaded GPU MTP block. GGUF projections retain their declared native
/// representation; residual activations and logits are F32.
pub struct MtpWeights {
    pub layer_index: u32,
    pub hidden_size: u32,
    pub vocab_size: u32,
    /// For dense MTP this is the dense FFN intermediate dim. For MoE MTP
    /// it's the per-expert (moe) intermediate dim — useful for diagnostics
    /// only; dispatch consults [`MtpFfnWeightsGpu`] directly.
    pub intermediate_size: u32,
    pub(super) loaded_tensor_names: Vec<String>,
    pub(super) enorm: MlxBuffer,
    pub(super) hnorm: MlxBuffer,
    pub(super) eh_proj: MlxBuffer,
    pub(super) eh_proj_ggml_type: GgmlType,
    /// MTP token-embedding table.
    ///
    /// `Some(...)` when the GGUF carries a dedicated `blk.{N}.nextn.embed_tokens.weight`
    /// (Qwen3.5 MTP convention; HF flag `mtp_use_dedicated_embeddings == True`).
    ///
    /// `None` when the MTP block shares the main verifier's `token_embd.weight`
    /// (Qwen3.6 27B + 35B-A3B convention; HF flag `False`). At draft time the
    /// caller of `forward_draft` already supplies the embedding (`embed_t`); the
    /// verifier embedding table itself lives on `Qwen35Model::token_embd` and is
    /// reused via the hot embed_tokens lookup path — no buffer duplication.
    #[allow(dead_code)]
    pub(super) embed_tokens: Option<MlxQWeight>,
    pub(super) shared_head_norm: MlxBuffer,
    pub(super) shared_head_head: MlxBuffer,
    pub(super) shared_head_head_ggml_type: GgmlType,
    pub(super) attn: MtpFullAttnWeightsGpu,
    pub(super) ffn: MtpFfnWeightsGpu,
}

/// Inner-FFN variant for the MTP block.
///
/// Qwen 3.6 27B dense-MTP target emits a SwiGLU dense FFN at the MTP block:
/// `blk.{N}.ffn_gate.weight`, `ffn_up.weight`, `ffn_down.weight`.
///
/// Qwen 3.5/3.6 35B-A3B MoE-MTP target emits the same MoE FFN schema used by
/// regular MoE layers at the MTP block: 8 tensors (`ffn_gate_inp`,
/// `ffn_gate_exps`, `ffn_up_exps`, `ffn_down_exps`, plus 4 shared-expert).
/// The MoE variant uses the production quantized path
/// ([`MoeFfnWeightsGpuQ`]) so expert weights stay native GGML blocks on Metal,
/// matching the rest of the verifier stack (no F32 expansion).
pub(super) enum MtpFfnWeightsGpu {
    /// Explicit floating-point storage, retained and executed as F32.
    Dense {
        weights: DenseFfnWeightsGpu,
        intermediate_size: u32,
    },
    /// Dense SwiGLU FFN retaining the GGUF's native quantized blocks.
    DenseQ { weights: DenseFfnWeightsGpuQ },
    /// Quantized MoE FFN (Qwen 3.5/3.6 35B-A3B MoE-MTP convention).
    Moe {
        weights: MoeFfnWeightsGpuQ,
        shape: MoeFfnShape,
    },
}

pub(super) struct MtpFullAttnWeightsGpu {
    pub(super) attn_norm: MlxBuffer,
    pub(super) post_attn_norm: MlxBuffer,
    pub(super) q_gate: MtpQGateWeightsGpu,
    pub(super) wk: MlxBuffer,
    pub(super) wk_ggml_type: GgmlType,
    pub(super) wv: MlxBuffer,
    pub(super) wv_ggml_type: GgmlType,
    pub(super) attn_q_norm: MlxBuffer,
    pub(super) attn_k_norm: MlxBuffer,
    pub(super) wo: MlxBuffer,
    pub(super) wo_ggml_type: GgmlType,
}

pub(super) enum MtpQGateWeightsGpu {
    Ungated {
        wq: MlxBuffer,
        wq_ggml_type: GgmlType,
    },
    Gated(FullAttnQGateWeightsGpu),
}

/// Test-friendly indicator for which inner-FFN variant a loaded MTP block
/// carries. Used by integration tests that need to assert the loader took
/// the dense or MoE path on a real GGUF without exposing the GPU buffers.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MtpFfnKind {
    Dense,
    Moe,
}

struct MtpDraftBody {
    hidden: MlxBuffer,
    profile_enabled: bool,
    projection_ms: f64,
    attention_ms: f64,
    ffn_ms: f64,
}

/// One lane of [`MtpWeights::forward_draft_greedy_batch`].
#[derive(Clone, Copy, Debug)]
pub struct MtpDraftLane<'a> {
    pub slot_id: crate::serve::multi_seq_kv::SlotId,
    /// `[1, H]` hidden carried into this depth: the verifier's carry row at
    /// depth 0, the previous depth's normalized MTP row after it.
    pub prev_hidden: &'a MlxBuffer,
    /// The token this depth embeds (the pending token, then each draft).
    pub token: u32,
    /// Its text position; the slot's MTP cursor must equal it.
    pub position: i32,
}

impl MtpWeights {
    /// Test-image custody for a serving draft's shared embedding producer.
    /// Drain the inherited scope before replacing it; check all local owners
    /// on either Result before the serving caller may restore or observe state.
    #[cfg(test)]
    pub(crate) fn with_checked_draft_commands<R>(run: impl FnOnce() -> Result<R>) -> Result<R> {
        super::encoder_stage::settle_submitted().context("MTP inherited original command drain")?;
        let _submitted = super::encoder_stage::SubmittedCommands::open(true);
        let encoded = run();
        super::encoder_stage::settle_submitted().context("MTP serving draft original command drain")?;
        encoded
    }

    pub fn len(&self) -> usize {
        self.loaded_tensor_names.len()
    }

    pub fn is_empty(&self) -> bool {
        self.loaded_tensor_names.is_empty()
    }

    /// Variant indicator for the inner FFN block. Mainly used by tests
    /// validating that the loader picked the right dispatch path for a
    /// given GGUF (dense for Qwen 3.6 27B; MoE for Qwen 3.5/3.6 35B-A3B).
    pub fn ffn_kind(&self) -> MtpFfnKind {
        match &self.ffn {
            MtpFfnWeightsGpu::Dense { .. } => MtpFfnKind::Dense,
            MtpFfnWeightsGpu::DenseQ { .. } => MtpFfnKind::Dense,
            MtpFfnWeightsGpu::Moe { .. } => MtpFfnKind::Moe,
        }
    }

    pub fn has_tensor_suffix(&self, suffix: &str) -> bool {
        let direct_prefix = format!("blk.{}.", self.layer_index);
        let nextn_prefix = format!("blk.{}.nextn.", self.layer_index);
        self.loaded_tensor_names.iter().any(|name| {
            name.strip_prefix(&nextn_prefix) == Some(suffix)
                || name.strip_prefix(&direct_prefix) == Some(suffix)
        })
    }

    /// Resolve the token embedding table required by this MTP artifact.
    /// Shared-table Qwen3.8 returns the caller-provided main embeddings;
    /// dedicated-table variants gather selected rows directly from their
    /// declared native representation.
    pub fn embeddings_for_tokens(
        &self,
        tokens: &[u32],
        shared_embed_rows: &MlxBuffer,
        device: &MlxDevice,
        registry: &mut KernelRegistry,
    ) -> Result<MlxBuffer> {
        ensure!(
            !tokens.is_empty(),
            "MTP embeddings: tokens must be non-empty"
        );
        let hidden_size = self.hidden_size as usize;
        ensure!(
            shared_embed_rows.element_count() == tokens.len() * hidden_size,
            "MTP embeddings: shared rows elements {} != {}",
            shared_embed_rows.element_count(),
            tokens.len() * hidden_size
        );
        let Some(dedicated) = self.embed_tokens.as_ref() else {
            return Ok(shared_embed_rows.clone());
        };
        ensure!(
            dedicated.affine.is_none() && dedicated.info.cols == hidden_size,
            "MTP dedicated embedding must be a native [vocab,{hidden_size}] GGUF tensor"
        );
        let vocab = dedicated.info.rows;
        let mut output = device
            .alloc_buffer(
                tokens.len() * hidden_size * 4,
                DType::F32,
                vec![tokens.len(), hidden_size],
            )
            .map_err(|e| anyhow!("MTP alloc dedicated embeddings: {e}"))?;
        let vocab_u32 =
            u32::try_from(vocab).context("MTP dedicated embedding vocab exceeds u32")?;
        super::forward_gpu::embed_tokens_gpu_into(
            tokens,
            &[],
            Some(dedicated),
            vocab_u32,
            self.hidden_size,
            device,
            registry,
            &mut output,
            #[cfg(test)]
            None,
        )
        .context("MTP dedicated native embedding gather")?;
        Ok(output)
    }

    /// Run the MTP block for a single-token draft step. Convenience wrapper
    /// over [`MtpWeights::forward_draft_with_hidden`] that drops the returned
    /// hidden buffer — use the `_with_hidden` variant when you intend to
    /// chain a second MTP step (K=N speculative decoding).
    ///
    /// Inputs:
    /// - `prev_hidden`: verifier hidden state for token `t`, shape `[1, H]`.
    /// - `embed_t`: embedding for accepted token `t + 1`, shape `[1, H]`.
    /// - `position_ids`: IMROPE text positions for `t + 1`, flat `[4]`.
    ///
    /// Returns draft logits for token `t + 2`, shape `[1, vocab]`, F32.
    pub fn forward_draft(
        &self,
        prev_hidden: &MlxBuffer,
        embed_t: &MlxBuffer,
        kv_cache: &mut HybridKvCache,
        slot_id: crate::serve::multi_seq_kv::SlotId,
        position_ids: &[i32],
        device: &MlxDevice,
        registry: &mut KernelRegistry,
        cfg: &Qwen35Config,
    ) -> Result<MlxBuffer> {
        let (logits, _hidden) = self.forward_draft_with_hidden(
            prev_hidden,
            embed_t,
            kv_cache,
            slot_id,
            position_ids,
            device,
            registry,
            cfg,
        )?;
        Ok(logits)
    }

    /// Artifact-aware draft entry point. Callers provide the verifier's
    /// shared embedding row, but a dedicated-table artifact replaces it with
    /// its own exact row before executing the draft block.
    #[allow(clippy::too_many_arguments)]
    pub fn forward_draft_for_token(
        &self,
        prev_hidden: &MlxBuffer,
        token: u32,
        shared_embed_t: &MlxBuffer,
        kv_cache: &mut HybridKvCache,
        slot_id: crate::serve::multi_seq_kv::SlotId,
        position_ids: &[i32],
        device: &MlxDevice,
        registry: &mut KernelRegistry,
        cfg: &Qwen35Config,
    ) -> Result<(MlxBuffer, MlxBuffer)> {
        #[cfg(test)]
        super::encoder_stage::settle_submitted().context("MTP inherited original command drain")?;
        #[cfg(test)]
        let _submitted = super::encoder_stage::SubmittedCommands::open(true);
        let encoded = (|| -> Result<(MlxBuffer, MlxBuffer)> {
        let embed = self.embeddings_for_tokens(&[token], shared_embed_t, device, registry)?;
        self.forward_draft_with_hidden(
            prev_hidden,
            &embed,
            kv_cache,
            slot_id,
            position_ids,
            device,
            registry,
            cfg,
        )
        })();
        #[cfg(test)]
        super::encoder_stage::settle_submitted().context("MTP draft original command drain")?;
        encoded
    }

    /// Greedy artifact-aware draft entry point for the server hot path.
    ///
    /// This is semantically identical to `forward_draft_for_token` followed
    /// by GPU argmax, but encodes shared-head RMSNorm, vocabulary projection,
    /// and argmax in one command buffer with a single terminal wait. The
    /// target verifier remains authoritative; this only removes an avoidable
    /// host/GPU round trip from each chained draft token.
    #[allow(clippy::too_many_arguments)]
    pub fn forward_draft_greedy_for_token(
        &self,
        prev_hidden: &MlxBuffer,
        token: u32,
        shared_embed_t: &MlxBuffer,
        kv_cache: &mut HybridKvCache,
        slot_id: crate::serve::multi_seq_kv::SlotId,
        position_ids: &[i32],
        device: &MlxDevice,
        registry: &mut KernelRegistry,
        cfg: &Qwen35Config,
    ) -> Result<(u32, MlxBuffer)> {
        #[cfg(test)]
        super::encoder_stage::settle_submitted().context("MTP inherited original command drain")?;
        #[cfg(test)]
        let _submitted = super::encoder_stage::SubmittedCommands::open(true);
        let encoded = (|| -> Result<(u32, MlxBuffer)> {
        let embed = self.embeddings_for_tokens(&[token], shared_embed_t, device, registry)?;
        let body = self.forward_draft_body(
            prev_hidden,
            &embed,
            kv_cache,
            slot_id,
            position_ids,
            device,
            registry,
            cfg,
        )?;
        let head_started = std::time::Instant::now();
        let (drafts, nextn_hidden) =
            self.forward_shared_head_greedy(&body.hidden, device, registry, cfg.rms_norm_eps)?;
        self.report_draft_profile(&body, head_started.elapsed().as_secs_f64() * 1000.0);
        Ok((drafts[0], nextn_hidden))
        })();
        #[cfg(test)]
        super::encoder_stage::settle_submitted().context("MTP draft original command drain")?;
        encoded
    }

    /// One greedy MTP draft step for several slots in one forward (joint
    /// MTP drafting, one call per draft depth). Row `i` is `lanes[i]`:
    /// its K/V is written at its slot's MTP cursor (advancing it) and it
    /// attends over its own slot through the single-row decode SDPA; the
    /// projections, norms, FFN and shared head run over the packed rows, and
    /// each row's draft is one GPU argmax over its own logits. Returns per
    /// lane its draft and its normalized hidden row, the next depth's
    /// `prev_hidden`.
    ///
    /// Deterministic. A packed row may differ in its last bits from the
    /// one-lane [`Self::forward_draft_greedy_for_token`]; drafts only decide
    /// how many rows the target accepts, never the committed tokens.
    /// `shared_embed_rows` holds the verifier's embedding of every lane's
    /// token, `[lanes, H]`.
    #[allow(clippy::too_many_arguments)]
    pub(crate) fn forward_draft_greedy_batch(
        &self,
        lanes: &[MtpDraftLane<'_>],
        shared_embed_rows: &MlxBuffer,
        kv_cache: &mut HybridKvCache,
        device: &MlxDevice,
        registry: &mut KernelRegistry,
        cfg: &Qwen35Config,
        projection: JointProjectionKernel,
    ) -> Result<Vec<(u32, MlxBuffer)>> {
        #[cfg(test)]
        super::encoder_stage::settle_submitted().context("MTP inherited original command drain")?;
        #[cfg(test)]
        let _submitted = super::encoder_stage::SubmittedCommands::open(true);
        let encoded = (|| -> Result<Vec<(u32, MlxBuffer)>> {
        ensure!(!lanes.is_empty(), "MTP batched draft needs a lane");
        let rows = lanes.len();
        let hs = self.hidden_size as usize;
        for (index, lane) in lanes.iter().enumerate() {
            ensure!(
                lane.prev_hidden.element_count() == hs,
                "MTP batched draft lane {index} prev_hidden has {} elements, expected {hs}",
                lane.prev_hidden.element_count()
            );
            ensure!(
                lanes[..index]
                    .iter()
                    .all(|earlier| earlier.slot_id != lane.slot_id),
                "MTP batched draft slot {} appears twice",
                lane.slot_id.0
            );
        }
        let tokens = lanes.iter().map(|lane| lane.token).collect::<Vec<_>>();
        let embed = self.embeddings_for_tokens(&tokens, shared_embed_rows, device, registry)?;
        let prev = device
            .alloc_buffer(rows * hs * 4, DType::F32, vec![rows, hs])
            .map_err(|e| anyhow!("MTP alloc batched prev hidden: {e}"))?;
        {
            let mut enc = device
                .command_encoder()
                .map(crate::inference::native_observation::encoder)
                .context("MTP enc batched prev hidden")?;
            for (row, lane) in lanes.iter().enumerate() {
                dispatch_copy_f32(
                    &mut enc,
                    registry,
                    device.metal_device(),
                    lane.prev_hidden,
                    &prev,
                    0,
                    row * hs,
                    hs,
                )
                .with_context(|| format!("MTP pack prev hidden row {row}"))?;
            }
            enc.commit();
            #[cfg(test)]
            super::encoder_stage::retain_submitted(enc, "mtp.draft.pack_prev_hidden");
        }
        let mut positions = vec![0_i32; 4 * rows];
        for axis in 0..4 {
            for (row, lane) in lanes.iter().enumerate() {
                positions[axis * rows + row] = lane.position;
            }
        }
        let pos_buf = upload_i32(&positions, device).context("MTP upload batched positions")?;
        let rows_u32 = u32::try_from(rows).context("MTP batched draft rows exceed u32")?;
        // Draft rows must keep the worker's selected arithmetic as lanes leave.
        let _projection = JointProjectionScope::enter_step(projection, rows_u32, false);
        let projected = self.project_embedding_and_hidden(
            &embed,
            &prev,
            rows_u32,
            device,
            registry,
            "adr065.mtp_draft",
        )?;
        let slots = lanes.iter().map(|lane| lane.slot_id).collect::<Vec<_>>();
        let attn_out = self.forward_full_attention_rows(
            &projected, &pos_buf, kv_cache, &slots, device, registry, cfg,
        )?;
        let hidden = self.forward_ffn_residual(&projected, &attn_out, device, registry, cfg)?;
        let (drafts, normed) =
            self.forward_shared_head_greedy(&hidden, device, registry, cfg.rms_norm_eps)?;
        drafts
            .into_iter()
            .enumerate()
            .map(|(row, draft)| {
                Ok((
                    draft,
                    super::spec_decode::nth_hidden_row(&normed, self.hidden_size, row as u64)?,
                ))
            })
            .collect()
        })();
        #[cfg(test)]
        super::encoder_stage::settle_submitted().context("MTP draft original command drain")?;
        encoded
    }

    /// Attention of the batched draft step: Q/K/V, norms and IMROPE over
    /// the packed rows, then per row the single-row decode route against
    /// its own slot's MTP cache (K/V write at the slot cursor, SDPA over
    /// it), then the gate and output projection over the packed rows.
    #[allow(clippy::too_many_arguments)]
    fn forward_full_attention_rows(
        &self,
        x: &MlxBuffer,
        positions: &MlxBuffer,
        kv_cache: &mut HybridKvCache,
        slots: &[crate::serve::multi_seq_kv::SlotId],
        device: &MlxDevice,
        registry: &mut KernelRegistry,
        cfg: &Qwen35Config,
    ) -> Result<MlxBuffer> {
        kv_cache.bind_fa_producer(device)?;
        let required_backing = slots.iter().try_fold(0usize, |required, &slot| {
            Ok::<_, anyhow::Error>(required.max(kv_cache.fa_append_requirement(slot, 1, true)?))
        })?;
        kv_cache.ensure_fa_backing(required_backing)?;
        let h = self.hidden_size;
        let rows = slots.len();
        let rows_u32 = rows as u32;
        let (n_heads, n_kv_heads, head_dim) = (
            cfg.num_attention_heads,
            cfg.num_key_value_heads,
            cfg.head_dim,
        );
        ensure!(
            head_dim % 32 == 0,
            "MTP batched draft attention requires head_dim % 32 == 0, got {head_dim}"
        );
        let q_total = n_heads * head_dim;
        let kv_total = n_kv_heads * head_dim;
        let attn = &self.attn;
        let max_seq_len = kv_cache.full_attn_capacity();
        let slot = kv_cache
            .mtp_slot
            .as_mut()
            .context("MTP batched draft requires HybridKvCache.mtp_slot")?;
        let mut enc = device
            .command_encoder()
            .map(crate::inference::native_observation::encoder)
            .context("MTP enc batched attention")?;
        let x_norm = rms_norm_with_weight(
            &mut enc,
            registry,
            device,
            x,
            &attn.attn_norm,
            rows_u32,
            h,
            cfg.rms_norm_eps,
        )?;
        enc.memory_barrier();
        let (q_flat, gate_flat) = match &attn.q_gate {
            MtpQGateWeightsGpu::Ungated { wq, wq_ggml_type } => (
                apply_linear_projection_f32_with_ggml_type(
                    &mut enc,
                    registry,
                    device,
                    &x_norm,
                    wq,
                    *wq_ggml_type,
                    rows_u32,
                    h,
                    q_total,
                )?,
                None,
            ),
            MtpQGateWeightsGpu::Gated(q_gate) => {
                let (q, gate) = apply_q_gate_projection_f32(
                    &mut enc, registry, device, &x_norm, q_gate, rows_u32, n_heads, head_dim, h,
                )?;
                (q, Some(gate))
            }
        };
        let k_flat = apply_linear_projection_f32_with_ggml_type(
            &mut enc,
            registry,
            device,
            &x_norm,
            &attn.wk,
            attn.wk_ggml_type,
            rows_u32,
            h,
            kv_total,
        )?;
        let v_flat = apply_linear_projection_f32_with_ggml_type(
            &mut enc,
            registry,
            device,
            &x_norm,
            &attn.wv,
            attn.wv_ggml_type,
            rows_u32,
            h,
            kv_total,
        )?;
        enc.memory_barrier();
        let q_normed = apply_q_or_k_per_head_rms_norm(
            &mut enc,
            registry,
            device,
            &q_flat,
            &attn.attn_q_norm,
            rows_u32,
            n_heads,
            head_dim,
            cfg.rms_norm_eps,
        )?;
        let k_normed = apply_q_or_k_per_head_rms_norm(
            &mut enc,
            registry,
            device,
            &k_flat,
            &attn.attn_k_norm,
            rows_u32,
            n_kv_heads,
            head_dim,
            cfg.rms_norm_eps,
        )?;
        enc.memory_barrier();
        let q_rope = apply_imrope(
            &mut enc,
            registry,
            device,
            &q_normed,
            positions,
            rows_u32,
            n_heads,
            head_dim,
            cfg.rotary_dim,
            cfg.rope_theta as f32,
            cfg.mrope_section,
        )?;
        let k_rope = apply_imrope(
            &mut enc,
            registry,
            device,
            &k_normed,
            positions,
            rows_u32,
            n_kv_heads,
            head_dim,
            cfg.rotary_dim,
            cfg.rope_theta as f32,
            cfg.mrope_section,
        )?;
        enc.memory_barrier();
        let (nh, nkv, hd) = (n_heads as usize, n_kv_heads as usize, head_dim as usize);
        let mut row_outputs = Vec::with_capacity(rows);
        for (row, &slot_id) in slots.iter().enumerate() {
            if row > 0 {
                enc.memory_barrier();
            }
            row_outputs.push(
                apply_sdpa_with_kv_cache_decode_into(
                    &mut enc,
                    device,
                    registry,
                    &row_view(&q_rope, row, vec![1, nh, hd])?,
                    &row_view(&k_rope, row, vec![1, nkv, hd])?,
                    &row_view(&v_flat, row, vec![1, kv_total as usize])?,
                    slot,
                    1,
                    n_heads,
                    n_kv_heads,
                    head_dim,
                    max_seq_len,
                    slot_id,
                )
                .with_context(|| format!("MTP batched draft SDPA row {row}"))?,
            );
        }
        enc.memory_barrier();
        let qt = q_total as usize;
        let attention = device
            .alloc_buffer(rows * qt * 4, DType::F32, vec![rows, qt])
            .map_err(|e| anyhow!("MTP alloc batched attention: {e}"))?;
        for (row, output) in row_outputs.iter().enumerate() {
            dispatch_copy_f32(
                &mut enc,
                registry,
                device.metal_device(),
                output,
                &attention,
                0,
                row * qt,
                qt,
            )
            .with_context(|| format!("MTP gather attention row {row}"))?;
        }
        enc.memory_barrier();
        let gated_or_attn = match gate_flat.as_ref() {
            Some(gate) => {
                let gated = apply_sigmoid_gate_multiply(
                    &mut enc,
                    registry,
                    device,
                    &attention,
                    gate,
                    rows_u32 * q_total,
                )?;
                enc.memory_barrier();
                gated
            }
            None => attention,
        };
        let out = apply_linear_projection_f32_with_ggml_type(
            &mut enc,
            registry,
            device,
            &gated_or_attn,
            &attn.wo,
            attn.wo_ggml_type,
            rows_u32,
            q_total,
            h,
        )?;
        enc.commit();
        #[cfg(test)]
        super::encoder_stage::retain_submitted(enc, "mtp.draft.batched_attention");
        Ok(out)
    }

    /// Same as [`forward_draft`] but also returns the MTP block's normalized
    /// `h_nextn` row (AFTER `shared_head_norm`). The normalized row is the
    /// only valid input for a chained second MTP step.
    ///
    /// Shape contract: `hidden.element_count() == hidden_size` (single-token
    /// draft step).
    pub fn forward_draft_with_hidden(
        &self,
        prev_hidden: &MlxBuffer,
        embed_t: &MlxBuffer,
        kv_cache: &mut HybridKvCache,
        slot_id: crate::serve::multi_seq_kv::SlotId,
        position_ids: &[i32],
        device: &MlxDevice,
        registry: &mut KernelRegistry,
        cfg: &Qwen35Config,
    ) -> Result<(MlxBuffer, MlxBuffer)> {
        #[cfg(test)]
        super::encoder_stage::settle_submitted().context("MTP inherited original command drain")?;
        #[cfg(test)]
        let _submitted = super::encoder_stage::SubmittedCommands::open(true);
        let encoded = (|| -> Result<(MlxBuffer, MlxBuffer)> {
        let body = self.forward_draft_body(
            prev_hidden,
            embed_t,
            kv_cache,
            slot_id,
            position_ids,
            device,
            registry,
            cfg,
        )?;
        let head_started = std::time::Instant::now();
        let (logits, nextn_hidden) =
            self.forward_shared_head(&body.hidden, device, registry, cfg.rms_norm_eps)?;
        self.report_draft_profile(&body, head_started.elapsed().as_secs_f64() * 1000.0);
        Ok((logits, nextn_hidden))
        })();
        #[cfg(test)]
        super::encoder_stage::settle_submitted().context("MTP draft original command drain")?;
        encoded
    }

    #[allow(clippy::too_many_arguments)]
    fn forward_draft_body(
        &self,
        prev_hidden: &MlxBuffer,
        embed_t: &MlxBuffer,
        kv_cache: &mut HybridKvCache,
        slot_id: crate::serve::multi_seq_kv::SlotId,
        position_ids: &[i32],
        device: &MlxDevice,
        registry: &mut KernelRegistry,
        cfg: &Qwen35Config,
    ) -> Result<MtpDraftBody> {
        ensure!(
            position_ids.len() == 4,
            "MTP forward_draft expects exactly 4 IMROPE position ids, got {}",
            position_ids.len()
        );
        let h = self.hidden_size;
        ensure!(
            prev_hidden.element_count() == h as usize,
            "MTP prev_hidden has {} elements, expected {}",
            prev_hidden.element_count(),
            h
        );
        ensure!(
            embed_t.element_count() == h as usize,
            "MTP embed_t has {} elements, expected {}",
            embed_t.element_count(),
            h
        );

        // ADR-028 iter-156: per-sub-step GPU-timing harness. Sets
        // commit_and_wait barriers between sub-steps when HF2Q_MTP_PROFILE=1
        // is set. Measurement-only — adds ~1-2ms total per draft. Default
        // path commits each sub-step's CB without sync (Apple Metal pipelines
        // them across the boundary).
        let mtp_substep_profile = std::env::var("HF2Q_MTP_PROFILE").as_deref() == Ok("1");
        let pos_buf = upload_i32(position_ids, device).context("MTP upload positions")?;

        let t0 = std::time::Instant::now();
        let projected = self.project_embedding_and_hidden(
            embed_t,
            prev_hidden,
            1,
            device,
            registry,
            "adr065.mtp_draft",
        )?;
        if mtp_substep_profile {
            // Force GPU sync to measure sub-step time accurately.
            let mut enc = device
                .command_encoder()
                .map(crate::inference::native_observation::encoder)
                .context("MTP profile sync 1")?;
            #[cfg(test)]
            enc.commit_and_wait().context("MTP checked profile sync 1")?;
            #[cfg(not(test))]
            enc.commit_and_wait().ok();
        }
        let t_proj = t0.elapsed().as_secs_f64() * 1000.0;

        let t1 = std::time::Instant::now();
        let attn_out = self.forward_full_attention(
            &projected, &pos_buf, kv_cache, slot_id, 1, device, registry, cfg,
        )?;
        if mtp_substep_profile {
            let mut enc = device
                .command_encoder()
                .map(crate::inference::native_observation::encoder)
                .context("MTP profile sync 2")?;
            #[cfg(test)]
            enc.commit_and_wait().context("MTP checked profile sync 2")?;
            #[cfg(not(test))]
            enc.commit_and_wait().ok();
        }
        let t_attn = t1.elapsed().as_secs_f64() * 1000.0;

        let t2 = std::time::Instant::now();
        let hidden = self.forward_ffn_residual(&projected, &attn_out, device, registry, cfg)?;
        if mtp_substep_profile {
            let mut enc = device
                .command_encoder()
                .map(crate::inference::native_observation::encoder)
                .context("MTP profile sync 3")?;
            #[cfg(test)]
            enc.commit_and_wait().context("MTP checked profile sync 3")?;
            #[cfg(not(test))]
            enc.commit_and_wait().ok();
        }
        let t_ffn = t2.elapsed().as_secs_f64() * 1000.0;

        Ok(MtpDraftBody {
            hidden,
            profile_enabled: mtp_substep_profile,
            projection_ms: t_proj,
            attention_ms: t_attn,
            ffn_ms: t_ffn,
        })
    }

    fn report_draft_profile(&self, body: &MtpDraftBody, head_ms: f64) {
        if body.profile_enabled {
            eprintln!(
                "[MTP_SUBSTEP] proj={:.2}ms attn={:.2}ms ffn={:.2}ms head={:.2}ms total={:.2}ms",
                body.projection_ms,
                body.attention_ms,
                body.ffn_ms,
                head_ms,
                body.projection_ms + body.attention_ms + body.ffn_ms + head_ms,
            );
        }
    }

    /// Reconcile a target batch into the MTP attention cache.
    ///
    /// `target_nextn` is the target model's post-output-RMSNorm hidden for
    /// the same tokens as `embed_rows`. MTP consumes that hidden shifted one
    /// row right: row zero is `pending_target_hidden` (or zero for a cold
    /// prompt), and row `i > 0` is target row `i - 1`.
    ///
    /// This is used for initial/full-prompt catch-up and after every verifier
    /// batch. It intentionally stops after the attention K/V write: MTP FFN
    /// and vocabulary logits have no persistent state.
    #[allow(clippy::too_many_arguments)]
    pub(crate) fn process_target_batch(
        &self,
        tokens: &[u32],
        pending_target_hidden: Option<&MlxBuffer>,
        target_nextn: &MlxBuffer,
        shared_embed_rows: &MlxBuffer,
        kv_cache: &mut HybridKvCache,
        slot_id: crate::serve::multi_seq_kv::SlotId,
        position_ids: &[i32],
        device: &MlxDevice,
        registry: &mut KernelRegistry,
        cfg: &Qwen35Config,
        projection: JointProjectionKernel,
    ) -> Result<()> {
        let hidden_size = self.hidden_size as usize;
        super::carry_memory::commit_catchup(cfg, tokens.len(), target_nextn, pending_target_hidden)?;
        let plan = super::carry_memory::CatchupPlan::new(cfg, tokens.len(), self.embed_tokens.is_some());
        let carry = super::carry_memory::CatchupScope::open(
            plan,
            &[target_nextn, shared_embed_rows, pending_target_hidden.unwrap_or(target_nextn)],
        )?;
        // Standalone calls own their phase. Serving starts both scopes before
        // shared embedding and joins them here, never replacing its commands.
        let _submitted = carry.owns_phase()
            .then(|| super::encoder_stage::SubmittedCommands::open(true));
        let encoded = (|| -> Result<()> {
        let embed_rows = self.embeddings_for_tokens(tokens, shared_embed_rows, device, registry)?;
        super::carry_memory::keep_operand(&embed_rows);
        ensure!(
            hidden_size > 0,
            "MTP process_target_batch: hidden_size must be > 0"
        );
        ensure!(
            embed_rows.element_count() % hidden_size == 0,
            "MTP process_target_batch: embed elements {} not divisible by hidden_size {hidden_size}",
            embed_rows.element_count()
        );
        let seq_len = embed_rows.element_count() / hidden_size;
        ensure!(
            tokens.len() == seq_len,
            "MTP process_target_batch: tokens {} != seq_len {seq_len}",
            tokens.len()
        );
        ensure!(seq_len > 0, "MTP process_target_batch: empty batch");
        ensure!(
            target_nextn.element_count() == seq_len * hidden_size,
            "MTP process_target_batch: target nextn elements {} != {}",
            target_nextn.element_count(),
            seq_len * hidden_size
        );
        ensure!(
            position_ids.len() == seq_len * 4,
            "MTP process_target_batch: positions {} != 4*seq_len {}",
            position_ids.len(),
            seq_len * 4
        );
        if let Some(pending) = pending_target_hidden {
            ensure!(
                pending.element_count() == hidden_size,
                "MTP process_target_batch: pending hidden elements {} != hidden_size {hidden_size}",
                pending.element_count()
            );
        }

        // mlx-native allocations are zero-initialized. Thus a cold prompt's
        // first shifted row remains exactly zero without a prompt-sized host
        // allocation or download.
        let shifted = device
            .alloc_buffer(
                seq_len * hidden_size * 4,
                DType::F32,
                vec![seq_len, hidden_size],
            )
            .map_err(|e| anyhow!("MTP alloc shifted target nextn: {e}"))?;
        super::carry_memory::keep_operand(&shifted);
        let plan = shifted_nextn_copy_plan(seq_len, hidden_size, pending_target_hidden.is_some())?;
        let mut enc = device
            .command_encoder()
            .map(crate::inference::native_observation::encoder)
            .context("MTP enc shifted target nextn")?;
        if let (Some(pending), Some(copy)) = (pending_target_hidden, plan.pending) {
            dispatch_copy_f32(
                &mut enc,
                registry,
                device.metal_device(),
                pending,
                &shifted,
                copy.src_offset,
                copy.dst_offset,
                copy.count,
            )
            .context("MTP copy pending target nextn")?;
        }
        if let Some(copy) = plan.target_prefix {
            dispatch_copy_f32(
                &mut enc,
                registry,
                device.metal_device(),
                target_nextn,
                &shifted,
                copy.src_offset,
                copy.dst_offset,
                copy.count,
            )
            .context("MTP shift target nextn prefix")?;
        }
        if crate::inference::stage_profile::enabled() {
            enc.commit_and_wait_labeled("adr065.mtp_catchup.shifted_copy")
                .context("profile MTP shifted target copy")?;
        } else {
            enc.commit();
        }
        super::encoder_stage::retain_submitted(enc, "mtp.catchup.shifted_copy");

        let seq_len_u32 = u32::try_from(seq_len).context("MTP batch seq_len exceeds u32")?;
        let pos_buf = upload_i32(position_ids, device).context("MTP upload batch positions")?;
        super::carry_memory::keep_operand(&pos_buf);
        // Joint accepted-row widths vary with each lane's accept walk.
        let _projection = JointProjectionScope::enter_step(projection, seq_len_u32, false);
        let projected = self.project_embedding_and_hidden(
            &embed_rows,
            &shifted,
            seq_len_u32,
            device,
            registry,
            "adr065.mtp_catchup",
        )?;
        self.append_attention_kv(
            &projected,
            &pos_buf,
            kv_cache,
            slot_id,
            seq_len_u32,
            device,
            registry,
            cfg,
        )?;
            Ok(())
        })();

        if carry.owns_phase() {
            // Check every submitted buffer, also after an encoding failure.
            super::encoder_stage::settle_submitted()
                .context("MTP target-batch checked command drain")?;
            encoded?;
            carry.finish()?;
        } else {
            // The outer serving transaction must settle shared gather and all
            // MTP stages together, including failures before this method ran.
            encoded?;
        }
        Ok(())
    }

    #[allow(clippy::too_many_arguments)]
    fn append_attention_kv(
        &self,
        x: &MlxBuffer,
        positions: &MlxBuffer,
        kv_cache: &mut HybridKvCache,
        slot_id: crate::serve::multi_seq_kv::SlotId,
        seq_len: u32,
        device: &MlxDevice,
        registry: &mut KernelRegistry,
        cfg: &Qwen35Config,
    ) -> Result<()> {
        kv_cache.bind_fa_producer(device)?;
        kv_cache.ensure_fa_append(slot_id, seq_len as usize, true)?;
        let h = self.hidden_size;
        let kv_total = cfg.num_key_value_heads * cfg.head_dim;
        let attn = &self.attn;
        let (k_rope, v_flat) = {
            let mut enc = device
                .command_encoder()
                .map(crate::inference::native_observation::encoder)
                .context("MTP enc KV-only projections")?;
            let x_norm = rms_norm_with_weight(
                &mut enc,
                registry,
                device,
                x,
                &attn.attn_norm,
                seq_len,
                h,
                cfg.rms_norm_eps,
            )?;
            super::carry_memory::keep_operand(&x_norm);
            enc.memory_barrier();
            let k_flat = apply_linear_projection_f32_with_ggml_type(
                &mut enc,
                registry,
                device,
                &x_norm,
                &attn.wk,
                attn.wk_ggml_type,
                seq_len,
                h,
                kv_total,
            )?;
            super::carry_memory::keep_operand(&k_flat);
            let v_flat = apply_linear_projection_f32_with_ggml_type(
                &mut enc,
                registry,
                device,
                &x_norm,
                &attn.wv,
                attn.wv_ggml_type,
                seq_len,
                h,
                kv_total,
            )?;
            super::carry_memory::keep_operand(&v_flat);
            enc.memory_barrier();
            let k_normed = apply_q_or_k_per_head_rms_norm(
                &mut enc,
                registry,
                device,
                &k_flat,
                &attn.attn_k_norm,
                seq_len,
                cfg.num_key_value_heads,
                cfg.head_dim,
                cfg.rms_norm_eps,
            )?;
            super::carry_memory::keep_operand(&k_normed);
            enc.memory_barrier();
            let k_rope = apply_imrope(
                &mut enc,
                registry,
                device,
                &k_normed,
                positions,
                seq_len,
                cfg.num_key_value_heads,
                cfg.head_dim,
                cfg.rotary_dim,
                cfg.rope_theta as f32,
                cfg.mrope_section,
            )?;
            super::carry_memory::keep_operand(&k_rope);
            if crate::inference::stage_profile::enabled() {
                enc.commit_and_wait_labeled("adr065.mtp_catchup.kv_projections")
                    .context("profile MTP catch-up KV projections")?;
            } else {
                enc.commit_labeled("mtp.kv_only_projections");
            }
            super::encoder_stage::retain_submitted(enc, "mtp.kv_only_projections");
            (k_rope, v_flat)
        };

        let max_seq_len = kv_cache.full_attn_capacity();
        let slot = kv_cache
            .mtp_slot
            .as_mut()
            .context("MTP KV-only append requires HybridKvCache.mtp_slot")?;
        append_kv_to_cache_without_attention(
            device,
            registry,
            &k_rope,
            &v_flat,
            slot,
            seq_len,
            cfg.num_key_value_heads,
            cfg.head_dim,
            max_seq_len,
            slot_id,
        )
        .context("MTP target-batch KV-only append")
    }

    fn project_embedding_and_hidden(
        &self,
        embed_t: &MlxBuffer,
        prev_hidden: &MlxBuffer,
        seq_len: u32,
        device: &MlxDevice,
        registry: &mut KernelRegistry,
        profile_label: &'static str,
    ) -> Result<MlxBuffer> {
        let h = self.hidden_size;
        let mut enc = device
            .command_encoder()
            .map(crate::inference::native_observation::encoder)
            .context("MTP enc eh_proj")?;
        let embed_norm = rms_norm_with_weight(
            &mut enc,
            registry,
            device,
            embed_t,
            &self.enorm,
            seq_len,
            h,
            1e-6,
        )?;
        super::carry_memory::keep_operand(&embed_norm);
        let hidden_norm = rms_norm_with_weight(
            &mut enc,
            registry,
            device,
            prev_hidden,
            &self.hnorm,
            seq_len,
            h,
            1e-6,
        )?;
        super::carry_memory::keep_operand(&hidden_norm);
        enc.memory_barrier();
        let concat = device
            .alloc_buffer(
                (seq_len * 2 * h) as usize * 4,
                DType::F32,
                vec![seq_len as usize, (2 * h) as usize],
            )
            .map_err(|e| anyhow!("MTP alloc eh_proj concat: {e}"))?;
        super::carry_memory::keep_operand(&concat);
        mlx_native::ops::feature_concat::register(registry);
        mlx_native::ops::feature_concat::dispatch_feature_concat_f32(
            &mut enc,
            registry,
            device.metal_device(),
            &embed_norm,
            &concat,
            seq_len,
            h,
            0,
            2 * h,
        )
        .context("MTP eh_proj concat embedding")?;
        mlx_native::ops::feature_concat::dispatch_feature_concat_f32(
            &mut enc,
            registry,
            device.metal_device(),
            &hidden_norm,
            &concat,
            seq_len,
            h,
            h,
            2 * h,
        )
        .context("MTP eh_proj concat hidden")?;
        enc.memory_barrier();
        let out = apply_linear_projection_f32_with_ggml_type(
            &mut enc,
            registry,
            device,
            &concat,
            &self.eh_proj,
            self.eh_proj_ggml_type,
            seq_len,
            2 * h,
            h,
        )?;
        super::carry_memory::keep_operand(&out);
        if crate::inference::stage_profile::enabled() {
            enc.commit_and_wait_labeled(profile_label)
                .context("profile MTP embedding and hidden projection")?;
        } else {
            enc.commit();
        }
        super::encoder_stage::retain_submitted(enc, "mtp.embedding_hidden_projection");
        Ok(out)
    }

    fn forward_full_attention(
        &self,
        x: &MlxBuffer,
        positions: &MlxBuffer,
        kv_cache: &mut HybridKvCache,
        slot_id: crate::serve::multi_seq_kv::SlotId,
        seq_len: u32,
        device: &MlxDevice,
        registry: &mut KernelRegistry,
        cfg: &Qwen35Config,
    ) -> Result<MlxBuffer> {
        kv_cache.bind_fa_producer(device)?;
        kv_cache.ensure_fa_append(slot_id, seq_len as usize, true)?;
        let h = self.hidden_size;
        let q_total = cfg.num_attention_heads * cfg.head_dim;
        let kv_total = cfg.num_key_value_heads * cfg.head_dim;
        let attn = &self.attn;

        let (q_rope, k_rope, v_flat, gate_flat) = {
            let mut enc = device
                .command_encoder()
                .map(crate::inference::native_observation::encoder)
                .context("MTP enc attn qkv")?;
            let x_norm = rms_norm_with_weight(
                &mut enc,
                registry,
                device,
                x,
                &attn.attn_norm,
                seq_len,
                h,
                cfg.rms_norm_eps,
            )?;
            enc.memory_barrier();
            let (q_flat, gate_flat) = match &attn.q_gate {
                MtpQGateWeightsGpu::Ungated { wq, wq_ggml_type } => (
                    apply_linear_projection_f32_with_ggml_type(
                        &mut enc,
                        registry,
                        device,
                        &x_norm,
                        wq,
                        *wq_ggml_type,
                        seq_len,
                        h,
                        q_total,
                    )?,
                    None,
                ),
                MtpQGateWeightsGpu::Gated(q_gate) => {
                    let (q, gate) = apply_q_gate_projection_f32(
                        &mut enc,
                        registry,
                        device,
                        &x_norm,
                        q_gate,
                        seq_len,
                        cfg.num_attention_heads,
                        cfg.head_dim,
                        h,
                    )?;
                    (q, Some(gate))
                }
            };
            let k_flat = apply_linear_projection_f32_with_ggml_type(
                &mut enc,
                registry,
                device,
                &x_norm,
                &attn.wk,
                attn.wk_ggml_type,
                seq_len,
                h,
                kv_total,
            )?;
            let v_flat = apply_linear_projection_f32_with_ggml_type(
                &mut enc,
                registry,
                device,
                &x_norm,
                &attn.wv,
                attn.wv_ggml_type,
                seq_len,
                h,
                kv_total,
            )?;
            enc.memory_barrier();
            let q_normed = apply_q_or_k_per_head_rms_norm(
                &mut enc,
                registry,
                device,
                &q_flat,
                &attn.attn_q_norm,
                seq_len,
                cfg.num_attention_heads,
                cfg.head_dim,
                cfg.rms_norm_eps,
            )?;
            let k_normed = apply_q_or_k_per_head_rms_norm(
                &mut enc,
                registry,
                device,
                &k_flat,
                &attn.attn_k_norm,
                seq_len,
                cfg.num_key_value_heads,
                cfg.head_dim,
                cfg.rms_norm_eps,
            )?;
            enc.memory_barrier();
            let q_rope = apply_imrope(
                &mut enc,
                registry,
                device,
                &q_normed,
                positions,
                seq_len,
                cfg.num_attention_heads,
                cfg.head_dim,
                cfg.rotary_dim,
                cfg.rope_theta as f32,
                cfg.mrope_section,
            )?;
            let k_rope = apply_imrope(
                &mut enc,
                registry,
                device,
                &k_normed,
                positions,
                seq_len,
                cfg.num_key_value_heads,
                cfg.head_dim,
                cfg.rotary_dim,
                cfg.rope_theta as f32,
                cfg.mrope_section,
            )?;
            if crate::inference::stage_profile::enabled() {
                enc.commit_and_wait_labeled("adr065.mtp_draft.attention_projections")
                    .context("profile MTP draft attention projections")?;
            } else {
                enc.commit();
                #[cfg(test)]
                super::encoder_stage::retain_submitted(enc, "mtp.draft.attention_projections");
            }
            (q_rope, k_rope, v_flat, gate_flat)
        };

        let max_seq_len = kv_cache.full_attn_capacity();
        let slot = kv_cache
            .mtp_slot
            .as_mut()
            .ok_or_else(|| anyhow!("MTP forward_draft requires HybridKvCache.mtp_slot"))?;
        let attn_out = apply_sdpa_with_kv_cache(
            device,
            registry,
            &q_rope,
            &k_rope,
            &v_flat,
            slot,
            seq_len,
            cfg.num_attention_heads,
            cfg.num_key_value_heads,
            cfg.head_dim,
            max_seq_len,
            None,
            slot_id,
        )
        .context("MTP SDPA")?;

        let mut enc = device
            .command_encoder()
            .map(crate::inference::native_observation::encoder)
            .context("MTP enc attn output")?;
        let gated_or_attn = if let Some(gate) = gate_flat.as_ref() {
            let gated = apply_sigmoid_gate_multiply(
                &mut enc,
                registry,
                device,
                &attn_out,
                gate,
                seq_len * q_total,
            )?;
            // RAW: the output projection reads `gated`. The encoder
            // dispatches concurrently, so without this barrier the
            // projection can read partially written rows and the MTP draft
            // changes from run to run (the target path's ADR-015 iter21 fix).
            enc.memory_barrier();
            gated
        } else {
            attn_out
        };
        let out = apply_linear_projection_f32_with_ggml_type(
            &mut enc,
            registry,
            device,
            &gated_or_attn,
            &attn.wo,
            attn.wo_ggml_type,
            seq_len,
            q_total,
            h,
        )?;
        if crate::inference::stage_profile::enabled() {
            enc.commit_and_wait_labeled("adr065.mtp_draft.attention_output")
                .context("profile MTP draft attention output")?;
        } else {
            enc.commit();
            #[cfg(test)]
            super::encoder_stage::retain_submitted(enc, "mtp.draft.attention_output");
        }
        Ok(out)
    }

    /// Post-attention residual norm and FFN over `[rows, H]` (one row on the
    /// single-draft path). MoE runs each row through the one-row expert path.
    fn forward_ffn_residual(
        &self,
        residual: &MlxBuffer,
        attn_out: &MlxBuffer,
        device: &MlxDevice,
        registry: &mut KernelRegistry,
        cfg: &Qwen35Config,
    ) -> Result<MlxBuffer> {
        let h = self.hidden_size;
        let hs = h as usize;
        ensure!(
            residual.element_count().is_multiple_of(hs) && residual.element_count() > 0,
            "MTP FFN residual has {} elements, not a positive multiple of {h}",
            residual.element_count()
        );
        let rows = residual.element_count() / hs;
        let ffn_input = device
            .alloc_buffer(rows * hs * 4, DType::F32, vec![rows, hs])
            .map_err(|e| anyhow!("MTP alloc ffn_input: {e}"))?;
        let ffn_residual = device
            .alloc_buffer(rows * hs * 4, DType::F32, vec![rows, hs])
            .map_err(|e| anyhow!("MTP alloc ffn_residual: {e}"))?;
        let mut enc = device
            .command_encoder()
            .map(crate::inference::native_observation::encoder)
            .context("MTP enc residual norm")?;
        dispatch_fused_residual_norm_f32(
            &mut enc,
            registry,
            device.metal_device(),
            residual,
            attn_out,
            &self.attn.post_attn_norm,
            &ffn_input,
            Some(&ffn_residual),
            rows as u32,
            h,
            cfg.rms_norm_eps,
        )
        .context("MTP fused residual norm")?;
        if crate::inference::stage_profile::enabled() {
            enc.commit_and_wait_labeled("adr065.mtp_draft.residual_norm")
                .context("profile MTP draft residual norm")?;
            #[cfg(test)]
            drop(enc);
        } else {
            enc.commit();
            #[cfg(test)]
            super::encoder_stage::retain_submitted(enc, "mtp.draft.residual_norm");
        }
        // Release before the FFN, whose teacher-scope route may wait.
        #[cfg(not(test))]
        drop(enc);

        match &self.ffn {
            MtpFfnWeightsGpu::Dense {
                weights,
                intermediate_size,
            } => build_dense_ffn_layer_gpu(
                device,
                registry,
                &ffn_input,
                weights,
                DenseFfnShape {
                    hidden_size: h,
                    intermediate_size: *intermediate_size,
                },
                Some(&ffn_residual),
            )
            .context("MTP exact F32 dense FFN"),
            MtpFfnWeightsGpu::DenseQ { weights } => build_dense_ffn_layer_gpu_q(
                device,
                registry,
                &ffn_input,
                weights,
                Some(&ffn_residual),
            )
            .context("MTP native dense FFN"),
            MtpFfnWeightsGpu::Moe { weights, shape } => {
                // ADR-034 post-codex audit (2026-05-21): route through the
                // external-encoder variant with the REAL MTP layer index
                // (`self.layer_index`, typically num_hidden_layers — e.g. 40
                // for Qwen 3.5 35B-A3B) so the imatrix intercept tag emitted
                // by `build_moe_ffn_layer_gpu_q_raw_into` reflects the actual
                // MTP block name (`blk.{layer_index}.ffn_*_exps.weight`).
                // The legacy wrapper `build_moe_ffn_layer_gpu_q` hardcodes
                // `layer_idx=0` (gpu_ffn.rs:2263); using it from production
                // would silently mis-tag MTP expert records.
                let mut enc = device
                    .command_encoder()
                    .map(crate::inference::native_observation::encoder)
                    .context("MTP enc moe_ffn_q")?;
                let out = if rows == 1 {
                    build_moe_ffn_layer_gpu_q_raw_into(
                        &mut enc,
                        device,
                        registry,
                        &ffn_input,
                        weights,
                        *shape,
                        Some(&ffn_residual),
                        self.layer_index as usize,
                    )
                    .context("MTP MoE FFN")?
                } else {
                    // Routed experts differ per row: one row at a time
                    // through the one-row path, then gathered.
                    let mut per_row = Vec::with_capacity(rows);
                    for row in 0..rows {
                        if row > 0 {
                            enc.memory_barrier();
                        }
                        per_row.push(
                            build_moe_ffn_layer_gpu_q_raw_into(
                                &mut enc,
                                device,
                                registry,
                                &row_view(&ffn_input, row, vec![1, hs])?,
                                weights,
                                *shape,
                                Some(&row_view(&ffn_residual, row, vec![1, hs])?),
                                self.layer_index as usize,
                            )
                            .with_context(|| format!("MTP MoE FFN row {row}"))?,
                        );
                    }
                    enc.memory_barrier();
                    let packed = device
                        .alloc_buffer(rows * hs * 4, DType::F32, vec![rows, hs])
                        .map_err(|e| anyhow!("MTP alloc packed MoE output: {e}"))?;
                    for (row, output) in per_row.iter().enumerate() {
                        dispatch_copy_f32(
                            &mut enc,
                            registry,
                            device.metal_device(),
                            output,
                            &packed,
                            0,
                            row * hs,
                            hs,
                        )
                        .with_context(|| format!("MTP gather MoE row {row}"))?;
                    }
                    packed
                };
                // Match the legacy wrapper's commit policy: seq=1 (the only
                // MTP draft shape) uses non-blocking commit so the next
                // command buffer pipelines across the boundary on Metal.
                if crate::inference::stage_profile::enabled() {
                    enc.commit_and_wait_labeled("adr065.mtp_draft.moe")
                        .context("profile MTP draft MoE")?;
                } else {
                    enc.commit();
                    #[cfg(test)]
                    super::encoder_stage::retain_submitted(enc, "mtp.draft.moe_ffn");
                }
                Ok(out)
            }
        }
    }

    fn forward_shared_head(
        &self,
        hidden: &MlxBuffer,
        device: &MlxDevice,
        registry: &mut KernelRegistry,
        eps: f32,
    ) -> Result<(MlxBuffer, MlxBuffer)> {
        // ADR-028 iter-155: consolidated single-CB shared-head — merges
        // the prior 2-buffer chain (head_norm + lm_head) into one
        // command buffer with a memory_barrier between RAW dependents.
        // Saves ~1ms per draft step on Apple Metal at decode shape.
        let h = self.hidden_size;
        let mut enc = device
            .command_encoder()
            .map(crate::inference::native_observation::encoder)
            .context("MTP enc shared head")?;
        let normed = rms_norm_with_weight(
            &mut enc,
            registry,
            device,
            hidden,
            &self.shared_head_norm,
            1,
            h,
            eps,
        )?;
        // RAW: lm_head reads `normed` produced by rms_norm above. Apple
        // Metal compute encoders run threadgroups in parallel by default;
        // memory_barrier required so the projection sees finalized norm.
        enc.memory_barrier();
        let logits = apply_linear_projection_f32_with_ggml_type(
            &mut enc,
            registry,
            device,
            &normed,
            &self.shared_head_head,
            self.shared_head_head_ggml_type,
            1,
            h,
            self.vocab_size,
        )
        .context("MTP shared head projection")?;
        if crate::inference::stage_profile::enabled() {
            enc.commit_and_wait_labeled("adr065.mtp_draft.shared_head")
                .context("profile MTP commit logits")?;
        } else {
            enc.commit_and_wait().context("MTP commit logits")?;
        }
        #[cfg(test)]
        super::encoder_stage::settle_submitted().context("MTP checked original owners before logits")?;
        Ok((logits, normed))
    }

    /// Shared-head norm, vocabulary projection and one GPU argmax per row
    /// over `[rows, H]`, in one command buffer with a single wait. Returns
    /// each row's draft and the normalized rows.
    fn forward_shared_head_greedy(
        &self,
        hidden: &MlxBuffer,
        device: &MlxDevice,
        registry: &mut KernelRegistry,
        eps: f32,
    ) -> Result<(Vec<u32>, MlxBuffer)> {
        let h = self.hidden_size;
        let hs = h as usize;
        ensure!(
            hidden.element_count().is_multiple_of(hs) && hidden.element_count() > 0,
            "MTP shared head input has {} elements, not a positive multiple of {h}",
            hidden.element_count()
        );
        let rows = hidden.element_count() / hs;
        let vocab = self.vocab_size as usize;
        let mut enc = device
            .command_encoder()
            .map(crate::inference::native_observation::encoder)
            .context("MTP enc fused shared head argmax")?;
        let normed = rms_norm_with_weight(
            &mut enc,
            registry,
            device,
            hidden,
            &self.shared_head_norm,
            rows as u32,
            h,
            eps,
        )?;
        enc.memory_barrier();
        let logits = apply_linear_projection_f32_with_ggml_type(
            &mut enc,
            registry,
            device,
            &normed,
            &self.shared_head_head,
            self.shared_head_head_ggml_type,
            rows as u32,
            h,
            self.vocab_size,
        )
        .context("MTP fused greedy shared-head projection")?;
        enc.memory_barrier();

        let mut params = device
            .alloc_buffer(4, DType::U32, vec![1])
            .map_err(|error| anyhow!("MTP alloc greedy argmax params: {error}"))?;
        params
            .as_mut_slice::<u32>()
            .map_err(|error| anyhow!("MTP greedy argmax params slice: {error}"))?[0] =
            self.vocab_size;
        let mut indices = Vec::with_capacity(rows);
        for row in 0..rows {
            let out_index = device
                .alloc_buffer(4, DType::U32, vec![1])
                .map_err(|error| anyhow!("MTP alloc greedy argmax index: {error}"))?;
            let out_value = device
                .alloc_buffer(4, DType::F32, vec![1])
                .map_err(|error| anyhow!("MTP alloc greedy argmax value: {error}"))?;
            let row_logits = if rows == 1 {
                logits.clone()
            } else {
                row_view(&logits, row, vec![1, vocab])?
            };
            dispatch_argmax_f32(
                &mut enc,
                registry,
                device.metal_device(),
                &row_logits,
                &out_index,
                &out_value,
                &params,
                self.vocab_size,
            )
            .with_context(|| format!("MTP fused greedy argmax row {row}"))?;
            indices.push((out_index, out_value));
        }
        enc.commit_and_wait_labeled("mtp.shared_head_argmax")
            .context("MTP commit fused shared-head argmax")?;
        #[cfg(test)]
        super::encoder_stage::settle_submitted().context("MTP checked original owners before argmax read")?;
        let tokens = indices
            .iter()
            .map(|(index, _)| {
                index
                    .as_slice::<u32>()
                    .map(|slice| slice[0])
                    .map_err(|error| anyhow!("MTP greedy argmax index slice: {error}"))
            })
            .collect::<Result<Vec<_>>>()?;
        Ok((tokens, normed))
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct HiddenCopy {
    src_offset: usize,
    dst_offset: usize,
    count: usize,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct ShiftedNextnCopyPlan {
    pending: Option<HiddenCopy>,
    target_prefix: Option<HiddenCopy>,
}

fn shifted_nextn_copy_plan(
    seq_len: usize,
    hidden_size: usize,
    has_pending: bool,
) -> Result<ShiftedNextnCopyPlan> {
    ensure!(seq_len > 0, "shifted nextn plan requires a non-empty batch");
    ensure!(
        hidden_size > 0,
        "shifted nextn plan requires hidden_size > 0"
    );
    let pending = has_pending.then_some(HiddenCopy {
        src_offset: 0,
        dst_offset: 0,
        count: hidden_size,
    });
    let target_prefix = (seq_len > 1).then_some(HiddenCopy {
        src_offset: 0,
        dst_offset: hidden_size,
        count: (seq_len - 1)
            .checked_mul(hidden_size)
            .context("shifted nextn target-prefix size overflow")?,
    });
    Ok(ShiftedNextnCopyPlan {
        pending,
        target_prefix,
    })
}

fn rms_norm_with_weight(
    encoder: &mut mlx_native::CommandEncoder,
    registry: &mut KernelRegistry,
    device: &MlxDevice,
    input: &MlxBuffer,
    weight: &MlxBuffer,
    seq_len: u32,
    hidden_size: u32,
    eps: f32,
) -> Result<MlxBuffer> {
    let out = device
        .alloc_buffer(
            (seq_len * hidden_size) as usize * 4,
            DType::F32,
            vec![seq_len as usize, hidden_size as usize],
        )
        .map_err(|e| anyhow!("alloc rms_norm out: {e}"))?;
    let mut params = device
        .alloc_buffer(8, DType::F32, vec![2])
        .map_err(|e| anyhow!("alloc rms_norm params: {e}"))?;
    super::carry_memory::keep_operand(&params);
    {
        let s = params.as_mut_slice::<f32>().map_err(|e| anyhow!("{e}"))?;
        s[0] = eps;
        s[1] = hidden_size as f32;
    }
    rms_norm::dispatch_rms_norm(
        encoder,
        registry,
        device.metal_device(),
        input,
        weight,
        &out,
        &params,
        seq_len,
        hidden_size,
    )
    .context("dispatch_rms_norm")?;
    Ok(out)
}

#[cfg(test)]
#[path = "mtp_tests.rs"]
mod tests;

fn upload_i32(data: &[i32], device: &MlxDevice) -> Result<MlxBuffer> {
    let mut buf = device
        .alloc_buffer(data.len() * 4, DType::I32, vec![data.len()])
        .map_err(|e| anyhow!("alloc i32 buffer: {e}"))?;
    buf.as_mut_slice::<i32>()
        .map_err(|e| anyhow!("i32 mut_slice: {e}"))?
        .copy_from_slice(data);
    Ok(buf)
}