mamba-rs 0.7.4

Mamba SSM and Mamba-3 SISO in Rust with optional CUDA acceleration: inference and training (BPTT through the SSM state, AdamW) on CPU and GPU, custom NVRTC-compiled kernels, CUDA Graph capture, f32 / bf16 / f16 storage, deterministic batch-invariant GEMMs by default with explicit cuBLAS Fast and Pedantic modes.
Documentation
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
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
//! Compile and register CUDA kernels for Mamba SSM.
//!
//! Uses NVRTC to compile `.cu` source to PTX. The CUDA Driver JIT loads the
//! PTX for the active device; no pre-built binary is required.

use super::dtype::WeightDtype;
use super::gemm_bi_triad::GemmBiKernels;
use super::gemm_bi_triad::modules::inference_bundle::InferenceSm89Bundle;
use cudarc::driver::{CudaContext, CudaFunction, CudaModule};
use std::ops::Deref;
use std::sync::Arc;

#[derive(Clone)]
pub(crate) struct CudaModuleAnchors {
    _modules: Arc<[Arc<CudaModule>]>,
}

impl CudaModuleAnchors {
    pub(crate) fn new(modules: Vec<Arc<CudaModule>>) -> Self {
        Self {
            _modules: modules.into(),
        }
    }
}

/// Dtype-indexed kernel holder for activation-touching kernels.
pub struct TypedKernel {
    pub f32: CudaFunction,
    pub bf16: CudaFunction,
    pub f16: CudaFunction,
}

impl TypedKernel {
    pub fn get(&self, dt: WeightDtype) -> &CudaFunction {
        match dt {
            WeightDtype::F32 | WeightDtype::Tf32 => &self.f32,
            WeightDtype::Bf16 => &self.bf16,
            WeightDtype::F16 => &self.f16,
        }
    }
}

/// Mixed-precision kernel holder with only half-dtype variants (bf16/f16).
/// Used for kernels that bridge f32 and half (e.g., `rmsnorm_fwd_f32in`
/// which reads f32 residual and writes bf16/f16 output).
pub struct HalfKernel {
    pub bf16: CudaFunction,
    pub f16: CudaFunction,
}

impl HalfKernel {
    pub fn get(&self, dt: WeightDtype) -> &CudaFunction {
        match dt {
            WeightDtype::Bf16 => &self.bf16,
            WeightDtype::F16 => &self.f16,
            WeightDtype::F32 | WeightDtype::Tf32 => {
                panic!("HalfKernel has no f32 variant (use the TypedKernel f32 path instead)")
            }
        }
    }
}

/// Standalone deterministic TF32 NN-forward tiles owned by the inference
/// module. No training layout is compiled into this holder.
pub struct FixedTf32Kernels {
    pub m128n64_s2: CudaFunction,
    pub m128n64_s3: CudaFunction,
    pub m64n64_s2: CudaFunction,
    pub m64n64_s3: CudaFunction,
    pub m16n32_s4: CudaFunction,
}

/// SM120 TMA TF32 NN-forward tiles owned by the inference module.
pub struct FixedSm120Tf32Kernels {
    pub m128n64_s2: CudaFunction,
    pub m128n64_s3: CudaFunction,
    pub m64n128_s2: CudaFunction,
    pub m64n128_s3: CudaFunction,
    pub m64n64_s2_producer_warp: CudaFunction,
    pub m64n64_s2: CudaFunction,
    pub(crate) m64n64_s2_pair_store: CudaFunction,
}

/// Qualified SM120 TMA half-precision NN-forward tiles.
pub struct FixedSm120HalfKernels {
    pub m64n64_bk64_s2: HalfKernel,
    pub m64n128_bk64_s2: HalfKernel,
    pub m128n64_bk32_s3: HalfKernel,
    pub m128n128_bk32_s2: HalfKernel,
    pub m128n128_bk32_s3: HalfKernel,
}

/// Inference-owned exact-F32 TMA tiles with post-dot bias or no-bias epilogues,
/// matching the deterministic inference arithmetic contract.
pub struct FixedSm120FmaPostbiasKernels {
    pub m128n64: CudaFunction,
    pub m64n128: CudaFunction,
    pub m128n96: CudaFunction,
    /// Independently admitted force-only K4 twin; controls remain available on rejection.
    pub m128n64_k4: Option<CudaFunction>,
    pub m128n64_k4_rejection: Option<String>,
    /// Independently admitted eight-warp A1 AUTO route; absent means control fallback.
    pub m128n64_t256: Option<CudaFunction>,
    pub m128n64_t256_rejection: Option<String>,
    /// Independently admitted force-only no-bias eight-warp twin.
    pub nobias_m128n64_t256: Option<CudaFunction>,
    pub nobias_m128n64_t256_rejection: Option<String>,
}

/// All compiled CUDA kernels needed for Mamba forward/backward.
///
/// Kernels are compiled once via NVRTC at startup. Grouped by pipeline stage.
pub struct MambaKernels {
    _modules: CudaModuleAnchors,

    compiler_identity: super::kernel_identity::CompilerIdentity,
    triad: GemmBiKernels,

    /// State-dimension capacity the kernels were compiled with (the
    /// per-thread register-array size). The engine and trainer
    /// constructors derive it from the model config, so a mismatched
    /// launch cannot be built through the public constructors; the M1
    /// launch-path asserts additionally compare against it, and the
    /// kernels carry their own capacity guards.
    pub state_cap: usize,

    // -- SSM recurrence --
    /// Single-step SSM forward (T=1 inference).
    pub ssm_step_fwd: CudaFunction,
    /// Multi-step SSM forward with activation saves for backward.
    pub ssm_burnin_fwd: CudaFunction,
    /// Multi-step SSM forward without activation saves (target network).
    pub ssm_burnin_fwd_nosave: CudaFunction,
    /// Per-(b,t,d,n) local SSM backward: dh, du, d_delta contributions.
    pub ssm_backward_local: CudaFunction,
    /// Fused dB+dC reduction `[B*T*d_state]` each, `=`-store (no memset
    /// precondition); .get(dtype) picks the input promotion variant.
    pub ssm_reduce_d_bc_typed: TypedKernel,
    /// T-major twin for the PARALLEL route's `[b][n][d][t]` locals (tape
    /// layout). Same ascending-d sum, same output values and layout.
    pub ssm_reduce_d_bc_tmajor_typed: TypedKernel,
    /// Reduce local SSM grads to dD `[d_inner]`.
    pub ssm_reduce_d_d: CudaFunction,
    /// Reduce local SSM grads to d_a_log `[d_inner*d_state]`.
    pub ssm_reduce_d_a_log: CudaFunction,
    /// Chunk-partial reducer for the fold backward's d_a_log slots.
    pub ssm_reduce_d_a_log_chunks: CudaFunction,

    // -- Conv1d --
    /// Nosave tiled twin (inference prefill): tile-0 seeds from the
    /// carry-in state, later tiles from the x_branch halo - bit-identical
    /// to the serial nosave walk at two orders more parallelism.
    pub conv1d_burnin_fwd_nosave_tiled: CudaFunction,
    pub conv1d_burnin_nosave_tiled_typed: TypedKernel,

    // -- Activations --
    /// Softplus forward: `ln(1 + exp(x))`.
    pub softplus_fwd: CudaFunction,
    /// Softplus backward: gradient through `ln(1 + exp(x))`.
    pub softplus_bwd: CudaFunction,

    // -- Norms --
    /// RMSNorm forward: `x * inv_rms * scale`.
    pub rmsnorm_fwd: CudaFunction,
    /// RMSNorm backward: gradients for x and scale.
    pub rmsnorm_bwd: CudaFunction,

    // -- Elementwise (Mamba-specific) --
    /// Broadcast bias `[N]` to every row of `Y[B,N]`.
    pub bias_broadcast: CudaFunction,
    /// Column-wise sum: `db[j] += sum_b(dy[b*N + j])`.
    pub colsum_accumulate: CudaFunction,
    /// Generic 2D axis-0 tree reduce: `out[d] = [out[d] +] sum_b(partials[b * dim + d])`.
    /// Stage-2 finalizer for Rule-B per-sample partials (replaces atomicAdd
    /// in backward accumulators). Deterministic across runs.
    pub reduce_sum_axis0: CudaFunction,
    /// In-place vector add: `a[i] += b[i]`.
    pub vec_add_inplace: CudaFunction,
    /// Negate and exponentiate: `out[i] = -exp(a_log[i])`.
    pub exp_negate: CudaFunction,
    pub exp_negate2: CudaFunction,
    /// Gather columns from a wide matrix into a contiguous buffer.
    pub gather_cols: CudaFunction,
    /// Gather B and C columns from xdbl output.
    pub gather_bc_cols: CudaFunction,
    /// T-major twin (`[b][n][t]` outputs) for the parallel-scan route —
    /// the scan reads B/C per (d, n) lane over consecutive t.
    pub gather_bc_cols_tmajor: CudaFunction,
    /// Staged-write twin of the t-major gather: smem transpose tile, writes
    /// t-contiguous. Falls back to the untiled kernel when the tile exceeds
    /// the 48 KB static smem budget (f32 at d_state > 186).
    pub gather_bc_cols_tmajor_tiled: CudaFunction,
    /// Gating forward that recomputes SiLU(gate) from the in_proj output's
    /// gate half, read through a row stride.
    pub gate_mul_silu: CudaFunction,
    /// Backward through gating: `y = ssm_out * gate_silu`.
    pub gating_backward: CudaFunction,
    /// Residual add: `out[i] += residual[i]`.
    pub residual_add: CudaFunction,
    /// Gather the last timestep from `[B*T*D]` into `[B*D]`.
    pub gather_last_timestep: CudaFunction,

    // -- Mixed precision casts (mixed inference only) --
    /// f32 → bf16 downcast for weight storage.
    pub cast_f32_to_bf16: CudaFunction,
    pub cast_bf16_to_f32: CudaFunction,
    pub cast_f16_to_f32: CudaFunction,
    /// f32 → f16 downcast for weight storage.
    pub cast_f32_to_f16: CudaFunction,

    // -- Typed training-forward kernels (bf16/f16 variants of burnin save) --
    /// bf16 multi-step SSM forward with typed I/O + f32 saves.
    pub ssm_burnin_fwd_bf16: CudaFunction,
    /// f16 multi-step SSM forward with typed I/O + f32 saves.
    pub ssm_burnin_fwd_f16: CudaFunction,

    // -- Typed training-backward kernels --
    /// Typed dispatch (f32/bf16/f16) for `gating_backward`. dx/dy/d_y/d_gate
    /// typed; matches DEFINE_GATING_BWD macro in elementwise.cu.
    pub gating_bwd_typed: TypedKernel,
    /// Typed dispatch for `rmsnorm_backward`. dx/dy typed; d_scale stays f32
    /// (Rule-B per-sample partials + reduce). Matches DEFINE_RMSNORM_BWD macro in
    /// norms.cu, follows NVIDIA Apex layer_norm pattern.
    pub rmsnorm_bwd_typed: TypedKernel,
    /// Tiled conv1d forward (S-conv): grid (b*di blocks, T tiles); the
    /// window seeds from x_branch halo — bit-identical to the serial walk.
    pub conv1d_burnin_fwd_tiled_typed: TypedKernel,
    /// Tiled conv backward: d_x (anticausal FIR, carry-seeded at the tile
    /// boundaries with the serial association order), the tap partials and
    /// the bias partials in one walk, the pre-activation recomputed from
    /// the x window.
    pub conv1d_bwd_tiled_typed: TypedKernel,
    /// Typed dispatch for `conv1d_burnin_backward`, the serial reference
    /// the typed backward parity test drives; production runs the tiled
    /// kernel above. Matches DEFINE_CONV1D_BURNIN_BWD in conv1d.cu.
    pub conv1d_burnin_bwd_typed: TypedKernel,

    // -- Typed training-backward kernels (the HOTTEST kernel) --
    /// Typed dispatch (f32/bf16/f16) for `ssm_backward_local` — the BPTT
    /// recurrence backward. delta/u/B/C/dy/d_delta/d_u/d_B_local/d_C_local
    /// typed; h_saved/a_neg/D/d_D_local/d_a_log_local stay f32 (BPTT state +
    /// T-length accumulators). Matches DEFINE_SSM_BACKWARD_LOCAL_BWD macro
    /// in mamba_ssm.cu. Validated against state-spaces/mamba reference.
    pub ssm_backward_local_typed: TypedKernel,
    /// One-kernel d_xdbl assembly (dt|B|C ranges tile the row): dt source
    /// typed, B/C sources f32 reduce outputs, FROM_F(0.0f + v) stores.
    pub pack_xdbl_cols_typed: TypedKernel,

