onnxruntime-ep-mlx 0.31.1

MLX-native ONNX Runtime execution provider (plugin EP) for Apple Silicon — binds mlx-c directly, no mlx-rs.
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
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
//! Unified compiled fast path (`mlx_compile`) — ONE parameterised core for every compiled strategy.
//!
//! The eager translator (`engine::TranslationContext::execute`) rebuilds and dispatches EVERY node
//! of a claimed subgraph as separate unfused MLX primitive launches on every Compute call. For a
//! decoder that is ~393 kernels per token; for a CNN/audio graph it is hundreds per inference —
//! dominating runtime. This module traces the WHOLE claimed subgraph translation into an
//! `mlx_closure` over its dynamic (non-constant) ctx inputs ONCE, compiles it with `mlx_compile`
//! (kernel fusion), caches the compiled closure on the plan, and on each subsequent call just
//! applies the compiled closure to the freshly-wrapped inputs.
//!
//! Historically this lived in two modules — a decode-only shapeless path and a general static-shape
//! path. They are now a single [`crate::engine::CompiledSubgraph`] core parameterised by a
//! [`crate::engine::CompiledConfig`] (`shape_mode` + the opt-in `kv_alias` / `rope_as_data` /
//! `delta_copyout` / `contiguous_outputs` features). The strategies are just configurations:
//!   * **decode**  = `{ Shapeless,  kv_alias, rope_as_data, delta_copyout }` — shapeless so a growing
//!     KV length never retraces; RoPE position + valid-past fed as DATA; delta KV copy-out.
//!   * **general** = `{ ShapeKeyed, contiguous_outputs }` — retraces on a shape change; no KV aliasing.
//!   * **prefill** = `{ ShapeKeyed, kv_alias, rope_as_data, delta_copyout }` (Phase 2) — the SAME
//!     decoder subgraph as decode but with a variable query length S>1, so it is shape-keyed.
//!
//! Two shapeless-compile tricks carried by `rope_as_data` (ported verbatim from the C++ blueprint):
//!   * **RoPE rotate-half via a `[hd,hd]` matmul** (only when `rotary_dim == head_dim`) so the
//!     compiled graph carries no Slice (which shapeless compile cannot shape-infer).
//!   * **Pre-sliced cos/sin rows fed as synthetic closure inputs**, so the position offset is DATA
//!     (not baked) and the compiled graph never slices a cos/sin cache at a runtime position.
//!
//! Ineligible plans and trace/apply failures fall back to the eager translator. Device evaluation
//! failures abort Run: published outputs may already alias inputs, making eager replay unsafe.

use std::collections::HashSet;
use std::os::raw::c_void;
use std::panic::AssertUnwindSafe;

use crate::engine::{
    DeltaWrite, DynInput, MlxError, NodeDesc, OutRef, Plan, ShapeMode, Slot, Src, SynthRope,
    TracePayload, TranslationContext, attention_cross_kv, attention_past_key, copy_out_raw_delta,
    dim_i32, is_separate_qkv_attention, mlx_dtype_from_onnx, publish_device_output,
    read_ctx_input_metadata, read_ctx_input_raw, read_ctx_input_value, rope_row_key,
};
use crate::mlx::{self, Array, Closure, VectorArray};
use crate::registry::CompileShapeSafety;
use crate::sys::mlx as mlxsys;
use crate::sys::ort;

/// Decide whether a compiled fast path is allowed for this plan. The caller passes whether that
/// particular route has an unsupported graph-structure dependency. Static control flow is allowed
/// on the general route and specialized below.
pub fn compile_enabled(has_control_flow: bool) -> bool {
    let killed = std::env::var_os("ONNXRUNTIME_EP_MLX_NO_COMPILE")
        .map(|v| v != "0" && !v.is_empty())
        .unwrap_or(false);
    !has_control_flow && !killed
}

fn node_compile_shape_safety(node: &NodeDesc) -> CompileShapeSafety {
    node.compile_shape_safety
}

fn compile_shape_mode_supported(nodes: &[NodeDesc], shapeless: bool) -> bool {
    nodes.iter().all(|node| {
        (if shapeless {
            node_compile_shape_safety(node).allows_shapeless()
        } else {
            node_compile_shape_safety(node).allows_shape_keyed()
        }) && node
            .subgraphs
            .iter()
            .all(|subgraph| compile_shape_mode_supported(&subgraph.nodes, shapeless))
    })
}

/// Decode has no host-value specialization key. Shape/Size literals and host-read parameters
/// must therefore be proven safe independently of MLX's primitive output-shape inference.
pub fn decode_enabled(has_control_flow: bool, nodes: &[NodeDesc]) -> bool {
    compile_enabled(has_control_flow)
        && compile_shape_mode_supported(nodes, true)
        && !nodes.iter().any(reads_data_dependent_parameter)
}

/// Op types whose subgraphs keep their existing (eager / compiled-decode) routes because they are
/// incompatible with a single static-shape fused trace:
///   * cached attention ops carry KV-cache aliasing + delta copy-out semantics not modelled by the
///     general config (they take the decode/prefill configs instead); cache-free standard Attention
///     can use ordinary shape-keyed capture;
///   * host-computed ops GPU-eval their DYNAMIC input DATA mid-translate (`contiguous_eval`) and/or
///     emit a data-dependent output shape — both illegal inside an `mlx_compile` trace (the
///     placeholder has no data), so a subgraph containing one is never general-compiled.
fn is_general_compile_unsafe(n: &NodeDesc) -> bool {
    let cache_free_attention = n.domain.is_empty()
        && n.op_type == "Attention"
        && n.inputs
            .iter()
            .skip(4)
            .all(|input| input.source == Src::Absent)
        && n.outputs
            .iter()
            .skip(1)
            .all(|output| output.name.is_empty());
    (is_separate_qkv_attention(n) && !cache_free_attention)
        || matches!(
            n.op_type.as_str(),
            "GroupQueryAttention"
                | "PagedAttention"
                | "SparseAttention"
                | "Det"
                | "ImageDecoder"
                | "NonMaxSuppression"
                | "NonZero"
                | "TfIdfVectorizer"
                | "Unique"
        )
        || n.subgraphs
            .iter()
            .any(|sg| sg.nodes.iter().any(is_general_compile_unsafe))
}

fn host_parameter_indices(n: &NodeDesc) -> &'static [usize] {
    if n.domain == "pkg.onnax" && n.op_type == "SumToShape" {
        return &[1];
    }
    match n.op_type.as_str() {
        "Reshape" | "Expand" | "AffineGrid" | "Squeeze" | "Unsqueeze" => &[1],
        "ConstantOfShape" => &[0],
        "Slice" => &[1, 2, 3, 4],
        "Range" => &[0, 1, 2],
        "DFT" => &[1, 2],
        "STFT" => &[1, 3],
        "HannWindow" | "HammingWindow" | "BlackmanWindow" => &[0],
        "MelWeightMatrix" => &[0, 1, 2, 3, 4],
        "MaxUnpool" => &[1, 2],
        "TensorScatter" => &[2],
        "Col2Im" => &[1, 2],
        "CumSum" | "CumProd" => &[1],
        "RNN" | "GRU" | "LSTM" => &[4],
        "TopK" | "Split" => &[1],
        "Pad" => &[1, 3],
        "ReduceSum" | "ReduceMean" | "ReduceMax" | "ReduceMin" | "ReduceProd"
        | "ReduceSumSquare" | "ReduceL1" | "ReduceL2" | "ReduceLogSum" | "ReduceLogSumExp" => &[1],
        _ => &[],
    }
}

fn guarded_boundary_parameter(n: &NodeDesc, r: &crate::engine::TensorRef) -> bool {
    if n.domain == "pkg.onnax" && n.op_type == "SumToShape" {
        return r.source == Src::CtxInput;
    }
    matches!(
        n.op_type.as_str(),
        "Reshape"
            | "Expand"
            | "AffineGrid"
            | "Slice"
            | "ConstantOfShape"
            | "DFT"
            | "STFT"
            | "HannWindow"
            | "HammingWindow"
            | "BlackmanWindow"
            | "RNN"
            | "GRU"
            | "LSTM"
            | "TopK"
            | "Split"
            | "Pad"
            | "Squeeze"
            | "Unsqueeze"
    ) && r.source == Src::CtxInput
}

pub fn boundary_shape_inputs(nodes: &[NodeDesc]) -> Vec<usize> {
    let mut inputs = Vec::new();
    for node in nodes {
        for &i in host_parameter_indices(node) {
            if let Some(r) = node.inputs.get(i)
                && !r.constant
                && !r.shape_const
                && guarded_boundary_parameter(node, r)
                && !inputs.contains(&r.ctx_index)
            {
                inputs.push(r.ctx_index);
            }
        }
        for subgraph in &node.subgraphs {
            for index in boundary_shape_inputs(&subgraph.nodes) {
                if !inputs.contains(&index) {
                    inputs.push(index);
                }
            }
        }
    }
    inputs
}

fn boundary_shape_specialization_key(
    indices: &[usize],
    api: *const ort::OrtApi,
    kctx: *mut ort::OrtKernelContext,
) -> Result<String, MlxError> {
    let mut key = String::new();
    for &index in indices {
        let (data, shape, dtype) = read_ctx_input_raw(api, kctx, index)?;
        let count = shape.iter().try_fold(1usize, |count, &dim| {
            usize::try_from(dim)
                .ok()
                .and_then(|dim| count.checked_mul(dim))
        });
        let count = count
            .filter(|&count| count > 0 && count <= 64)
            .ok_or_else(|| "boundary shape specialization requires 1..=64 integers".to_string())?;
        if data.is_null() {
            return Err("boundary shape specialization input has null data".to_string());
        }
        let values: Vec<i64> = match dtype {
            ort::ONNXTensorElementDataType_ONNX_TENSOR_ELEMENT_DATA_TYPE_INT64 => unsafe {
                std::slice::from_raw_parts(data as *const i64, count).to_vec()
            },
            ort::ONNXTensorElementDataType_ONNX_TENSOR_ELEMENT_DATA_TYPE_INT32 => unsafe {
                std::slice::from_raw_parts(data as *const i32, count)
                    .iter()
                    .map(|&v| i64::from(v))
                    .collect()
            },
            _ => return Err("boundary shape specialization requires int32/int64".to_string()),
        };
        key.push_str(&format!("P{index}:{dtype}:{shape:?}={values:?};"));
    }
    Ok(key)
}