    // -- Typed inference kernels (f32/bf16/f16 variants) --
    pub softplus_fwd_typed: TypedKernel,
    /// RMSNorm that first adds a typed branch output into the f32 residual
    /// in place: the previous layer's residual add and this layer's norm in
    /// one launch on the decode routes. The f32 entry takes an f32 branch.
    pub rmsnorm_fwd_resadd_typed: TypedKernel,
    pub bias_broadcast_typed: TypedKernel,
    pub elementwise_mul_typed: TypedKernel,
    pub residual_add_typed: TypedKernel,
    pub gather_cols_typed: TypedKernel,
    pub gather_bc_cols_typed: TypedKernel,
    /// T-major twin of the typed gather (parallel-scan route).
    pub gather_bc_cols_tmajor_typed: TypedKernel,
    /// Staged-write twin of the typed t-major gather.
    pub gather_bc_cols_tmajor_tiled_typed: TypedKernel,
    /// Typed gating forward recomputing SiLU(gate).
    pub gate_mul_silu_typed: TypedKernel,
    /// 16-byte vectorized twins of the hot elementwise kernels: one uint4
    /// per operand per thread, same per-element arithmetic in the same
    /// order. Selected by `vec8_ok` when the shape and every operand
    /// pointer allow it.
    pub gate_mul_silu_v_typed: TypedKernel,
    pub elementwise_mul_v_typed: TypedKernel,
    pub softplus_copy_typed: TypedKernel,
    /// The decode SSM step with softplus, the B/C gather and the gating
    /// folded in: the decode routes' only step kernel.
    pub ssm_step_fwd_fused_typed: TypedKernel,
    /// Conv1d step with the SiLU fused into its store: the decode routes'
    /// only conv step kernel, one launch per layer.
    pub conv1d_step_fwd_silu_typed: TypedKernel,
    pub ssm_burnin_nosave_typed: TypedKernel,
    pub softplus_bwd_typed: TypedKernel,
    pub gather_last_timestep_typed: TypedKernel,
    pub vec_cast_zplus_typed: TypedKernel,
    /// Typed concat_halves — pure load/store with typed src/dst. Used by mixed
    /// backward to concat `d_x_branch` and `d_gate_pre` into `d_proj` before
    /// the in_proj dX backward.
    pub concat_halves_typed: TypedKernel,
    /// Typed scatter_add_cols — `dst[b, off+d] += src[b, d]` with typed src/dst.
    /// Used to scatter `d_delta_raw`, `d_B`, `d_C` into the combined `d_xdbl`
    /// buffer that feeds x_proj dW backward.
    pub scatter_add_cols_typed: TypedKernel,
    /// Typed bias reduction — `d_bias[i] += sum_{b,t} dy[b, t, i]` with
    /// typed `dy` and f32 `d_bias` master grad. Used by mixed dt_proj
    /// backward (dt_proj has a learned bias; dW goes via typed GemmEx,
    /// bias grad via this launch).
    pub reduce_bias_typed: TypedKernel,

    // -- Dual-dtype kernels for end-to-end bf16/f16 inference --
    /// RMSNorm: f32 residual input → half output. Keeps residual stream in
    /// f32 across layers while feeding the branch path in bf16/f16.
    pub rmsnorm_fwd_f32in_typed: HalfKernel,
    /// Residual add: f32 accumulator + half branch → f32 output. Paired with
    /// `rmsnorm_fwd_f32in_typed` to preserve `residual_in_fp32` semantics.
    pub residual_add_f32_typed: HalfKernel,
    /// RmsNorm backward: typed `dy` + f32 `x` → f32 `dx`, f32 `d_scale`.
    /// Dual-dtype twin of `rmsnorm_fwd_f32in_typed`. Used in mixed backward
    /// per-layer rmsnorm where `d_norm` arrives typed (from in_proj dX) but
    /// the residual stream `d_pre_norm` must be f32 to accumulate into the
    /// f32 outer `d_temporal`.
    pub rmsnorm_bwd_f32in_typed: HalfKernel,

    // -- Parallel scan (optional, for T>128) --
    /// Parallel prefix scan SSM forward with activation saves.
    pub ssm_parallel_fwd: CudaFunction,
    /// Parallel prefix scan SSM forward without saves (target network).
    pub ssm_parallel_fwd_nosave: CudaFunction,
    /// Typed parallel scan forward — typed delta/u/B/C/y_out,
    /// all scan state (smem_run, block scan, h, h_saved) remains f32 per
    /// `state-spaces/mamba` `scan_t = float2` invariant.
    pub ssm_parallel_fwd_typed: TypedKernel,
    /// Typed parallel scan forward nosave twin (target network / prefill).
    pub ssm_parallel_fwd_nosave_typed: TypedKernel,

    // -- M1 parallel scan BACKWARD --
    /// Parallel reverse-scan backward, mirrors state-spaces/mamba
    /// `selective_scan_bwd_kernel.cuh`. Uses h_saved (per-t fwd state save)
    /// to skip forward re-derivation. Outputs follow the existing _local
    /// convention so the existing reduction kernels work unchanged.
    /// f32 / bf16 / f16 instantiations from one DEFINE_* macro.
    pub ssm_parallel_bwd_typed: TypedKernel,
    /// d-group fold variant: each block folds SCAN_BWD_DGROUP d lanes'
    /// dB/dC terms and writes one partial row per group. Used when
    /// d_inner is divisible by the group size; f32 staging needs the
    /// MAX_DYNAMIC_SHARED opt-in (~65 KB).
    pub ssm_parallel_bwd_fold_typed: TypedKernel,
    ssm_parallel_bwd_fold_short_typed: Option<HalfKernel>,
    ssm_parallel_bwd_fold_staged_typed: Option<HalfKernel>,

    // -- AMP loss scaler helpers --
    /// Scan an f32 grad buffer for inf/nan, atomicOr into device int.
    pub check_inf_nan_f32: CudaFunction,
    /// In-place multiply f32 grads by a scalar (unscale, clip, etc.).
    pub scale_grads_f32: CudaFunction,
    /// CUDA-Graph-capturable conditional unscale: zeros grads if the
    /// overflow flag is set, otherwise multiplies by 1/loss_scale.
    pub scale_grads_skip_f32: CudaFunction,
    /// Deterministic global-norm support: fixed-grid sum-of-squares partial
    /// reduction with f64 accumulators (kernels/grad_clip.cu). The host sums
    /// the fixed 512 partials in order; scaling reuses `scale_grads_f32`.
    pub grad_sumsq_partial_f32: CudaFunction,
    /// Single-thread fold of the 512 partials into the PRE-clip norm and
    /// the clip coefficient, on device - removes the host drain between
    /// the norm and the scaling pass.
    pub grad_clip_coef_f32: CudaFunction,
    /// `scale_grads_f32` with the factor read from device memory.
    pub scale_grads_dev_f32: CudaFunction,
    /// Region twin of `grad_sumsq_partial_f32`: fixed-grid f64 partials
    /// over a per-layer two-block region (a strided column stripe plus a
    /// contiguous vector) of the flat arena.
    pub grad_region_sumsq_partial_f32: CudaFunction,
    /// Region twin of `scale_grads_dev_f32`: scales ONLY the described
    /// region by the device-resident clip coefficient.
    pub grad_region_scale_dev_f32: CudaFunction,

    // -- AdamW optimizer --
    /// Fused AdamW step on f32 master weights + f32 optimizer state.
    pub adamw_step_f32: CudaFunction,
    /// CUDA-Graph-capturable variant: reads bias-correction factors from a
    /// 2-elem device buffer instead of scalar args.
    pub adamw_step_f32_capturable: CudaFunction,
    /// Fused multi-tensor AdamW, one variant per shadow dtype (the f32
    /// variant serves the no-shadow lane).
    pub adamw_step_multi: TypedKernel,

    // -- Batch-invariant GEMM (bf16 cross-batch determinism fix) --
    /// Batch-invariant GEMM bf16×bf16→bf16. Tensor-Core inner GEMM via
    /// `nvcuda::wmma` (m16n16k16 fragments, f32 accumulator). Fixed
    /// 64x64x32 tile, no split-K. `C[i, j]` is bit-identical regardless
    /// of batch size M of A.
    pub gemm_bi_bf16_bf16: CudaFunction,
    /// Batch-invariant GEMM f16×f16→f16. Tensor Cores via WMMA.
    pub gemm_bi_f16_f16: CudaFunction,
    /// Batch-invariant GEMM bf16×bf16→f32 (tied lm_head: f32 logits).
    pub gemm_bi_bf16_f32: CudaFunction,
    /// Batch-invariant GEMM f16×f16→f32. Tensor Cores via WMMA.
    pub gemm_bi_f16_f32: CudaFunction,
    /// Former exact-f32 CUDA-core route retained as the qualification oracle.
    pub gemm_bi_f32_f32: CudaFunction,
    /// Portable exact-f32 64x64 two-stage route and AUTO fallback.
    pub gemm_bi_f32_f32_s2: CudaFunction,
    /// Exact-f32 64x128 route for the measured A/B AUTO points and forced tests.
    pub gemm_bi_f32_f32_n128_s2: CudaFunction,
    /// Deterministic TF32 NN-forward tile ladder owned by Fixed/inference.
    pub gemm_bi_nn_tf32: FixedTf32Kernels,
    /// SM120 TMA counterpart of the deterministic inference TF32 ladder.
    pub gemm_bi_nn_tf32_sm120: Option<FixedSm120Tf32Kernels>,
    /// SM120 TMA BF16/F16 inference candidates.
    pub gemm_bi_nn_half_sm120: Option<FixedSm120HalfKernels>,
    /// SM120 TMA BF16/F16-input, F32-output inference candidates.
    pub gemm_bi_nn_half_sm120_f32out: Option<FixedSm120HalfKernels>,
    /// Independently admitted Ada Tc128 half pipeline; used by qualified AUTO rows.
    pub fixed_sm89_half_pipeline: Option<HalfKernel>,
    /// Why the optional Ada pipeline is absent (including non-Ada targets).
    pub fixed_sm89_half_pipeline_rejection: Option<String>,
    /// Independent Ada-only packed/XOR homogeneous-half candidate.
    pub fixed_sm89_half_swizzle: Option<HalfKernel>,
    /// Why only the swizzle holder is absent; never disables the incumbent.
    pub fixed_sm89_half_swizzle_rejection: Option<String>,
    /// Independently admitted Ada three-stage homogeneous-half pair.
    pub fixed_sm89_half_s3: Option<HalfKernel>,
    pub fixed_sm89_half_s3_rejection: Option<String>,
    /// Fixed-owned Ada RNA-wide TF32 route; retains qualified AUTO rows and fallbacks.
    pub fixed_sm89_tf32_rna_wide: Option<CudaFunction>,
    pub fixed_sm89_tf32_rna_wide_rejection: Option<String>,
    /// Fixed-owned Ada RNA M128xN96 TF32; qualified E0 AUTO since revision 45.
    pub fixed_sm89_tf32_rna_n96: Option<CudaFunction>,
    pub fixed_sm89_tf32_rna_n96_rejection: Option<String>,
    /// Independently admitted Ada F16 D route; qualified CUDA13.2 AUTO since revision 45.
    pub fixed_sm89_half_m64n64_s3_f16: Option<CudaFunction>,
    pub fixed_sm89_half_m64n64_s3_f16_rejection: Option<String>,
    /// Independently admitted Ada F16 E route; qualified CUDA13.2 AUTO since revision 45.
    pub fixed_sm89_half_m128n64_s2_f16: Option<CudaFunction>,
    pub fixed_sm89_half_m128n64_s2_f16_rejection: Option<String>,
    /// Optional Ada exact-F32 N64 copy-plan; admitted independently of incumbents.
    pub fixed_sm89_f32_n64_copyplan: Option<CudaFunction>,
    pub fixed_sm89_f32_n64_copyplan_rejection: Option<String>,
    /// The Ada-measured inference cells, by symbol, from their own module.
    pub(crate) fixed_sm89_cells: std::collections::HashMap<&'static str, CudaFunction>,
    /// The compiler identity of the inference cells module, when composed.
    pub(crate) sm89_cells_compiler_identity: Option<super::kernel_identity::CompilerIdentity>,
    pub(crate) inference_sm89_bundle: InferenceSm89Bundle,
    /// Optional CC12.0 exact-F32 N64 copy-plan, separate from the Ada route.
    pub fixed_sm120_f32_n64_copyplan: Option<CudaFunction>,
    pub fixed_sm120_f32_n64_copyplan_rejection: Option<String>,
    /// Independently admitted force-only 256-thread CopyPlan twin.
    pub fixed_sm120_f32_n64_copyplan_t256: Option<CudaFunction>,
    pub fixed_sm120_f32_n64_copyplan_t256_rejection: Option<String>,
    pub fixed_sm120_f32_m128n64_copyplan_t256: Option<CudaFunction>,
    pub fixed_sm120_f32_m128n64_copyplan_t256_rejection: Option<String>,
    /// Independent compute_120 sliced exact-F32 route; no scratch.
    pub fixed_sm120_f32_n64_sliced: Option<CudaFunction>,
    pub fixed_sm120_f32_n64_sliced_rejection: Option<String>,
    /// Optional CC12.0 exact-FMA tiles with Fixed's post-dot bias order.
    pub fixed_sm120_fma_postbias: Option<FixedSm120FmaPostbiasKernels>,
    pub fixed_sm120_fma_postbias_rejection: Option<String>,