/// Shape-keyed MLX caches do not observe input values. Only initializers, proven shape-constant
/// intermediates, or explicitly value-guarded general-route boundary parameters are safe.
fn reads_data_dependent_parameter(n: &NodeDesc) -> bool {
    reads_unguarded_parameter(n, false)
}

fn reads_unguarded_parameter(n: &NodeDesc, guard_boundary_values: bool) -> bool {
    if n.subgraphs.iter().any(|sg| {
        sg.nodes
            .iter()
            .any(|n| reads_unguarded_parameter(n, guard_boundary_values))
    }) {
        return true;
    }
    host_parameter_indices(n).iter().any(|&i| {
        n.inputs.get(i).is_some_and(|tr| {
            !matches!(tr.source, Src::Initializer | Src::Absent)
                && !tr.constant
                && !tr.shape_const
                && !(guard_boundary_values && guarded_boundary_parameter(n, tr))
        })
    })
}

/// General capture shares NO_COMPILE and also honors NO_GENERAL_COMPILE for eager A/B validation.
pub fn general_enabled(has_control_flow: bool, nodes: &[NodeDesc]) -> bool {
    if !compile_enabled(has_control_flow) {
        return false;
    }
    if std::env::var_os("ONNXRUNTIME_EP_MLX_NO_GENERAL_COMPILE")
        .map(|v| v != "0" && !v.is_empty())
        .unwrap_or(false)
    {
        return false;
    }
    !nodes
        .iter()
        .any(|n| is_general_compile_unsafe(n) || reads_unguarded_parameter(n, true))
        && compile_shape_mode_supported(nodes, false)
}

fn host_scalar_i64(
    r: &crate::engine::TensorRef,
    api: *const ort::OrtApi,
    kctx: *mut ort::OrtKernelContext,
) -> Result<i64, MlxError> {
    let data = match r.source {
        Src::Initializer => r
            .init
            .as_ref()
            .map(|init| init.data)
            .ok_or_else(|| format!("control-flow initializer {} has no data", r.name))?,
        Src::CtxInput => read_ctx_input_raw(api, kctx, r.ctx_index)?.0,
        _ => {
            return Err(format!(
                "control-flow input {} is not host-readable",
                r.name
            ));
        }
    };
    if data.is_null() {
        return Err(format!("control-flow input {} has null data", r.name));
    }
    Ok(unsafe { *(data as *const i64) })
}

fn host_scalar_bool(
    r: &crate::engine::TensorRef,
    api: *const ort::OrtApi,
    kctx: *mut ort::OrtKernelContext,
) -> Result<bool, MlxError> {
    let data = match r.source {
        Src::Initializer => r
            .init
            .as_ref()
            .map(|init| init.data)
            .ok_or_else(|| format!("control-flow initializer {} has no data", r.name))?,
        Src::CtxInput => read_ctx_input_raw(api, kctx, r.ctx_index)?.0,
        _ => {
            return Err(format!(
                "control-flow input {} is not host-readable",
                r.name
            ));
        }
    };
    if data.is_null() {
        return Err(format!("control-flow input {} has null data", r.name));
    }
    Ok(unsafe { *(data as *const u8) } != 0)
}

/// Values that change the graph emitted by the eager Rust translator. MLX's shape-keyed cache only
/// observes tensor shapes/dtypes, so specialize the general closure explicitly when these scalar
/// values change. Scan needs no data key: its unroll count is the scan input's shape and therefore
/// already participates in MLX's native shape key.
pub fn control_flow_specialization_key(
    nodes: &[NodeDesc],
    api: *const ort::OrtApi,
    kctx: *mut ort::OrtKernelContext,
) -> Result<String, MlxError> {
    fn append(
        nodes: &[NodeDesc],
        api: *const ort::OrtApi,
        kctx: *mut ort::OrtKernelContext,
        out: &mut String,
        ordinal: &mut usize,
    ) -> Result<(), MlxError> {
        for node in nodes {
            match node.op_type.as_str() {
                "If" => {
                    let cond_ref = node
                        .inputs
                        .first()
                        .ok_or_else(|| "control-flow If has no condition input".to_string())?;
                    let cond = host_scalar_bool(cond_ref, api, kctx)?;
                    let current = *ordinal;
                    *ordinal += 1;
                    out.push_str(&format!("I{current}={};", u8::from(cond)));
                    let branch = if cond { "then_branch" } else { "else_branch" };
                    if let Some(sg) = node.subgraphs.iter().find(|sg| sg.attr_name == branch) {
                        append(&sg.nodes, api, kctx, out, ordinal)?;
                    }
                }
                "Loop" => {
                    let trip_ref = node
                        .inputs
                        .first()
                        .ok_or_else(|| "control-flow Loop has no trip-count input".to_string())?;
                    let cond_ref = node
                        .inputs
                        .get(1)
                        .ok_or_else(|| "control-flow Loop has no condition input".to_string())?;
                    let trip = host_scalar_i64(trip_ref, api, kctx)?;
                    let cond = host_scalar_bool(cond_ref, api, kctx)?;
                    let current = *ordinal;
                    *ordinal += 1;
                    out.push_str(&format!("L{current}={trip},{};", u8::from(cond)));
                    if cond
                        && trip > 0
                        && let Some(sg) = node.subgraphs.iter().find(|sg| sg.attr_name == "body")
                    {
                        append(&sg.nodes, api, kctx, out, ordinal)?;
                    }
                }
                "Scan" => {
                    if let Some(sg) = node.subgraphs.iter().find(|sg| sg.attr_name == "body") {
                        append(&sg.nodes, api, kctx, out, ordinal)?;
                    }
                }
                _ => {}
            }
        }
        Ok(())
    }

    let mut key = String::new();
    let mut ordinal = 0;
    append(nodes, api, kctx, &mut key, &mut ordinal)?;
    Ok(key)
}

/// Decide whether the compiled-PREFILL fast path (Phase 2) is allowed for this plan. Prefill is the
/// same decoder subgraph as decode but at query length S>1, so it needs a `GroupQueryAttention` node
/// (the RoPE/KV machinery build declines otherwise) and no control-flow.
///
/// DEFAULT ON. The shape-keyed prefill trace statically narrows attention to the valid prefix
/// `[0, valid_past+S)` (S and valid_past are fixed per shape key), so it no longer runs SDPA over the
/// full KV capacity — the compute-bound waste that made it a ~15-20% TTFT regression. With the
/// narrowing it is byte-identical to eager AND beats eager TTFT, so it is enabled by default. The
/// kill-switch `ONNXRUNTIME_EP_MLX_NO_PREFILL_COMPILE=1` forces eager prefill (A/B numerical validation / safety)
/// without touching the decode path. Also honours the global compile kill-switch. The build itself
/// falls back to eager for any non-decoder / partial-rotary shape.
pub fn prefill_enabled(has_control_flow: bool, nodes: &[NodeDesc]) -> bool {
    if !compile_enabled(has_control_flow) {
        return false;
    }
    if std::env::var_os("ONNXRUNTIME_EP_MLX_NO_PREFILL_COMPILE")
        .map(|v| v != "0" && !v.is_empty())
        .unwrap_or(false)
    {
        return false;
    }
    compile_shape_mode_supported(nodes, false)
        && !nodes.iter().any(reads_data_dependent_parameter)
        && nodes.iter().any(|n| n.op_type == "GroupQueryAttention")
}

/// Query sequence length S = trailing dim of the `input_ids` dynamic ctx input (decode => 1, prefill
/// => prompt length). Scans the plan nodes directly so it works before the compiled closure's
/// `dyn_inputs` list is built. Returns `None` if the input is not found / has no shape.
pub fn detect_seq_len(
    api: *const ort::OrtApi,
    kctx: *mut ort::OrtKernelContext,
    plan: &Plan,
) -> Option<i32> {
    for node in &plan.nodes {
        for inp in &node.inputs {
            if inp.source == Src::CtxInput
                && !inp.constant
                && inp.name == "input_ids"
                && let Ok((shape, _t)) = read_ctx_input_metadata(api, kctx, inp.ctx_index)
            {
                return shape.last().map(|&d| d as i32);
            }
        }
    }
    if plan
        .nodes
        .iter()
        .any(|node| is_separate_qkv_attention(node) || node.op_type == "GroupQueryAttention")
    {
        // Decoder partitions may consume embeddings directly, or a dynamic position-id island may
        // split `input_ids` away from the main partition. The attention partition still receives a
        // rank-3 token-state boundary tensor [B,S,H], whose middle dimension is the query length.
        let mut seen = HashSet::new();
        for inp in plan.nodes.iter().flat_map(|n| n.inputs.iter()) {
            if inp.source == Src::CtxInput
                && !inp.constant
                && seen.insert(inp.ctx_index)
                && let Ok((shape, _t)) = read_ctx_input_metadata(api, kctx, inp.ctx_index)
                && shape.len() == 3
                && shape[1] > 0
            {
                return Some(shape[1] as i32);
            }
        }
    }
    None
}

pub fn detect_batch_size(
    api: *const ort::OrtApi,
    kctx: *mut ort::OrtKernelContext,
    plan: &Plan,
) -> Option<i32> {
    let mut seen = HashSet::new();
    for inp in plan.nodes.iter().flat_map(|node| node.inputs.iter()) {
        if inp.source == Src::CtxInput
            && !inp.constant
            && seen.insert(inp.ctx_index)
            && let Ok((shape, _dtype)) = read_ctx_input_metadata(api, kctx, inp.ctx_index)
            && inp.name == "input_ids"
        {
            return shape.first().map(|&dim| dim as i32);
        }
    }
    None
}

fn kv_sequence_length(shape: &[i64]) -> Option<i64> {
    match shape.len() {
        3 => shape.get(1).copied(),
        4 => shape.get(2).copied(),
        _ => None,
    }
}

/// Sequence length of the first self-attention past-K cache. A zero-length self cache marks a new
/// generation and invalidates cached cross K/V for both contrib MHA and native ONNX Attention.
pub fn detect_attention_self_past_len(
    api: *const ort::OrtApi,
    kctx: *mut ort::OrtKernelContext,
    plan: &Plan,
) -> Option<i64> {
    for node in &plan.nodes {
        let Some(past) = attention_past_key(node) else {
            continue;
        };
        if past.source != Src::CtxInput {
            continue;
        }
        let (shape, _dtype) = read_ctx_input_metadata(api, kctx, past.ctx_index).ok()?;
        return shape.get(2).copied();
    }
    None
}

/// Stable identity for the active attention generation: the first long cross-attention K boundary
/// buffer. Interleaved generators have different buffers, so a key change invalidates private MLX
/// input reuse instead of accidentally sharing one generator's cache with another.
pub fn detect_attention_generation_key(
    api: *const ort::OrtApi,
    kctx: *mut ort::OrtKernelContext,
    plan: &Plan,
) -> Option<usize> {
    for node in &plan.nodes {
        let Some((key, _value)) = attention_cross_kv(node) else {
            continue;
        };
        let (data, shape, _dtype) = read_ctx_input_raw(api, kctx, key.ctx_index).ok()?;
        if kv_sequence_length(&shape).is_some_and(|length| length > 1) {
            return Some(data as usize);
        }
    }
    None
}

/// Detect whether this session drives a fixed-capacity SHARED KV buffer (present aliased onto past at
/// a runtime-owned max length) as opposed to the growing past/present contract. Reads live ctx once
/// at compiled-closure build time: `cap` = a GQA past-KV cache's seq-axis (2) length, and `total` =
/// valid keys after this step (= `total_sequence_length[0]`, or the `attention_mask` width when that
/// scalar is computed in-subgraph). Shared iff `cap > total - S` (the buffer holds more than the
/// valid past). Returns `(shared, mask_ctx_index, total_seq_ctx_index)`. `None` when the shape is not
/// a recognizable decoder GQA.
fn detect_shared_kv(
    plan: &Plan,
    api: *const ort::OrtApi,
    kctx: *mut ort::OrtKernelContext,
) -> Option<(bool, i32, i32)> {
    let s = detect_seq_len(api, kctx, plan).unwrap_or(1);
    let mask_ctx_index = plan
        .nodes
        .iter()
        .flat_map(|n| n.inputs.iter())
        .find(|inp| inp.source == Src::CtxInput && inp.name == "attention_mask")
        .map(|inp| inp.ctx_index as i32)
        .unwrap_or(-1);
    for node in &plan.nodes {
        if node.op_type != "GroupQueryAttention" || node.inputs.len() < 7 {
            continue;
        }
        // Capacity from the past-KV cache (input[3], a ctx input) seq axis.
        let past_k = &node.inputs[3];
        if past_k.source != Src::CtxInput {
            continue;
        }
        let cap = match read_ctx_input_raw(api, kctx, past_k.ctx_index) {
            Ok((_d, shape, _t)) => *shape.get(2)? as i32,
            Err(_) => continue,
        };
        // Valid keys after this step. Prefer total_sequence_length (input[6]) when it arrives as a
        // ctx input; otherwise recover it from the attention_mask width (the in-subgraph scalar is
        // Cast(Gather(Shape(attention_mask),1))).
        let ts = &node.inputs[6];
        let mut total_seq_ctx_index = -1;
        let total = if ts.source == Src::CtxInput && !ts.constant {
            total_seq_ctx_index = ts.ctx_index as i32;
            match read_ctx_input_raw(api, kctx, ts.ctx_index) {
                Ok((data, _shape, _t)) if !data.is_null() => unsafe { *(data as *const i32) },
                _ => continue,
            }
        } else if mask_ctx_index >= 0 {
            match read_ctx_input_raw(api, kctx, mask_ctx_index as usize) {
                Ok((_d, shape, _t)) => *shape.get(1)? as i32,
                Err(_) => continue,
            }
        } else {
            continue;
        };
        if s <= 0 || total < s || total - s > cap {
            return None;
        }
        return Some((cap > total - s, mask_ctx_index, total_seq_ctx_index));
    }
    None
}

/// A contiguous unit-stride slice `[start, stop)` -> owning [`Array`].
fn mk_slice(
    a: mlxsys::mlx_array,
    start: &[i32],
    stop: &[i32],
    stream: mlxsys::mlx_stream,
) -> Result<Array, MlxError> {
    let stride = vec![1i32; start.len()];
    let mut res = unsafe { mlxsys::mlx_array_new() };
    let rc = unsafe {
        mlxsys::mlx_slice(
            &mut res,
            a,
            start.as_ptr(),
            start.len(),
            stop.as_ptr(),
            stop.len(),
            stride.as_ptr(),
            stride.len(),
            stream,
        )
    };
    if rc != 0 {
        unsafe { mlxsys::mlx_array_free(res) };
        return Err("mlx_slice failed".to_string());
    }
    Ok(Array::from_raw(res))
}

/// Concatenate two arrays along `axis` -> owning [`Array`].
fn mk_concat(
    a: mlxsys::mlx_array,
    b: mlxsys::mlx_array,
    axis: i32,
    stream: mlxsys::mlx_stream,
) -> Result<Array, MlxError> {
    let mut v = VectorArray::new();
    v.append(a);
    v.append(b);
    let mut res = unsafe { mlxsys::mlx_array_new() };
    let rc = unsafe { mlxsys::mlx_concatenate_axis(&mut res, v.as_raw(), axis, stream) };
    if rc != 0 {
        unsafe { mlxsys::mlx_array_free(res) };
        return Err("mlx_concatenate_axis failed".to_string());
    }
    Ok(Array::from_raw(res))
}

/// Shape (as `i32`) of a borrowed raw array handle.
fn shape_of(a: mlxsys::mlx_array) -> Vec<i32> {
    let nd = unsafe { mlxsys::mlx_array_ndim(a) };
    let sh = unsafe { mlxsys::mlx_array_shape(a) };
    (0..nd).map(|i| unsafe { *sh.add(i) }).collect()
}