    // -- Batch-invariant matvec (M=1 specialization) --
    /// Specialized M=1 matvec. The GEMM kernels above waste 98% of smem
    /// bandwidth at M=1 (load BLOCK_M=64 rows, only row 0 is real).
    /// Decode (single-token per step) uses this instead — one thread per
    /// output column, K-loop with register-scalar f32 accumulator, no
    /// cross-thread reductions. Trivially batch-invariant (M=1 has no
    /// batch dim) and ~5× faster than gemm_bi_* at M=1.
    pub matvec_bi_bf16_bf16: CudaFunction,
    pub matvec_bi_f16_f16: CudaFunction,
    pub matvec_bi_bf16_f32: CudaFunction,
    pub matvec_bi_f16_f32: CudaFunction,
    pub matvec_bi_f32_f32: CudaFunction,

    /// The Inference family's inference ladder (`kernels/gemm_bi_inference/`,
    /// GBF namespace): bit-identical copies of the forward TC tiles,
    /// owned by the inference kernel.
    pub gemm_bi_nn_tc128_typed: HalfKernel,
    /// Portable ladder variants retaining the tensor-core accumulator in f32 at store.
    pub gemm_bi_nn_tc128_f32out: HalfKernel,
    pub gemm_bi_nn_tc64_f32out: HalfKernel,
    pub gemm_bi_nn_tc16_f32out: HalfKernel,
    pub gemm_bi_nn_tc64_typed: HalfKernel,
    pub gemm_bi_nn_tc16_typed: HalfKernel,
    /// Fragment-reuse 128x128 rung with 64x64 warp tiles.
    pub gemm_bi_nn_tcw64_typed: HalfKernel,
    /// Wide 128x256 sibling of the fragment-reuse rung.
    pub gemm_bi_nn_tcwn64_typed: HalfKernel,
    /// The Hopper wgmma rung (compiled only for sm_90a; the dispatcher
    /// never routes here until the rung is hardware-qualified - the
    /// forced census entry is its only caller).
    pub gemm_bi_nn_sm90_typed: Option<HalfKernel>,
    /// Datacenter-Blackwell tcgen05 rung, available only on admitted
    /// architecture-family targets.
    pub gemm_bi_nn_sm100_typed: Option<HalfKernel>,
}

impl Deref for MambaKernels {
    type Target = GemmBiKernels;

    fn deref(&self) -> &Self::Target {
        &self.triad
    }
}

/// NVRTC library version, part of the kernel-cache key. `(0, 0)` means the
/// query failed; persistent caching still requires exact runtime and builtins
/// library hashes.
pub(crate) fn nvrtc_version() -> (i32, i32) {
    let mut major: core::ffi::c_int = 0;
    let mut minor: core::ffi::c_int = 0;
    let rc = unsafe { cudarc::nvrtc::sys::nvrtcVersion(&mut major, &mut minor) };
    if rc == cudarc::nvrtc::sys::nvrtcResult::NVRTC_SUCCESS {
        (major, minor)
    } else {
        (0, 0)
    }
}

/// Linux kernel-cache directory. `MAMBA_RS_KERNEL_CACHE` overrides with an
/// absolute path, or `0`/`off` disables it. Relative paths, symlinked
/// components, writable ancestors, and non-private final directories disable
/// persistent caching. Other platforms currently leave it disabled.
pub(crate) fn kernel_cache_dir() -> Option<std::path::PathBuf> {
    let path = match std::env::var("MAMBA_RS_KERNEL_CACHE") {
        Ok(v) if matches!(v.trim(), "0" | "off" | "OFF") => None,
        Ok(v) if !v.trim().is_empty() => Some(std::path::PathBuf::from(v.trim())),
        _ => {
            let base = std::env::var("XDG_CACHE_HOME")
                .map(std::path::PathBuf::from)
                .or_else(|_| {
                    std::env::var("HOME").map(|home| std::path::PathBuf::from(home).join(".cache"))
                })
                .ok()?;
            Some(base.join("mamba-rs").join("kernels"))
        }
    }?;
    super::kernel_identity::prepare_private_cache_dir(&path)
}

/// Round a model's `d_state` up to the register-array capacity the
/// kernels are compiled with. The capacity is a JIT-time knob: raising
/// it grows per-thread register pressure (the compiler spills to local
/// memory past its budget — correct, slower, accepted), so it is kept
/// as tight as the model allows. The 256 ceiling is the reference
/// implementation's own range; a larger `d_state` has no upstream
/// precedent and is refused loudly rather than silently mis-run.
pub fn state_capacity(d_state: usize) -> Result<usize, String> {
    if d_state == 0 {
        return Err("d_state must be positive".into());
    }
    if d_state > 256 {
        return Err(format!(
            "d_state {d_state} exceeds the supported range (reference \
             implementations go to 256)"
        ));
    }
    // 16-granular: the cap sizes the per-thread state register arrays in
    // every scan kernel and bounds their loops, which unroll to constant
    // indices so the arrays stay in registers; a 64-floor at d_state=16
    // made them 4x oversized. Callers with larger states still get the
    // exact padded fit.
    Ok(d_state.div_ceil(16) * 16)
}

/// The optional Triad modules a board loads: the common tier and, when
/// the toolkit qualifies it, the board's own architecture module.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct TriadModulePlan {
    pub common: bool,
    pub architecture: Option<super::kernel_identity::ModuleKind>,
}

impl MambaKernels {
    /// Exact membership in loaded inference terminal holders, including the borrowed wide holder.
    pub(in crate::mamba_ssm::gpu) fn inference_terminal_function(
        &self,
        symbol: &str,
    ) -> Option<&CudaFunction> {
        match symbol {
            "f32_f32_s2" => Some(&self.gemm_bi_f32_f32_s2),
            "f32_f32_n128_s2" => Some(&self.gemm_bi_f32_f32_n128_s2),
            "nn_sm89_f32_n64_copyplan" => self.fixed_sm89_f32_n64_copyplan.as_ref(),
            "nn_sm89_tc128_f32out_s3_bf16" => {
                self.inference_sm89_bundle.half_f32out_s3_bf16.as_ref().ok()
            }
            "nn_sm89_tc128_f32out_s3_f16" => {
                self.inference_sm89_bundle.half_f32out_s3_f16.as_ref().ok()
            }
            "nn_sm89_f32_m128n64_tail_copyplan" => {
                self.inference_sm89_bundle.exact_m128n64_tail.as_ref().ok()
            }
            "nn_sm120_f32_n64_copyplan" => self.fixed_sm120_f32_n64_copyplan.as_ref(),
            "nn_sm120_f32_n64_copyplan_t256" => self.fixed_sm120_f32_n64_copyplan_t256.as_ref(),
            "nn_sm120_f32_n64_copyplan_m128n64_t256" => {
                self.fixed_sm120_f32_m128n64_copyplan_t256.as_ref()
            }
            "nn_sm120_f32_n64_sliced" => self.fixed_sm120_f32_n64_sliced.as_ref(),
            "bf16_bf16" => Some(&self.gemm_bi_bf16_bf16),
            "matvec_bi_bf16_bf16" => Some(&self.matvec_bi_bf16_bf16),
            "bf16_f32" => Some(&self.gemm_bi_bf16_f32),
            "matvec_bi_bf16_f32" => Some(&self.matvec_bi_bf16_f32),
            "nn_tc128_bf16" => Some(&self.gemm_bi_nn_tc128_typed.bf16),
            "nn_tc128_f32out_bf16" => Some(&self.gemm_bi_nn_tc128_f32out.bf16),
            "nn_tcw64_bf16" => Some(&self.gemm_bi_nn_tcw64_typed.bf16),
            "nn_tcwn64_bf16" => Some(&self.gemm_bi_nn_tcwn64_typed.bf16),
            "nn_tc64_bf16" => Some(&self.gemm_bi_nn_tc64_typed.bf16),
            "nn_tc64_f32out_bf16" => Some(&self.gemm_bi_nn_tc64_f32out.bf16),
            "nn_tc16_bf16" => Some(&self.gemm_bi_nn_tc16_typed.bf16),
            "nn_tc16_f32out_bf16" => Some(&self.gemm_bi_nn_tc16_f32out.bf16),
            "nn_sm89_tc128_pipeline_bf16" => Some(&self.fixed_sm89_half_pipeline.as_ref()?.bf16),
            "nn_sm89_tc128_swizzle_bf16" => Some(&self.fixed_sm89_half_swizzle.as_ref()?.bf16),
            "nn_sm89_tc128_s3_bf16" => Some(&self.fixed_sm89_half_s3.as_ref()?.bf16),
            "nn_sm120_tma_64x64_bk64_s2_bf16" => {
                Some(&self.gemm_bi_nn_half_sm120.as_ref()?.m64n64_bk64_s2.bf16)
            }
            "nn_sm120_tma_64x64_bk64_s2_f32out_bf16" => Some(
                &self
                    .gemm_bi_nn_half_sm120_f32out
                    .as_ref()?
                    .m64n64_bk64_s2
                    .bf16,
            ),
            "nn_sm120_tma_64x128_bk64_s2_bf16" => {
                Some(&self.gemm_bi_nn_half_sm120.as_ref()?.m64n128_bk64_s2.bf16)
            }
            "nn_sm120_tma_64x128_bk64_s2_f32out_bf16" => Some(
                &self
                    .gemm_bi_nn_half_sm120_f32out
                    .as_ref()?
                    .m64n128_bk64_s2
                    .bf16,
            ),
            "nn_sm120_tma_128x64_bk32_s3_bf16" => {
                Some(&self.gemm_bi_nn_half_sm120.as_ref()?.m128n64_bk32_s3.bf16)
            }
            "nn_sm120_tma_128x64_bk32_s3_f32out_bf16" => Some(
                &self
                    .gemm_bi_nn_half_sm120_f32out
                    .as_ref()?
                    .m128n64_bk32_s3
                    .bf16,
            ),
            "nn_sm120_tma_128x128_bk32_s2_bf16" => {
                Some(&self.gemm_bi_nn_half_sm120.as_ref()?.m128n128_bk32_s2.bf16)
            }
            "nn_sm120_tma_128x128_bk32_s2_f32out_bf16" => Some(
                &self
                    .gemm_bi_nn_half_sm120_f32out
                    .as_ref()?
                    .m128n128_bk32_s2
                    .bf16,
            ),
            "nn_sm120_tma_128x128_bk32_s3_bf16" => {
                Some(&self.gemm_bi_nn_half_sm120.as_ref()?.m128n128_bk32_s3.bf16)
            }
            "nn_sm120_tma_128x128_bk32_s3_f32out_bf16" => Some(
                &self
                    .gemm_bi_nn_half_sm120_f32out
                    .as_ref()?
                    .m128n128_bk32_s3
                    .bf16,
            ),
            "nn_sm90a_wgmma_wg1_bf16" => Some(&self.gemm_bi_nn_sm90_typed.as_ref()?.bf16),
            "nn_sm100_tcgen_c4_bf16" => Some(&self.gemm_bi_nn_sm100_typed.as_ref()?.bf16),
            "f16_f16" => Some(&self.gemm_bi_f16_f16),
            "matvec_bi_f16_f16" => Some(&self.matvec_bi_f16_f16),
            "f16_f32" => Some(&self.gemm_bi_f16_f32),
            "matvec_bi_f16_f32" => Some(&self.matvec_bi_f16_f32),
            "nn_tc128_f16" => Some(&self.gemm_bi_nn_tc128_typed.f16),
            "nn_tc128_f32out_f16" => Some(&self.gemm_bi_nn_tc128_f32out.f16),
            "nn_tcw64_f16" => Some(&self.gemm_bi_nn_tcw64_typed.f16),
            "nn_tcwn64_f16" => Some(&self.gemm_bi_nn_tcwn64_typed.f16),
            "nn_tc64_f16" => Some(&self.gemm_bi_nn_tc64_typed.f16),
            "nn_tc64_f32out_f16" => Some(&self.gemm_bi_nn_tc64_f32out.f16),
            "nn_tc16_f16" => Some(&self.gemm_bi_nn_tc16_typed.f16),
            "nn_tc16_f32out_f16" => Some(&self.gemm_bi_nn_tc16_f32out.f16),
            "nn_sm89_tc128_pipeline_f16" => Some(&self.fixed_sm89_half_pipeline.as_ref()?.f16),
            "nn_sm89_tc128_swizzle_f16" => Some(&self.fixed_sm89_half_swizzle.as_ref()?.f16),
            "nn_sm89_tc128_s3_f16" => Some(&self.fixed_sm89_half_s3.as_ref()?.f16),
            "nn_sm120_tma_64x64_bk64_s2_f16" => {
                Some(&self.gemm_bi_nn_half_sm120.as_ref()?.m64n64_bk64_s2.f16)
            }
            "nn_sm120_tma_64x64_bk64_s2_f32out_f16" => Some(
                &self
                    .gemm_bi_nn_half_sm120_f32out
                    .as_ref()?
                    .m64n64_bk64_s2
                    .f16,
            ),
            "nn_sm120_tma_64x128_bk64_s2_f16" => {
                Some(&self.gemm_bi_nn_half_sm120.as_ref()?.m64n128_bk64_s2.f16)
            }
            "nn_sm120_tma_64x128_bk64_s2_f32out_f16" => Some(
                &self
                    .gemm_bi_nn_half_sm120_f32out
                    .as_ref()?
                    .m64n128_bk64_s2
                    .f16,
            ),
            "nn_sm120_tma_128x64_bk32_s3_f16" => {
                Some(&self.gemm_bi_nn_half_sm120.as_ref()?.m128n64_bk32_s3.f16)
            }
            "nn_sm120_tma_128x64_bk32_s3_f32out_f16" => Some(
                &self
                    .gemm_bi_nn_half_sm120_f32out
                    .as_ref()?
                    .m128n64_bk32_s3
                    .f16,
            ),
            "nn_sm120_tma_128x128_bk32_s2_f16" => {
                Some(&self.gemm_bi_nn_half_sm120.as_ref()?.m128n128_bk32_s2.f16)
            }
            "nn_sm120_tma_128x128_bk32_s2_f32out_f16" => Some(
                &self
                    .gemm_bi_nn_half_sm120_f32out
                    .as_ref()?
                    .m128n128_bk32_s2
                    .f16,
            ),
            "nn_sm120_tma_128x128_bk32_s3_f16" => {
                Some(&self.gemm_bi_nn_half_sm120.as_ref()?.m128n128_bk32_s3.f16)
            }
            "nn_sm120_tma_128x128_bk32_s3_f32out_f16" => Some(
                &self
                    .gemm_bi_nn_half_sm120_f32out
                    .as_ref()?
                    .m128n128_bk32_s3
                    .f16,
            ),
            "nn_sm90a_wgmma_wg1_f16" => Some(&self.gemm_bi_nn_sm90_typed.as_ref()?.f16),
            "nn_sm100_tcgen_c4_f16" => Some(&self.gemm_bi_nn_sm100_typed.as_ref()?.f16),
            "matvec_bi_f32_f32" => Some(&self.matvec_bi_f32_f32),
            "nn_sm89_m64n64_bk64_s3_f16" => self.fixed_sm89_half_m64n64_s3_f16.as_ref(),
            "nn_sm89_m128n64_bk64_s2_f16" => self.fixed_sm89_half_m128n64_s2_f16.as_ref(),
            "nn_tf32_m128n64_bk32_s2" => Some(&self.gemm_bi_nn_tf32.m128n64_s2),
            "nn_tf32_m128n64_bk32_s3" => Some(&self.gemm_bi_nn_tf32.m128n64_s3),
            "nn_tf32_m64n64_bk32_s2" => Some(&self.gemm_bi_nn_tf32.m64n64_s2),
            "nn_tf32_m64n64_bk32_s3" => Some(&self.gemm_bi_nn_tf32.m64n64_s3),
            "nn_tf32_m16n32_bk32_s4" => Some(&self.gemm_bi_nn_tf32.m16n32_s4),
            "nn_rna_wide_tf32_m128n128_bk32_s3" => self.fixed_sm89_tf32_rna_wide.as_ref(),
            "nn_sm89_rna_tf32_m128n96_bk32_s3" => self.fixed_sm89_tf32_rna_n96.as_ref(),
            "nn_sm80_mma_tf32_m128n128_bk32_s3" => self.triad_kernels().tf32_function(symbol),
            "nn_sm120_tma_tf32_m128n64_bk32_s2" => {
                Some(&self.gemm_bi_nn_tf32_sm120.as_ref()?.m128n64_s2)
            }
            "nn_sm120_tma_tf32_m128n64_bk32_s3" => {
                Some(&self.gemm_bi_nn_tf32_sm120.as_ref()?.m128n64_s3)
            }
            "nn_sm120_tma_tf32_m64n128_bk32_s2" => {
                Some(&self.gemm_bi_nn_tf32_sm120.as_ref()?.m64n128_s2)
            }
            "nn_sm120_tma_tf32_m64n128_bk32_s3" => {
                Some(&self.gemm_bi_nn_tf32_sm120.as_ref()?.m64n128_s3)
            }
            "nn_sm120_tma_tf32_m64n64_bk32_s2_producer_warp" => {
                Some(&self.gemm_bi_nn_tf32_sm120.as_ref()?.m64n64_s2_producer_warp)
            }
            "nn_sm120_tma_tf32_m64n64_bk32_s2" => {
                Some(&self.gemm_bi_nn_tf32_sm120.as_ref()?.m64n64_s2)
            }
            "nn_sm120_tma_tf32_m64n64_bk32_s2_pair_store" => {
                Some(&self.gemm_bi_nn_tf32_sm120.as_ref()?.m64n64_s2_pair_store)
            }
            "nn_sm120_tma_fma_postbias_m128n64_bk16_s2" => {
                Some(&self.fixed_sm120_fma_postbias.as_ref()?.m128n64)
            }
            "nn_sm120_tma_fma_postbias_m64n128_bk16_s2" => {
                Some(&self.fixed_sm120_fma_postbias.as_ref()?.m64n128)
            }
            "nn_sm120_tma_fma_postbias_m128n96_bk16_s2" => {
                Some(&self.fixed_sm120_fma_postbias.as_ref()?.m128n96)
            }
            "nn_sm120_tma_fma_postbias_m128n64_bk16_s2_k4" => {
                self.fixed_sm120_fma_postbias.as_ref()?.m128n64_k4.as_ref()
            }
            "nn_sm120_tma_fma_postbias_m128n64_t256_bk16_s2" => self
                .fixed_sm120_fma_postbias
                .as_ref()?
                .m128n64_t256
                .as_ref(),
            "nn_sm120_tma_fma_nobias_m128n64_t256_bk16_s2" => self
                .fixed_sm120_fma_postbias
                .as_ref()?
                .nobias_m128n64_t256
                .as_ref(),
            other => self.fixed_sm89_cells.get(other),
        }
    }