/// The compiled fast path for the given `slot`. Returns:
///   * `Ok(true)`  — the call was handled by the compiled closure (outputs already copied out).
///   * `Ok(false)` — the plan is not compile-eligible (caller must fall back to the eager path).
///   * `Err(_)`    — a hard failure copying results out.
///
/// The plan is accessed through `plan_ptr` (raw) so that NO Rust `&mut Plan` is alive across
/// `mlx_closure_apply` — the trace thunk (invoked synchronously on the first apply / a retrace)
/// needs its own `&mut Plan`, so only one mutable access is ever live at a time (single-threaded,
/// reentrant via FFI, exactly like the C++ EP).
pub fn try_compiled(
    plan_ptr: *mut Plan,
    slot: Slot,
    api: *const ort::OrtApi,
    kctx: *mut ort::OrtKernelContext,
    stream: mlxsys::mlx_stream,
) -> Result<bool, MlxError> {
    let mut cfg = slot.get(unsafe { &*plan_ptr }).config;
    if !slot.get(unsafe { &*plan_ptr }).enabled {
        return Ok(false);
    }
    {
        let nodes: Vec<_> = unsafe { &*plan_ptr }
            .nodes
            .iter()
            .filter(|n| crate::ops::vision::needs_roi_validation(n))
            .cloned()
            .collect();
        if !nodes.is_empty() {
            let mut ctx = TranslationContext::new(unsafe { &mut *plan_ptr }, api, kctx, stream);
            if !crate::ops::vision::validate_compiled_rois(&mut ctx, &nodes)? {
                return Ok(false);
            }
        }
    }
    {
        let nodes: Vec<_> = unsafe { &*plan_ptr }
            .nodes
            .iter()
            .filter(|n| crate::ops::attention::needs_rotary_position_validation(n))
            .cloned()
            .collect();
        if !nodes.is_empty() {
            let mut ctx = TranslationContext::new(unsafe { &mut *plan_ptr }, api, kctx, stream);
            if !crate::ops::attention::validate_compiled_rotary_positions(&mut ctx, &nodes)? {
                return Ok(false);
            }
        }
    }
    let general = &unsafe { &*plan_ptr }.general;
    let host_specialization_token = if slot == Slot::General
        && (general.control_flow_specialization_enabled
            || !general.boundary_shape_inputs.is_empty())
    {
        let mut key = if general.control_flow_specialization_enabled {
            match control_flow_specialization_key(&unsafe { &*plan_ptr }.nodes, api, kctx) {
                Ok(key) => key,
                Err(_) => return Ok(false),
            }
        } else {
            String::new()
        };
        match boundary_shape_specialization_key(&general.boundary_shape_inputs, api, kctx) {
            Ok(parameters) => key.push_str(&parameters),
            Err(error) => {
                log::debug!("boundary shape specialization declined ({error}); using eager");
                return Ok(false);
            }
        }
        if key.is_empty() {
            None
        } else {
            let general = slot.get_mut(unsafe { &mut *plan_ptr });
            if let Some(&token) = general.host_specializations.get(&key) {
                general.host_specialization_key = Some(key);
                Some(token)
            } else if general.host_specializations.len() >= 16 {
                log::debug!("host-value specialization limit reached; using eager for new values");
                return Ok(false);
            } else {
                let token = general.host_specializations.len() as i32 + 1;
                general.host_specializations.insert(key.clone(), token);
                general.host_specialization_key = Some(key);
                Some(token)
            }
        }
    } else {
        None
    };
    {
        let plan = unsafe { &*plan_ptr };
        let has_gqa = plan
            .nodes
            .iter()
            .any(|n| n.op_type == "GroupQueryAttention");
        let has_separate_attention = plan.nodes.iter().any(is_separate_qkv_attention);
        let has_fused_gqa_rope = plan.nodes.iter().any(|n| {
            n.op_type == "GroupQueryAttention" && n.ints.get("do_rotary").copied().unwrap_or(0) != 0
        });
        if has_gqa && !has_fused_gqa_rope {
            // Qwen exports standalone RotaryEmbedding nodes and sets GQA do_rotary=0. Keep the
            // compiled KV-cache machinery, but do not require or synthesize GQA cos/sin inputs.
            cfg.rope_as_data = false;
            slot.get_mut(unsafe { &mut *plan_ptr }).config = cfg;
        }
        if matches!(cfg.shape_mode, ShapeMode::Shapeless) && has_separate_attention && !has_gqa {
            // Separate-QKV Attention decode has no RoPE inputs and uses growing (non-aliased) KV
            // outputs. Its current-token Q/K/V shapes stay fixed at S=1 while concat carries the
            // dynamic past extent inside MLX, so shapeless compile can replay it without GQA's
            // synthetic RoPE or shared-buffer machinery.
            cfg.rope_as_data = false;
            cfg.kv_alias = false;
            cfg.delta_copyout = false;
            cfg.contiguous_outputs = true;
            slot.get_mut(unsafe { &mut *plan_ptr }).config = cfg;
        }
    }
    // One-time discovery + compile. `build_closure` returns `false` for a TRANSIENT decline (the
    // constant cos/sin cache is not resident yet — it is only populated during the first eager
    // translation, which for prefill is the very first Compute call); in that case we leave
    // `attempted` unset so a later call retries once the cache is warm. Every other outcome (compiled,
    // or a permanent ineligibility / compile failure) is terminal and marks `attempted`.
    if !slot.get(unsafe { &*plan_ptr }).attempted {
        // Timing attribution: the one-time trace + `mlx_compile` (compile) phase.
        let terminal = {
            let _phase = crate::trace::tracer().phase("compile");
            build_closure(plan_ptr, slot, api, kctx, stream)
        };
        if terminal {
            slot.get_mut(unsafe { &mut *plan_ptr }).attempted = true;
        }
    }
    if !slot.get(unsafe { &*plan_ptr }).valid {
        return Ok(false);
    }
    if cfg.kv_alias {
        let Some((shared, _, _)) = detect_shared_kv(unsafe { &*plan_ptr }, api, kctx) else {
            return Ok(false);
        };
        if shared != slot.get(unsafe { &*plan_ptr }).shared_kv {
            // Equal-shaped live lengths may switch growing versus fixed-capacity cache semantics.
            // Reuse the closure only for the cache contract it traced.
            return Ok(false);
        }
    }

    // Skip shape-keyed tracing for empty or native-unrepresentable input geometry on EVERY call.
    // Empty traces can abort inside MLX; dimensions beyond i32 cannot be wrapped as MLX arrays.
    // Metadata-only eager consumers need neither operation.
    if cfg.shape_mode == ShapeMode::ShapeKeyed {
        let plan = unsafe { &*plan_ptr };
        for di in &slot.get(plan).dyn_inputs {
            let (shape, _dtype) = read_ctx_input_metadata(api, kctx, di.ctx_index)?;
            if shape.contains(&0) || shape.iter().any(|&dim| dim > i32::MAX as i64) {
                return Ok(false);
            }
        }
    }

    // Gather the dynamic ctx inputs in closure order. Cross-attention K/V are immutable for one
    // generation and very large, so separate-QKV attention decoders copy them into MLX once and
    // reuse them for both contrib MHA and native ONNX Attention.
    let mut arena: Vec<Array> = Vec::new();
    let mut borrowed_inputs = false;
    let mut input = VectorArray::new();
    {
        let dyn_inputs = slot.get(unsafe { &*plan_ptr }).dyn_inputs.clone();
        let has_gqa = unsafe { &*plan_ptr }
            .nodes
            .iter()
            .any(|n| n.op_type == "GroupQueryAttention");
        let stable_cross_indices = if unsafe { &*plan_ptr }.stable_cross_cache_enabled {
            unsafe { &*plan_ptr }.stable_cross_ctx_indices.clone()
        } else {
            Vec::new()
        };
        for di in &dyn_inputs {
            let value = read_ctx_input_value(api, kctx, di.ctx_index)?;
            if let Some(array) = crate::device::array(unsafe { &*api }, value)? {
                input.append(array.as_raw());
                arena.push(array);
                continue;
            }
            if stable_cross_indices.contains(&di.ctx_index)
                && let Some(arr) = slot
                    .get(unsafe { &*plan_ptr })
                    .stable_cross_inputs
                    .get(&di.ctx_index)
            {
                input.append(arr.as_raw());
                continue;
            }
            let (data, shape, dtype) = read_ctx_input_raw(api, kctx, di.ctx_index)?;
            if crate::engine::packed4_signed(dtype).is_some() {
                return Ok(false);
            }
            // Empty boundaries can become read-only K/V through intermediate elementwise ops.
            // Never replay the decoder trace's total-S append or delta writeback for that call.
            if has_gqa && shape.contains(&0) {
                return Ok(false);
            }
            let ishape: Vec<i32> = shape
                .iter()
                .map(|&d| dim_i32(d))
                .collect::<Result<_, _>>()?;
            if stable_cross_indices.contains(&di.ctx_index)
                && kv_sequence_length(&shape).is_some_and(|length| length > 1)
            {
                let arr = Array::from_data(data, &ishape, mlx_dtype_from_onnx(dtype));
                input.append(arr.as_raw());
                slot.get_mut(unsafe { &mut *plan_ptr })
                    .stable_cross_inputs
                    .insert(di.ctx_index, arr);
                continue;
            }
            // Zero-copy wrap of the live ORT input buffer: MLX borrows it (no-op deallocator) for
            // this apply only. `arena` keeps the wrapper alive until after eval + copy-out, and the
            // ORT tensor is valid for the whole Compute call, so the borrow never dangles. In
            // shared-buffer mode this is the per-token past K/V full [B,kv,cap,hd] buffer whose
            // memcpy dominated decode. (ORT KV buffers are 16 KB page-aligned, so Metal takes the
            // true no-copy path; small unaligned inputs fall back to MLX's internal copy.)
            let arr = Array::from_data_managed(data, &ishape, mlx_dtype_from_onnx(dtype));
            borrowed_inputs = true;
            input.append(arr.as_raw());
            arena.push(arr);
        }
        if let Some(token) = host_specialization_token {
            let zeros = vec![0u8; token as usize];
            let arr = Array::from_data(
                zeros.as_ptr() as *const std::os::raw::c_void,
                &[token],
                mlxsys::mlx_dtype__MLX_UINT8,
            );
            input.append(arr.as_raw());
            arena.push(arr);
        }
    }

    // ---- decode/prefill prologue: position + valid_past fed as DATA ------------------------------
    // `past` = RoPE start = position of the new query rows, and `s` = number of new rows (1 for
    // decode, S for prefill). Both the synth cos/sin rows and the KV delta below use them.
    let mut past: i32 = 0;
    let mut s_new: i32 = 0;
    if cfg.rope_as_data || cfg.kv_alias {
        let s = detect_seq_len(api, kctx, unsafe { &*plan_ptr }).unwrap_or(1);
        s_new = s;
        // Current RoPE start. In the growing contract this is the past-KV sequence length (past_k's
        // seq axis). In the shared-buffer contract past_k's seq axis is the fixed capacity, so the
        // true position is the VALID past = attention_mask width - S. Fed as DATA via the rows below.
        past = {
            let plan = unsafe { &*plan_ptr };
            let c = slot.get(plan);
            if c.shared_kv && c.total_seq_ctx_index >= 0 {
                let (data, _shape, _t) =
                    read_ctx_input_raw(api, kctx, c.total_seq_ctx_index as usize)?;
                if data.is_null() {
                    return Ok(false);
                }
                (unsafe { *(data as *const i32) }) - s
            } else if c.shared_kv && c.mask_ctx_index >= 0 {
                let (shape, _t) = read_ctx_input_metadata(api, kctx, c.mask_ctx_index as usize)?;
                (*shape.get(1).unwrap_or(&0) as i32) - s
            } else {
                if c.rope_past_ctx_index < 0 {
                    return Ok(false);
                }
                let idx = c.rope_past_ctx_index as usize;
                let axis = c.rope_past_axis as usize;
                let (shape, _t) = read_ctx_input_metadata(api, kctx, idx)?;
                if axis < shape.len() {
                    shape[axis] as i32
                } else {
                    0
                }
            }
        };

        if cfg.rope_as_data {
            // Pre-slice each fused-GQA RoPE cos/sin cache outside the compiled graph.
            let synth = slot.get(unsafe { &*plan_ptr }).synth_ropes.clone();
            for sr in &synth {
                let cache_raw = match unsafe { &*plan_ptr }.cache.get(&sr.cache_name) {
                    Some(a) => a.as_raw(),
                    None => return Ok(false),
                };
                let half = shape_of(cache_raw).get(1).copied().unwrap_or(0);
                if half == 0 {
                    return Ok(false);
                }
                let row = mk_slice(cache_raw, &[past, 0], &[past + s, half], stream)?;
                let full = mk_concat(row.as_raw(), row.as_raw(), 1, stream)?;
                input.append(full.as_raw());
                arena.push(row);
                arena.push(full);
            }
        }

        // Shared-buffer contract: append the live `valid_past` (= `past`) as a [1] int32 closure
        // input AFTER the synth RoPE rows. The compiled GQA op reads it to place the in-place K/V
        // write and build the causal mask — as pure per-step data, so shapeless compile never freezes
        // the growing offset. Kept in the arena for the apply's life.
        if slot.get(unsafe { &*plan_ptr }).shared_kv {
            let vp = [past];
            let arr = Array::from_data(
                vp.as_ptr() as *const std::os::raw::c_void,
                &[1],
                mlxsys::mlx_dtype__MLX_INT32,
            );
            input.append(arr.as_raw());
            arena.push(arr);
        }
    }

    // Refresh the live kernel context on the trace payload (only read during a (re)trace).
    unsafe {
        if let Some(p) = slot.get_mut(&mut *plan_ptr).payload.as_mut() {
            p.kctx = kctx;
        }
    }

    // Take the compiled closure OUT of the plan so no `&mut Plan` field is borrowed across apply
    // (the trace thunk mutates the plan through `plan_ptr`).
    let closure = match slot.get_mut(unsafe { &mut *plan_ptr }).closure.take() {
        Some(c) => c,
        None => return Ok(false),
    };
    let apply_res = closure.apply(&input);
    slot.get_mut(unsafe { &mut *plan_ptr }).closure = Some(closure);

    let outs = match apply_res {
        Ok(v) => v,
        Err(e) => {
            if crate::trace::tracer().active() {
                log::debug!("compiled closure apply failed ({e}); falling back to eager");
            }
            slot.get_mut(unsafe { &mut *plan_ptr }).valid = false; // disable, fall back to eager
            return Ok(false);
        }
    };
    // Timing attribution: the synchronous GPU-inclusive eval phase of the compiled closure.
    let tr = crate::trace::tracer();
    let eval_t0 = if tr.active() {
        Some(std::time::Instant::now())
    } else {
        None
    };
    let ext_len = slot.get(unsafe { &*plan_ptr }).ext_outputs.len();
    if outs.size() != ext_len {
        return Err("MLX compiled closure returned an unexpected output count".into());
    }
    let mut device_outputs = !borrowed_inputs && ext_len > 0;
    if device_outputs {
        for i in 0..ext_len {
            let output = &slot.get(unsafe { &*plan_ptr }).ext_outputs[i];
            device_outputs &= publish_device_output(api, kctx, output, &outs.get(i))?;
        }
    }
    // A captured boundary must finish before its guard stops Metal capture.
    let capture = tr.begin_gpu_capture();
    let eval_result = if device_outputs && capture.is_none() {
        let result = unsafe { mlxsys::mlx_async_eval(outs.as_raw()) };
        if result == 0 {
            Ok(())
        } else {
            Err("MLX compiled device evaluation failed; inspect GPU availability".into())
        }
    } else {
        let _eval_region = tr.eval_region();
        mlx::eval(&outs)
    };
    if let Err(error) = eval_result {
        slot.get_mut(unsafe { &mut *plan_ptr }).valid = false;
        if crate::device::enabled() {
            return Err(format!(
                "MLX compiled evaluation failed: {error}; device state may have changed, so reinitialize bound state tensors before retrying with sufficient GPU memory"
            ));
        }
        return Ok(false);
    }
    if let Some(t0) = eval_t0 {
        tr.record_phase("eval", t0.elapsed());
    }

    let ext_len = slot.get(unsafe { &*plan_ptr }).ext_outputs.len();
    if outs.size() != ext_len {
        slot.get_mut(unsafe { &mut *plan_ptr }).valid = false;
        return Ok(false);
    }

    // Shared-buffer KV `present` outputs alias `past` in ORT memory, so their per-call copy-out is a
    // delta write of only the `S` new rows at axis-2 offset `past` instead of the whole
    // [B,kv,cap,hd] buffer (the O(1) copy-out win). The write is gated on the present buffer actually
    // matching the recorded `past` pointer, so a non-aliasing runtime safely takes the full copy.
    // Empty set (growing / non-delta paths) => every output takes the full copy.
    let kv_present: Vec<(String, usize)> = if cfg.delta_copyout {
        slot.get(unsafe { &*plan_ptr }).kv_present_names.clone()
    } else {
        Vec::new()
    };
    for i in 0..ext_len {
        let a = outs.get(i);
        // Clone the small OutRef so we don't hold a plan borrow across the copy-out FFI.
        let o: OutRef = slot.get(unsafe { &*plan_ptr }).ext_outputs[i].clone();
        let delta = kv_present
            .iter()
            .find(|(name, _)| *name == o.name)
            .and_then(|(_, past_ctx)| {
                read_ctx_input_raw(api, kctx, *past_ctx)
                    .ok()
                    .map(|(p, _, _)| DeltaWrite {
                        axis: 2,
                        offset: past as i64,
                        count: s_new as i64,
                        alias_ptr: p as usize,
                    })
            });
        copy_out_raw_delta(api, kctx, &o, a.as_raw(), delta)?;
    }
    // `arena` (transient per-call inputs) drops here — after eval + copy-out have consumed them.
    Ok(true)
}

/// One-time discovery + compile of the closure for `slot`. Populates the slot's `dyn_inputs`
/// (ordered dynamic ctx inputs = closure inputs) and `ext_outputs` (boundary outputs in append
/// order); for `rope_as_data` slots it also discovers `synth_ropes`, the RoPE-start source, and the
/// shared-KV contract. Compiles the closure (shapeless iff `shape_mode == Shapeless`). Leaves
/// `valid = false` (=> caller falls back to eager) if the plan is not eligible or the compile fails.
///
/// Returns whether the outcome is TERMINAL: `true` after a successful compile or a permanent
/// ineligibility, `false` for a transient decline (the constant cos/sin cache is not resident yet)
/// so the caller retries on a later call once the constants have been warmed by an eager translation.
fn build_closure(
    plan_ptr: *mut Plan,
    slot: Slot,
    api: *const ort::OrtApi,
    kctx: *mut ort::OrtKernelContext,
    stream: mlxsys::mlx_stream,
) -> bool {
    let cfg = slot.get(unsafe { &*plan_ptr }).config;
    if !compile_shape_mode_supported(
        &unsafe { &*plan_ptr }.nodes,
        cfg.shape_mode == ShapeMode::Shapeless,
    ) {
        return true;
    }

    let mut dyn_inputs: Vec<DynInput> = Vec::new();
    let mut ext_outputs: Vec<OutRef> = Vec::new();
    let mut synth_ropes: Vec<SynthRope> = Vec::new();
    let mut rope_past: i32 = -1;
    {
        let plan = unsafe { &*plan_ptr };
        // Ordered, de-duplicated dynamic (non-constant) ctx inputs = the closure input vector.
        let mut seen: HashSet<String> = HashSet::new();
        fn collect_body_inputs(
            nodes: &[NodeDesc],
            seen: &mut HashSet<String>,
            dyn_inputs: &mut Vec<DynInput>,
        ) {
            for node in nodes {
                for (i, inp) in node.inputs.iter().enumerate() {
                    if node.op_type == "CastLike" && i == 1 {
                        // Only the statically declared dtype is used, never target data or shape.
                        continue;
                    }
                    if inp.source == Src::CtxInput && !inp.constant && seen.insert(inp.name.clone())
                    {
                        dyn_inputs.push(DynInput {
                            name: inp.name.clone(),
                            ctx_index: inp.ctx_index,
                        });
                    }
                }
                if matches!(node.op_type.as_str(), "If" | "Loop" | "Scan") {
                    for sg in &node.subgraphs {
                        collect_body_inputs(&sg.nodes, seen, dyn_inputs);
                    }
                }
            }
        }
        for node in &plan.nodes {
            for (i, inp) in node.inputs.iter().enumerate() {
                if node.op_type == "CastLike" && i == 1 {
                    // Other consumers still collect this input if they need its live data.
                    continue;
                }
                if inp.source == Src::CtxInput && !inp.constant && seen.insert(inp.name.clone()) {
                    dyn_inputs.push(DynInput {
                        name: inp.name.clone(),
                        ctx_index: inp.ctx_index,
                    });
                    if (cfg.rope_as_data || cfg.kv_alias)
                        && rope_past < 0
                        && inp.name.contains(".key")
                    {
                        rope_past = inp.ctx_index as i32;
                    }
                }
            }
            if rope_past < 0 {
                rope_past = plan
                    .nodes
                    .iter()
                    .find(|n| n.op_type == "GroupQueryAttention" && n.inputs.len() > 3)
                    .map(|n| &n.inputs[3])
                    .filter(|inp| inp.source == Src::CtxInput && !inp.constant)
                    .map(|inp| inp.ctx_index as i32)
                    .unwrap_or(-1);
            }
            if slot == Slot::General && matches!(node.op_type.as_str(), "If" | "Loop" | "Scan") {
                for sg in &node.subgraphs {
                    collect_body_inputs(&sg.nodes, &mut seen, &mut dyn_inputs);
                }
            }
        }
        // External boundary outputs, in stable node/output order.
        for node in &plan.nodes {
            for o in &node.outputs {
                if o.external {
                    ext_outputs.push(o.clone());
                }
            }
        }
        // Distinct RoPE cos/sin caches (GQA inputs[7]/[8] when do_rotary). Their per-step rows are
        // fed as synthetic closure inputs so the compiled graph never slices a cache at runtime.
        if cfg.rope_as_data {
            let mut synth_seen: HashSet<String> = HashSet::new();
            for node in &plan.nodes {
                if node.op_type != "GroupQueryAttention" || node.inputs.len() < 9 {
                    continue;
                }
                let do_rotary = node.ints.get("do_rotary").copied().unwrap_or(0) != 0;
                if !do_rotary {
                    continue;
                }
                for idx in [7usize, 8usize] {
                    let nm = &node.inputs[idx].name;
                    if synth_seen.insert(nm.clone()) {
                        synth_ropes.push(SynthRope {
                            key: rope_row_key(nm),
                            cache_name: node.inputs[idx].cache_key(),
                        });
                    }
                }
            }
        }
    }

    // Need at least one dynamic input (else the graph is fully constant — cheap to leave eager) and
    // at least one boundary output. `rope_as_data` slots additionally need the decoder RoPE shape.
    if dyn_inputs.is_empty() || ext_outputs.is_empty() {
        if crate::trace::tracer().active() {
            log::debug!(
                "compiled closure declined: {} dynamic inputs, {} external outputs",
                dyn_inputs.len(),
                ext_outputs.len()
            );
        }
        return true;
    }
    if cfg.rope_as_data && (rope_past < 0 || synth_ropes.is_empty()) {
        return true; // not the expected decoder shape; stay on the eager path
    }

    let mut shared_kv = false;
    let mut mask_ctx_index = -1;
    let mut total_seq_ctx_index = -1;
    if cfg.rope_as_data {
        // The compiled RoPE uses a [hd,hd] rotate-half matmul, which requires rotary_dim == head_dim
        // (rot == hd). Validate from the live ctx (head dim = last axis of the past-KV cache) vs the
        // cos cache width (half); fall back to eager for a partial-rotary head.
        let hd = match read_ctx_input_raw(api, kctx, rope_past as usize) {
            Ok((_d, shape, _t)) => shape.last().copied().unwrap_or(0) as i32,
            Err(_) => 0,
        };
        // The cos/sin cache is a constant only loaded into `plan.cache` during an eager translation.
        // On the very first Compute call (prefill) it is not resident yet — a TRANSIENT condition, so
        // return non-terminal and retry once the constants are warm (the apply prologue needs them
        // too). A resident cache with the wrong width is a PERMANENT decline (partial-rotary head).
        let (half, cache_resident) = {
            let plan = unsafe { &*plan_ptr };
            match plan.cache.get(&synth_ropes[0].cache_name) {
                Some(a) => {
                    let sh = a.shape();
                    (if sh.len() >= 2 { sh[1] as i32 } else { 0 }, true)
                }
                None => (0, false),
            }
        };
        if !cache_resident {
            if crate::trace::tracer().active() {
                log::debug!("compiled closure deferred: RoPE cache is not resident");
            }
            return false; // transient — constants not warmed yet
        }
        if hd == 0 || half == 0 || 2 * half != hd {
            if crate::trace::tracer().active() {
                log::debug!(
                    "compiled closure declined: unsupported RoPE width (head={hd}, half={half})"
                );
            }
            return true; // partial-rotary head not supported by the compiled path
        }
    }
    if cfg.kv_alias {
        // Detect a fixed-capacity shared KV buffer once from live ctx. In that contract GQA writes
        // the new K/V in place at the (data-dependent) valid-past offset and emits present at the
        // buffer's full capacity — the trace handles both via a data start index + a static-shape
        // additive mask, so the compiled fast path still applies.
        let detected =
            detect_shared_kv(unsafe { &*plan_ptr }, api, kctx).unwrap_or((false, -1, -1));
        shared_kv = detected.0;
        mask_ctx_index = detected.1;
        total_seq_ctx_index = detected.2;
    }

    // Publish the discovered schema + a stable trace payload onto the slot.
    unsafe {
        let c = slot.get_mut(&mut *plan_ptr);
        c.config = cfg;
        c.shared_kv = shared_kv;
        c.mask_ctx_index = mask_ctx_index;
        c.total_seq_ctx_index = total_seq_ctx_index;
        c.dyn_inputs = dyn_inputs;
        c.ext_outputs = ext_outputs;
        c.synth_ropes = synth_ropes;
        c.rope_past_ctx_index = rope_past;
        c.rope_past_axis = 2;
        c.payload = Some(Box::new(TracePayload {
            plan: plan_ptr,
            ort_api: api,
            kctx,
            stream,
            slot,
        }));
    }

    let payload_ptr: *mut c_void = unsafe {
        slot.get_mut(&mut *plan_ptr)
            .payload
            .as_mut()
            .unwrap()
            .as_mut() as *mut TracePayload as *mut c_void
    };
    // Shapeless (decode) so the growing KV length never triggers a recompile; shape-keyed (general /
    // prefill) so a changed input shape safely retraces (re-invokes the thunk) rather than
    // miscomputes. The trace thunk runs lazily on the first `apply`.
    let shapeless = matches!(cfg.shape_mode, ShapeMode::Shapeless);
    let base = Closure::new_func_payload(trace_thunk, payload_ptr);
    match Closure::compile(&base, shapeless) {
        Ok(compiled) => {
            let c = slot.get_mut(unsafe { &mut *plan_ptr });
            c.closure = Some(compiled);
            c.valid = true;
        }
        Err(e) => {
            if crate::trace::tracer().active() {
                log::debug!("compiled closure creation failed ({e}); falling back to eager");
            }
        }
    }
    true
}