    /// Which optional Triad modules a board loads. The common
    /// SM89-measured modules load on every SM80-tier board, beside the
    /// board's own architecture module where the toolkit qualifies one;
    /// the loader follows this plan and the host tests read it without a
    /// device.
    pub(crate) fn triad_module_plan(
        arch: &str,
        device_cc: Option<(i32, i32)>,
        nvrtc: (i32, i32),
    ) -> TriadModulePlan {
        use super::kernel_identity::ModuleKind;
        let common = super::gemm_bi_triad::modules::sm80_tier_module_compiles(arch, device_cc);
        let architecture = match (arch, device_cc) {
            // The WGMMA kernel waits with `wgmma.wait_group 1`; ptxas
            // before CUDA 13.3 could copy-propagate across that wait and
            // drop the register moves it protects (CUDA 13.3 release
            // notes), so the older toolkits get the common tier alone.
            ("sm_90a", Some((9, 0))) if nvrtc >= (13, 3) => Some(ModuleKind::TriadSm90a),
            ("sm_90a", Some((9, 0))) => None,
            ("sm_100a", Some((10, 0)))
            | ("sm_103a", Some((10, 3)))
            | ("sm_107a", Some((10, 7)))
            | ("sm_110a", Some((11, 0))) => Some(ModuleKind::TriadSm100),
            (_, Some((12, _))) => Some(ModuleKind::TriadSm120),
            _ => None,
        };
        TriadModulePlan {
            common,
            architecture,
        }
    }

    /// Compile with the default state capacity of 64 — the common
    /// shapes' tightest register budget. Models with a larger `d_state`
    /// use [`Self::compile_with_state_cap`].
    pub fn compile(ctx: &Arc<CudaContext>, arch: &'static str) -> Result<Self, String> {
        Self::compile_with_state_cap(ctx, arch, 64)
    }

    /// Compile all CUDA kernels from source. Persistent PTX caching is used
    /// only when the complete header and NVRTC toolchain domains are known.
    /// An invalid entry is ignored and compilation proceeds normally.
    ///
    /// `state_cap` sizes the per-thread state register arrays (see
    /// [`state_capacity`]); it rides the compile options and therefore
    /// the cache key.
    pub fn compile_with_state_cap(
        ctx: &Arc<CudaContext>,
        arch: &'static str,
        state_cap: usize,
    ) -> Result<Self, String> {
        let device_cc = match ctx.compute_capability() {
            Ok(device_cc) => Some(device_cc),
            Err(error) => {
                static UNKNOWN_CC: std::sync::Once = std::sync::Once::new();
                super::diagnostics::warn_once(&UNKNOWN_CC, || {
                    format!(
                        "the driver did not report a compute capability ({error:?}); only the \
                         portable kernels are compiled"
                    )
                });
                None
            }
        };
        // The loader and the selectors share one family predicate, so a
        // kernel is never compiled for a board that cannot select it.
        let sm120_board = device_cc.is_some_and(|(major, minor)| {
            u32::try_from(major)
                .and_then(|major| Ok((major, u32::try_from(minor)?)))
                .is_ok_and(super::device::is_sm120_family)
        });
        if !sm120_board && matches!(arch, "compute_120" | "compute_121") {
            static UNQUALIFIED: std::sync::Once = std::sync::Once::new();
            super::diagnostics::warn_once(&UNQUALIFIED, || {
                format!(
                    "compute capability {device_cc:?} is not a qualified family; only the \
                     portable kernels serve it"
                )
            });
        }
        let sm120_artifacts = match device_cc {
            Some(device_cc) if sm120_board => {
                super::gemm_bi_triad::modules::compile_sm120_artifact_set(
                    ctx,
                    state_cap,
                    device_cc,
                    nvrtc_version(),
                )
            }
            _ => None,
        };
        let compile = |module_kind| {
            super::gemm_bi_triad::modules::compile_module(
                super::gemm_bi_triad::modules::CompileModuleRequest {
                    ctx,
                    arch,
                    state_cap,
                    module_kind,
                },
            )
        };
        let plan = Self::triad_module_plan(arch, device_cc, nvrtc_version());
        let (
            fixed,
            scalar,
            sm80,
            finalist,
            finalist_rejection,
            sm89_half,
            sm89_half_rejection,
            sm89_exact_f32,
            sm89_exact_f32_rejection,
            sm89_exact_f32_d128,
            sm89_exact_f32_d128_rejection,
            sm89_tf32_joint,
            sm89_tf32_joint_rejection,
            specialized,
        ) = if let Some(artifacts) = sm120_artifacts {
            // The common SM89-measured modules are sm_80-tier PTX; a CC 12.x
            // board compiles them beside its own set, for the target that set
            // was compiled with (CC 12.1 may have fallen back to compute_120),
            // and admits them through the first-use proof like every other
            // board.
            let set_target = artifacts.fixed.compiler_identity.target;
            let tier_arch = device_cc
                .and_then(|device_cc| {
                    super::gemm_bi_triad::sm120_target_candidates(device_cc, nvrtc_version())
                        .iter()
                        .map(|candidate| candidate.nvrtc_arch)
                        .find(|candidate| *candidate == set_target.as_str())
                })
                .unwrap_or(arch);
            let portable_tier = |module_kind| {
                if Self::triad_module_plan(tier_arch, device_cc, nvrtc_version()).common {
                    match super::gemm_bi_triad::modules::compile_module(
                        super::gemm_bi_triad::modules::CompileModuleRequest {
                            ctx,
                            arch: tier_arch,
                            state_cap,
                            module_kind,
                        },
                    ) {
                        Ok(module) => (Some(module), None),
                        Err(error) => (None, Some(error)),
                    }
                } else {
                    (None, None)
                }
            };
            let (finalist, finalist_rejection) =
                portable_tier(super::kernel_identity::ModuleKind::TriadSm89Finalist);
            let (sm89_half, sm89_half_rejection) =
                portable_tier(super::kernel_identity::ModuleKind::TriadSm89Half);
            let (sm89_exact_f32, sm89_exact_f32_rejection) =
                portable_tier(super::kernel_identity::ModuleKind::TriadSm89ExactF32);
            let (sm89_exact_f32_d128, sm89_exact_f32_d128_rejection) =
                portable_tier(super::kernel_identity::ModuleKind::TriadSm89ExactF32D128);
            let (sm89_tf32_joint, sm89_tf32_joint_rejection) =
                portable_tier(super::kernel_identity::ModuleKind::TriadSm89Tf32Joint);
            (
                artifacts.fixed,
                artifacts.scalar,
                artifacts.sm80,
                finalist,
                finalist_rejection,
                sm89_half,
                sm89_half_rejection,
                sm89_exact_f32,
                sm89_exact_f32_rejection,
                sm89_exact_f32_d128,
                sm89_exact_f32_d128_rejection,
                sm89_tf32_joint,
                sm89_tf32_joint_rejection,
                artifacts.specialized,
            )
        } else {
            let fixed = compile(super::kernel_identity::ModuleKind::Fixed)?;
            let scalar = compile(super::kernel_identity::ModuleKind::TriadScalar)?;
            let sm80 = compile(super::kernel_identity::ModuleKind::TriadSm80)?;
            let (finalist, finalist_rejection) = if plan.common {
                match compile(super::kernel_identity::ModuleKind::TriadSm89Finalist) {
                    Ok(module) => (Some(module), None),
                    Err(error) => (None, Some(error)),
                }
            } else {
                (None, None)
            };
            let (sm89_half, sm89_half_rejection) = if plan.common {
                match compile(super::kernel_identity::ModuleKind::TriadSm89Half) {
                    Ok(module) => (Some(module), None),
                    Err(error) => (None, Some(error)),
                }
            } else {
                (None, None)
            };
            let (sm89_exact_f32, sm89_exact_f32_rejection) = if plan.common {
                match compile(super::kernel_identity::ModuleKind::TriadSm89ExactF32) {
                    Ok(module) => (Some(module), None),
                    Err(error) => (None, Some(error)),
                }
            } else {
                (None, None)
            };
            let (sm89_exact_f32_d128, sm89_exact_f32_d128_rejection) = if plan.common {
                match compile(super::kernel_identity::ModuleKind::TriadSm89ExactF32D128) {
                    Ok(module) => (Some(module), None),
                    Err(error) => (None, Some(error)),
                }
            } else {
                (None, None)
            };
            let (sm89_tf32_joint, sm89_tf32_joint_rejection) = if plan.common {
                match compile(super::kernel_identity::ModuleKind::TriadSm89Tf32Joint) {
                    Ok(module) => (Some(module), None),
                    Err(error) => (None, Some(error)),
                }
            } else {
                (None, None)
            };
            let specialized = match (arch, device_cc) {
                ("sm_90a", Some((9, 0)))
                    if plan.architecture
                        == Some(super::kernel_identity::ModuleKind::TriadSm90a) =>
                {
                    compile(super::kernel_identity::ModuleKind::TriadSm90a)
                        .ok()
                        .and_then(|module| {
                            super::gemm_bi_triad::modules::qualify_specialized_module(module).ok()
                        })
                }
                ("sm_90a", Some((9, 0))) => {
                    static WGMMA_TOOLKIT: std::sync::Once = std::sync::Once::new();
                    super::diagnostics::warn_once(&WGMMA_TOOLKIT, || {
                        format!(
                            "the SM90a WGMMA module needs CUDA 13.3 or newer (ptxas before 13.3 \
                             could drop the register moves after wgmma.wait_group); NVRTC {:?} \
                             serves this board with the common tier",
                            nvrtc_version()
                        )
                    });
                    None
                }
                ("sm_100a", Some(device_cc @ (10, 0)))
                | ("sm_103a", Some(device_cc @ (10, 3)))
                | ("sm_107a", Some(device_cc @ (10, 7))) => {
                    super::gemm_bi_triad::modules::compile_sm100_optional(ctx, state_cap, device_cc)
                }
                ("sm_110a", Some(device_cc @ (11, 0))) => {
                    super::gemm_bi_triad::modules::compile_sm100_optional(ctx, state_cap, device_cc)
                }
                _ => None,
            };
            (
                fixed,
                scalar,
                sm80,
                finalist,
                finalist_rejection,
                sm89_half,
                sm89_half_rejection,
                sm89_exact_f32,
                sm89_exact_f32_rejection,
                sm89_exact_f32_d128,
                sm89_exact_f32_d128_rejection,
                sm89_tf32_joint,
                sm89_tf32_joint_rejection,
                specialized,
            )
        };
        // The inference cells are their own module on every board the Fixed
        // overlay serves; the CC 12.x family, whose Fixed module stays
        // byte-identical to its cohorts, composes neither.
        let (sm89_cells_module, sm89_cells_rejection) = if plan.common
            && super::gemm_bi_triad::modules::fixed_portable_overlay_composed(arch)
        {
            match compile(super::kernel_identity::ModuleKind::InferenceSm89Cells) {
                Ok(module) => (Some(module), None),
                Err(error) => (None, Some(error)),
            }
        } else {
            (None, None)
        };
        if let Some(error) = sm89_cells_rejection.as_ref() {
            static CELLS: std::sync::Once = std::sync::Once::new();
            super::diagnostics::warn_once(&CELLS, || {
                format!(
                    "the SM89 inference cells module did not compile ({error}); the Fixed \
                     overlay serves its shapes without the cells"
                )
            });
        }
        let compiler_identity = fixed.compiler_identity;
        let fold_transport_admitted = super::fold_transport::compiler_admitted(
            device_cc,
            arch,
            state_cap,
            compiler_identity.nvrtc_version,
        );
        let (fixed_sm89_half_pipeline, fixed_sm89_half_pipeline_rejection) =
            super::gemm_bi_triad::modules::load_fixed_sm89_half_pipeline(ctx, &fixed);
        let (fixed_sm89_half_swizzle, fixed_sm89_half_swizzle_rejection) =
            super::gemm_bi_triad::modules::load_fixed_sm89_half_swizzle(ctx, &fixed);
        let (fixed_sm89_half_s3, fixed_sm89_half_s3_rejection) =
            super::gemm_bi_triad::modules::load_fixed_sm89_half_s3(ctx, &fixed);
        let (fixed_sm89_tf32_rna_wide, fixed_sm89_tf32_rna_wide_rejection) =
            super::gemm_bi_triad::modules::load_fixed_sm89_rna_wide(ctx, &fixed);
        let (fixed_sm89_tf32_rna_n96, fixed_sm89_tf32_rna_n96_rejection) =
            super::gemm_bi_triad::modules::load_fixed_sm89_rna_n96(ctx, &fixed);
        let (fixed_sm89_half_m64n64_s3_f16, fixed_sm89_half_m64n64_s3_f16_rejection) =
            super::gemm_bi_triad::modules::load_fixed_sm89_half_m64n64_s3(ctx, &fixed);
        let (fixed_sm89_half_m128n64_s2_f16, fixed_sm89_half_m128n64_s2_f16_rejection) =
            super::gemm_bi_triad::modules::load_fixed_sm89_half_m128n64_s2(ctx, &fixed);
        let (fixed_sm89_f32_n64_copyplan, fixed_sm89_f32_n64_copyplan_rejection) =
            super::gemm_bi_triad::modules::load_fixed_sm89_f32_n64_copyplan(ctx, &fixed);
        let (fixed_sm89_cells, fixed_sm89_cell_rejections) = match sm89_cells_module.as_ref() {
            Some(module) => super::gemm_bi_triad::modules::load_fixed_sm89_cells(ctx, module),
            None => (std::collections::HashMap::new(), Vec::new()),
        };
        let (fixed_sm120_f32_n64_copyplan, fixed_sm120_f32_n64_copyplan_rejection) =
            super::gemm_bi_triad::modules::load_fixed_sm120_f32_n64_copyplan(ctx, &fixed);
        let (fixed_sm120_f32_n64_copyplan_t256, fixed_sm120_f32_n64_copyplan_t256_rejection) =
            super::gemm_bi_triad::modules::load_fixed_sm120_f32_n64_copyplan_t256(ctx, &fixed);
        let (
            fixed_sm120_f32_m128n64_copyplan_t256,
            fixed_sm120_f32_m128n64_copyplan_t256_rejection,
        ) = super::gemm_bi_triad::modules::load_fixed_sm120_f32_m128n64_copyplan_t256(ctx, &fixed);
        let (fixed_sm120_f32_n64_sliced, fixed_sm120_f32_n64_sliced_rejection) =
            super::gemm_bi_triad::modules::load_fixed_sm120_f32_n64_sliced(ctx, &fixed);
        let (fixed_sm120_fma_postbias, fixed_sm120_fma_postbias_rejection) =
            super::gemm_bi_triad::modules::load_fixed_sm120_fma_postbias(ctx, &fixed);
        let inference_sm89_bundle =
            super::gemm_bi_triad::modules::inference_bundle::load_inference_sm89_bundle(
                ctx, &fixed, state_cap,
            );
        let triad = GemmBiKernels::load(
            ctx,
            super::gemm_bi_triad::modules::GemmBiModuleSet {
                fixed_artifact: fixed.artifact_identity,
                scalar,
                sm80,
                finalist,
                finalist_compile_rejection: finalist_rejection,
                sm89_half,
                sm89_half_compile_rejection: sm89_half_rejection,
                sm89_exact_f32,
                sm89_exact_f32_compile_rejection: sm89_exact_f32_rejection,
                sm89_exact_f32_d128,
                sm89_exact_f32_d128_compile_rejection: sm89_exact_f32_d128_rejection,
                sm89_tf32_joint,
                sm89_tf32_joint_compile_rejection: sm89_tf32_joint_rejection,
                specialized,
                sm89_cells: sm89_cells_module
                    .as_ref()
                    .map(|module| module.artifact_identity),
            },
        )?;
        // A rejected TF32 module used to be recorded and never shown: the
        // process booted green and every TF32 request quietly ran scalar.
        if let Some(reason) = triad.portable_tf32_rejection() {
            static PORTABLE: std::sync::Once = std::sync::Once::new();
            super::diagnostics::warn_once(&PORTABLE, || {
                format!(
                    "the portable TF32 routes are not bound on this board ({reason}); the \
                     exact f32 kernels serve every TF32 request"
                )
            });
        }
        // The same disease on the retained Inference members: a rejected
        // bundle used to leave the slower legacy kernels serving every
        // retained shape with nothing said.
        for (member, slot) in [
            (
                "nn_sm89_tc128_f32out_s3_bf16",
                &inference_sm89_bundle.half_f32out_s3_bf16,
            ),
            (
                "nn_sm89_tc128_f32out_s3_f16",
                &inference_sm89_bundle.half_f32out_s3_f16,
            ),
            (
                "nn_sm89_f32_m128n64_tail_copyplan",
                &inference_sm89_bundle.exact_m128n64_tail,
            ),
        ] {
            if let Err(reason) = slot {
                static RETAINED: std::sync::Once = std::sync::Once::new();
                let reason = reason.clone();
                super::diagnostics::warn_once(&RETAINED, || {
                    format!(
                        "the retained Inference member {member} is not bound on this board                          ({reason}); its shapes fall back to the legacy kernels"
                    )
                });
            }
        }
        // The Ada inference cells are optional on every board, but a cell the
        // overlay composed and the loader still declined should be said aloud.
        if super::gemm_bi_triad::modules::fixed_portable_overlay_composed(arch)
            && !fixed_sm89_cell_rejections.is_empty()
        {
            static CELLS: std::sync::Once = std::sync::Once::new();
            let listed = fixed_sm89_cell_rejections
                .iter()
                .map(|(symbol, reason)| format!("{symbol} ({reason})"))
                .collect::<Vec<_>>()
                .join(", ");
            super::diagnostics::warn_once(&CELLS, || {
                format!(
                    "the Ada inference cells {listed} are not bound on this board; the ladder \
                     serves their shapes"
                )
            });
        }
        let excluded = triad.tf32_excluded_symbols();
        if !excluded.is_empty() {
            static EXCLUDED: std::sync::Once = std::sync::Once::new();
            super::diagnostics::warn_once(&EXCLUDED, || {
                format!(
                    "{} TF32 route(s) are excluded on this toolkit and decline to the exact \
                     family: {}",
                    excluded.len(),
                    excluded
                        .iter()
                        .map(|exclusion| exclusion.reason.as_str())
                        .collect::<Vec<_>>()
                        .join("; ")
                )
            });
        }
        if let Some(reason) = triad.specialized_tf32_rejection() {
            static SPECIALIZED: std::sync::Once = std::sync::Once::new();
            super::diagnostics::warn_once(&SPECIALIZED, || {
                format!(
                    "the specialized TF32 module is not bound on this board ({reason}); the \
                     portable or exact kernels serve every TF32 request"
                )
            });
        }
        if let Some(reason) = triad.finalist_tf32_rejection() {
            static FINALIST: std::sync::Once = std::sync::Once::new();
            super::diagnostics::warn_once(&FINALIST, || {
                format!(
                    "the optional Ada TF32 finalist is not bound ({reason}); the portable or exact kernels remain available"
                )
            });
        }
        if let Some(reason) = triad.sm89_half_rejection() {
            static HALF_MODULE: std::sync::Once = std::sync::Once::new();
            super::diagnostics::warn_once(&HALF_MODULE, || {
                format!(
                    "the optional Ada half-Triad module is not bound ({reason}); the existing typed kernels remain available"
                )
            });
        }
        let half_excluded = triad.sm89_half_exclusions();
        if !half_excluded.is_empty() {
            static HALF_EXCLUDED: std::sync::Once = std::sync::Once::new();
            super::diagnostics::warn_once(&HALF_EXCLUDED, || {
                format!(
                    "{} Ada half-Triad symbol(s) failed resource admission while their siblings remain bound: {}",
                    half_excluded.len(),
                    half_excluded
                        .iter()
                        .map(|exclusion| format!("{}: {}", exclusion.symbol, exclusion.reason))
                        .collect::<Vec<_>>()
                        .join("; ")
                )
            });
        }
        if let Some(reason) = triad.sm89_exact_f32_rejection() {
            static EXACT_F32_MODULE: std::sync::Once = std::sync::Once::new();
            super::diagnostics::warn_once(&EXACT_F32_MODULE, || {
                format!(
                    "the optional Ada exact-F32 Triad module is not bound ({reason}); the existing exact kernels remain available"
                )
            });
        }
        for exclusion in triad.sm89_exact_f32_exclusions() {
            static D768_IN: std::sync::Once = std::sync::Once::new();
            static D768_OUT: std::sync::Once = std::sync::Once::new();
            static PRISM: std::sync::Once = std::sync::Once::new();
            let once = match exclusion.symbol {
                super::gemm_bi_triad::D768_IN_FUSED_SYMBOL => &D768_IN,
                super::gemm_bi_triad::D768_OUT_RAW_SYMBOL => &D768_OUT,
                super::gemm_bi_triad::PRISM_RAW_SYMBOL => &PRISM,
                _ => continue,
            };
            super::diagnostics::warn_once(once, || {
                format!(
                    "Ada exact-F32 Triad symbol {} is excluded while its siblings remain available: {}",
                    exclusion.symbol, exclusion.reason
                )
            });
        }
        if let Some(reason) = triad.sm89_exact_f32_d128_rejection() {
            static EXACT_F32_D128_MODULE: std::sync::Once = std::sync::Once::new();
            super::diagnostics::warn_once(&EXACT_F32_D128_MODULE, || {
                format!(
                    "the optional Ada exact-F32 d128 Triad module is not bound ({reason}); the SplitM64 kernels remain available"
                )
            });
        }
        for exclusion in triad.sm89_exact_f32_d128_exclusions() {
            static D128_IN: std::sync::Once = std::sync::Once::new();
            static D128_OUT: std::sync::Once = std::sync::Once::new();
            let once = match exclusion.symbol {
                super::gemm_bi_triad::D128_IN_SYMBOL => &D128_IN,
                super::gemm_bi_triad::D128_OUT_SYMBOL => &D128_OUT,
                _ => continue,
            };
            super::diagnostics::warn_once(once, || {
                format!(
                    "Ada exact-F32 d128 Triad symbol {} is excluded while its sibling remains available: {}",
                    exclusion.symbol, exclusion.reason
                )
            });
        }
        if let Some(reason) = triad.sm89_tf32_joint_rejection() {
            static TF32_JOINT_MODULE: std::sync::Once = std::sync::Once::new();
            super::diagnostics::warn_once(&TF32_JOINT_MODULE, || {
                format!(
                    "the optional Ada TF32 joint module is not bound ({reason}); the existing deterministic TF32 kernels remain available"
                )
            });
        }
        for exclusion in triad.sm89_tf32_joint_exclusions() {
            static TRANSPOSE: std::sync::Once = std::sync::Once::new();
            static TN_N96: std::sync::Once = std::sync::Once::new();
            static TN_M64N64: std::sync::Once = std::sync::Once::new();
            static TN_M64N96_S2: std::sync::Once = std::sync::Once::new();
            static NN_N96: std::sync::Once = std::sync::Once::new();
            static NN_N96_BASELINE: std::sync::Once = std::sync::Once::new();
            static NT_A_LDMATRIX_N96: std::sync::Once = std::sync::Once::new();
            static NT_RNA_M144N96_S2: std::sync::Once = std::sync::Once::new();
            static NT_ROWSTAGE_M128N192_S2: std::sync::Once = std::sync::Once::new();
            static TN_DIRECT_M192N192_S2: std::sync::Once = std::sync::Once::new();
            static TN_M96N192_S2: std::sync::Once = std::sync::Once::new();
            static TN_M96N96_S3: std::sync::Once = std::sync::Once::new();
            let once = match exclusion.symbol {
                super::gemm_bi_triad::TN_PRE_RNA_TRANSPOSE_SYMBOL => &TRANSPOSE,
                super::gemm_bi_triad::TN_PRE_RNA_N96_SYMBOL => &TN_N96,
                super::gemm_bi_triad::TN_PRE_RNA_M64N64_SYMBOL => &TN_M64N64,
                super::gemm_bi_triad::TN_PRE_RNA_M64N96_S2_SYMBOL => &TN_M64N96_S2,
                super::gemm_bi_triad::NN_ADD_HALF_DIRECT_N96_SYMBOL => &NN_N96,
                super::gemm_bi_triad::NN_ADD_HALF_N96_SYMBOL => &NN_N96_BASELINE,
                super::gemm_bi_triad::NT_A_LDMATRIX_N96_SYMBOL => &NT_A_LDMATRIX_N96,
                super::gemm_bi_triad::NT_RNA_M144N96_S2_SYMBOL => &NT_RNA_M144N96_S2,
                super::gemm_bi_triad::NT_ROWSTAGE_M128N192_S2_SYMBOL => &NT_ROWSTAGE_M128N192_S2,
                super::gemm_bi_triad::TN_DIRECT_M192N192_S2_SYMBOL => &TN_DIRECT_M192N192_S2,
                super::gemm_bi_triad::TN_PRE_RNA_M96N192_S2_SYMBOL => &TN_M96N192_S2,
                super::gemm_bi_triad::TN_PRE_RNA_M96N96_S3_SYMBOL => &TN_M96N96_S3,
                _ => continue,
            };
            super::diagnostics::warn_once(once, || {
                format!(
                    "Ada TF32 joint symbol {} is excluded while its siblings remain available: {}",
                    exclusion.symbol, exclusion.reason
                )
            });
        }
        let module = fixed.module;

        let get = |name: &str| -> Result<CudaFunction, String> {
            module
                .load_function(name)
                .map_err(|e| format!("Kernel '{name}' not found: {e:?}"))
        };
        let load_typed = |base: &str| -> Result<TypedKernel, String> {
            Ok(TypedKernel {
                f32: get(&format!("{base}_f32"))?,
                bf16: get(&format!("{base}_bf16"))?,
                f16: get(&format!("{base}_f16"))?,
            })
        };
        let load_half = |base: &str| -> Result<HalfKernel, String> {
            Ok(HalfKernel {
                bf16: get(&format!("{base}_bf16"))?,
                f16: get(&format!("{base}_f16"))?,
            })
        };
        // Like `load_half`, plus the MAX_DYNAMIC_SHARED carveout for kernels
        // whose staging exceeds the 48 KB static cap: 34 KB for the typed
        // Big tiles (2-stage f32 smem = 33 KB), 74 KB for the BK=64 TC
        // family (NN 70 KB / TN 68 KB / NT 72 KB, padded bf16/f16).
        let load_half_dynsmem = |base: &str, bytes: i32| -> Result<HalfKernel, String> {
            let k = load_half(base)?;
            for f in [&k.bf16, &k.f16] {
                f.set_attribute(
                    cudarc::driver::sys::CUfunction_attribute_enum::CU_FUNC_ATTRIBUTE_MAX_DYNAMIC_SHARED_SIZE_BYTES,
                    bytes,
                )
                .map_err(|e| format!("set MAX_DYNAMIC_SHARED for {base}: {e:?}"))?;
            }
            Ok(k)
        };
        let load_sm120_half = |suffix: &str| -> Result<FixedSm120HalfKernels, String> {
            let kernels = FixedSm120HalfKernels {
                m64n64_bk64_s2: load_half(&format!("nn_sm120_tma_64x64_bk64_s2{suffix}"))?,
                m64n128_bk64_s2: load_half(&format!("nn_sm120_tma_64x128_bk64_s2{suffix}"))?,
                m128n64_bk32_s3: load_half(&format!("nn_sm120_tma_128x64_bk32_s3{suffix}"))?,
                m128n128_bk32_s2: load_half(&format!("nn_sm120_tma_128x128_bk32_s2{suffix}"))?,
                m128n128_bk32_s3: load_half(&format!("nn_sm120_tma_128x128_bk32_s3{suffix}"))?,
            };
            for (tile, bytes) in [
                (&kernels.m64n64_bk64_s2, 32_896),
                (&kernels.m64n128_bk64_s2, 49_280),
                (&kernels.m128n64_bk32_s3, 36_992),
                (&kernels.m128n128_bk32_s2, 32_896),
                (&kernels.m128n128_bk32_s3, 49_280),
            ] {
                for function in [&tile.bf16, &tile.f16] {
                    function
                        .set_attribute(
                            cudarc::driver::sys::CUfunction_attribute_enum::CU_FUNC_ATTRIBUTE_MAX_DYNAMIC_SHARED_SIZE_BYTES,
                            bytes,
                        )
                        .map_err(|error| {
                            format!(
                                "set MAX_DYNAMIC_SHARED for Fixed SM120 half{suffix}: {error:?}"
                            )
                        })?;
                }
            }
            Ok(kernels)
        };

        Ok(Self {
            state_cap,
            compiler_identity,
            triad,
            // SSM
            ssm_step_fwd: get("ssm_step_forward")?,
            ssm_burnin_fwd: get("ssm_burnin_forward")?,
            ssm_burnin_fwd_nosave: get("ssm_burnin_forward_nosave")?,
            ssm_backward_local: get("ssm_backward_local")?,
            ssm_reduce_d_bc_typed: TypedKernel {
                f32: get("ssm_reduce_d_BC_f32")?,
                bf16: get("ssm_reduce_d_BC_bf16")?,
                f16: get("ssm_reduce_d_BC_f16")?,
            },
            ssm_reduce_d_bc_tmajor_typed: TypedKernel {
                f32: get("ssm_reduce_d_BC_tmajor_f32")?,
                bf16: get("ssm_reduce_d_BC_tmajor_bf16")?,
                f16: get("ssm_reduce_d_BC_tmajor_f16")?,
            },
            ssm_reduce_d_d: get("ssm_reduce_d_D")?,
            ssm_reduce_d_a_log: get("ssm_reduce_d_a_log")?,
            ssm_reduce_d_a_log_chunks: get("ssm_reduce_d_a_log_chunks")?,
            // conv1d
            conv1d_burnin_fwd_nosave_tiled: get("conv1d_burnin_forward_nosave_tiled_f32")?,
            conv1d_burnin_nosave_tiled_typed: load_typed("conv1d_burnin_forward_nosave_tiled")?,
            // activations
            softplus_fwd: get("softplus_forward")?,
            softplus_bwd: get("softplus_backward")?,
            // norms
            rmsnorm_fwd: get("rmsnorm_forward")?,
            rmsnorm_bwd: get("rmsnorm_backward")?,
            // elementwise
            bias_broadcast: get("bias_broadcast")?,
            colsum_accumulate: get("colsum_accumulate")?,
            reduce_sum_axis0: get("reduce_sum_axis0")?,
            vec_add_inplace: get("vec_add_inplace")?,
            exp_negate: get("exp_negate")?,
            exp_negate2: get("exp_negate2")?,
            gather_cols: get("gather_cols")?,
            gather_bc_cols: get("gather_bc_cols")?,
            gather_bc_cols_tmajor: get("gather_bc_cols_tmajor")?,
            gather_bc_cols_tmajor_tiled: get("gather_bc_cols_tmajor_tiled")?,
            gate_mul_silu: get("gate_mul_silu")?,
            gating_backward: get("gating_backward")?,
            residual_add: get("residual_add")?,
            gather_last_timestep: get("gather_last_timestep")?,

            // mixed precision casts
            cast_f32_to_bf16: get("cast_f32_to_bf16")?,
            cast_f32_to_f16: get("cast_f32_to_f16")?,
            cast_bf16_to_f32: get("cast_bf16_to_f32")?,
            cast_f16_to_f32: get("cast_f16_to_f32")?,
            ssm_burnin_fwd_bf16: get("ssm_burnin_forward_bf16")?,
            ssm_burnin_fwd_f16: get("ssm_burnin_forward_f16")?,

            // parallel scan
            ssm_parallel_fwd: get("ssm_parallel_scan_fwd")?,
            ssm_parallel_fwd_nosave: get("ssm_parallel_scan_fwd_nosave")?,
            ssm_parallel_fwd_typed: TypedKernel {
                f32: get("ssm_parallel_scan_fwd")?,
                bf16: get("ssm_parallel_scan_fwd_bf16")?,
                f16: get("ssm_parallel_scan_fwd_f16")?,
            },
            ssm_parallel_fwd_nosave_typed: TypedKernel {
                f32: get("ssm_parallel_scan_fwd_nosave")?,
                bf16: get("ssm_parallel_scan_fwd_nosave_bf16")?,
                f16: get("ssm_parallel_scan_fwd_nosave_f16")?,
            },
            ssm_parallel_bwd_typed: TypedKernel {
                f32: get("ssm_parallel_scan_bwd_f32")?,
                bf16: get("ssm_parallel_scan_bwd_bf16")?,
                f16: get("ssm_parallel_scan_bwd_f16")?,
            },
            ssm_parallel_bwd_fold_typed: {
                let k = TypedKernel {
                    f32: get("ssm_parallel_scan_bwd_fold_f32")?,
                    bf16: get("ssm_parallel_scan_bwd_fold_bf16")?,
                    f16: get("ssm_parallel_scan_bwd_fold_f16")?,
                };
                // The f32 delta/u/dy stage is ~49 KB on top of the ~16 KB
                // f32 workspace — past the 48 KB static cap.
                for f in [&k.f32, &k.bf16, &k.f16] {
                    f.set_attribute(
                        cudarc::driver::sys::CUfunction_attribute_enum::CU_FUNC_ATTRIBUTE_MAX_DYNAMIC_SHARED_SIZE_BYTES,
                        67_584,
                    )
                    .map_err(|e| {
                        format!("set MAX_DYNAMIC_SHARED for scan_bwd_fold: {e:?}")
                    })?;
                }
                k
            },
            ssm_parallel_bwd_fold_short_typed: if fold_transport_admitted {
                Some(load_half_dynsmem(
                    "ssm_parallel_scan_bwd_fold_short",
                    67_584,
                )?)
            } else {
                None
            },
            ssm_parallel_bwd_fold_staged_typed: if fold_transport_admitted {
                Some(load_half_dynsmem(
                    "ssm_parallel_scan_bwd_fold_staged",
                    67_584,
                )?)
            } else {
                None
            },

            // AMP loss scaler
            check_inf_nan_f32: get("check_inf_nan_f32")?,
            scale_grads_f32: get("scale_grads_f32")?,
            scale_grads_skip_f32: get("scale_grads_skip_f32")?,
            grad_sumsq_partial_f32: get("grad_sumsq_partial_f32")?,
            grad_clip_coef_f32: get("grad_clip_coef_f32")?,
            scale_grads_dev_f32: get("scale_grads_dev_f32")?,
            grad_region_sumsq_partial_f32: get("grad_region_sumsq_partial_f32")?,
            grad_region_scale_dev_f32: get("grad_region_scale_dev_f32")?,

            // AdamW
            adamw_step_f32: get("adamw_step_f32")?,
            adamw_step_f32_capturable: get("adamw_step_f32_capturable")?,
            adamw_step_multi: load_typed("adamw_step_multi")?,

            // Batch-invariant GEMM
            gemm_bi_bf16_bf16: get("bf16_bf16")?,
            gemm_bi_f16_f16: get("f16_f16")?,
            gemm_bi_bf16_f32: get("bf16_f32")?,
            gemm_bi_f16_f32: get("f16_f32")?,
            gemm_bi_f32_f32: get("f32_f32")?,
            gemm_bi_f32_f32_s2: get("f32_f32_s2")?,
            gemm_bi_f32_f32_n128_s2: get("f32_f32_n128_s2")?,
            fixed_sm89_half_pipeline,
            fixed_sm89_half_pipeline_rejection,
            fixed_sm89_half_swizzle,
            fixed_sm89_half_swizzle_rejection,
            fixed_sm89_half_s3,
            fixed_sm89_half_s3_rejection,
            fixed_sm89_tf32_rna_wide,
            fixed_sm89_tf32_rna_wide_rejection,
            fixed_sm89_tf32_rna_n96,
            fixed_sm89_tf32_rna_n96_rejection,
            fixed_sm89_half_m64n64_s3_f16,
            fixed_sm89_half_m64n64_s3_f16_rejection,
            fixed_sm89_half_m128n64_s2_f16,
            fixed_sm89_half_m128n64_s2_f16_rejection,
            fixed_sm89_f32_n64_copyplan,
            fixed_sm89_f32_n64_copyplan_rejection,
            fixed_sm89_cells,
            sm89_cells_compiler_identity: sm89_cells_module
                .as_ref()
                .map(|module| module.compiler_identity),
            inference_sm89_bundle,
            fixed_sm120_f32_n64_copyplan,
            fixed_sm120_f32_n64_copyplan_rejection,
            fixed_sm120_f32_n64_copyplan_t256,
            fixed_sm120_f32_n64_copyplan_t256_rejection,
            fixed_sm120_f32_m128n64_copyplan_t256,
            fixed_sm120_f32_m128n64_copyplan_t256_rejection,
            fixed_sm120_f32_n64_sliced,
            fixed_sm120_f32_n64_sliced_rejection,
            fixed_sm120_fma_postbias,
            fixed_sm120_fma_postbias_rejection,
            gemm_bi_nn_tf32: {
                let kernels = FixedTf32Kernels {
                    m128n64_s2: get("nn_tf32_m128n64_bk32_s2")?,
                    m128n64_s3: get("nn_tf32_m128n64_bk32_s3")?,
                    m64n64_s2: get("nn_tf32_m64n64_bk32_s2")?,
                    m64n64_s3: get("nn_tf32_m64n64_bk32_s3")?,
                    m16n32_s4: get("nn_tf32_m16n32_bk32_s4")?,
                };
                for (function, bytes) in [
                    (&kernels.m128n64_s2, 55_296),
                    (&kernels.m128n64_s3, 82_944),
                    (&kernels.m64n64_s2, 32_768),
                    (&kernels.m64n64_s3, 55_296),
                    (&kernels.m16n32_s4, 29_696),
                ] {
                    function
                        .set_attribute(
                            cudarc::driver::sys::CUfunction_attribute_enum::CU_FUNC_ATTRIBUTE_MAX_DYNAMIC_SHARED_SIZE_BYTES,
                            bytes,
                        )
                        .map_err(|error| {
                            format!("set MAX_DYNAMIC_SHARED for Fixed TF32: {error:?}")
                        })?;
                }
                kernels
            },
            gemm_bi_nn_tf32_sm120: if sm120_board
                && matches!(arch, "sm_120" | "sm_121" | "compute_120" | "compute_121")
            {
                let kernels = FixedSm120Tf32Kernels {
                    m128n64_s2: get("nn_sm120_tma_tf32_m128n64_bk32_s2")?,
                    m128n64_s3: get("nn_sm120_tma_tf32_m128n64_bk32_s3")?,
                    m64n128_s2: get("nn_sm120_tma_tf32_m64n128_bk32_s2")?,
                    m64n128_s3: get("nn_sm120_tma_tf32_m64n128_bk32_s3")?,
                    m64n64_s2_producer_warp: get("nn_sm120_tma_tf32_m64n64_bk32_s2_producer_warp")?,
                    m64n64_s2: get("nn_sm120_tma_tf32_m64n64_bk32_s2")?,
                    m64n64_s2_pair_store: get("nn_sm120_tma_tf32_m64n64_bk32_s2_pair_store")?,
                };
                for (function, bytes) in [
                    (&kernels.m128n64_s2, 49_280),
                    (&kernels.m128n64_s3, 73_856),
                    (&kernels.m64n128_s2, 49_280),
                    (&kernels.m64n128_s3, 73_856),
                    (&kernels.m64n64_s2_producer_warp, 32_896),
                    (&kernels.m64n64_s2, 32_896),
                    (&kernels.m64n64_s2_pair_store, 32_896),
                ] {
                    function
                        .set_attribute(
                            cudarc::driver::sys::CUfunction_attribute_enum::CU_FUNC_ATTRIBUTE_MAX_DYNAMIC_SHARED_SIZE_BYTES,
                            bytes,
                        )
                        .map_err(|error| {
                            format!("set MAX_DYNAMIC_SHARED for Fixed SM120 TF32: {error:?}")
                        })?;
                }
                Some(kernels)
            } else {
                None
            },
            gemm_bi_nn_half_sm120: if sm120_board
                && matches!(arch, "sm_120" | "sm_121" | "compute_120" | "compute_121")
            {
                Some(load_sm120_half("")?)
            } else {
                None
            },
            gemm_bi_nn_half_sm120_f32out: if sm120_board
                && matches!(arch, "sm_120" | "sm_121" | "compute_120" | "compute_121")
            {
                Some(load_sm120_half("_f32out")?)
            } else {
                None
            },

            // Batch-invariant matvec (M=1 specialization)
            matvec_bi_bf16_bf16: get("matvec_bi_bf16_bf16")?,
            matvec_bi_f16_f16: get("matvec_bi_f16_f16")?,
            matvec_bi_bf16_f32: get("matvec_bi_bf16_f32")?,
            matvec_bi_f16_f32: get("matvec_bi_f16_f32")?,
            matvec_bi_f32_f32: get("matvec_bi_f32_f32")?,

            // typed inference kernels
            softplus_fwd_typed: load_typed("softplus_forward")?,
            rmsnorm_fwd_resadd_typed: load_typed("rmsnorm_forward_resadd_f32in")?,
            bias_broadcast_typed: load_typed("bias_broadcast")?,
            elementwise_mul_typed: load_typed("elementwise_mul")?,
            residual_add_typed: load_typed("residual_add")?,
            gather_cols_typed: load_typed("gather_cols")?,
            gather_bc_cols_typed: load_typed("gather_bc_cols")?,
            gather_bc_cols_tmajor_typed: load_typed("gather_bc_cols_tmajor")?,
            gather_bc_cols_tmajor_tiled_typed: load_typed("gather_bc_cols_tmajor_tiled")?,
            gate_mul_silu_typed: load_typed("gate_mul_silu")?,
            gate_mul_silu_v_typed: load_typed("gate_mul_silu_v")?,
            elementwise_mul_v_typed: load_typed("elementwise_mul_v")?,
            softplus_copy_typed: load_typed("softplus_copy")?,
            ssm_step_fwd_fused_typed: load_typed("ssm_step_forward_fused")?,
            conv1d_step_fwd_silu_typed: load_typed("conv1d_step_forward_silu")?,
            ssm_burnin_nosave_typed: load_typed("ssm_burnin_forward_nosave")?,
            softplus_bwd_typed: load_typed("softplus_backward")?,
            gather_last_timestep_typed: load_typed("gather_last_timestep")?,
            vec_cast_zplus_typed: load_typed("vec_cast_zplus")?,
            concat_halves_typed: load_typed("concat_halves")?,
            scatter_add_cols_typed: load_typed("scatter_add_cols")?,
            reduce_bias_typed: load_typed("reduce_bias")?,

            // typed training-backward kernels
            gating_bwd_typed: load_typed("gating_backward")?,
            rmsnorm_bwd_typed: load_typed("rmsnorm_backward")?,
            conv1d_burnin_bwd_typed: load_typed("conv1d_burnin_backward")?,
            conv1d_burnin_fwd_tiled_typed: load_typed("conv1d_burnin_forward_tiled")?,
            conv1d_bwd_tiled_typed: load_typed("conv1d_bwd_tiled")?,
            // ssm_backward_local typed + typed-input reducers
            ssm_backward_local_typed: load_typed("ssm_backward_local")?,
            pack_xdbl_cols_typed: TypedKernel {
                f32: get("pack_xdbl_cols_f32")?,
                bf16: get("pack_xdbl_cols_bf16")?,
                f16: get("pack_xdbl_cols_f16")?,
            },

            // dual-dtype (half-only)
            rmsnorm_fwd_f32in_typed: load_half("rmsnorm_forward_f32in")?,
            rmsnorm_bwd_f32in_typed: load_half("rmsnorm_backward_f32in")?,
            residual_add_f32_typed: load_half("residual_add_f32")?,

            gemm_bi_nn_tc128_typed: load_half_dynsmem("nn_tc128", 71_680)?,
            gemm_bi_nn_tc128_f32out: load_half_dynsmem("nn_tc128_f32out", 71_680)?,
            gemm_bi_nn_tc64_f32out: load_half("nn_tc64_f32out")?,
            gemm_bi_nn_tc16_f32out: load_half("nn_tc16_f32out")?,
            gemm_bi_nn_tc64_typed: load_half("nn_tc64")?,
            gemm_bi_nn_tc16_typed: load_half("nn_tc16")?,
            gemm_bi_nn_tcw64_typed: load_half_dynsmem("nn_tcw64", 65_536)?,
            gemm_bi_nn_tcwn64_typed: load_half_dynsmem("nn_tcwn64", 98_304)?,
            gemm_bi_nn_sm90_typed: if arch == "sm_90a" {
                Some(load_half_dynsmem("nn_sm90a_wgmma_wg1", 49_152)?)
            } else {
                None
            },
            gemm_bi_nn_sm100_typed: if matches!(arch, "sm_100a" | "sm_103a" | "sm_107a" | "sm_110a")
            {
                Some(load_half_dynsmem("nn_sm100_tcgen_c4", 65_536)?)
            } else {
                None
            },
            _modules: CudaModuleAnchors::new(vec![module]),
        })
    }