/// `mlx_closure` trace thunk (payload = [`TracePayload`]): seed each dynamic ctx input (+ pre-sliced
/// RoPE row / valid_past for `rope_as_data`) from the closure input vector, translate the whole
/// subgraph, and return the cast external boundary outputs. Invoked lazily by mlx on the first
/// `apply` and again after any input shape change (shape-keyed slots). Never unwinds across the FFI
/// boundary.
extern "C" fn trace_thunk(
    out: *mut mlxsys::mlx_vector_array,
    input: mlxsys::mlx_vector_array,
    payload: *mut c_void,
) -> std::os::raw::c_int {
    let result = std::panic::catch_unwind(AssertUnwindSafe(|| trace_body(out, input, payload)));
    match result {
        Ok(Ok(())) => 0,
        Ok(Err(e)) => {
            // Correctness fallback (compiled trace failed → eager). Env-gated so a traced-off run
            // stays silent; the eager path still produces the correct result.
            if crate::trace::tracer().active() {
                log::debug!("compiled trace failed ({e}); falling back to eager");
            }
            1
        }
        Err(payload) => {
            // A panic inside the trace thunk is unexpected (a bug), unlike the benign trace-failed
            // decline above — never keep it silent, even with tracing off. The eager path below
            // still produces a correct result.
            let detail = crate::panic_payload_message(&payload);
            log::warn!(
                "compiled trace panicked ({detail}); falling back to the eager path (result still correct)"
            );
            1
        }
    }
}

fn trace_body(
    out: *mut mlxsys::mlx_vector_array,
    input: mlxsys::mlx_vector_array,
    payload: *mut c_void,
) -> Result<(), MlxError> {
    let pl = unsafe { &mut *(payload as *mut TracePayload) };
    let plan_ptr = pl.plan;
    let api = pl.ort_api;
    let kctx = pl.kctx;
    let stream = pl.stream;
    let slot = pl.slot;

    // Snapshot the closure schema (clone the small metadata) so no plan borrow overlaps translation.
    let (cfg, dyn_inputs, synth_ropes, ext_outputs, shared_kv) = {
        let c = slot.get(unsafe { &*plan_ptr });
        (
            c.config,
            c.dyn_inputs.clone(),
            c.synth_ropes.clone(),
            c.ext_outputs.clone(),
            c.shared_kv,
        )
    };

    // Shape-keyed (prefill) shared-buffer traces know `valid_past` statically for this shape key (the
    // attention_mask width = valid_past + S is part of the key). Recover it here from the live ctx so
    // the compiled attention can be narrowed to the valid prefix `[0, valid_past+S)` instead of masked
    // over the full KV capacity. Only enable the narrow path when valid_past is reliably known (mask
    // width available and non-negative); otherwise leave `narrow` off so the trace keeps the safe
    // full-cap masked path. Shapeless (decode) traces feed valid_past as data and never narrow.
    let mut narrow = false;
    let mut static_valid_past = 0i32;
    if matches!(cfg.shape_mode, ShapeMode::ShapeKeyed) && cfg.rope_as_data && shared_kv {
        let s = detect_seq_len(api, kctx, unsafe { &*plan_ptr }).unwrap_or(1);
        let mask_idx = slot.get(unsafe { &*plan_ptr }).mask_ctx_index;
        if mask_idx >= 0
            && let Ok((_d, shape, _t)) = read_ctx_input_raw(api, kctx, mask_idx as usize)
        {
            let vp = (*shape.get(1).unwrap_or(&0) as i32) - s;
            if vp >= 0 {
                narrow = true;
                static_valid_past = vp;
            }
        }
    }

    let (res_raw, arena, kv_present) = {
        let plan = unsafe { &mut *plan_ptr };
        let native_attention_decode = plan.native_attention_decode;
        let mut tc = TranslationContext::new(plan, api, kctx, stream);
        // Independent of the branch below: record ShapeKeyed-ness alone (general/prefill vs decode),
        // for opt-in fast kernels that must never run during (Shapeless) decode.
        tc.set_shape_keyed_compile(matches!(cfg.shape_mode, ShapeMode::ShapeKeyed));
        if (matches!(cfg.shape_mode, ShapeMode::Shapeless) && native_attention_decode)
            || cfg.rope_as_data
            || cfg.kv_alias
        {
            // RoPE uses the pre-sliced cos/sin ROW placeholders + a matmul rotate-half, so the graph
            // carries no dynamic Slice (which shapeless `mlx_compile` cannot shape-infer).
            tc.set_compiled_trace(shared_kv, narrow, static_valid_past);
        } else {
            // General trace: dynamic inputs are shapeless placeholders with no host data, so any
            // mid-graph host eval must fail the trace cleanly (=> eager) rather than eval a tracer.
            tc.set_general_trace();
        }

        // Seed the dynamic ctx input placeholders (closure inputs [0..ndyn)).
        let ndyn = dyn_inputs.len();
        for (i, di) in dyn_inputs.iter().enumerate() {
            let mut a = unsafe { mlxsys::mlx_array_new() };
            unsafe { mlxsys::mlx_vector_array_get(&mut a, input, i) };
            let raw = tc.keep(Array::from_raw(a));
            tc.seed(di.name.clone(), raw);
        }
        if cfg.rope_as_data {
            // Seed the pre-sliced RoPE cos/sin row placeholders (closure inputs [ndyn..ndyn+nsynth)).
            for (j, sr) in synth_ropes.iter().enumerate() {
                let mut a = unsafe { mlxsys::mlx_array_new() };
                unsafe { mlxsys::mlx_vector_array_get(&mut a, input, ndyn + j) };
                let raw = tc.keep(Array::from_raw(a));
                tc.seed(sr.key.clone(), raw);
            }
        }
        // Shared-buffer: seed the live `valid_past` scalar after any synthetic RoPE rows.
        if shared_kv {
            let mut a = unsafe { mlxsys::mlx_array_new() };
            unsafe { mlxsys::mlx_vector_array_get(&mut a, input, ndyn + synth_ropes.len()) };
            let raw = tc.keep(Array::from_raw(a));
            tc.seed(crate::engine::GQA_VALID_PAST_KEY.to_string(), raw);
        }

        let res = tc.run_trace(&ext_outputs, cfg.contiguous_outputs)?;
        let kv_present = tc.take_compiled_kv_present();
        let arena = tc.take_arena();
        (res.into_raw(), arena, kv_present)
        // `tc` dropped here (its arena already taken) — releases the plan borrow.
    };

    // Hand the trace's transient handles + discovered KV-present set to the slot so the compiled
    // graph (walked after this thunk returns) keeps its inputs alive; they are freed once with the
    // plan.
    unsafe {
        let c = slot.get_mut(&mut *plan_ptr);
        c.trace_transient.extend(arena);
        if cfg.delta_copyout {
            c.kv_present_names = kv_present;
        }
        *out = res_raw;
    }
    Ok(())
}