    /// Compiler invocation that produced the loaded Fixed module.
    ///
    /// Scalar and SM80 triad compiler identities are exposed through the
    /// embedded [`GemmBiKernels`] aggregate.
    pub fn compiler_identity(&self) -> super::kernel_identity::CompilerIdentity {
        self.compiler_identity
    }

    pub fn ssm_parallel_bwd_fold_for_shape(
        &self,
        dtype: WeightDtype,
        batch: usize,
        time: usize,
        inner: usize,
        state: usize,
    ) -> &CudaFunction {
        let legacy = self.ssm_parallel_bwd_fold_typed.get(dtype);
        match super::fold_transport::route_for_shape(dtype, batch, time, inner, state) {
            super::fold_transport::FoldRoute::Short => self
                .ssm_parallel_bwd_fold_short_typed
                .as_ref()
                .map_or(legacy, |kernel| kernel.get(dtype)),
            super::fold_transport::FoldRoute::Staged => self
                .ssm_parallel_bwd_fold_staged_typed
                .as_ref()
                .map_or(legacy, |kernel| kernel.get(dtype)),
            super::fold_transport::FoldRoute::Legacy => legacy,
        }
    }

    pub(crate) fn specialized_compiler_identity(
        &self,
    ) -> Option<super::kernel_identity::CompilerIdentity> {
        self.triad
            .sm120_compiler_identity()
            .or_else(|| self.triad.sm100_compiler_identity())
            .or_else(|| self.triad.sm90a_compiler_identity())
    }