#[cfg(test)]
mod shape_inference_tests {
    use super::*;
    use crate::engine::TensorRef;

    fn input(name: &str, source: Src) -> TensorRef {
        TensorRef {
            name: name.to_string(),
            source,
            ctx_index: 0,
            constant: false,
            shape_const: false,
            init: None,
        }
    }

    fn cumulative(op_type: &str, axis_source: Src, reason: &'static str) -> NodeDesc {
        let mut node = NodeDesc::new(
            op_type.to_string(),
            String::new(),
            14,
            CompileShapeSafety::ShapeKeyedOnly { reason },
        );
        node.inputs = vec![input("data", Src::CtxInput), input("axis", axis_source)];
        node
    }

    #[test]
    fn general_capture_allows_only_cache_free_standard_attention() {
        let mut node = NodeDesc::new(
            "Attention".to_string(),
            String::new(),
            23,
            CompileShapeSafety::Shapeless,
        );
        node.inputs = ["q", "k", "v", "mask"]
            .into_iter()
            .map(|name| input(name, Src::CtxInput))
            .collect();
        assert!(!is_general_compile_unsafe(&node));
        node.inputs.push(input("", Src::Absent));
        node.inputs.push(input("", Src::Absent));
        assert!(!is_general_compile_unsafe(&node));
        node.inputs[4] = input("past_key", Src::CtxInput);
        assert!(is_general_compile_unsafe(&node));
        node.inputs[4] = input("", Src::Absent);
        node.inputs[5] = input("past_value", Src::CtxInput);
        assert!(is_general_compile_unsafe(&node));
        node.inputs[5] = input("", Src::Absent);
        node.outputs = vec![
            OutRef {
                name: "output".to_string(),
                external: true,
                ctx_index: 0,
                otype: ort::ONNXTensorElementDataType_ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT,
            },
            OutRef {
                name: "present_key".to_string(),
                external: true,
                ctx_index: 1,
                otype: ort::ONNXTensorElementDataType_ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT,
            },
        ];
        assert!(is_general_compile_unsafe(&node));
        node.outputs.pop();
        assert!(!is_general_compile_unsafe(&node));
        node.op_type = "MultiHeadAttention".to_string();
        node.domain = "com.microsoft".to_string();
        assert!(is_general_compile_unsafe(&node));
        node.op_type = "Attention".to_string();
        node.domain.clear();
        node.compile_shape_safety = CompileShapeSafety::EagerOnly {
            reason: "composed attention form",
        };
        assert!(!compile_shape_mode_supported(&[node], false));
    }

    #[test]
    fn missing_output_shape_emitters_require_shape_specialized_compile() {
        for node in [
            NodeDesc::new(
                "CumSum".to_string(),
                String::new(),
                14,
                CompileShapeSafety::ShapeKeyedOnly {
                    reason: crate::registry::MLX_SCAN_SHAPE_REASON,
                },
            ),
            NodeDesc::new(
                "CumProd".to_string(),
                String::new(),
                26,
                CompileShapeSafety::ShapeKeyedOnly {
                    reason: crate::registry::MLX_SCAN_SHAPE_REASON,
                },
            ),
            NodeDesc::new(
                "Slice".to_string(),
                String::new(),
                14,
                CompileShapeSafety::ShapeKeyedOnly {
                    reason: crate::registry::MLX_SLICE_SHAPE_REASON,
                },
            ),
            NodeDesc::new(
                "LinearAttention".to_string(),
                "com.microsoft".to_string(),
                1,
                CompileShapeSafety::ShapeKeyedOnly {
                    reason: "emits MLX Slice, Pad, and Scan",
                },
            ),
            NodeDesc::new(
                "QMoE".to_string(),
                "com.microsoft".to_string(),
                1,
                CompileShapeSafety::ShapeKeyedOnly {
                    reason: crate::registry::MLX_SLICE_SHAPE_REASON,
                },
            ),
        ] {
            assert!(!node_compile_shape_safety(&node).allows_shapeless());
        }
        assert!(
            node_compile_shape_safety(&NodeDesc::new(
                "Add".to_string(),
                String::new(),
                14,
                CompileShapeSafety::Shapeless,
            ))
            .allows_shapeless()
        );
        assert!(compile_shape_mode_supported(
            &[NodeDesc::new(
                "CumSum".to_string(),
                String::new(),
                14,
                CompileShapeSafety::ShapeKeyedOnly {
                    reason: crate::registry::MLX_SCAN_SHAPE_REASON,
                },
            )],
            false
        ));
        assert!(!compile_shape_mode_supported(
            &[NodeDesc::new(
                "CumSum".to_string(),
                String::new(),
                14,
                CompileShapeSafety::ShapeKeyedOnly {
                    reason: crate::registry::MLX_SCAN_SHAPE_REASON,
                },
            )],
            true
        ));
        let eager_only_gqa = NodeDesc::new(
            "GroupQueryAttention".to_string(),
            "com.microsoft".to_string(),
            1,
            CompileShapeSafety::EagerOnly {
                reason: "attention-bias form",
            },
        );
        assert!(!compile_shape_mode_supported(
            std::slice::from_ref(&eager_only_gqa),
            false
        ));
        assert!(!compile_shape_mode_supported(&[eager_only_gqa], true));
    }

    #[test]
    fn constant_of_shape_guards_the_first_input() {
        let mut node = NodeDesc::new(
            "ConstantOfShape".to_string(),
            String::new(),
            23,
            CompileShapeSafety::ShapeKeyedOnly {
                reason: "runtime fill shape requires shape/value guards",
            },
        );
        let mut shape = input("shape", Src::CtxInput);
        shape.ctx_index = 3;
        node.inputs = vec![shape];
        assert!(reads_data_dependent_parameter(&node));
        assert!(!reads_unguarded_parameter(&node, true));
        assert_eq!(boundary_shape_inputs(std::slice::from_ref(&node)), vec![3]);
        assert!(!compile_shape_mode_supported(
            std::slice::from_ref(&node),
            true
        ));
        node.inputs[0].source = Src::Intermediate;
        assert!(reads_unguarded_parameter(&node, true));
        node.inputs[0].shape_const = true;
        assert!(!reads_unguarded_parameter(&node, true));
    }

    #[test]
    fn affine_grid_size_values_guard_general_capture_only() {
        let mut node = NodeDesc::new(
            "AffineGrid".to_string(),
            String::new(),
            20,
            CompileShapeSafety::ShapeKeyedOnly {
                reason: "host-generated homogeneous grid",
            },
        );
        let mut size = input("size", Src::CtxInput);
        size.ctx_index = 1;
        node.inputs = vec![input("theta", Src::CtxInput), size];
        assert!(reads_data_dependent_parameter(&node));
        assert!(!reads_unguarded_parameter(&node, true));
        assert_eq!(boundary_shape_inputs(std::slice::from_ref(&node)), vec![1]);
        assert!(!compile_shape_mode_supported(
            std::slice::from_ref(&node),
            true
        ));
        node.inputs[1].source = Src::Intermediate;
        assert!(reads_unguarded_parameter(&node, true));
        assert!(boundary_shape_inputs(std::slice::from_ref(&node)).is_empty());
        node.inputs[1].shape_const = true;
        assert!(!reads_data_dependent_parameter(&node));
        node.inputs[1].shape_const = false;
        node.inputs[1].source = Src::Initializer;
        assert!(!reads_data_dependent_parameter(&node));
    }

    #[test]
    fn max_unpool_host_reads_require_constant_indices_and_output_shape() {
        let mut node = NodeDesc::new(
            "MaxUnpool".to_string(),
            String::new(),
            22,
            CompileShapeSafety::ShapeKeyedOnly {
                reason: "host-validated indices and output shape",
            },
        );
        node.inputs = vec![
            input("x", Src::CtxInput),
            input("indices", Src::Initializer),
            input("shape", Src::Initializer),
        ];
        assert!(!reads_unguarded_parameter(&node, true));
        for index in [1, 2] {
            for source in [Src::CtxInput, Src::Intermediate] {
                node.inputs[index].source = source;
                assert!(reads_unguarded_parameter(&node, true));
                assert!(reads_data_dependent_parameter(&node));
            }

            node.inputs[index].source = Src::Initializer;
        }
    }

    #[test]
    fn col2im_host_geometry_requires_constant_controls() {
        let mut node = NodeDesc::new(
            "Col2Im".to_string(),
            String::new(),
            18,
            CompileShapeSafety::ShapeKeyedOnly {
                reason: "host-read image and block shapes",
            },
        );
        node.inputs = vec![
            input("x", Src::CtxInput),
            input("image", Src::Initializer),
            input("block", Src::Initializer),
        ];
        assert!(!reads_unguarded_parameter(&node, true));
        for index in [1, 2] {
            for source in [Src::CtxInput, Src::Intermediate] {
                node.inputs[index].source = source;
                assert!(reads_unguarded_parameter(&node, true));
                assert!(reads_data_dependent_parameter(&node));
            }
            node.inputs[index].source = Src::Initializer;
        }
    }

    #[test]
    fn tensor_scatter_never_captures_live_write_indices() {
        let mut node = NodeDesc::new(
            "TensorScatter".to_string(),
            String::new(),
            24,
            CompileShapeSafety::ShapeKeyedOnly {
                reason: crate::registry::MLX_SLICE_SHAPE_REASON,
            },
        );
        node.inputs = vec![
            input("past", Src::CtxInput),
            input("update", Src::CtxInput),
            input("indices", Src::Initializer),
        ];
        assert!(!reads_data_dependent_parameter(&node));
        for source in [Src::CtxInput, Src::Intermediate] {
            node.inputs[2].source = source;
            assert!(reads_data_dependent_parameter(&node));
            assert!(reads_unguarded_parameter(&node, true));
            assert!(boundary_shape_inputs(std::slice::from_ref(&node)).is_empty());
            assert!(!general_enabled(false, std::slice::from_ref(&node)));
        }
        node.inputs[2].source = Src::Absent;
        assert!(!reads_data_dependent_parameter(&node));
    }

    #[test]
    fn reduction_axes_are_guarded_for_all_compile_routes() {
        for op in [
            "ReduceSum",
            "ReduceMean",
            "ReduceMax",
            "ReduceMin",
            "ReduceProd",
            "ReduceSumSquare",
            "ReduceL1",
            "ReduceL2",
            "ReduceLogSum",
            "ReduceLogSumExp",
        ] {
            for source in [Src::CtxInput, Src::Intermediate] {
                let mut node = cumulative(op, source, "runtime reduction axes");
                assert!(reads_data_dependent_parameter(&node));
                assert!(reads_unguarded_parameter(&node, true));
                assert!(!compile_shape_mode_supported(
                    std::slice::from_ref(&node),
                    true
                ));
                assert!(compile_shape_mode_supported(
                    std::slice::from_ref(&node),
                    false
                ));
                if source == Src::Intermediate {
                    node.inputs[1].shape_const = true;
                    assert!(!reads_unguarded_parameter(&node, true));
                }
            }
            let node = cumulative(op, Src::Initializer, "runtime reduction axes");
            assert!(!reads_data_dependent_parameter(&node));
        }
    }

    #[test]
    fn signal_parameters_require_value_keys_or_eager_execution() {
        for (op, parameters) in [
            ("DFT", vec![1, 2]),
            ("STFT", vec![1, 3]),
            ("HannWindow", vec![0]),
            ("HammingWindow", vec![0]),
            ("BlackmanWindow", vec![0]),
        ] {
            let mut node = NodeDesc::new(
                op.to_string(),
                String::new(),
                20,
                CompileShapeSafety::ShapeKeyedOnly {
                    reason: "host-read signal controls",
                },
            );
            node.inputs = (0..4)
                .map(|i| {
                    let mut r = input(&format!("input{i}"), Src::CtxInput);
                    r.ctx_index = i;
                    r
                })
                .collect();
            assert!(reads_data_dependent_parameter(&node));
            assert!(!reads_unguarded_parameter(&node, true));
            assert_eq!(
                boundary_shape_inputs(std::slice::from_ref(&node)),
                parameters
            );
            for &i in &parameters {
                node.inputs[i].source = Src::Intermediate;
            }
            assert!(reads_unguarded_parameter(&node, true));
            for &i in &parameters {
                node.inputs[i].source = Src::Initializer;
            }
            assert!(!reads_data_dependent_parameter(&node));
        }
        let mut mel = NodeDesc::new(
            "MelWeightMatrix".to_string(),
            String::new(),
            17,
            CompileShapeSafety::ShapeKeyedOnly {
                reason: "host-computed weights",
            },
        );
        mel.inputs = (0..5)
            .map(|i| input(&format!("input{i}"), Src::CtxInput))
            .collect();
        assert!(reads_unguarded_parameter(&mel, true));
        assert!(boundary_shape_inputs(&[mel]).is_empty());
    }

    #[test]
    fn runtime_cumsum_axis_is_not_a_shape_key() {
        assert!(
            reads_data_dependent_parameter(&cumulative(
                "CumSum",
                Src::CtxInput,
                crate::registry::MLX_SCAN_SHAPE_REASON,
            )),
            "a runtime axis value must not be baked into a shape-keyed closure"
        );
        assert!(
            !reads_data_dependent_parameter(&cumulative(
                "CumSum",
                Src::Initializer,
                crate::registry::MLX_SCAN_SHAPE_REASON,
            )),
            "an initializer axis is compile-time data"
        );
    }

    #[test]
    fn runtime_cumprod_axis_is_not_a_shape_key() {
        assert!(
            reads_data_dependent_parameter(&cumulative(
                "CumProd",
                Src::CtxInput,
                crate::registry::MLX_SCAN_SHAPE_REASON,
            )),
            "a runtime axis value must not be baked into a shape-keyed closure"
        );
        assert!(
            !reads_data_dependent_parameter(&cumulative(
                "CumProd",
                Src::Initializer,
                crate::registry::MLX_SCAN_SHAPE_REASON,
            )),
            "an initializer axis is compile-time data"
        );
    }

    #[test]
    fn direct_runtime_shape_parameter_is_value_guarded() {
        let mut reshape = NodeDesc::new(
            "Reshape".to_string(),
            String::new(),
            14,
            CompileShapeSafety::Shapeless,
        );
        reshape.inputs = vec![input("data", Src::CtxInput), input("shape", Src::CtxInput)];
        assert!(reads_data_dependent_parameter(&reshape));
        assert!(!reads_unguarded_parameter(&reshape, true));
        assert_eq!(
            boundary_shape_inputs(std::slice::from_ref(&reshape)),
            vec![0]
        );

        reshape.inputs[1].source = Src::Intermediate;
        assert!(reads_data_dependent_parameter(&reshape));
        reshape.inputs[1].shape_const = true;
        assert!(
            !reads_data_dependent_parameter(&reshape),
            "Shape/Size-derived parameters change with the shape-keyed cache key"
        );
    }

    #[test]
    fn specialized_routes_do_not_exempt_general_route_boundary_guards() {
        for op in [
            "Reshape",
            "Expand",
            "Slice",
            "TopK",
            "Split",
            "Pad",
            "Squeeze",
            "Unsqueeze",
        ] {
            let mut parameterized = NodeDesc::new(
                op.to_string(),
                String::new(),
                14,
                CompileShapeSafety::Shapeless,
            );
            parameterized.inputs = vec![
                input("data", Src::CtxInput),
                input("parameter", Src::CtxInput),
                input("end", Src::CtxInput),
            ];
            assert!(reads_data_dependent_parameter(&parameterized));
            assert!(!reads_unguarded_parameter(&parameterized, true));
            assert!(!decode_enabled(false, std::slice::from_ref(&parameterized)));
            let gqa = NodeDesc::new(
                "GroupQueryAttention".to_string(),
                "com.microsoft".to_string(),
                1,
                CompileShapeSafety::Shapeless,
            );
            assert!(!prefill_enabled(false, &[gqa, parameterized]));
        }
    }

    #[test]
    fn recurrent_lengths_require_value_guards_for_validation() {
        for op in ["RNN", "GRU", "LSTM"] {
            let mut node = NodeDesc::new(
                op.to_string(),
                String::new(),
                22,
                CompileShapeSafety::ShapeKeyedOnly {
                    reason: crate::registry::MLX_SLICE_SHAPE_REASON,
                },
            );
            node.inputs = vec![crate::engine::TensorRef::absent(); 5];
            node.inputs[4] = input("lengths", Src::CtxInput);
            node.inputs[4].ctx_index = 4;
            assert!(reads_data_dependent_parameter(&node));
            assert!(!reads_unguarded_parameter(&node, true));
            assert_eq!(boundary_shape_inputs(std::slice::from_ref(&node)), vec![4]);
            node.inputs[4].source = Src::Intermediate;
            assert!(reads_unguarded_parameter(&node, true));
            node.inputs[4].shape_const = true;
            assert!(!reads_unguarded_parameter(&node, true));
        }
    }
}