    /// Ordered artifact set available to deterministic GEMM dispatch.
    pub fn artifact_set_identity(&self) -> super::kernel_identity::ArtifactSetIdentity {
        self.triad.artifact_set_identity()
    }

    pub fn f32_triad_availability(&self) -> super::gemm_bi_triad::F32TriadAvailability {
        self.triad.f32_triad_availability()
    }

    /// The reason the specialized TF32 module is not bound, when it is not.
    pub fn specialized_tf32_rejection(&self) -> Option<&str> {
        self.triad.specialized_tf32_rejection()
    }

    pub fn finalist_tf32_rejection(&self) -> Option<&str> {
        self.triad.finalist_tf32_rejection()
    }

    pub fn triad_scalar_compiler_identity(&self) -> super::kernel_identity::CompilerIdentity {
        self.triad.scalar_compiler_identity()
    }

    pub(crate) fn triad_scalar_compute_capability(&self) -> (u32, u32) {
        self.triad.compute_capability()
    }

    pub(crate) fn tc64_streamk_resident_ctas(&self) -> u32 {
        self.triad.tc64_streamk_resident_ctas()
    }

    pub(crate) fn sm89_half_relay_resident_ctas(&self) -> u32 {
        self.triad.sm89_half_relay_resident_ctas()
    }

    pub(crate) fn triad_sm80_compiler_identity(&self) -> super::kernel_identity::CompilerIdentity {
        self.triad.sm80_compiler_identity()
    }

    pub(crate) fn triad_sm89_finalist_compiler_identity(
        &self,
    ) -> Option<super::kernel_identity::CompilerIdentity> {
        self.triad.sm89_finalist_compiler_identity()
    }

    pub fn triad_sm89_half_compiler_identity(
        &self,
    ) -> Option<super::kernel_identity::CompilerIdentity> {
        self.triad.sm89_half_compiler_identity()
    }

    pub fn triad_sm89_half_artifact_identity(
        &self,
    ) -> Option<super::kernel_identity::ArtifactIdentity> {
        self.triad.artifact_set_identity().sm89_half
    }

    pub fn triad_sm89_half_rejection(&self) -> Option<&str> {
        self.triad.sm89_half_rejection()
    }

    pub fn triad_sm89_half_exclusions(&self) -> Vec<(&'static str, &str)> {
        self.triad
            .sm89_half_exclusions()
            .iter()
            .map(|excluded| (excluded.symbol, excluded.reason.as_str()))
            .collect()
    }

    #[doc(hidden)]
    pub fn triad_sm89_half_function(
        &self,
        route: super::gemm_bi_triad::Sm89HalfRoute,
        dtype: super::dtype::WeightDtype,
    ) -> Option<&CudaFunction> {
        self.triad.sm89_half_function(route, dtype)
    }

    pub(crate) fn fixed_sm89_cell_function(&self, symbol: &str) -> Option<&CudaFunction> {
        self.fixed_sm89_cells.get(symbol)
    }

    pub(in crate::mamba_ssm::gpu) fn triad_sm89_half_runtime_function(
        &self,
        symbol: &str,
    ) -> Option<&CudaFunction> {
        self.triad.sm89_half_runtime_function(symbol)
    }

    pub fn triad_sm89_exact_f32_compiler_identity(
        &self,
    ) -> Option<super::kernel_identity::CompilerIdentity> {
        self.triad.sm89_exact_f32_compiler_identity()
    }

    pub fn triad_sm89_exact_f32_artifact_identity(
        &self,
    ) -> Option<super::kernel_identity::ArtifactIdentity> {
        self.triad.artifact_set_identity().sm89_exact_f32
    }

    pub fn triad_sm89_exact_f32_rejection(&self) -> Option<&str> {
        self.triad.sm89_exact_f32_rejection()
    }

    pub fn triad_sm89_exact_f32_exclusions(&self) -> Vec<(&'static str, &str)> {
        self.triad
            .sm89_exact_f32_exclusions()
            .iter()
            .map(|excluded| (excluded.symbol, excluded.reason.as_str()))
            .collect()
    }

    #[doc(hidden)]
    pub fn triad_sm89_exact_f32_function(&self, symbol: &str) -> Option<&CudaFunction> {
        self.triad.sm89_exact_f32_function(symbol)
    }

    pub fn triad_sm89_exact_f32_d128_compiler_identity(
        &self,
    ) -> Option<super::kernel_identity::CompilerIdentity> {
        self.triad.sm89_exact_f32_d128_compiler_identity()
    }

    pub fn triad_sm89_exact_f32_d128_artifact_identity(
        &self,
    ) -> Option<super::kernel_identity::ArtifactIdentity> {
        self.triad.artifact_set_identity().sm89_exact_f32_d128
    }

    pub fn triad_sm89_exact_f32_d128_rejection(&self) -> Option<&str> {
        self.triad.sm89_exact_f32_d128_rejection()
    }

    pub fn triad_sm89_exact_f32_d128_exclusions(&self) -> Vec<(&'static str, &str)> {
        self.triad
            .sm89_exact_f32_d128_exclusions()
            .iter()
            .map(|excluded| (excluded.symbol, excluded.reason.as_str()))
            .collect()
    }

    #[doc(hidden)]
    pub fn triad_sm89_exact_f32_d128_function(&self, symbol: &str) -> Option<&CudaFunction> {
        self.triad.sm89_exact_f32_d128_function(symbol)
    }

    pub fn triad_sm89_tf32_joint_compiler_identity(
        &self,
    ) -> Option<super::kernel_identity::CompilerIdentity> {
        self.triad.sm89_tf32_joint_compiler_identity()
    }

    pub fn sm89_cells_compiler_identity(&self) -> Option<super::kernel_identity::CompilerIdentity> {
        self.sm89_cells_compiler_identity
    }

    pub fn triad_sm89_tf32_joint_artifact_identity(
        &self,
    ) -> Option<super::kernel_identity::ArtifactIdentity> {
        self.triad.artifact_set_identity().sm89_tf32_joint
    }

    pub fn triad_sm89_tf32_joint_rejection(&self) -> Option<&str> {
        self.triad.sm89_tf32_joint_rejection()
    }

    pub fn triad_sm89_tf32_joint_exclusions(&self) -> Vec<(&'static str, &str)> {
        self.triad
            .sm89_tf32_joint_exclusions()
            .iter()
            .map(|excluded| (excluded.symbol, excluded.reason.as_str()))
            .collect()
    }

    #[doc(hidden)]
    pub fn triad_sm89_tf32_joint_function(&self, symbol: &str) -> Option<&CudaFunction> {
        self.triad.sm89_tf32_joint_function(symbol)
    }

    pub(crate) fn tf32_function(&self, symbol: &str) -> Option<&CudaFunction> {
        self.triad.tf32_function(symbol)
    }

    pub(crate) fn triad_kernels(&self) -> &GemmBiKernels {
        &self.triad
    }

    /// The Split-K/Split-M partial scratch (8M f32 = 32 MB), allocated on
    /// first batch-invariant use. Zero-initialized like the old eager
    /// alloc; the partial kernels overwrite their region before the
    /// reduce reads it, so first-use contents were never load-bearing.
    pub fn splitk_scratch_buf(
        &self,
        stream: &Arc<cudarc::driver::CudaStream>,
    ) -> Result<&cudarc::driver::CudaSlice<f32>, String> {
        self.triad.splitk_scratch_buf(stream)
    }

    /// The W-transpose staging scratch (4,718,592 f32 = 18 MiB), allocated on
    /// first batch-invariant bwd_dx wide-path use.
    pub fn transpose_scratch_buf(
        &self,
        stream: &Arc<cudarc::driver::CudaStream>,
    ) -> Result<&cudarc::driver::CudaSlice<f32>, String> {
        self.triad.transpose_scratch_buf(stream)
    }
}

/// Discover CUDA include directory (for cuda_fp16.h, cuda_bf16.h).
/// Checks CUDA_HOME, CUDA_PATH, CUDA_ROOT, then standard install paths.
pub fn cuda_include_paths() -> Vec<String> {
    let mut candidates: Vec<String> = Vec::new();
    for var in ["CUDA_HOME", "CUDA_PATH", "CUDA_ROOT"] {
        if let Ok(p) = std::env::var(var) {
            candidates.push(format!("{p}/include"));
        }
    }
    for std_path in [
        "/usr/local/cuda/include",
        "/usr/local/cuda-13.2/include",
        "/usr/local/cuda-12.8/include",
        "/usr/local/cuda-12.6/include",
        "/usr/local/cuda-12.4/include",
        "/usr/local/cuda-12.2/include",
        "/opt/cuda/include",
    ] {
        candidates.push(std_path.to_string());
    }
    candidates
        .into_iter()
        .filter(|p| std::path::Path::new(p).join("cuda_fp16.h").exists())
        .collect()
}

#[cfg(test)]
mod module_plan_tests {
    use super::super::device::GpuDevice;
    use super::super::kernel_identity::ModuleKind;
    use super::MambaKernels;

    /// Every board loads the common tier; a board with its own module
    /// loads it beside the common tier, never instead of it. This is the
    /// policy the loader follows, read here without a device.
    #[test]
    fn every_sm80_board_loads_the_common_tier_beside_its_own_module() {
        let expected = [
            ((8, 0), None),
            ((8, 6), None),
            ((8, 7), None),
            ((8, 9), None),
            ((9, 0), Some(ModuleKind::TriadSm90a)),
            ((10, 0), Some(ModuleKind::TriadSm100)),
            ((10, 3), Some(ModuleKind::TriadSm100)),
            ((10, 7), Some(ModuleKind::TriadSm100)),
            ((11, 0), Some(ModuleKind::TriadSm100)),
            ((12, 0), Some(ModuleKind::TriadSm120)),
            ((12, 1), Some(ModuleKind::TriadSm120)),
        ];
        for (cc, architecture) in expected {
            let arch = GpuDevice::resolve_nvrtc_target(cc).unwrap();
            let device_cc = Some((cc.0 as i32, cc.1 as i32));
            let plan = MambaKernels::triad_module_plan(arch, device_cc, (13, 4));
            assert!(plan.common, "CC {cc:?} ({arch}) must load the common tier");
            assert_eq!(plan.architecture, architecture, "CC {cc:?} ({arch})");
        }
    }

    #[test]
    fn the_wgmma_module_waits_for_the_fixed_ptxas() {
        for nvrtc in [(13, 0), (13, 1), (13, 2)] {
            let plan = MambaKernels::triad_module_plan("sm_90a", Some((9, 0)), nvrtc);
            assert!(plan.common);
            assert_eq!(plan.architecture, None, "NVRTC {nvrtc:?}");
        }
        let plan = MambaKernels::triad_module_plan("sm_90a", Some((9, 0)), (13, 3));
        assert_eq!(plan.architecture, Some(ModuleKind::TriadSm90a));
    }

    #[test]
    fn an_unknown_capability_loads_neither_tier() {
        let plan = MambaKernels::triad_module_plan("sm_75", Some((7, 5)), (13, 2));
        assert!(!plan.common);
        assert_eq!(plan.architecture, None);
        let plan = MambaKernels::triad_module_plan("sm_80", None, (13, 2));
        assert!(!plan.common);
    }
}