gam-sae 0.3.152

Sparse-autoencoder latent-manifold terms for the gam penalized-likelihood engine
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
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
//! Complete softmax SAE reconstruction row jets on CPU and CUDA (#2304).
//!
//! The quasi-Laplace consumers differentiate
//!
//! ```text
//! f_c = sum_k z_k D_{k,c}(t_k),       z = softmax(inv_tau * logits),
//! ```
//!
//! in the live row coordinates: free gate logits and every active latent
//! coordinate.  The authoritative contract here is therefore the complete
//! packed schedule consumed by `SaeRowJets`: reconstruction first and second
//! channels, decoder-border values, and decoder-border mixed channels.  There
//! is no gate-only compatibility surface.
//!
//! The CUDA kernels evaluate the same centered-moment identities as
//! `crate::row_jet_program::execute_softmax_row_program`.  In particular they
//! emit both orientations of every logit-by-coordinate block and the
//! same-atom coordinate Hessian from the live decoded basis jets.  Every output
//! element costs O(1); the old seeded dense-jet recurrence, whose intermediates
//! carried O(q^2) channels and whose logit Hessian cost O(K) per output, is gone.
//!
//! Production calls this module through a bounded tile plan.  The plan accounts
//! for every host and device input/output byte, uses the calibrated row-kernel
//! crossover, and makes the CPU/device choice before allocating or launching.
//! Once a device tile is selected, NVRTC/allocation/launch failures are errors;
//! they are never hidden by retrying the tile on the CPU.

use crate::row_jet_program::{
    SaeOrder2RowProgramSource, SaeRowPrimary, SaeScheduledRowJets, execute_softmax_row_program,
};

/// One primary in a complete SAE reconstruction row.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SaeRowJetPrimary {
    Logit { atom: usize },
    Coordinate { atom: usize, axis: usize },
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SaeCoordinateSlot {
    pub atom: usize,
    pub axis: usize,
    pub slot: usize,
}

fn public_primary(value: SaeRowPrimary) -> SaeRowJetPrimary {
    match value {
        SaeRowPrimary::Logit { atom } => SaeRowJetPrimary::Logit { atom },
        SaeRowPrimary::Coord { atom, axis } => SaeRowJetPrimary::Coordinate { atom, axis },
    }
}

fn scheduled_primary(value: SaeRowJetPrimary) -> SaeRowPrimary {
    match value {
        SaeRowJetPrimary::Logit { atom } => SaeRowPrimary::Logit { atom },
        SaeRowJetPrimary::Coordinate { atom, axis } => SaeRowPrimary::Coord { atom, axis },
    }
}

/// `(is_logit, atom)` for one primary — the discriminant the θ-adjoint trace
/// reduction branches on.
fn primary_kind_atom(value: SaeRowJetPrimary) -> (bool, usize) {
    match value {
        SaeRowJetPrimary::Logit { atom } => (true, atom),
        SaeRowJetPrimary::Coordinate { atom, .. } => (false, atom),
    }
}

/// Derivative of the softmax data-weight product `z_{atom_a}·z_{atom_b}` with
/// respect to logit `atom_w`, divided out to a per-pair factor. Mirrors
/// `SaeManifoldTerm::softmax_data_weight_product_logit_factor` so the contracted
/// trace oracle differentiates the exact same operator the dense adjoint does.
fn softmax_data_weight_product_logit_factor(
    assignments: &[f64],
    atom_a: usize,
    atom_b: usize,
    atom_w: usize,
    inv_tau: f64,
) -> f64 {
    let a_w = assignments[atom_w];
    let left = if atom_w == atom_a { 1.0 } else { 0.0 } - a_w;
    let right = if atom_w == atom_b { 1.0 } else { 0.0 } - a_w;
    (left + right) * inv_tau
}

/// Complete semantic inputs for one softmax row.
///
/// `decoded_first` is indexed by the live primary slot (`q * p`); logit slots
/// are exact zero. `decoded_second` is `q * q * p`; only same-atom coordinate
/// pairs can be nonzero. These are contractions of the term's live basis
/// Jacobian/Hessian with its decoder, not derivatives reconstructed from stale
/// coordinates or a second basis implementation.
#[derive(Debug, Clone, PartialEq)]
pub struct SaeSoftmaxRowJetInput {
    pub n_atoms: usize,
    pub out_dim: usize,
    pub primaries: Vec<SaeRowJetPrimary>,
    pub coordinate_slots: Vec<SaeCoordinateSlot>,
    pub gate_values: Vec<f64>,
    pub active_atoms: Vec<bool>,
    pub sqrt_row_weight: f64,
    pub decoded: Vec<f64>,
    pub decoded_first: Vec<f64>,
    pub decoded_second: Vec<f64>,
    pub beta_atoms: std::sync::Arc<[usize]>,
    pub beta_basis_values: Vec<f64>,
    pub beta_basis_first: Vec<f64>,
    pub beta_outputs: std::sync::Arc<[f64]>,
}

impl SaeSoftmaxRowJetInput {
    pub fn coordinate_slots_for(primaries: &[SaeRowJetPrimary]) -> Vec<SaeCoordinateSlot> {
        let mut slots: Vec<SaeCoordinateSlot> = primaries
            .iter()
            .copied()
            .enumerate()
            .filter_map(|(slot, primary)| match primary {
                SaeRowJetPrimary::Coordinate { atom, axis } => {
                    Some(SaeCoordinateSlot { atom, axis, slot })
                }
                SaeRowJetPrimary::Logit { .. } => None,
            })
            .collect();
        slots.sort_unstable_by_key(|entry| (entry.atom, entry.axis));
        slots
    }

    #[inline]
    pub fn n_primaries(&self) -> usize {
        self.primaries.len()
    }

    #[inline]
    pub fn n_beta_borders(&self) -> usize {
        self.beta_atoms.len()
    }

    /// Snapshot the authoritative borrowed production row source into a bounded
    /// tile input. No gate or basis law is re-evaluated here: values and decoded
    /// derivatives come from the exact live source used by the CPU schedule.
    pub(crate) fn from_source<S: SaeOrder2RowProgramSource>(
        source: &S,
        sqrt_row_weight: f64,
        shared_beta_layout: Option<(std::sync::Arc<[usize]>, std::sync::Arc<[f64]>)>,
    ) -> Result<Self, String> {
        let n_atoms = source.n_atoms();
        let out_dim = source.out_dim();
        let q = source.n_primaries();
        let n_beta = source.n_beta_borders();
        let primaries: Vec<SaeRowJetPrimary> = (0..q)
            .map(|slot| public_primary(source.primary(slot)))
            .collect();

        let decoded_len = n_atoms
            .checked_mul(out_dim)
            .ok_or_else(|| "SAE row-jet decoded length overflow".to_string())?;
        let first_len = q
            .checked_mul(out_dim)
            .ok_or_else(|| "SAE row-jet decoded-first length overflow".to_string())?;
        let second_len = q
            .checked_mul(q)
            .and_then(|value| value.checked_mul(out_dim))
            .ok_or_else(|| "SAE row-jet decoded-second length overflow".to_string())?;
        let beta_first_len = q
            .checked_mul(n_beta)
            .ok_or_else(|| "SAE row-jet beta-first length overflow".to_string())?;
        let beta_output_len = n_beta
            .checked_mul(out_dim)
            .ok_or_else(|| "SAE row-jet beta-output length overflow".to_string())?;

        let mut decoded = vec![0.0; decoded_len];
        for atom in 0..n_atoms {
            source.fill_decoded(atom, &mut decoded[atom * out_dim..(atom + 1) * out_dim]);
        }

        let mut decoded_first = vec![0.0; first_len];
        for (slot, primary) in primaries.iter().copied().enumerate() {
            if let SaeRowJetPrimary::Coordinate { atom, axis } = primary {
                source.fill_decoded_first(
                    atom,
                    axis,
                    &mut decoded_first[slot * out_dim..(slot + 1) * out_dim],
                );
            }
        }

        let mut decoded_second = vec![0.0; second_len];
        for (slot_a, primary_a) in primaries.iter().copied().enumerate() {
            let SaeRowJetPrimary::Coordinate {
                atom: atom_a,
                axis: axis_a,
            } = primary_a
            else {
                continue;
            };
            for (slot_b, primary_b) in primaries.iter().copied().enumerate() {
                let SaeRowJetPrimary::Coordinate {
                    atom: atom_b,
                    axis: axis_b,
                } = primary_b
                else {
                    continue;
                };
                if atom_a == atom_b {
                    let start = (slot_a * q + slot_b) * out_dim;
                    source.fill_decoded_second(
                        atom_a,
                        axis_a,
                        axis_b,
                        &mut decoded_second[start..start + out_dim],
                    );
                }
            }
        }

        let beta_atoms: std::sync::Arc<[usize]> = shared_beta_layout.as_ref().map_or_else(
            || {
                (0..n_beta)
                    .map(|border| source.beta_border_atom(border))
                    .collect::<Vec<_>>()
                    .into()
            },
            |(atoms, _)| atoms.clone(),
        );
        let beta_basis_values: Vec<f64> = (0..n_beta)
            .map(|border| source.beta_border_basis_value(border))
            .collect();
        let mut beta_basis_first = vec![0.0; beta_first_len];
        for (slot, primary) in primaries.iter().copied().enumerate() {
            let SaeRowJetPrimary::Coordinate { atom, axis } = primary else {
                continue;
            };
            for border in 0..n_beta {
                if beta_atoms[border] == atom {
                    beta_basis_first[slot * n_beta + border] =
                        source.beta_border_basis_first(border, axis);
                }
            }
        }
        let beta_outputs: std::sync::Arc<[f64]> = match shared_beta_layout {
            Some((_, outputs)) => outputs,
            None => {
                let mut outputs = vec![0.0; beta_output_len];
                for border in 0..n_beta {
                    outputs[border * out_dim..(border + 1) * out_dim]
                        .copy_from_slice(source.beta_border_output(border));
                }
                outputs.into()
            }
        };

        let input = Self {
            n_atoms,
            out_dim,
            coordinate_slots: Self::coordinate_slots_for(&primaries),
            primaries,
            gate_values: (0..n_atoms).map(|atom| source.gate_value(atom)).collect(),
            active_atoms: (0..n_atoms)
                .map(|atom| source.atom_is_active(atom))
                .collect(),
            sqrt_row_weight,
            decoded,
            decoded_first,
            decoded_second,
            beta_atoms,
            beta_basis_values,
            beta_basis_first,
            beta_outputs,
        };
        input.validate()?;
        Ok(input)
    }

    pub fn validate(&self) -> Result<(), String> {
        let k = self.n_atoms;
        let p = self.out_dim;
        let q = self.n_primaries();
        let n_beta = self.n_beta_borders();
        if k == 0 || p == 0 {
            return Err(format!(
                "complete SAE row jet requires nonzero atoms/output dimension; got K={k}, p={p}"
            ));
        }
        if !self.sqrt_row_weight.is_finite() || self.sqrt_row_weight < 0.0 {
            return Err(format!(
                "SAE row-jet sqrt_row_weight must be finite and nonnegative; got {}",
                self.sqrt_row_weight
            ));
        }
        let expected = [
            ("gate_values", self.gate_values.len(), k),
            ("active_atoms", self.active_atoms.len(), k),
            ("decoded", self.decoded.len(), checked_product(&[k, p])?),
            (
                "decoded_first",
                self.decoded_first.len(),
                checked_product(&[q, p])?,
            ),
            (
                "decoded_second",
                self.decoded_second.len(),
                checked_product(&[q, q, p])?,
            ),
            ("beta_basis_values", self.beta_basis_values.len(), n_beta),
            (
                "beta_basis_first",
                self.beta_basis_first.len(),
                checked_product(&[q, n_beta])?,
            ),
            (
                "beta_outputs",
                self.beta_outputs.len(),
                checked_product(&[n_beta, p])?,
            ),
        ];
        for (label, got, want) in expected {
            if got != want {
                return Err(format!(
                    "SAE row-jet {label} length {got} != expected {want}"
                ));
            }
        }
        if let Some((atom, value)) = self
            .gate_values
            .iter()
            .copied()
            .enumerate()
            .find(|(_, value)| *value < 0.0 || *value > 1.0)
        {
            return Err(format!(
                "SAE row-jet gate_values[{atom}] must be a probability; got {value}"
            ));
        }
        let gate_sum: f64 = self.gate_values.iter().sum();
        let rounding_mass = (k as f64) * f64::EPSILON;
        if rounding_mass >= 1.0 {
            return Err(format!(
                "SAE row-jet K={k} is too large to certify a normalized f64 softmax"
            ));
        }
        let summation_bound = rounding_mass / (1.0 - rounding_mass);
        let normalization_bound = f64::EPSILON + summation_bound + f64::EPSILON * summation_bound;
        if (gate_sum - 1.0).abs() > normalization_bound * gate_sum.abs().max(1.0) {
            return Err(format!(
                "SAE row-jet gate values must sum to one within the f64 normalization bound {normalization_bound:e}; got {gate_sum:e}"
            ));
        }
        for (slot, primary) in self.primaries.iter().copied().enumerate() {
            let atom = match primary {
                SaeRowJetPrimary::Logit { atom } | SaeRowJetPrimary::Coordinate { atom, .. } => {
                    atom
                }
            };
            if atom >= k {
                return Err(format!(
                    "SAE row-jet primary {slot} references atom {atom} outside K={k}"
                ));
            }
            if self.primaries[..slot].contains(&primary) {
                return Err(format!(
                    "SAE row-jet primary {primary:?} appears more than once"
                ));
            }
        }
        let expected_coordinate_slots = Self::coordinate_slots_for(&self.primaries);
        if self.coordinate_slots != expected_coordinate_slots {
            return Err(
                "SAE row-jet coordinate slot table is not the canonical primary-derived table"
                    .to_string(),
            );
        }
        for (border, &atom) in self.beta_atoms.iter().enumerate() {
            if atom >= k {
                return Err(format!(
                    "SAE row-jet beta border {border} references atom {atom} outside K={k}"
                ));
            }
        }
        for (label, values) in [
            ("gate_values", self.gate_values.as_slice()),
            ("decoded", self.decoded.as_slice()),
            ("decoded_first", self.decoded_first.as_slice()),
            ("decoded_second", self.decoded_second.as_slice()),
            ("beta_basis_values", self.beta_basis_values.as_slice()),
            ("beta_basis_first", self.beta_basis_first.as_slice()),
            ("beta_outputs", self.beta_outputs.as_ref()),
        ] {
            if let Some((index, value)) = values
                .iter()
                .copied()
                .enumerate()
                .find(|(_, value)| !value.is_finite())
            {
                return Err(format!(
                    "SAE row-jet {label}[{index}] must be finite; got {value}"
                ));
            }
        }
        Ok(())
    }
}

fn checked_product(factors: &[usize]) -> Result<usize, String> {
    factors.iter().try_fold(1usize, |product, &factor| {
        product
            .checked_mul(factor)
            .ok_or_else(|| format!("SAE row-jet shape product overflow for {factors:?}"))
    })
}

/// Flattened complete channels for a same-shape row tile.
#[derive(Debug, Clone, PartialEq)]
pub struct SaeRowJetChannels {
    pub n_rows: usize,
    pub n_atoms: usize,
    pub q: usize,
    pub p: usize,
    pub n_beta: usize,
    pub first: Vec<f64>,
    pub second: Vec<f64>,
    pub beta: Vec<f64>,
    /// The mathematically identical `beta_deriv` and `beta_l_deriv` channel is
    /// stored once and expanded only when constructing `SaeScheduledRowJets`.
    pub beta_mixed: Vec<f64>,
}

impl SaeRowJetChannels {
    fn zeros(
        n_rows: usize,
        n_atoms: usize,
        q: usize,
        p: usize,
        n_beta: usize,
    ) -> Result<Self, String> {
        Ok(Self {
            n_rows,
            n_atoms,
            q,
            p,
            n_beta,
            first: vec![0.0; checked_product(&[n_rows, q, p])?],
            second: vec![0.0; checked_product(&[n_rows, q, q, p])?],
            beta: vec![0.0; checked_product(&[n_rows, n_beta, p])?],
            beta_mixed: vec![0.0; checked_product(&[n_rows, q, n_beta, p])?],
        })
    }

    pub(crate) fn into_scheduled_rows(self) -> Vec<SaeScheduledRowJets> {
        let mut rows = Vec::with_capacity(self.n_rows);
        let first_row_len = self.q * self.p;
        let second_row_len = self.q * self.q * self.p;
        let beta_row_len = self.n_beta * self.p;
        let mixed_row_len = self.q * self.n_beta * self.p;
        for row in 0..self.n_rows {
            let mut scheduled = SaeScheduledRowJets::zeros(self.q, self.p, self.n_beta);
            for slot in 0..self.q {
                let source = row * first_row_len + slot * self.p;
                scheduled
                    .first_mut(slot)
                    .copy_from_slice(&self.first[source..source + self.p]);
                for other in 0..self.q {
                    let source = row * second_row_len + (slot * self.q + other) * self.p;
                    scheduled
                        .second_mut(slot, other)
                        .copy_from_slice(&self.second[source..source + self.p]);
                }
            }
            for border in 0..self.n_beta {
                let source = row * beta_row_len + border * self.p;
                scheduled
                    .beta_mut(border)
                    .copy_from_slice(&self.beta[source..source + self.p]);
                for slot in 0..self.q {
                    let source = row * mixed_row_len + (slot * self.n_beta + border) * self.p;
                    let values = &self.beta_mixed[source..source + self.p];
                    scheduled
                        .beta_deriv_mut(slot, border)
                        .copy_from_slice(values);
                    scheduled
                        .beta_l_deriv_mut(slot, border)
                        .copy_from_slice(values);
                }
            }
            rows.push(scheduled);
        }
        rows
    }
}

/// One resident contraction against the complete row-jet tower (#2304).
///
/// The A10 trace on the elementwise tile proved the tower itself is cheap
/// (9.9 ms of kernels) while materializing and downloading the packed
/// channels is not (646 ms of copies, 98.5% of copy+kernel time): the sole
/// production consumers immediately reduce every channel against per-row
/// vectors. These contraction shapes keep the tower on device: the kernels
/// compute the same channel formulas but reduce them in place, so the only
/// downloads are the `n·q` t-outputs and `n·n_beta` β-outputs and the only
/// row-varying upload beyond the semantic inputs is one `n·p` probe vector
/// (plus the small direction coefficients for the bilinear form).
#[derive(Debug, Clone, Copy)]
pub enum SaeRowJetContraction<'a> {
    /// `t[r][a] = ⟨first(r,a,·), probe_r⟩`, `beta[r][c] = ⟨beta(r,c,·), probe_r⟩`.
    ///
    /// This is the IFT right-hand-side shape: the consumer's whitened metric
    /// (when present) folds into the probe as `probe = U(Uᵀv)` because the
    /// consumer dot is `⟨Uᵀ jet, Uᵀ v⟩ = ⟨jet, U Uᵀ v⟩` exactly.
    Linear {
        /// Row-major `n_rows × p`.
        probe: &'a [f64],
    },
    /// The exact-Hessian HVP residual-curvature shape:
    ///
    /// `t[r][a]    = Σ_b ⟨probe_r, second(r,a,b,·)⟩ v_t[r][b]
    ///             + Σ_c ⟨probe_r, mixed(r,a,c,·)⟩ v_beta[r][c]`
    /// `beta[r][c] = Σ_a ⟨probe_r, mixed(r,a,c,·)⟩ v_t[r][a]`
    ///
    /// with `probe` the (metric-applied) per-row residual.
    Bilinear {
        /// Row-major `n_rows × p`.
        probe: &'a [f64],
        /// Row-major `n_rows × q`.
        v_t: &'a [f64],
        /// Row-major `n_rows × n_beta`.
        v_beta: &'a [f64],
    },
    /// The `Γ = tr(H⁻¹ ∂H/∂θ)` log-det θ-adjoint shape (#2304).
    ///
    /// For every t-direction `w` and every β-direction `w_β` the consumer
    /// (`logdet_theta_adjoint_for_block`) reduces the derivative matrix of the
    /// tower against the per-row selected inverse. With
    /// `dh_w[a][b] = ⟨second(a,w),first(b)⟩ + ⟨first(a),second(b,w)⟩` (and the
    /// softmax data-weight-product substitution below for logit `w` over
    /// coordinate pairs) the outputs are
    ///
    /// `t[r][w]    = Σ_{a,b} E_tt[r][a][b] dh_w[a][b]
    ///             + Σ_a Σ_c 2·inv_vβ[r][a][c] (⟨second(a,w),β(c)⟩ + ⟨first(a),βderiv(w,c)⟩)
    ///             + Σ_{i,j} βinv[i][j] (⟨βderiv(w,i),β(j)⟩ + ⟨β(i),βderiv(w,j)⟩)`,
    /// `β[r][c_w] = Σ_{a,b} E_tt[r][a][b] (⟨βderiv(a,c_w),first(b)⟩ + ⟨first(a),βderiv(b,c_w)⟩)
    ///             + Σ_a Σ_c 2·inv_vβ[r][a][c] ⟨βderiv(a,c_w),β(c)⟩`.
    ///
    /// `E_tt` is the row-local t–t weight with the Daleckii–Krein deflation
    /// correction already folded in (`E = U(W⊙F)Uᵀ`, `W = Uᵀ inv_vv U`); it
    /// equals `inv_vv` when the row carries no deflation. The t–β and β–β
    /// blocks are never deflated, so they contract the raw `inv_vβ` / `βinv`.
    /// The purely scalar diagonal channels (softmax majorizer, ARD, ordered
    /// Beta–Bernoulli prior, empirical-mass column pass) are NOT part of this
    /// tower reduction; the caller adds them as an `E_tt`-weighted host
    /// post-fold, which is exact because `tr(E·(dh+dh_scalar))` is linear.
    Trace {
        /// Row-major `n_rows × q × q` deflation-folded t–t weight `E_tt`.
        e_tt: &'a [f64],
        /// Row-major `n_rows × q × n_beta` t–β selected inverse, restricted to
        /// this tile's border channels.
        inv_vbeta: &'a [f64],
        /// Row-major `n_beta × n_beta` β–β selected inverse, shared across the
        /// same-shape tile (the border layout is identical for every row).
        beta_inv: &'a [f64],
    },
}

/// Reduced outputs of one contracted tile: per-row t and β coefficients only.
#[derive(Debug, Clone, PartialEq)]
pub struct SaeRowJetContractedTile {
    pub n_rows: usize,
    pub q: usize,
    pub n_beta: usize,
    /// Row-major `n_rows × q`.
    pub t: Vec<f64>,
    /// Row-major `n_rows × n_beta`.
    pub beta: Vec<f64>,
}

impl<'a> SaeRowJetContraction<'a> {
    fn validate(&self, n: usize, q: usize, p: usize, n_beta: usize) -> Result<(), String> {
        let expect = |label: &str, got: usize, want: usize| -> Result<(), String> {
            if got != want {
                return Err(format!(
                    "SAE row-jet contraction {label} length {got} != expected {want}"
                ));
            }
            Ok(())
        };
        match *self {
            SaeRowJetContraction::Linear { probe } => {
                expect("probe", probe.len(), checked_product(&[n, p])?)?;
                finite_or_err("probe", probe)
            }
            SaeRowJetContraction::Bilinear { probe, v_t, v_beta } => {
                expect("probe", probe.len(), checked_product(&[n, p])?)?;
                expect("v_t", v_t.len(), checked_product(&[n, q])?)?;
                expect("v_beta", v_beta.len(), checked_product(&[n, n_beta])?)?;
                finite_or_err("probe", probe)?;
                finite_or_err("v_t", v_t)?;
                finite_or_err("v_beta", v_beta)
            }
            SaeRowJetContraction::Trace {
                e_tt,
                inv_vbeta,
                beta_inv,
            } => {
                expect("e_tt", e_tt.len(), checked_product(&[n, q, q])?)?;
                expect(
                    "inv_vbeta",
                    inv_vbeta.len(),
                    checked_product(&[n, q, n_beta])?,
                )?;
                expect(
                    "beta_inv",
                    beta_inv.len(),
                    checked_product(&[n_beta, n_beta])?,
                )?;
                finite_or_err("e_tt", e_tt)?;
                finite_or_err("inv_vbeta", inv_vbeta)?;
                finite_or_err("beta_inv", beta_inv)
            }
        }
    }
}

fn finite_or_err(label: &str, values: &[f64]) -> Result<(), String> {
    if let Some((index, value)) = values
        .iter()
        .copied()
        .enumerate()
        .find(|(_, value)| !value.is_finite())
    {
        return Err(format!(
            "SAE row-jet contraction {label}[{index}] must be finite; got {value}"
        ));
    }
    Ok(())
}

/// Evaluate one already-planned bounded tile REDUCED against a contraction,
/// never materializing the packed channel tensors on the host. Device failures
/// propagate; the CPU path evaluates the identical reduction against the
/// authoritative row program one row at a time.
pub fn execute_softmax_row_jet_tile_contracted(
    rows: &[SaeSoftmaxRowJetInput],
    inv_tau: f64,
    path: SaeRowJetPath,
    contraction: SaeRowJetContraction<'_>,
) -> Result<SaeRowJetContractedTile, String> {
    if !inv_tau.is_finite() || inv_tau <= 0.0 {
        return Err(format!(
            "SAE row-jet inverse temperature must be finite and positive; got {inv_tau}"
        ));
    }
    let (_k, q, p, n_beta) = validate_tile(rows)?;
    contraction.validate(rows.len(), q, p, n_beta)?;
    if rows.is_empty() {
        return Ok(SaeRowJetContractedTile {
            n_rows: 0,
            q: 0,
            n_beta: 0,
            t: Vec::new(),
            beta: Vec::new(),
        });
    }
    match path {
        SaeRowJetPath::Cpu => cpu_contracted_tile(rows, inv_tau, q, p, n_beta, contraction),
        SaeRowJetPath::Device => {
            #[cfg(target_os = "linux")]
            {
                device::device_contracted_tile(rows, inv_tau, contraction)
                    .map_err(|error| error.to_string())
            }
            #[cfg(not(target_os = "linux"))]
            {
                Err("contracted SAE row-jet device tile requested on a non-Linux host".to_string())
            }
        }
    }
}

/// CPU reduction: run the authoritative row program per row and apply the SAME
/// dot/accumulation order the production consumers use on scheduled rows. Only
/// one row's channels are live at a time — the tile's packed tensors are never
/// materialized.
fn cpu_contracted_tile(
    rows: &[SaeSoftmaxRowJetInput],
    inv_tau: f64,
    q: usize,
    p: usize,
    n_beta: usize,
    contraction: SaeRowJetContraction<'_>,
) -> Result<SaeRowJetContractedTile, String> {
    let n = rows.len();
    let dot = |a: &[f64], b: &[f64]| -> f64 { a.iter().zip(b.iter()).map(|(&x, &y)| x * y).sum() };
    let mut t = vec![0.0_f64; checked_product(&[n, q])?];
    let mut beta = vec![0.0_f64; checked_product(&[n, n_beta])?];
    for (row, input) in rows.iter().enumerate() {
        let source = InputSource::new(input);
        let scheduled = execute_softmax_row_program(&source, inv_tau, input.sqrt_row_weight);
        source.finish()?;
        match contraction {
            SaeRowJetContraction::Linear { probe } => {
                let probe_row = &probe[row * p..(row + 1) * p];
                for slot in 0..q {
                    t[row * q + slot] = dot(scheduled.first(slot), probe_row);
                }
                for border in 0..n_beta {
                    beta[row * n_beta + border] = dot(scheduled.beta(border), probe_row);
                }
            }
            SaeRowJetContraction::Bilinear { probe, v_t, v_beta } => {
                let probe_row = &probe[row * p..(row + 1) * p];
                let v_t_row = &v_t[row * q..(row + 1) * q];
                let v_beta_row = &v_beta[row * n_beta..(row + 1) * n_beta];
                for a in 0..q {
                    let mut acc = 0.0_f64;
                    for b in 0..q {
                        acc += dot(probe_row, scheduled.second(a, b)) * v_t_row[b];
                    }
                    for border in 0..n_beta {
                        acc += dot(probe_row, scheduled.beta_deriv(a, border)) * v_beta_row[border];
                    }
                    t[row * q + a] = acc;
                }
                for border in 0..n_beta {
                    let mut acc = 0.0_f64;
                    for a in 0..q {
                        acc += dot(probe_row, scheduled.beta_deriv(a, border)) * v_t_row[a];
                    }
                    beta[row * n_beta + border] = acc;
                }
            }
            SaeRowJetContraction::Trace {
                e_tt,
                inv_vbeta,
                beta_inv,
            } => {
                let e_row = &e_tt[row * q * q..(row + 1) * q * q];
                let vbeta_row = &inv_vbeta[row * q * n_beta..(row + 1) * q * n_beta];
                // One t-adjoint direction `w` (a live logit or coordinate slot).
                for w in 0..q {
                    let (w_is_logit, atom_w) = primary_kind_atom(input.primaries[w]);
                    let mut gamma = 0.0_f64;
                    for a in 0..q {
                        let (a_is_logit, atom_a) = primary_kind_atom(input.primaries[a]);
                        for b in 0..q {
                            let (b_is_logit, atom_b) = primary_kind_atom(input.primaries[b]);
                            // Under softmax a logit `w` differentiates the
                            // coordinate-pair data curvature `⟨J_a,J_b⟩` through
                            // the assignment weights, not through second jets.
                            let dh = if w_is_logit && !a_is_logit && !b_is_logit {
                                dot(scheduled.first(a), scheduled.first(b))
                                    * softmax_data_weight_product_logit_factor(
                                        &input.gate_values,
                                        atom_a,
                                        atom_b,
                                        atom_w,
                                        inv_tau,
                                    )
                            } else {
                                dot(scheduled.second(a, w), scheduled.first(b))
                                    + dot(scheduled.first(a), scheduled.second(b, w))
                            };
                            gamma += e_row[a * q + b] * dh;
                        }
                        for border in 0..n_beta {
                            let dh = dot(scheduled.second(a, w), scheduled.beta(border))
                                + dot(scheduled.first(a), scheduled.beta_deriv(w, border));
                            gamma += 2.0 * vbeta_row[a * n_beta + border] * dh;
                        }
                    }
                    for i in 0..n_beta {
                        for j in 0..n_beta {
                            let dh = dot(scheduled.beta_deriv(w, i), scheduled.beta(j))
                                + dot(scheduled.beta(i), scheduled.beta_deriv(w, j));
                            gamma += beta_inv[i * n_beta + j] * dh;
                        }
                    }
                    t[row * q + w] = gamma;
                }
                // One β-adjoint direction `w_beta`.
                for w_beta in 0..n_beta {
                    let mut gamma = 0.0_f64;
                    for a in 0..q {
                        for b in 0..q {
                            let dh = dot(scheduled.beta_l_deriv(a, w_beta), scheduled.first(b))
                                + dot(scheduled.first(a), scheduled.beta_l_deriv(b, w_beta));
                            gamma += e_row[a * q + b] * dh;
                        }
                        for border in 0..n_beta {
                            let dh = dot(scheduled.beta_l_deriv(a, w_beta), scheduled.beta(border));
                            gamma += 2.0 * vbeta_row[a * n_beta + border] * dh;
                        }
                    }
                    beta[row * n_beta + w_beta] = gamma;
                }
            }
        }
    }
    Ok(SaeRowJetContractedTile {
        n_rows: n,
        q,
        n_beta,
        t,
        beta,
    })
}

/// Exact byte accounting for one same-shape row-jet tile.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SaeRowJetMemoryLedger {
    pub fixed_device_bytes: usize,
    pub device_bytes_per_row: usize,
    pub fixed_host_bytes: usize,
    pub cpu_host_bytes_per_row: usize,
    pub host_bytes_per_row: usize,
}

impl SaeRowJetMemoryLedger {
    pub fn for_shape(k: usize, q: usize, p: usize, n_beta: usize) -> Result<Self, String> {
        let f64_bytes = std::mem::size_of::<f64>();
        let i32_bytes = std::mem::size_of::<i32>();
        let f64_count = |shape: &[usize]| -> Result<usize, String> {
            checked_product(shape)?
                .checked_mul(f64_bytes)
                .ok_or_else(|| format!("SAE row-jet f64 byte count overflow for {shape:?}"))
        };
        let i32_count = |shape: &[usize]| -> Result<usize, String> {
            checked_product(shape)?
                .checked_mul(i32_bytes)
                .ok_or_else(|| format!("SAE row-jet i32 byte count overflow for {shape:?}"))
        };

        let mut fixed_device_bytes = f64_count(&[n_beta, p])?
            .checked_add(i32_count(&[n_beta])?)
            .ok_or_else(|| "SAE row-jet fixed device-byte sum overflow".to_string())?;
        let mut empty_handle_bytes = 0usize;
        let mut add_empty_handle = |bytes: usize| -> Result<(), String> {
            empty_handle_bytes = empty_handle_bytes
                .checked_add(bytes)
                .ok_or_else(|| "SAE row-jet empty-handle byte sum overflow".to_string())?;
            Ok(())
        };
        if q == 0 {
            add_empty_handle(i32_count(&[2])?)?; // primary kind + atom
            add_empty_handle(f64_count(&[4])?)?; // d1 + d2 + first + second
        }
        if n_beta == 0 {
            add_empty_handle(i32_count(&[1])?)?; // beta atom
            add_empty_handle(f64_count(&[3])?)?; // beta phi + output + beta result
        }
        if q == 0 || n_beta == 0 {
            add_empty_handle(f64_count(&[2])?)?; // beta first + mixed result
        }
        fixed_device_bytes = fixed_device_bytes
            .checked_add(empty_handle_bytes)
            .ok_or_else(|| "SAE row-jet fixed empty-handle byte sum overflow".to_string())?;
        let input_f64 = [
            f64_count(&[k])?,
            f64_count(&[1])?,
            f64_count(&[k, p])?,
            f64_count(&[q, p])?,
            f64_count(&[q, q, p])?,
            f64_count(&[n_beta])?,
            f64_count(&[q, n_beta])?,
        ]
        .into_iter()
        .try_fold(0usize, |sum, value| sum.checked_add(value))
        .ok_or_else(|| "SAE row-jet input-byte sum overflow".to_string())?;
        let input_i32 = [i32_count(&[k])?, i32_count(&[q])?, i32_count(&[q])?]
            .into_iter()
            .try_fold(0usize, |sum, value| sum.checked_add(value))
            .ok_or_else(|| "SAE row-jet descriptor-byte sum overflow".to_string())?;
        let output = [
            f64_count(&[q, p])?,
            f64_count(&[q, q, p])?,
            f64_count(&[n_beta, p])?,
            f64_count(&[q, n_beta, p])?,
        ]
        .into_iter()
        .try_fold(0usize, |sum, value| sum.checked_add(value))
        .ok_or_else(|| "SAE row-jet output-byte sum overflow".to_string())?;
        let device_bytes_per_row = input_f64
            .checked_add(input_i32)
            .and_then(|value| value.checked_add(output))
            .ok_or_else(|| "SAE row-jet device row-byte sum overflow".to_string())?;

        // Host residency has two phases. During a launch, the authoritative
        // semantic row snapshots coexist with contiguous CUDA staging and the
        // allocated download vectors. During production expansion, those row
        // snapshots and flat downloads coexist with `SaeScheduledRowJets` (whose
        // two mixed arrays intentionally duplicate the single wire channel).
        let active_bool_bytes = k
            .checked_mul(std::mem::size_of::<bool>())
            .ok_or_else(|| "SAE row-jet active-mask byte count overflow".to_string())?;
        let primary_bytes = q
            .checked_mul(std::mem::size_of::<SaeRowJetPrimary>())
            .ok_or_else(|| "SAE row-jet primary byte count overflow".to_string())?;
        // The shape planner runs before row snapshots exist, so charge the
        // canonical slot table at its exact worst-case length q. Logit entries
        // make the realized table smaller, never larger than this bound.
        let coordinate_slot_bytes = q
            .checked_mul(std::mem::size_of::<SaeCoordinateSlot>())
            .ok_or_else(|| "SAE row-jet coordinate-slot byte count overflow".to_string())?;
        let beta_atom_bytes = n_beta
            .checked_mul(std::mem::size_of::<usize>())
            .ok_or_else(|| "SAE row-jet beta-atom byte count overflow".to_string())?;
        let semantic_input = [
            input_f64,
            f64_count(&[n_beta, p])?,
            active_bool_bytes,
            primary_bytes,
            coordinate_slot_bytes,
            beta_atom_bytes,
            std::mem::size_of::<SaeSoftmaxRowJetInput>(),
        ]
        .into_iter()
        .try_fold(0usize, |sum, value| sum.checked_add(value))
        .ok_or_else(|| "SAE row-jet semantic-input byte sum overflow".to_string())?;
        let production_layout = q
            .checked_mul(std::mem::size_of::<SaeRowJetPrimary>())
            .and_then(|value| value.checked_add(std::mem::size_of::<Vec<SaeRowJetPrimary>>()))
            .ok_or_else(|| "SAE row-jet production-layout byte sum overflow".to_string())?;
        let staging_input = input_f64
            .checked_add(input_i32)
            .ok_or_else(|| "SAE row-jet staging-input byte sum overflow".to_string())?;
        let scheduled_output = output
            .checked_add(f64_count(&[q, n_beta, p])?)
            .and_then(|value| value.checked_add(std::mem::size_of::<SaeScheduledRowJets>()))
            .ok_or_else(|| "SAE row-jet scheduled-output byte sum overflow".to_string())?;
        let shared_host = semantic_input
            .checked_add(production_layout)
            .and_then(|value| value.checked_add(output))
            .ok_or_else(|| "SAE row-jet shared host byte sum overflow".to_string())?;
        let cpu_host_bytes_per_row = shared_host
            .checked_add(scheduled_output)
            .ok_or_else(|| "SAE row-jet CPU host row-byte sum overflow".to_string())?;
        let host_bytes_per_row = shared_host
            .checked_add(staging_input.max(scheduled_output))
            .ok_or_else(|| "SAE row-jet host row-byte sum overflow".to_string())?;
        let fixed_host_bytes = [
            f64_count(&[k])?,
            f64_count(&[n_beta, p])?,
            n_beta
                .checked_mul(std::mem::size_of::<usize>())
                .ok_or_else(|| "SAE row-jet shared beta-atom byte overflow".to_string())?,
            i32_count(&[n_beta])?,
            std::mem::size_of::<SaeRowJetChannels>(),
            4 * std::mem::size_of::<Vec<()>>(),
        ]
        .into_iter()
        .try_fold(0usize, |sum, value| sum.checked_add(value))
        .ok_or_else(|| "SAE row-jet fixed host byte sum overflow".to_string())?;
        Ok(Self {
            fixed_device_bytes,
            device_bytes_per_row,
            fixed_host_bytes,
            cpu_host_bytes_per_row,
            host_bytes_per_row,
        })
    }

    /// Byte accounting for a REDUCED (contracted) same-shape tile (#2304).
    ///
    /// The contracted seam never materializes the packed channel tower on
    /// either wire. Relative to [`Self::for_shape`] the `q²·p` second channel
    /// and the `q·n_beta·p` mixed channel leave the host download and the
    /// device output entirely; the device instead keeps the phase-one dot
    /// tables (`k + q + n_beta + q²`) and the reduced outputs (`q + n_beta`)
    /// resident, and the only row-varying host↔device traffic is the `p` probe
    /// upload plus the `q + n_beta` reduced scalars downloaded. This models the
    /// `Linear`/`Bilinear` contracted tiles — the live contracted callers.
    pub fn for_contracted_shape(
        k: usize,
        q: usize,
        p: usize,
        n_beta: usize,
    ) -> Result<Self, String> {
        let f64_bytes = std::mem::size_of::<f64>();
        let i32_bytes = std::mem::size_of::<i32>();
        let f64_count = |shape: &[usize]| -> Result<usize, String> {
            checked_product(shape)?
                .checked_mul(f64_bytes)
                .ok_or_else(|| format!("SAE row-jet f64 byte count overflow for {shape:?}"))
        };
        let i32_count = |shape: &[usize]| -> Result<usize, String> {
            checked_product(shape)?
                .checked_mul(i32_bytes)
                .ok_or_else(|| format!("SAE row-jet i32 byte count overflow for {shape:?}"))
        };
        let sum = |terms: &[usize], label: &str| -> Result<usize, String> {
            terms
                .iter()
                .copied()
                .try_fold(0usize, |acc, value| acc.checked_add(value))
                .ok_or_else(|| format!("SAE row-jet contracted {label} byte sum overflow"))
        };

        let elementwise = Self::for_shape(k, q, p, n_beta)?;
        // Semantic device inputs staged per row (same tensors the elementwise
        // tile uploads) plus the reduced phase-one/phase-two device state.
        let input_f64 = sum(
            &[
                f64_count(&[k])?,
                f64_count(&[1])?,
                f64_count(&[k, p])?,
                f64_count(&[q, p])?,
                f64_count(&[q, q, p])?,
                f64_count(&[n_beta])?,
                f64_count(&[q, n_beta])?,
            ],
            "input-f64",
        )?;
        let input_i32 = sum(
            &[i32_count(&[k])?, i32_count(&[q])?, i32_count(&[q])?],
            "input-i32",
        )?;
        let staging_input = input_f64
            .checked_add(input_i32)
            .ok_or_else(|| "SAE row-jet contracted staging-input byte sum overflow".to_string())?;
        let device_reduced = sum(
            &[
                f64_count(&[k])?,      // decoded·probe dots
                f64_count(&[q])?,      // d1·probe dots
                f64_count(&[n_beta])?, // beta_output·probe dots
                f64_count(&[q, q])?,   // d2·probe dots (bilinear)
                f64_count(&[q])?,      // reduced t output
                f64_count(&[n_beta])?, // reduced beta output
                f64_count(&[p])?,      // probe upload
                f64_count(&[q])?,      // v_t upload (bilinear)
                f64_count(&[n_beta])?, // v_beta upload (bilinear)
            ],
            "device-reduced",
        )?;
        let device_bytes_per_row = staging_input
            .checked_add(device_reduced)
            .ok_or_else(|| "SAE row-jet contracted device row-byte sum overflow".to_string())?;

        // Host residency drops the packed-channel download and its
        // `SaeScheduledRowJets` expansion; only the reduced scalars accumulate
        // per row, and one row's probe/direction operands are live at a time.
        // `shared_host` is the semantic snapshot + production layout the
        // contracted tile still holds, WITHOUT the `output` tower.
        let semantic_input = sum(
            &[
                input_f64,
                f64_count(&[n_beta, p])?,
                k.checked_mul(std::mem::size_of::<bool>())
                    .ok_or_else(|| "SAE row-jet contracted active-mask overflow".to_string())?,
                q.checked_mul(std::mem::size_of::<SaeRowJetPrimary>())
                    .ok_or_else(|| "SAE row-jet contracted primary overflow".to_string())?,
                q.checked_mul(std::mem::size_of::<SaeCoordinateSlot>())
                    .ok_or_else(|| "SAE row-jet contracted coordinate-slot overflow".to_string())?,
                n_beta
                    .checked_mul(std::mem::size_of::<usize>())
                    .ok_or_else(|| "SAE row-jet contracted beta-atom overflow".to_string())?,
                std::mem::size_of::<SaeSoftmaxRowJetInput>(),
            ],
            "semantic-input",
        )?;
        let production_layout = q
            .checked_mul(std::mem::size_of::<SaeRowJetPrimary>())
            .and_then(|value| value.checked_add(std::mem::size_of::<Vec<SaeRowJetPrimary>>()))
            .ok_or_else(|| "SAE row-jet contracted production-layout overflow".to_string())?;
        let shared_host = semantic_input
            .checked_add(production_layout)
            .ok_or_else(|| "SAE row-jet contracted shared-host byte sum overflow".to_string())?;
        let reduced_operands = sum(
            &[
                f64_count(&[q])?,
                f64_count(&[n_beta])?,
                f64_count(&[p])?,
                f64_count(&[q])?,
                f64_count(&[n_beta])?,
            ],
            "reduced-operands",
        )?;
        let cpu_host_bytes_per_row = shared_host
            .checked_add(reduced_operands)
            .ok_or_else(|| "SAE row-jet contracted CPU host row-byte sum overflow".to_string())?;
        let host_bytes_per_row = shared_host
            .checked_add(reduced_operands)
            .and_then(|value| value.checked_add(staging_input.saturating_sub(reduced_operands)))
            .ok_or_else(|| "SAE row-jet contracted host row-byte sum overflow".to_string())?;
        // The CPU path materializes exactly one packed row at a time; charge it
        // once as a fixed cost rather than per tile row.
        let one_scheduled_row = sum(
            &[
                f64_count(&[q, p])?,
                f64_count(&[q, q, p])?,
                f64_count(&[n_beta, p])?,
                f64_count(&[q, n_beta, p])?,
                std::mem::size_of::<SaeScheduledRowJets>(),
            ],
            "one-scheduled-row",
        )?;
        let fixed_host_bytes = elementwise
            .fixed_host_bytes
            .checked_add(one_scheduled_row)
            .ok_or_else(|| "SAE row-jet contracted fixed host byte sum overflow".to_string())?;
        Ok(Self {
            fixed_device_bytes: elementwise.fixed_device_bytes,
            device_bytes_per_row,
            fixed_host_bytes,
            cpu_host_bytes_per_row,
            host_bytes_per_row,
        })
    }

    /// The only production caller is the CUDA tile planner, which compiles
    /// under `cfg(target_os = "linux")`; off-Linux the lib target therefore has
    /// no caller and `-D dead-code` rejects the method (this class of break has
    /// been failing the macOS/Windows wheel jobs). `test` keeps it available for
    /// the platform-independent ledger fixture rather than gating that fixture
    /// to Linux, which would drop real coverage everywhere else.
    #[cfg(target_os = "linux")]
    fn maximum_rows(self, device_budget: usize, host_budget: usize) -> usize {
        let device_rows = device_budget
            .checked_sub(self.fixed_device_bytes)
            .map_or(0, |bytes| bytes / self.device_bytes_per_row.max(1));
        let host_rows = host_budget
            .checked_sub(self.fixed_host_bytes)
            .map_or(0, |bytes| bytes / self.host_bytes_per_row.max(1));
        device_rows.min(host_rows)
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SaeRowJetPath {
    Device,
    Cpu,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SaeRowJetExecutionPlan {
    pub path: SaeRowJetPath,
    pub tile_rows: usize,
    pub ledger: SaeRowJetMemoryLedger,
}

/// Decide the backend and bounded tile width before any CUDA launch.
pub fn plan_softmax_row_jets(
    total_rows: usize,
    k: usize,
    q: usize,
    p: usize,
    n_beta: usize,
    mode: gam_gpu::GpuPolicy,
    host_budget: usize,
) -> Result<SaeRowJetExecutionPlan, String> {
    if k == 0 || p == 0 {
        return Err(format!(
            "complete SAE row-jet plan requires nonzero K and p; got K={k}, p={p}"
        ));
    }
    let ledger = SaeRowJetMemoryLedger::for_shape(k, q, p, n_beta)?;
    plan_dispatch(total_rows, k, q, p, n_beta, mode, ledger, host_budget)
}

/// Decide the backend and bounded tile width for a REDUCED (contracted) tile.
///
/// The contracted ledger charges the smaller reduced download plus probe
/// upload instead of the full packed tower, so a same-shape tile fits more
/// rows per host/device budget than the elementwise plan admits. Used by the
/// resident-contraction consumers (`contracted_softmax_linear_rhs`,
/// `contracted_softmax_bilinear_hvp`).
pub fn plan_softmax_row_jets_contracted(
    total_rows: usize,
    k: usize,
    q: usize,
    p: usize,
    n_beta: usize,
    mode: gam_gpu::GpuPolicy,
    host_budget: usize,
) -> Result<SaeRowJetExecutionPlan, String> {
    if k == 0 || p == 0 {
        return Err(format!(
            "contracted SAE row-jet plan requires nonzero K and p; got K={k}, p={p}"
        ));
    }
    let ledger = SaeRowJetMemoryLedger::for_contracted_shape(k, q, p, n_beta)?;
    plan_dispatch(total_rows, k, q, p, n_beta, mode, ledger, host_budget)
}

/// Shared backend/tile-width dispatch for an already-computed shape ledger.
fn plan_dispatch(
    total_rows: usize,
    k: usize,
    q: usize,
    p: usize,
    n_beta: usize,
    mode: gam_gpu::GpuPolicy,
    ledger: SaeRowJetMemoryLedger,
    host_budget: usize,
) -> Result<SaeRowJetExecutionPlan, String> {
    if total_rows == 0 {
        return Ok(SaeRowJetExecutionPlan {
            path: SaeRowJetPath::Cpu,
            tile_rows: 0,
            ledger,
        });
    }
    let one_row_host_bytes = ledger
        .fixed_host_bytes
        .checked_add(ledger.cpu_host_bytes_per_row)
        .ok_or_else(|| "complete SAE one-row host-byte sum overflow".to_string())?;
    if one_row_host_bytes > host_budget {
        let second_channel_bytes = checked_product(&[q, q, p])?
            .checked_mul(std::mem::size_of::<f64>())
            .ok_or_else(|| "complete SAE q^2 p channel-byte sum overflow".to_string())?;
        return Err(format!(
            "complete SAE row jet refuses one K={k}, q={q}, p={p}, beta={n_beta} row: the q^2*p second channel alone is {second_channel_bytes} bytes and simultaneous host residency is {one_row_host_bytes} bytes, exceeding the cgroup-aware budget {host_budget}"
        ));
    }
    if mode == gam_gpu::GpuPolicy::Off {
        return Ok(SaeRowJetExecutionPlan {
            path: SaeRowJetPath::Cpu,
            tile_rows: 1,
            ledger,
        });
    }

    // This lower bound is derived from the smallest runtime calibration point.
    // It lets ordinary CPU-sized fits avoid creating a CUDA context at all.
    if mode == gam_gpu::GpuPolicy::Auto
        && total_rows < gam_gpu::policy::GpuDispatchPolicy::MIN_CALIBRATABLE_ROW_KERNEL_N
    {
        return Ok(SaeRowJetExecutionPlan {
            path: SaeRowJetPath::Cpu,
            tile_rows: 1,
            ledger,
        });
    }

    #[cfg(not(target_os = "linux"))]
    {
        if mode == gam_gpu::GpuPolicy::Required {
            return Err(
                "complete SAE row jet requires CUDA, which is unavailable on this platform"
                    .to_string(),
            );
        }
        Ok(SaeRowJetExecutionPlan {
            path: SaeRowJetPath::Cpu,
            tile_rows: 1,
            ledger,
        })
    }

    #[cfg(target_os = "linux")]
    {
        let runtime = if mode == gam_gpu::GpuPolicy::Required {
            gam_gpu::device_runtime::GpuRuntime::require()
                .map_err(|error| format!("complete SAE row jet requires CUDA: {error}"))?
        } else {
            let Some(runtime) = gam_gpu::device_runtime::GpuRuntime::resolve(mode)
                .map_err(|error| format!("complete SAE row-jet CUDA admission failed: {error}"))?
            else {
                return Ok(SaeRowJetExecutionPlan {
                    path: SaeRowJetPath::Cpu,
                    tile_rows: 1,
                    ledger,
                });
            };
            runtime
        };
        if mode == gam_gpu::GpuPolicy::Auto && total_rows < runtime.policy.row_kernel_min_n {
            return Ok(SaeRowJetExecutionPlan {
                path: SaeRowJetPath::Cpu,
                tile_rows: 1,
                ledger,
            });
        }
        let tile_rows = ledger.maximum_rows(runtime.memory_budget_bytes, host_budget);
        if tile_rows == 0 {
            if mode == gam_gpu::GpuPolicy::Required {
                return Err(format!(
                    "complete SAE row jet cannot fit one tile: fixed_device={} device_per_row={} host_per_row={} device_budget={} host_budget={}",
                    ledger.fixed_device_bytes,
                    ledger.device_bytes_per_row,
                    ledger.host_bytes_per_row,
                    runtime.memory_budget_bytes,
                    host_budget
                ));
            }
            return Ok(SaeRowJetExecutionPlan {
                path: SaeRowJetPath::Cpu,
                tile_rows: 1,
                ledger,
            });
        }
        Ok(SaeRowJetExecutionPlan {
            path: SaeRowJetPath::Device,
            tile_rows: tile_rows.min(total_rows),
            ledger,
        })
    }
}

fn validate_tile(rows: &[SaeSoftmaxRowJetInput]) -> Result<(usize, usize, usize, usize), String> {
    let Some(first) = rows.first() else {
        return Ok((0, 0, 0, 0));
    };
    first.validate()?;
    let shape = (
        first.n_atoms,
        first.n_primaries(),
        first.out_dim,
        first.n_beta_borders(),
    );
    for (row, input) in rows.iter().enumerate().skip(1) {
        input.validate()?;
        let candidate = (
            input.n_atoms,
            input.n_primaries(),
            input.out_dim,
            input.n_beta_borders(),
        );
        if candidate != shape {
            return Err(format!(
                "SAE row-jet tile row {row} shape {candidate:?} != first-row shape {shape:?}"
            ));
        }
        if input.beta_outputs != first.beta_outputs {
            return Err(format!(
                "SAE row-jet tile row {row} has a different decoder-border output frame"
            ));
        }
        if input.beta_atoms != first.beta_atoms {
            return Err(format!(
                "SAE row-jet tile row {row} has a different decoder-border atom layout"
            ));
        }
    }
    Ok(shape)
}

/// Evaluate one already-planned bounded tile. Device failures propagate.
pub fn execute_softmax_row_jet_tile(
    rows: &[SaeSoftmaxRowJetInput],
    inv_tau: f64,
    path: SaeRowJetPath,
) -> Result<SaeRowJetChannels, String> {
    if !inv_tau.is_finite() || inv_tau <= 0.0 {
        return Err(format!(
            "SAE row-jet inverse temperature must be finite and positive; got {inv_tau}"
        ));
    }
    let (k, q, p, n_beta) = validate_tile(rows)?;
    if rows.is_empty() {
        return SaeRowJetChannels::zeros(0, 0, 0, 0, 0);
    }
    match path {
        SaeRowJetPath::Cpu => cpu_tile(rows, inv_tau, k, q, p, n_beta),
        SaeRowJetPath::Device => {
            #[cfg(target_os = "linux")]
            {
                device::device_tile(rows, inv_tau, k, q, p, n_beta)
                    .map_err(|error| error.to_string())
            }
            #[cfg(not(target_os = "linux"))]
            {
                Err("complete SAE row-jet device tile requested on a non-Linux host".to_string())
            }
        }
    }
}

struct InputSource<'a> {
    input: &'a SaeSoftmaxRowJetInput,
    structural_error: std::cell::RefCell<Option<String>>,
}

impl<'a> InputSource<'a> {
    fn new(input: &'a SaeSoftmaxRowJetInput) -> Self {
        Self {
            input,
            structural_error: std::cell::RefCell::new(None),
        }
    }

    fn coordinate_slot(&self, atom: usize, axis: usize, context: &'static str) -> Option<usize> {
        let slot = self
            .input
            .coordinate_slots
            .binary_search_by_key(&(atom, axis), |entry| (entry.atom, entry.axis))
            .ok()
            .map(|index| self.input.coordinate_slots[index].slot);
        if slot.is_none() {
            let mut error = self.structural_error.borrow_mut();
            if error.is_none() {
                *error = Some(format!(
                    "{context} requested absent SAE coordinate ({atom}, {axis})"
                ));
            }
        }
        slot
    }

    fn finish(&self) -> Result<(), String> {
        match self.structural_error.borrow_mut().take() {
            Some(error) => Err(error),
            None => Ok(()),
        }
    }
}

impl SaeOrder2RowProgramSource for InputSource<'_> {
    fn n_atoms(&self) -> usize {
        self.input.n_atoms
    }
    fn out_dim(&self) -> usize {
        self.input.out_dim
    }
    fn n_primaries(&self) -> usize {
        self.input.n_primaries()
    }
    fn primary(&self, slot: usize) -> SaeRowPrimary {
        scheduled_primary(self.input.primaries[slot])
    }
    fn gate_value(&self, atom: usize) -> f64 {
        self.input.gate_values[atom]
    }
    fn atom_is_active(&self, atom: usize) -> bool {
        self.input.active_atoms[atom]
    }
    fn fill_decoded(&self, atom: usize, out: &mut [f64]) {
        let p = self.input.out_dim;
        out.copy_from_slice(&self.input.decoded[atom * p..(atom + 1) * p]);
    }
    fn fill_decoded_first(&self, atom: usize, axis: usize, out: &mut [f64]) {
        let p = self.input.out_dim;
        let Some(slot) = self.coordinate_slot(atom, axis, "decoded first channel") else {
            out.fill(0.0);
            return;
        };
        out.copy_from_slice(&self.input.decoded_first[slot * p..(slot + 1) * p]);
    }
    fn fill_decoded_second(&self, atom: usize, axis_a: usize, axis_b: usize, out: &mut [f64]) {
        let p = self.input.out_dim;
        let q = self.input.n_primaries();
        let Some(slot_a) = self.coordinate_slot(atom, axis_a, "decoded second left channel") else {
            out.fill(0.0);
            return;
        };
        let Some(slot_b) = self.coordinate_slot(atom, axis_b, "decoded second right channel")
        else {
            out.fill(0.0);
            return;
        };
        let start = (slot_a * q + slot_b) * p;
        out.copy_from_slice(&self.input.decoded_second[start..start + p]);
    }
    fn n_beta_borders(&self) -> usize {
        self.input.n_beta_borders()
    }
    fn beta_border_atom(&self, border: usize) -> usize {
        self.input.beta_atoms[border]
    }
    fn beta_border_basis_value(&self, border: usize) -> f64 {
        self.input.beta_basis_values[border]
    }
    fn beta_border_basis_first(&self, border: usize, axis: usize) -> f64 {
        let atom = self.input.beta_atoms[border];
        self.coordinate_slot(atom, axis, "beta-border first channel")
            .map_or(0.0, |slot| {
                self.input.beta_basis_first[slot * self.input.n_beta_borders() + border]
            })
    }
    fn beta_border_output(&self, border: usize) -> &[f64] {
        let p = self.input.out_dim;
        &self.input.beta_outputs[border * p..(border + 1) * p]
    }
}

fn cpu_tile(
    rows: &[SaeSoftmaxRowJetInput],
    inv_tau: f64,
    k: usize,
    q: usize,
    p: usize,
    n_beta: usize,
) -> Result<SaeRowJetChannels, String> {
    let mut out = SaeRowJetChannels::zeros(rows.len(), k, q, p, n_beta)?;
    for (row, input) in rows.iter().enumerate() {
        let source = InputSource::new(input);
        let scheduled = execute_softmax_row_program(&source, inv_tau, input.sqrt_row_weight);
        source.finish()?;
        for slot in 0..q {
            let target = row * q * p + slot * p;
            out.first[target..target + p].copy_from_slice(scheduled.first(slot));
            for other in 0..q {
                let target = row * q * q * p + (slot * q + other) * p;
                out.second[target..target + p].copy_from_slice(scheduled.second(slot, other));
            }
        }
        for border in 0..n_beta {
            let target = row * n_beta * p + border * p;
            out.beta[target..target + p].copy_from_slice(scheduled.beta(border));
            for slot in 0..q {
                let target = row * q * n_beta * p + (slot * n_beta + border) * p;
                let mixed = scheduled.beta_deriv(slot, border);
                if mixed != scheduled.beta_l_deriv(slot, border) {
                    return Err(format!(
                        "SAE CPU row program produced unequal beta mixed channels at row={row}, slot={slot}, border={border}"
                    ));
                }
                out.beta_mixed[target..target + p].copy_from_slice(mixed);
            }
        }
    }
    Ok(out)
}

/// Direct centered-moment CUDA implementation. No per-primary jet arrays are
/// materialized; each thread writes one packed output element.
pub const COMPLETE_SOFTMAX_KERNEL_SOURCE: &str = r#"
extern "C" __global__ void sae_rowjet_first(
    const double* z, const int* active, const int* kind, const int* atom,
    const double* decoded, const double* d1, const double* sqrt_w,
    double inv_tau, int k, int q, int p, unsigned long long total,
    double* first)
{
  unsigned long long index=(unsigned long long)blockIdx.x*blockDim.x+threadIdx.x;
  if(index>=total) return;
  int c=(int)(index%(unsigned long long)p);
  unsigned long long rem=index/(unsigned long long)p;
  int slot=(int)(rem%(unsigned long long)q);
  int row=(int)(rem/(unsigned long long)q);
  int a=atom[row*q+slot];
  double root=sqrt_w[row];
  if(kind[row*q+slot]==0){
    double mean=0.0;
    for(int component=0;component<k;++component){
      if(active[row*k+component]) mean += z[row*k+component]*decoded[(row*k+component)*p+c];
    }
    double component=active[row*k+a] ? decoded[(row*k+a)*p+c] : 0.0;
    double centered=component-mean;
    double coefficient=root*(inv_tau*z[row*k+a]);
    first[index]=coefficient*centered;
  }else{
    if(!active[row*k+a]) { first[index]=0.0; return; }
    double coefficient=z[row*k+a]*root;
    first[index]=coefficient*d1[(row*q+slot)*p+c];
  }
}

extern "C" __global__ void sae_rowjet_second(
    const double* z, const int* active, const int* kind, const int* atom,
    const double* decoded, const double* d1, const double* d2,
    const double* sqrt_w, double inv_tau, int k, int q, int p,
    unsigned long long total, double* second)
{
  unsigned long long index=(unsigned long long)blockIdx.x*blockDim.x+threadIdx.x;
  if(index>=total) return;
  int c=(int)(index%(unsigned long long)p);
  unsigned long long rem=index/(unsigned long long)p;
  int slot_b=(int)(rem%(unsigned long long)q); rem/=(unsigned long long)q;
  int slot_a=(int)(rem%(unsigned long long)q);
  int row=(int)(rem/(unsigned long long)q);
  int ka=kind[row*q+slot_a], kb=kind[row*q+slot_b];
  int aa=atom[row*q+slot_a], ab=atom[row*q+slot_b];
  double root=sqrt_w[row];
  if(ka==0 && kb==0){
    double mean=0.0;
    for(int component=0;component<k;++component){
      if(active[row*k+component]) mean += z[row*k+component]*decoded[(row*k+component)*p+c];
    }
    double component_a=active[row*k+aa] ? decoded[(row*k+aa)*p+c] : 0.0;
    double component_b=active[row*k+ab] ? decoded[(row*k+ab)*p+c] : 0.0;
    double centered_a=component_a-mean;
    double centered_b=component_b-mean;
    double za=z[row*k+aa], zb=z[row*k+ab];
    double diagonal=aa==ab ? 1.0 : 0.0;
    double common=inv_tau*inv_tau*za;
    double coefficient_a=root*(common*(diagonal-zb));
    double coefficient_b=root*(-common*zb);
    second[index]=coefficient_a*centered_a+coefficient_b*centered_b;
  }else if(ka==0 || kb==0){
    int logit_atom=ka==0 ? aa : ab;
    int coord_atom=ka==0 ? ab : aa;
    int coord_slot=ka==0 ? slot_b : slot_a;
    if(!active[row*k+coord_atom]) { second[index]=0.0; return; }
    double diagonal=coord_atom==logit_atom ? 1.0 : 0.0;
    double coefficient=z[row*k+coord_atom]*(diagonal-z[row*k+logit_atom])*inv_tau;
    coefficient*=root;
    second[index]=coefficient*d1[(row*q+coord_slot)*p+c];
  }else if(aa==ab){
    if(!active[row*k+aa]) { second[index]=0.0; return; }
    double coefficient=z[row*k+aa]*root;
    second[index]=coefficient*d2[((row*q+slot_a)*q+slot_b)*p+c];
  }else{
    second[index]=0.0;
  }
}

extern "C" __global__ void sae_rowjet_beta(
    const double* z, const int* active, const int* beta_atom,
    const double* beta_phi, const double* beta_output, const double* sqrt_w,
    int k, int p, int nb, unsigned long long total, double* beta)
{
  unsigned long long index=(unsigned long long)blockIdx.x*blockDim.x+threadIdx.x;
  if(index>=total) return;
  int c=(int)(index%(unsigned long long)p);
  unsigned long long rem=index/(unsigned long long)p;
  int border=(int)(rem%(unsigned long long)nb);
  int row=(int)(rem/(unsigned long long)nb);
  int a=beta_atom[border];
  if(!active[row*k+a]) { beta[index]=0.0; return; }
  double base=z[row*k+a]*beta_phi[row*nb+border];
  base*=sqrt_w[row];
  beta[index]=base*beta_output[border*p+c];
}

extern "C" __global__ void sae_rowjet_beta_mixed(
    const double* z, const int* active, const int* kind, const int* atom,
    const int* beta_atom, const double* beta_phi, const double* beta_first,
    const double* beta_output, const double* sqrt_w, double inv_tau,
    int k, int q, int p, int nb, unsigned long long total, double* mixed)
{
  unsigned long long index=(unsigned long long)blockIdx.x*blockDim.x+threadIdx.x;
  if(index>=total) return;
  int c=(int)(index%(unsigned long long)p);
  unsigned long long rem=index/(unsigned long long)p;
  int border=(int)(rem%(unsigned long long)nb); rem/=(unsigned long long)nb;
  int slot=(int)(rem%(unsigned long long)q);
  int row=(int)(rem/(unsigned long long)q);
  int target=beta_atom[border];
  if(!active[row*k+target]) { mixed[index]=0.0; return; }
  int source_atom=atom[row*q+slot];
  double scalar;
  if(kind[row*q+slot]==0){
    double diagonal=target==source_atom ? 1.0 : 0.0;
    scalar=z[row*k+target]*(diagonal-z[row*k+source_atom])*inv_tau;
    scalar*=beta_phi[row*nb+border];
  }else if(source_atom==target){
    scalar=z[row*k+target]*beta_first[(row*q+slot)*nb+border];
  }else{
    scalar=0.0;
  }
  scalar*=sqrt_w[row];
  mixed[index]=scalar*beta_output[border*p+c];
}

// ---- resident contraction kernels (#2304) ----
//
// The tower is reduced on device instead of materialized: phase one computes
// per-row dot tables of the semantic input tensors against one probe vector,
// phase two assembles the contracted t/beta outputs from the SAME scalar
// coefficient formulas the elementwise kernels above use. Both phases are
// gated against the authoritative CPU row program, so the formulas cannot
// drift apart silently.

// out[row*m + j] = sum_c mat[(row*m + j)*p + c] * x[row*p + c]
extern "C" __global__ void sae_rowjet_dot_rowmat(
    const double* mat, const double* x, int m, int p,
    unsigned long long total, double* out)
{
  unsigned long long index=(unsigned long long)blockIdx.x*blockDim.x+threadIdx.x;
  if(index>=total) return;
  int row=(int)(index/(unsigned long long)m);
  double acc=0.0;
  for(int c=0;c<p;++c) acc += mat[index*(unsigned long long)p+c]*x[row*p+c];
  out[index]=acc;
}

// out[row*m + j] = sum_c mat[j*p + c] * x[row*p + c]   (row-shared matrix)
extern "C" __global__ void sae_rowjet_dot_shared(
    const double* mat, const double* x, int m, int p,
    unsigned long long total, double* out)
{
  unsigned long long index=(unsigned long long)blockIdx.x*blockDim.x+threadIdx.x;
  if(index>=total) return;
  int j=(int)(index%(unsigned long long)m);
  int row=(int)(index/(unsigned long long)m);
  double acc=0.0;
  for(int c=0;c<p;++c) acc += mat[j*p+c]*x[row*p+c];
  out[index]=acc;
}

// t[row*q + slot] = <first(row,slot,.), probe_row> via the dot tables.
extern "C" __global__ void sae_rowjet_contract_linear_t(
    const double* z, const int* active, const int* kind, const int* atom,
    const double* dd, const double* d1dot, const double* sqrt_w,
    double inv_tau, int k, int q, unsigned long long total, double* t_out)
{
  unsigned long long index=(unsigned long long)blockIdx.x*blockDim.x+threadIdx.x;
  if(index>=total) return;
  int slot=(int)(index%(unsigned long long)q);
  int row=(int)(index/(unsigned long long)q);
  int a=atom[row*q+slot];
  double root=sqrt_w[row];
  if(kind[row*q+slot]==0){
    double mean_dot=0.0;
    for(int component=0;component<k;++component){
      if(active[row*k+component]) mean_dot += z[row*k+component]*dd[row*k+component];
    }
    double component=active[row*k+a] ? dd[row*k+a] : 0.0;
    t_out[index]=root*(inv_tau*z[row*k+a])*(component-mean_dot);
  }else{
    t_out[index]=active[row*k+a] ? z[row*k+a]*root*d1dot[row*q+slot] : 0.0;
  }
}

// beta[row*nb + border] = <beta(row,border,.), probe_row> via the shared dot.
extern "C" __global__ void sae_rowjet_contract_linear_beta(
    const double* z, const int* active, const int* beta_atom,
    const double* beta_phi, const double* bodot, const double* sqrt_w,
    int k, int nb, unsigned long long total, double* beta_out)
{
  unsigned long long index=(unsigned long long)blockIdx.x*blockDim.x+threadIdx.x;
  if(index>=total) return;
  int border=(int)(index%(unsigned long long)nb);
  int row=(int)(index/(unsigned long long)nb);
  int a=beta_atom[border];
  if(!active[row*k+a]) { beta_out[index]=0.0; return; }
  beta_out[index]=sqrt_w[row]*z[row*k+a]*beta_phi[row*nb+border]*bodot[row*nb+border];
}

// t[row*q + a] = sum_b <probe, second(a,b)> v_t[b] + sum_c <probe, mixed(a,c)> v_beta[c]
extern "C" __global__ void sae_rowjet_contract_bilinear_t(
    const double* z, const int* active, const int* kind, const int* atom,
    const int* beta_atom, const double* beta_phi, const double* beta_first,
    const double* dd, const double* d1dot, const double* d2dot,
    const double* bodot, const double* v_t, const double* v_beta,
    const double* sqrt_w, double inv_tau, int k, int q, int nb,
    unsigned long long total, double* t_out)
{
  unsigned long long index=(unsigned long long)blockIdx.x*blockDim.x+threadIdx.x;
  if(index>=total) return;
  int slot_a=(int)(index%(unsigned long long)q);
  int row=(int)(index/(unsigned long long)q);
  int ka=kind[row*q+slot_a];
  int aa=atom[row*q+slot_a];
  double root=sqrt_w[row];
  double mean_dot=0.0;
  for(int component=0;component<k;++component){
    if(active[row*k+component]) mean_dot += z[row*k+component]*dd[row*k+component];
  }
  double acc=0.0;
  for(int slot_b=0;slot_b<q;++slot_b){
    int kb=kind[row*q+slot_b];
    int ab=atom[row*q+slot_b];
    double sdot;
    if(ka==0 && kb==0){
      double component_a=active[row*k+aa] ? dd[row*k+aa] : 0.0;
      double component_b=active[row*k+ab] ? dd[row*k+ab] : 0.0;
      double centered_a=component_a-mean_dot;
      double centered_b=component_b-mean_dot;
      double za=z[row*k+aa], zb=z[row*k+ab];
      double diagonal=aa==ab ? 1.0 : 0.0;
      double common=inv_tau*inv_tau*za;
      sdot=root*(common*(diagonal-zb))*centered_a+root*(-common*zb)*centered_b;
    }else if(ka==0 || kb==0){
      int logit_atom=ka==0 ? aa : ab;
      int coord_atom=ka==0 ? ab : aa;
      int coord_slot=ka==0 ? slot_b : slot_a;
      if(active[row*k+coord_atom]){
        double diagonal=coord_atom==logit_atom ? 1.0 : 0.0;
        double coefficient=z[row*k+coord_atom]*(diagonal-z[row*k+logit_atom])*inv_tau;
        sdot=root*coefficient*d1dot[row*q+coord_slot];
      }else{
        sdot=0.0;
      }
    }else if(aa==ab){
      sdot=active[row*k+aa] ? root*z[row*k+aa]*d2dot[(row*q+slot_a)*q+slot_b] : 0.0;
    }else{
      sdot=0.0;
    }
    acc += sdot*v_t[row*q+slot_b];
  }
  for(int border=0;border<nb;++border){
    int target=beta_atom[border];
    double scalar=0.0;
    if(active[row*k+target]){
      if(ka==0){
        double diagonal=target==aa ? 1.0 : 0.0;
        scalar=z[row*k+target]*(diagonal-z[row*k+aa])*inv_tau*beta_phi[row*nb+border];
      }else if(aa==target){
        scalar=z[row*k+target]*beta_first[(row*q+slot_a)*nb+border];
      }
      scalar*=root;
    }
    acc += scalar*bodot[row*nb+border]*v_beta[row*nb+border];
  }
  t_out[index]=acc;
}

// beta[row*nb + border] = sum_a <probe, mixed(a,border)> v_t[a]
extern "C" __global__ void sae_rowjet_contract_bilinear_beta(
    const double* z, const int* active, const int* kind, const int* atom,
    const int* beta_atom, const double* beta_phi, const double* beta_first,
    const double* bodot, const double* v_t, const double* sqrt_w,
    double inv_tau, int k, int q, int nb, unsigned long long total,
    double* beta_out)
{
  unsigned long long index=(unsigned long long)blockIdx.x*blockDim.x+threadIdx.x;
  if(index>=total) return;
  int border=(int)(index%(unsigned long long)nb);
  int row=(int)(index/(unsigned long long)nb);
  int target=beta_atom[border];
  if(!active[row*k+target]) { beta_out[index]=0.0; return; }
  double root=sqrt_w[row];
  double acc=0.0;
  for(int slot=0;slot<q;++slot){
    int source_atom=atom[row*q+slot];
    double scalar;
    if(kind[row*q+slot]==0){
      double diagonal=target==source_atom ? 1.0 : 0.0;
      scalar=z[row*k+target]*(diagonal-z[row*k+source_atom])*inv_tau;
      scalar*=beta_phi[row*nb+border];
    }else if(source_atom==target){
      scalar=z[row*k+target]*beta_first[(row*q+slot)*nb+border];
    }else{
      scalar=0.0;
    }
    acc += root*scalar*bodot[row*nb+border]*v_t[row*q+slot];
  }
  beta_out[index]=acc;
}

// ---- resident Trace (θ-adjoint) reduction kernels (#2304) ----
//
// The Trace shape reduces jet-against-jet inner products (no single probe), so
// it materializes the same first/second/beta/mixed tower the elementwise path
// builds and contracts it in place against the small per-row selected-inverse
// weights `E_tt` (q×q, deflation-folded), `inv_vbeta` (q×nb) and the shared
// `beta_inv` (nb×nb). Only the reduced `n·q` t-outputs and `n·nb` β-outputs
// leave the device; the packed tower never touches the host. The scalar formula
// mirrors `cpu_contracted_tile`'s Trace arm term for term, including the softmax
// data-weight substitution for a logit direction over a coordinate pair. The
// `sqrt_row_weight` is already folded into every materialized channel, so a jet
// dot carries it squared exactly as the CPU oracle's does.
__device__ double sae_rj_dot(const double* x, const double* y, int p){
  double acc=0.0;
  for(int c=0;c<p;++c) acc += x[c]*y[c];
  return acc;
}

// t[row*q + w] = tr(E · dh_w) over the t–t block + 2·inv_vβ t–β block + beta_inv β–β block.
extern "C" __global__ void sae_rowjet_trace_t(
    const double* z, const int* kind, const int* atom,
    const double* first, const double* second, const double* beta, const double* mixed,
    const double* e_tt, const double* inv_vbeta, const double* beta_inv,
    double inv_tau, int k, int q, int p, int nb,
    unsigned long long total, double* t_out)
{
  unsigned long long index=(unsigned long long)blockIdx.x*blockDim.x+threadIdx.x;
  if(index>=total) return;
  int w=(int)(index%(unsigned long long)q);
  int row=(int)(index/(unsigned long long)q);
  int kw=kind[row*q+w];
  int atom_w=atom[row*q+w];
  const double* first_row=first+(unsigned long long)row*(unsigned long long)q*p;
  const double* second_row=second+(unsigned long long)row*(unsigned long long)q*q*p;
  const double* beta_row=beta+(unsigned long long)row*(unsigned long long)nb*p;
  const double* mixed_row=mixed+(unsigned long long)row*(unsigned long long)q*nb*p;
  const double* e_row=e_tt+(unsigned long long)row*(unsigned long long)q*q;
  const double* vbeta_row=inv_vbeta+(unsigned long long)row*(unsigned long long)q*nb;
  double gamma=0.0;
  for(int a=0;a<q;++a){
    int ka=kind[row*q+a];
    int atom_a=atom[row*q+a];
    const double* fa=first_row+(unsigned long long)a*p;
    const double* s_aw=second_row+((unsigned long long)a*q+w)*p;
    for(int b=0;b<q;++b){
      int kb=kind[row*q+b];
      int atom_b=atom[row*q+b];
      const double* fb=first_row+(unsigned long long)b*p;
      const double* s_bw=second_row+((unsigned long long)b*q+w)*p;
      double dh;
      if(kw==0 && ka!=0 && kb!=0){
        double aw=z[row*k+atom_w];
        double left=(atom_w==atom_a ? 1.0 : 0.0)-aw;
        double right=(atom_w==atom_b ? 1.0 : 0.0)-aw;
        dh=sae_rj_dot(fa,fb,p)*((left+right)*inv_tau);
      }else{
        dh=sae_rj_dot(s_aw,fb,p)+sae_rj_dot(fa,s_bw,p);
      }
      gamma+=e_row[a*q+b]*dh;
    }
    for(int border=0;border<nb;++border){
      const double* bbeta=beta_row+(unsigned long long)border*p;
      const double* m_w=mixed_row+((unsigned long long)w*nb+border)*p;
      double dh=sae_rj_dot(s_aw,bbeta,p)+sae_rj_dot(fa,m_w,p);
      gamma+=2.0*vbeta_row[a*nb+border]*dh;
    }
  }
  for(int i=0;i<nb;++i){
    const double* m_wi=mixed_row+((unsigned long long)w*nb+i)*p;
    const double* beta_i=beta_row+(unsigned long long)i*p;
    for(int j=0;j<nb;++j){
      const double* m_wj=mixed_row+((unsigned long long)w*nb+j)*p;
      const double* beta_j=beta_row+(unsigned long long)j*p;
      double dh=sae_rj_dot(m_wi,beta_j,p)+sae_rj_dot(beta_i,m_wj,p);
      gamma+=beta_inv[i*nb+j]*dh;
    }
  }
  t_out[index]=gamma;
}

// beta[row*nb + w_beta] = tr(E · dh_wβ) over t–t + 2·inv_vβ t–β block.
extern "C" __global__ void sae_rowjet_trace_beta(
    const double* first, const double* beta, const double* mixed,
    const double* e_tt, const double* inv_vbeta,
    int q, int p, int nb, unsigned long long total, double* beta_out)
{
  unsigned long long index=(unsigned long long)blockIdx.x*blockDim.x+threadIdx.x;
  if(index>=total) return;
  int wb=(int)(index%(unsigned long long)nb);
  int row=(int)(index/(unsigned long long)nb);
  const double* first_row=first+(unsigned long long)row*(unsigned long long)q*p;
  const double* beta_row=beta+(unsigned long long)row*(unsigned long long)nb*p;
  const double* mixed_row=mixed+(unsigned long long)row*(unsigned long long)q*nb*p;
  const double* e_row=e_tt+(unsigned long long)row*(unsigned long long)q*q;
  const double* vbeta_row=inv_vbeta+(unsigned long long)row*(unsigned long long)q*nb;
  double gamma=0.0;
  for(int a=0;a<q;++a){
    const double* fa=first_row+(unsigned long long)a*p;
    const double* m_a=mixed_row+((unsigned long long)a*nb+wb)*p;
    for(int b=0;b<q;++b){
      const double* fb=first_row+(unsigned long long)b*p;
      const double* m_b=mixed_row+((unsigned long long)b*nb+wb)*p;
      double dh=sae_rj_dot(m_a,fb,p)+sae_rj_dot(fa,m_b,p);
      gamma+=e_row[a*q+b]*dh;
    }
    for(int border=0;border<nb;++border){
      const double* bbeta=beta_row+(unsigned long long)border*p;
      double dh=sae_rj_dot(m_a,bbeta,p);
      gamma+=2.0*vbeta_row[a*nb+border]*dh;
    }
  }
  beta_out[index]=gamma;
}
"#;

#[cfg(target_os = "linux")]
mod device {
    use super::{
        COMPLETE_SOFTMAX_KERNEL_SOURCE, SaeRowJetChannels, SaeRowJetContractedTile,
        SaeRowJetContraction, SaeRowJetPrimary, SaeSoftmaxRowJetInput, checked_product,
    };
    use cudarc::driver::{CudaModule, CudaSlice, CudaStream, LaunchConfig, PushKernelArg};
    use gam_gpu::gpu_error::{GpuError, GpuResultExt};
    use std::borrow::Cow;
    use std::sync::{Arc, OnceLock};

    struct Backend {
        stream: Arc<CudaStream>,
        module: Arc<CudaModule>,
    }

    fn backend() -> Result<&'static Backend, GpuError> {
        static BACKEND: OnceLock<Result<Backend, GpuError>> = OnceLock::new();
        BACKEND
            .get_or_init(|| {
                let parts = gam_gpu::backend_probe::probe_cuda_backend("sae_rowjet_complete")?;
                let ptx = gam_gpu::device_cache::compile_ptx_arch(COMPLETE_SOFTMAX_KERNEL_SOURCE)
                    .gpu_ctx("complete SAE row-jet NVRTC compile")?;
                let module = parts
                    .ctx
                    .load_module(ptx)
                    .gpu_ctx("complete SAE row-jet module load")?;
                Ok(Backend {
                    stream: parts.stream,
                    module,
                })
            })
            .as_ref()
            .map_err(GpuError::clone)
    }

    fn grid(total: usize) -> Result<LaunchConfig, GpuError> {
        // Eight warps expose enough independent fp64 outputs per block without
        // consuming shared memory or constraining register occupancy.
        const THREADS: u32 = 8 * 32;
        let total_u64 = u64::try_from(total)
            .map_err(|_| gam_gpu::gpu_err!("complete SAE row-jet output length overflow"))?;
        let blocks = total_u64.div_ceil(u64::from(THREADS));
        let blocks = u32::try_from(blocks)
            .map_err(|_| gam_gpu::gpu_err!("complete SAE row-jet grid overflow"))?;
        Ok(LaunchConfig {
            grid_dim: (blocks, 1, 1),
            block_dim: (THREADS, 1, 1),
            shared_mem_bytes: 0,
        })
    }

    fn device_length(shape: &[usize]) -> Result<usize, GpuError> {
        checked_product(shape).map_err(|reason| gam_gpu::gpu_err!("{reason}"))
    }

    fn nonempty_f64(values: &[f64]) -> Cow<'_, [f64]> {
        if values.is_empty() {
            Cow::Owned(vec![0.0])
        } else {
            Cow::Borrowed(values)
        }
    }

    fn nonempty_i32(values: &[i32]) -> Cow<'_, [i32]> {
        if values.is_empty() {
            Cow::Owned(vec![0])
        } else {
            Cow::Borrowed(values)
        }
    }

    /// Uploaded semantic inputs shared by the elementwise and contracted tiles.
    struct Staged {
        z_dev: CudaSlice<f64>,
        active_dev: CudaSlice<i32>,
        kind_dev: CudaSlice<i32>,
        atom_dev: CudaSlice<i32>,
        decoded_dev: CudaSlice<f64>,
        d1_dev: CudaSlice<f64>,
        d2_dev: CudaSlice<f64>,
        sqrt_w_dev: CudaSlice<f64>,
        beta_atom_dev: CudaSlice<i32>,
        beta_phi_dev: CudaSlice<f64>,
        beta_first_dev: CudaSlice<f64>,
        beta_output_dev: CudaSlice<f64>,
        k_i32: i32,
        q_i32: i32,
        p_i32: i32,
        nb_i32: i32,
    }

    fn stage_inputs(
        stream: &Arc<CudaStream>,
        rows: &[SaeSoftmaxRowJetInput],
        k: usize,
        q: usize,
        p: usize,
        n_beta: usize,
    ) -> Result<Staged, GpuError> {
        let n = rows.len();
        let mut z = Vec::with_capacity(device_length(&[n, k])?);
        let mut active = Vec::with_capacity(device_length(&[n, k])?);
        let mut kind = Vec::with_capacity(device_length(&[n, q])?);
        let mut atom = Vec::with_capacity(device_length(&[n, q])?);
        let mut decoded = Vec::with_capacity(device_length(&[n, k, p])?);
        let mut d1 = Vec::with_capacity(device_length(&[n, q, p])?);
        let mut d2 = Vec::with_capacity(device_length(&[n, q, q, p])?);
        let mut sqrt_w = Vec::with_capacity(n);
        let mut beta_phi = Vec::with_capacity(device_length(&[n, n_beta])?);
        let mut beta_first = Vec::with_capacity(device_length(&[n, q, n_beta])?);
        for input in rows {
            z.extend_from_slice(&input.gate_values);
            active.extend(input.active_atoms.iter().map(|&value| i32::from(value)));
            for primary in &input.primaries {
                match *primary {
                    SaeRowJetPrimary::Logit { atom: source } => {
                        kind.push(0_i32);
                        atom.push(i32::try_from(source).map_err(|_| {
                            gam_gpu::gpu_err!("complete SAE row-jet atom index overflows i32")
                        })?);
                    }
                    SaeRowJetPrimary::Coordinate { atom: source, .. } => {
                        kind.push(1_i32);
                        atom.push(i32::try_from(source).map_err(|_| {
                            gam_gpu::gpu_err!("complete SAE row-jet atom index overflows i32")
                        })?);
                    }
                }
            }
            decoded.extend_from_slice(&input.decoded);
            d1.extend_from_slice(&input.decoded_first);
            d2.extend_from_slice(&input.decoded_second);
            sqrt_w.push(input.sqrt_row_weight);
            beta_phi.extend_from_slice(&input.beta_basis_values);
            beta_first.extend_from_slice(&input.beta_basis_first);
        }
        let beta_atom: Vec<i32> = rows[0]
            .beta_atoms
            .iter()
            .map(|&source| {
                i32::try_from(source).map_err(|_| {
                    gam_gpu::gpu_err!("complete SAE row-jet beta atom index overflows i32")
                })
            })
            .collect::<Result<_, _>>()?;

        // cudarc does not guarantee zero-length allocations. Dummy cells are
        // never read because the corresponding kernel has total=0 and is not launched.
        let z_dev = stream.clone_htod(&z).gpu_ctx("SAE row-jet htod gates")?;
        let active_dev = stream
            .clone_htod(&active)
            .gpu_ctx("SAE row-jet htod active mask")?;
        let kind_dev = stream
            .clone_htod(nonempty_i32(&kind).as_ref())
            .gpu_ctx("SAE row-jet htod primary kinds")?;
        let atom_dev = stream
            .clone_htod(nonempty_i32(&atom).as_ref())
            .gpu_ctx("SAE row-jet htod primary atoms")?;
        let decoded_dev = stream
            .clone_htod(&decoded)
            .gpu_ctx("SAE row-jet htod decoded")?;
        let d1_dev = stream
            .clone_htod(nonempty_f64(&d1).as_ref())
            .gpu_ctx("SAE row-jet htod decoded first")?;
        let d2_dev = stream
            .clone_htod(nonempty_f64(&d2).as_ref())
            .gpu_ctx("SAE row-jet htod decoded second")?;
        let sqrt_w_dev = stream
            .clone_htod(&sqrt_w)
            .gpu_ctx("SAE row-jet htod row weights")?;
        let beta_atom_dev = stream
            .clone_htod(nonempty_i32(&beta_atom).as_ref())
            .gpu_ctx("SAE row-jet htod beta atoms")?;
        let beta_phi_dev = stream
            .clone_htod(nonempty_f64(&beta_phi).as_ref())
            .gpu_ctx("SAE row-jet htod beta basis values")?;
        let beta_first_dev = stream
            .clone_htod(nonempty_f64(&beta_first).as_ref())
            .gpu_ctx("SAE row-jet htod beta basis first")?;
        let beta_output_dev = stream
            .clone_htod(nonempty_f64(rows[0].beta_outputs.as_ref()).as_ref())
            .gpu_ctx("SAE row-jet htod beta outputs")?;
        let k_i32 =
            i32::try_from(k).map_err(|_| gam_gpu::gpu_err!("SAE row-jet K overflows i32"))?;
        let q_i32 =
            i32::try_from(q).map_err(|_| gam_gpu::gpu_err!("SAE row-jet q overflows i32"))?;
        let p_i32 =
            i32::try_from(p).map_err(|_| gam_gpu::gpu_err!("SAE row-jet p overflows i32"))?;
        let nb_i32 = i32::try_from(n_beta)
            .map_err(|_| gam_gpu::gpu_err!("SAE row-jet beta count overflows i32"))?;
        Ok(Staged {
            z_dev,
            active_dev,
            kind_dev,
            atom_dev,
            decoded_dev,
            d1_dev,
            d2_dev,
            sqrt_w_dev,
            beta_atom_dev,
            beta_phi_dev,
            beta_first_dev,
            beta_output_dev,
            k_i32,
            q_i32,
            p_i32,
            nb_i32,
        })
    }

    /// Materialized first/second/beta/mixed tower channels resident on device.
    struct TowerChannels {
        first_dev: CudaSlice<f64>,
        second_dev: CudaSlice<f64>,
        beta_dev: CudaSlice<f64>,
        mixed_dev: CudaSlice<f64>,
        first_len: usize,
        second_len: usize,
        beta_len: usize,
        mixed_len: usize,
    }

    /// Build the packed first/second/beta/mixed channels on device from staged
    /// inputs, launching the four elementwise tower kernels. Shared by the
    /// elementwise `device_tile` (which downloads them) and the Trace contracted
    /// tile (which reduces them in place and downloads only the coefficients).
    fn materialize_tower_channels(
        stream: &Arc<CudaStream>,
        b: &Backend,
        staged: &Staged,
        inv_tau: f64,
        n: usize,
        q: usize,
        p: usize,
        n_beta: usize,
    ) -> Result<TowerChannels, GpuError> {
        let first_len = device_length(&[n, q, p])?;
        let second_len = device_length(&[n, q, q, p])?;
        let beta_len = device_length(&[n, n_beta, p])?;
        let mixed_len = device_length(&[n, q, n_beta, p])?;
        let mut first_dev = stream
            .alloc_zeros::<f64>(first_len.max(1))
            .gpu_ctx("SAE row-jet alloc first")?;
        let mut second_dev = stream
            .alloc_zeros::<f64>(second_len.max(1))
            .gpu_ctx("SAE row-jet alloc second")?;
        let mut beta_dev = stream
            .alloc_zeros::<f64>(beta_len.max(1))
            .gpu_ctx("SAE row-jet alloc beta")?;
        let mut mixed_dev = stream
            .alloc_zeros::<f64>(mixed_len.max(1))
            .gpu_ctx("SAE row-jet alloc beta mixed")?;

        if first_len != 0 {
            let function = b
                .module
                .load_function("sae_rowjet_first")
                .gpu_ctx("SAE row-jet first load")?;
            let total = u64::try_from(first_len)
                .map_err(|_| gam_gpu::gpu_err!("SAE row-jet first length overflows u64"))?;
            let mut launch = stream.launch_builder(&function);
            launch
                .arg(&staged.z_dev)
                .arg(&staged.active_dev)
                .arg(&staged.kind_dev)
                .arg(&staged.atom_dev)
                .arg(&staged.decoded_dev)
                .arg(&staged.d1_dev)
                .arg(&staged.sqrt_w_dev)
                .arg(&inv_tau)
                .arg(&staged.k_i32)
                .arg(&staged.q_i32)
                .arg(&staged.p_i32)
                .arg(&total)
                .arg(&mut first_dev);
            // SAFETY: the loaded kernel's argument ABI matches this builder, and
            // `grid(first_len)` covers only the `first_len` allocated outputs.
            unsafe { launch.launch(grid(first_len)?) }.gpu_ctx("SAE row-jet first launch")?;
        }
        if second_len != 0 {
            let function = b
                .module
                .load_function("sae_rowjet_second")
                .gpu_ctx("SAE row-jet second load")?;
            let total = u64::try_from(second_len)
                .map_err(|_| gam_gpu::gpu_err!("SAE row-jet second length overflows u64"))?;
            let mut launch = stream.launch_builder(&function);
            launch
                .arg(&staged.z_dev)
                .arg(&staged.active_dev)
                .arg(&staged.kind_dev)
                .arg(&staged.atom_dev)
                .arg(&staged.decoded_dev)
                .arg(&staged.d1_dev)
                .arg(&staged.d2_dev)
                .arg(&staged.sqrt_w_dev)
                .arg(&inv_tau)
                .arg(&staged.k_i32)
                .arg(&staged.q_i32)
                .arg(&staged.p_i32)
                .arg(&total)
                .arg(&mut second_dev);
            // SAFETY: the loaded kernel's argument ABI matches this builder, and
            // `grid(second_len)` covers only the `second_len` allocated outputs.
            unsafe { launch.launch(grid(second_len)?) }.gpu_ctx("SAE row-jet second launch")?;
        }
        if beta_len != 0 {
            let function = b
                .module
                .load_function("sae_rowjet_beta")
                .gpu_ctx("SAE row-jet beta load")?;
            let total = u64::try_from(beta_len)
                .map_err(|_| gam_gpu::gpu_err!("SAE row-jet beta length overflows u64"))?;
            let mut launch = stream.launch_builder(&function);
            launch
                .arg(&staged.z_dev)
                .arg(&staged.active_dev)
                .arg(&staged.beta_atom_dev)
                .arg(&staged.beta_phi_dev)
                .arg(&staged.beta_output_dev)
                .arg(&staged.sqrt_w_dev)
                .arg(&staged.k_i32)
                .arg(&staged.p_i32)
                .arg(&staged.nb_i32)
                .arg(&total)
                .arg(&mut beta_dev);
            // SAFETY: the loaded kernel's argument ABI matches this builder, and
            // `grid(beta_len)` covers only the `beta_len` allocated outputs.
            unsafe { launch.launch(grid(beta_len)?) }.gpu_ctx("SAE row-jet beta launch")?;
        }
        if mixed_len != 0 {
            let function = b
                .module
                .load_function("sae_rowjet_beta_mixed")
                .gpu_ctx("SAE row-jet beta mixed load")?;
            let total = u64::try_from(mixed_len)
                .map_err(|_| gam_gpu::gpu_err!("SAE row-jet mixed length overflows u64"))?;
            let mut launch = stream.launch_builder(&function);
            launch
                .arg(&staged.z_dev)
                .arg(&staged.active_dev)
                .arg(&staged.kind_dev)
                .arg(&staged.atom_dev)
                .arg(&staged.beta_atom_dev)
                .arg(&staged.beta_phi_dev)
                .arg(&staged.beta_first_dev)
                .arg(&staged.beta_output_dev)
                .arg(&staged.sqrt_w_dev)
                .arg(&inv_tau)
                .arg(&staged.k_i32)
                .arg(&staged.q_i32)
                .arg(&staged.p_i32)
                .arg(&staged.nb_i32)
                .arg(&total)
                .arg(&mut mixed_dev);
            // SAFETY: the loaded kernel's argument ABI matches this builder, and
            // `grid(mixed_len)` covers only the `mixed_len` allocated outputs.
            unsafe { launch.launch(grid(mixed_len)?) }.gpu_ctx("SAE row-jet beta mixed launch")?;
        }
        Ok(TowerChannels {
            first_dev,
            second_dev,
            beta_dev,
            mixed_dev,
            first_len,
            second_len,
            beta_len,
            mixed_len,
        })
    }

    pub(super) fn device_tile(
        rows: &[SaeSoftmaxRowJetInput],
        inv_tau: f64,
        k: usize,
        q: usize,
        p: usize,
        n_beta: usize,
    ) -> Result<SaeRowJetChannels, GpuError> {
        let n = rows.len();
        let b = backend()?;
        let stream = b.stream.clone();
        let staged = stage_inputs(&stream, rows, k, q, p, n_beta)?;
        let TowerChannels {
            first_dev,
            second_dev,
            beta_dev,
            mixed_dev,
            first_len,
            second_len,
            beta_len,
            mixed_len,
        } = materialize_tower_channels(&stream, b, &staged, inv_tau, n, q, p, n_beta)?;

        let mut out = SaeRowJetChannels::zeros(n, k, q, p, n_beta)
            .map_err(|reason| gam_gpu::gpu_err!("{reason}"))?;
        if first_len != 0 {
            stream
                .memcpy_dtoh(&first_dev, &mut out.first)
                .gpu_ctx("SAE row-jet dtoh first")?;
        }
        if second_len != 0 {
            stream
                .memcpy_dtoh(&second_dev, &mut out.second)
                .gpu_ctx("SAE row-jet dtoh second")?;
        }
        if beta_len != 0 {
            stream
                .memcpy_dtoh(&beta_dev, &mut out.beta)
                .gpu_ctx("SAE row-jet dtoh beta")?;
        }
        if mixed_len != 0 {
            stream
                .memcpy_dtoh(&mixed_dev, &mut out.beta_mixed)
                .gpu_ctx("SAE row-jet dtoh beta mixed")?;
        }
        stream.synchronize().gpu_ctx("SAE row-jet synchronize")?;
        Ok(out)
    }

    /// Resident Trace (θ-adjoint) contracted tile (#2304): materialize the
    /// first/second/beta/mixed tower on device, reduce it in place against the
    /// per-row deflation-folded `E_tt` (q×q), `inv_vbeta` (q×nb) and the shared
    /// `beta_inv` (nb×nb) selected-inverse weights, and download only the
    /// `n·q` t and `n·nb` β coefficients. The packed tower never touches the
    /// host. Gated bit-comparably (≤1e-12) against `cpu_contracted_tile`'s Trace
    /// arm, whose scalar formulas these kernels transcribe.
    fn device_trace_tile(
        stream: &Arc<CudaStream>,
        b: &Backend,
        staged: &Staged,
        inv_tau: f64,
        e_tt: &[f64],
        inv_vbeta: &[f64],
        beta_inv: &[f64],
        n: usize,
        q: usize,
        p: usize,
        n_beta: usize,
    ) -> Result<SaeRowJetContractedTile, GpuError> {
        let tower = materialize_tower_channels(stream, b, staged, inv_tau, n, q, p, n_beta)?;
        let e_tt_dev = stream
            .clone_htod(nonempty_f64(e_tt).as_ref())
            .gpu_ctx("SAE row-jet htod trace e_tt")?;
        let inv_vbeta_dev = stream
            .clone_htod(nonempty_f64(inv_vbeta).as_ref())
            .gpu_ctx("SAE row-jet htod trace inv_vbeta")?;
        let beta_inv_dev = stream
            .clone_htod(nonempty_f64(beta_inv).as_ref())
            .gpu_ctx("SAE row-jet htod trace beta_inv")?;

        let t_len = device_length(&[n, q])?;
        let beta_len = device_length(&[n, n_beta])?;
        let mut t_dev = stream
            .alloc_zeros::<f64>(t_len.max(1))
            .gpu_ctx("SAE row-jet alloc trace t")?;
        let mut beta_dev = stream
            .alloc_zeros::<f64>(beta_len.max(1))
            .gpu_ctx("SAE row-jet alloc trace beta")?;

        if t_len != 0 {
            let function = b
                .module
                .load_function("sae_rowjet_trace_t")
                .gpu_ctx("SAE row-jet trace t load")?;
            let total = u64::try_from(t_len)
                .map_err(|_| gam_gpu::gpu_err!("SAE row-jet trace t length overflows u64"))?;
            let mut launch = stream.launch_builder(&function);
            launch
                .arg(&staged.z_dev)
                .arg(&staged.kind_dev)
                .arg(&staged.atom_dev)
                .arg(&tower.first_dev)
                .arg(&tower.second_dev)
                .arg(&tower.beta_dev)
                .arg(&tower.mixed_dev)
                .arg(&e_tt_dev)
                .arg(&inv_vbeta_dev)
                .arg(&beta_inv_dev)
                .arg(&inv_tau)
                .arg(&staged.k_i32)
                .arg(&staged.q_i32)
                .arg(&staged.p_i32)
                .arg(&staged.nb_i32)
                .arg(&total)
                .arg(&mut t_dev);
            // SAFETY: the loaded kernel's argument ABI matches this builder, and
            // `grid(t_len)` covers only the `t_len` allocated outputs.
            unsafe { launch.launch(grid(t_len)?) }.gpu_ctx("SAE row-jet trace t launch")?;
        }
        if beta_len != 0 {
            let function = b
                .module
                .load_function("sae_rowjet_trace_beta")
                .gpu_ctx("SAE row-jet trace beta load")?;
            let total = u64::try_from(beta_len)
                .map_err(|_| gam_gpu::gpu_err!("SAE row-jet trace beta length overflows u64"))?;
            let mut launch = stream.launch_builder(&function);
            launch
                .arg(&tower.first_dev)
                .arg(&tower.beta_dev)
                .arg(&tower.mixed_dev)
                .arg(&e_tt_dev)
                .arg(&inv_vbeta_dev)
                .arg(&staged.q_i32)
                .arg(&staged.p_i32)
                .arg(&staged.nb_i32)
                .arg(&total)
                .arg(&mut beta_dev);
            // SAFETY: the loaded kernel's argument ABI matches this builder, and
            // `grid(beta_len)` covers only the `beta_len` allocated outputs.
            unsafe { launch.launch(grid(beta_len)?) }.gpu_ctx("SAE row-jet trace beta launch")?;
        }

        let mut t = vec![0.0_f64; t_len];
        let mut beta = vec![0.0_f64; beta_len];
        if t_len != 0 {
            stream
                .memcpy_dtoh(&t_dev, &mut t)
                .gpu_ctx("SAE row-jet dtoh trace t")?;
        }
        if beta_len != 0 {
            stream
                .memcpy_dtoh(&beta_dev, &mut beta)
                .gpu_ctx("SAE row-jet dtoh trace beta")?;
        }
        stream
            .synchronize()
            .gpu_ctx("SAE row-jet trace synchronize")?;
        Ok(SaeRowJetContractedTile {
            n_rows: n,
            q,
            n_beta,
            t,
            beta,
        })
    }

    /// Resident contracted tile (#2304): the tower is reduced on device and
    /// only the `n·q` / `n·n_beta` coefficients come back to the host. The
    /// packed `q²p`-class channel tensors are never allocated on either side.
    pub(super) fn device_contracted_tile(
        rows: &[SaeSoftmaxRowJetInput],
        inv_tau: f64,
        contraction: SaeRowJetContraction<'_>,
    ) -> Result<SaeRowJetContractedTile, GpuError> {
        let n = rows.len();
        let first = &rows[0];
        let (k, q, p, n_beta) = (
            first.n_atoms,
            first.n_primaries(),
            first.out_dim,
            first.n_beta_borders(),
        );
        let b = backend()?;
        let stream = b.stream.clone();
        let staged = stage_inputs(&stream, rows, k, q, p, n_beta)?;

        // The θ-adjoint Trace reduction has a distinct resident form (materialize
        // the tower, reduce in place against the per-row `E_tt`/`inv_vβ`/`beta_inv`
        // selected-inverse weights) with no single probe, so it delegates to
        // `device_trace_tile` and returns before the probe machinery. The
        // remaining two shapes share the probe dot-table path; narrowing to them
        // here keeps the probe-free Trace branch out of the assembly match below.
        enum DeviceContraction<'a> {
            Linear,
            Bilinear { v_t: &'a [f64], v_beta: &'a [f64] },
        }
        let (probe, device) = match contraction {
            SaeRowJetContraction::Linear { probe } => (probe, DeviceContraction::Linear),
            SaeRowJetContraction::Bilinear { probe, v_t, v_beta } => {
                (probe, DeviceContraction::Bilinear { v_t, v_beta })
            }
            SaeRowJetContraction::Trace {
                e_tt,
                inv_vbeta,
                beta_inv,
            } => {
                return device_trace_tile(
                    &stream, b, &staged, inv_tau, e_tt, inv_vbeta, beta_inv, n, q, p, n_beta,
                );
            }
        };
        let probe_dev = stream
            .clone_htod(nonempty_f64(probe).as_ref())
            .gpu_ctx("SAE row-jet htod contraction probe")?;

        // Phase one: dot tables of the semantic tensors against the probe.
        let dd_len = device_length(&[n, k])?;
        let d1dot_len = device_length(&[n, q])?;
        let bodot_len = device_length(&[n, n_beta])?;
        let mut dd_dev = stream
            .alloc_zeros::<f64>(dd_len.max(1))
            .gpu_ctx("SAE row-jet alloc decoded dots")?;
        let mut d1dot_dev = stream
            .alloc_zeros::<f64>(d1dot_len.max(1))
            .gpu_ctx("SAE row-jet alloc d1 dots")?;
        let mut bodot_dev = stream
            .alloc_zeros::<f64>(bodot_len.max(1))
            .gpu_ctx("SAE row-jet alloc beta-output dots")?;
        let dot_rowmat = b
            .module
            .load_function("sae_rowjet_dot_rowmat")
            .gpu_ctx("SAE row-jet dot rowmat load")?;
        let launch_rowmat = |mat: &CudaSlice<f64>,
                             m: usize,
                             out: &mut CudaSlice<f64>,
                             label: &'static str|
         -> Result<(), GpuError> {
            let total = device_length(&[n, m])?;
            if total == 0 {
                return Ok(());
            }
            let m_i32 = i32::try_from(m)
                .map_err(|_| gam_gpu::gpu_err!("SAE row-jet dot width overflows i32"))?;
            let total_u64 = u64::try_from(total)
                .map_err(|_| gam_gpu::gpu_err!("SAE row-jet dot length overflows u64"))?;
            let mut launch = stream.launch_builder(&dot_rowmat);
            launch
                .arg(mat)
                .arg(&probe_dev)
                .arg(&m_i32)
                .arg(&staged.p_i32)
                .arg(&total_u64)
                .arg(out);
            // SAFETY: the loaded kernel's argument ABI matches this builder,
            // and `grid(total)` covers only the `total` allocated outputs.
            unsafe { launch.launch(grid(total)?) }.gpu_ctx(label)?;
            Ok(())
        };
        launch_rowmat(
            &staged.decoded_dev,
            k,
            &mut dd_dev,
            "SAE row-jet decoded dots",
        )?;
        launch_rowmat(&staged.d1_dev, q, &mut d1dot_dev, "SAE row-jet d1 dots")?;
        if bodot_len != 0 {
            let function = b
                .module
                .load_function("sae_rowjet_dot_shared")
                .gpu_ctx("SAE row-jet dot shared load")?;
            let total_u64 = u64::try_from(bodot_len)
                .map_err(|_| gam_gpu::gpu_err!("SAE row-jet dot length overflows u64"))?;
            let mut launch = stream.launch_builder(&function);
            launch
                .arg(&staged.beta_output_dev)
                .arg(&probe_dev)
                .arg(&staged.nb_i32)
                .arg(&staged.p_i32)
                .arg(&total_u64)
                .arg(&mut bodot_dev);
            // SAFETY: the loaded kernel's argument ABI matches this builder,
            // and `grid(bodot_len)` covers only the allocated outputs.
            unsafe { launch.launch(grid(bodot_len)?) }.gpu_ctx("SAE row-jet beta-output dots")?;
        }

        // Phase two: assemble the reduced outputs.
        let t_len = device_length(&[n, q])?;
        let beta_len = device_length(&[n, n_beta])?;
        let mut t_dev = stream
            .alloc_zeros::<f64>(t_len.max(1))
            .gpu_ctx("SAE row-jet alloc contracted t")?;
        let mut beta_dev = stream
            .alloc_zeros::<f64>(beta_len.max(1))
            .gpu_ctx("SAE row-jet alloc contracted beta")?;
        match device {
            DeviceContraction::Linear => {
                if t_len != 0 {
                    let function = b
                        .module
                        .load_function("sae_rowjet_contract_linear_t")
                        .gpu_ctx("SAE row-jet linear t load")?;
                    let total_u64 = u64::try_from(t_len)
                        .map_err(|_| gam_gpu::gpu_err!("SAE row-jet t length overflows u64"))?;
                    let mut launch = stream.launch_builder(&function);
                    launch
                        .arg(&staged.z_dev)
                        .arg(&staged.active_dev)
                        .arg(&staged.kind_dev)
                        .arg(&staged.atom_dev)
                        .arg(&dd_dev)
                        .arg(&d1dot_dev)
                        .arg(&staged.sqrt_w_dev)
                        .arg(&inv_tau)
                        .arg(&staged.k_i32)
                        .arg(&staged.q_i32)
                        .arg(&total_u64)
                        .arg(&mut t_dev);
                    // SAFETY: the loaded kernel's argument ABI matches this
                    // builder, and `grid(t_len)` covers only the outputs.
                    unsafe { launch.launch(grid(t_len)?) }
                        .gpu_ctx("SAE row-jet linear t launch")?;
                }
                if beta_len != 0 {
                    let function = b
                        .module
                        .load_function("sae_rowjet_contract_linear_beta")
                        .gpu_ctx("SAE row-jet linear beta load")?;
                    let total_u64 = u64::try_from(beta_len)
                        .map_err(|_| gam_gpu::gpu_err!("SAE row-jet beta length overflows u64"))?;
                    let mut launch = stream.launch_builder(&function);
                    launch
                        .arg(&staged.z_dev)
                        .arg(&staged.active_dev)
                        .arg(&staged.beta_atom_dev)
                        .arg(&staged.beta_phi_dev)
                        .arg(&bodot_dev)
                        .arg(&staged.sqrt_w_dev)
                        .arg(&staged.k_i32)
                        .arg(&staged.nb_i32)
                        .arg(&total_u64)
                        .arg(&mut beta_dev);
                    // SAFETY: the loaded kernel's argument ABI matches this
                    // builder, and `grid(beta_len)` covers only the outputs.
                    unsafe { launch.launch(grid(beta_len)?) }
                        .gpu_ctx("SAE row-jet linear beta launch")?;
                }
            }
            DeviceContraction::Bilinear { v_t, v_beta } => {
                let d2dot_len = device_length(&[n, q, q])?;
                let mut d2dot_dev = stream
                    .alloc_zeros::<f64>(d2dot_len.max(1))
                    .gpu_ctx("SAE row-jet alloc d2 dots")?;
                launch_rowmat(
                    &staged.d2_dev,
                    checked_product(&[q, q]).map_err(|reason| gam_gpu::gpu_err!("{reason}"))?,
                    &mut d2dot_dev,
                    "SAE row-jet d2 dots",
                )?;
                let v_t_dev = stream
                    .clone_htod(nonempty_f64(v_t).as_ref())
                    .gpu_ctx("SAE row-jet htod v_t")?;
                let v_beta_dev = stream
                    .clone_htod(nonempty_f64(v_beta).as_ref())
                    .gpu_ctx("SAE row-jet htod v_beta")?;
                if t_len != 0 {
                    let function = b
                        .module
                        .load_function("sae_rowjet_contract_bilinear_t")
                        .gpu_ctx("SAE row-jet bilinear t load")?;
                    let total_u64 = u64::try_from(t_len)
                        .map_err(|_| gam_gpu::gpu_err!("SAE row-jet t length overflows u64"))?;
                    let mut launch = stream.launch_builder(&function);
                    launch
                        .arg(&staged.z_dev)
                        .arg(&staged.active_dev)
                        .arg(&staged.kind_dev)
                        .arg(&staged.atom_dev)
                        .arg(&staged.beta_atom_dev)
                        .arg(&staged.beta_phi_dev)
                        .arg(&staged.beta_first_dev)
                        .arg(&dd_dev)
                        .arg(&d1dot_dev)
                        .arg(&d2dot_dev)
                        .arg(&bodot_dev)
                        .arg(&v_t_dev)
                        .arg(&v_beta_dev)
                        .arg(&staged.sqrt_w_dev)
                        .arg(&inv_tau)
                        .arg(&staged.k_i32)
                        .arg(&staged.q_i32)
                        .arg(&staged.nb_i32)
                        .arg(&total_u64)
                        .arg(&mut t_dev);
                    // SAFETY: the loaded kernel's argument ABI matches this
                    // builder, and `grid(t_len)` covers only the outputs.
                    unsafe { launch.launch(grid(t_len)?) }
                        .gpu_ctx("SAE row-jet bilinear t launch")?;
                }
                if beta_len != 0 {
                    let function = b
                        .module
                        .load_function("sae_rowjet_contract_bilinear_beta")
                        .gpu_ctx("SAE row-jet bilinear beta load")?;
                    let total_u64 = u64::try_from(beta_len)
                        .map_err(|_| gam_gpu::gpu_err!("SAE row-jet beta length overflows u64"))?;
                    let mut launch = stream.launch_builder(&function);
                    launch
                        .arg(&staged.z_dev)
                        .arg(&staged.active_dev)
                        .arg(&staged.kind_dev)
                        .arg(&staged.atom_dev)
                        .arg(&staged.beta_atom_dev)
                        .arg(&staged.beta_phi_dev)
                        .arg(&staged.beta_first_dev)
                        .arg(&bodot_dev)
                        .arg(&v_t_dev)
                        .arg(&staged.sqrt_w_dev)
                        .arg(&inv_tau)
                        .arg(&staged.k_i32)
                        .arg(&staged.q_i32)
                        .arg(&staged.nb_i32)
                        .arg(&total_u64)
                        .arg(&mut beta_dev);
                    // SAFETY: the loaded kernel's argument ABI matches this
                    // builder, and `grid(beta_len)` covers only the outputs.
                    unsafe { launch.launch(grid(beta_len)?) }
                        .gpu_ctx("SAE row-jet bilinear beta launch")?;
                }
            }
        }

        let mut t = vec![0.0_f64; t_len];
        let mut beta = vec![0.0_f64; beta_len];
        if t_len != 0 {
            stream
                .memcpy_dtoh(&t_dev, &mut t)
                .gpu_ctx("SAE row-jet dtoh contracted t")?;
        }
        if beta_len != 0 {
            stream
                .memcpy_dtoh(&beta_dev, &mut beta)
                .gpu_ctx("SAE row-jet dtoh contracted beta")?;
        }
        stream
            .synchronize()
            .gpu_ctx("SAE row-jet contracted synchronize")?;
        Ok(SaeRowJetContractedTile {
            n_rows: n,
            q,
            n_beta,
            t,
            beta,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// #2422 device-free half, shared by this module's three CUDA-gated tests.
    ///
    /// Each of them returned on `Ok(None)` before its first assertion, so on a
    /// device-free host -- which is every CI runner -- they reported `passed`
    /// having measured nothing. Without a device the production entry's
    /// `Device` path must REFUSE: `execute_softmax_row_jet_tile` dispatches
    /// straight to `device::device_tile`, which opens with `backend()?`, and
    /// maps the error out rather than falling back to the host. An `Ok` here
    /// would mean the seam quietly computed the CPU answer under a `Device`
    /// request -- the #1551 silent-fallback class, and exactly what a `return`
    /// before the first assertion could never see.
    ///
    /// The fixture is small on purpose: the refusal is a property of the absent
    /// device, not of the workload, and the gated tests' own 1<<17-row fixtures
    /// would cost real time on a host that is going to refuse anyway. The CPU
    /// arm is asserted first so a fixture that is broken for some unrelated
    /// reason cannot make the refusal check pass for the wrong reason.
    fn assert_row_jet_device_path_declines_without_cuda() {
        let rows = complete_fixture(64);
        let cpu = execute_softmax_row_jet_tile(&rows, 1.0, SaeRowJetPath::Cpu)
            .expect("the CPU row-jet path must succeed on every host");
        assert_eq!(
            cpu.n_rows, 64,
            "the device-free half needs a CPU result over the whole fixture, or it \
             proves nothing about the seam"
        );
        if let Ok(channels) = execute_softmax_row_jet_tile(&rows, 1.0, SaeRowJetPath::Device) {
            panic!(
                "no CUDA runtime on this host, yet the Device row-jet path returned Ok with \
                 n_rows={} -- the seam fell back to the host silently (#1551 class)",
                channels.n_rows
            );
        }
    }

    /// Resolve the device for the three `..._when_admitted_2304` gates, and on a
    /// device-free host both COUNT the skip and assert the seam refuses.
    ///
    /// Each of those three tests opened with its own `GpuRuntime::resolve`
    /// match. The seam assertion they ran was real, but the skip itself was
    /// invisible: nothing incremented the #2422 counter and nothing printed the
    /// shared `SKIPPED(no-cuda):` marker, so a CI ledger scraping a green
    /// CPU-only run could not tell that three device-parity gates had declined.
    /// Curing it in one helper rather than at three call sites is deliberate —
    /// a fix applied per-site is a fix the next gate written here will miss.
    #[cfg(target_os = "linux")]
    fn row_jet_device_gate(label: &str) -> bool {
        let skips_before = gam_gpu::test_gate::skipped_for_absent_device();
        match gam_gpu::test_gate::gpu_for_test(label) {
            gam_gpu::test_gate::GpuTestGate::Ready(_) => true,
            gam_gpu::test_gate::GpuTestGate::AbsentDevice => {
                gam_gpu::test_gate::assert_absent_device_was_counted(skips_before);
                assert_row_jet_device_path_declines_without_cuda();
                false
            }
        }
    }

    #[cfg(not(target_os = "linux"))]
    #[test]
    fn device_path_declines_on_unsupported_host_2422() {
        assert_row_jet_device_path_declines_without_cuda();
    }

    fn complete_fixture(n: usize) -> Vec<SaeSoftmaxRowJetInput> {
        let k = 3;
        let p = 2;
        let primaries = vec![
            SaeRowJetPrimary::Logit { atom: 0 },
            SaeRowJetPrimary::Logit { atom: 1 },
            SaeRowJetPrimary::Coordinate { atom: 0, axis: 0 },
            SaeRowJetPrimary::Coordinate { atom: 1, axis: 0 },
            SaeRowJetPrimary::Coordinate { atom: 1, axis: 1 },
            SaeRowJetPrimary::Coordinate { atom: 2, axis: 0 },
        ];
        let q = primaries.len();
        (0..n)
            .map(|row| {
                let logits: Vec<f64> = (0..k)
                    .map(|atom| 0.4 * ((row * 17 + atom * 11 + 1) as f64 * 0.07).sin())
                    .collect();
                let shift = logits.iter().copied().fold(f64::NEG_INFINITY, f64::max);
                let exps: Vec<f64> = logits.iter().map(|value| (value - shift).exp()).collect();
                let sum: f64 = exps.iter().sum();
                let gate_values = exps.iter().map(|value| value / sum).collect();
                let decoded: Vec<f64> = (0..k * p)
                    .map(|index| ((row * 13 + index * 7 + 3) as f64 * 0.09).cos())
                    .collect();
                let mut decoded_first = vec![0.0; q * p];
                for slot in 2..q {
                    for c in 0..p {
                        decoded_first[slot * p + c] =
                            ((row * 19 + slot * 5 + c + 2) as f64 * 0.04).sin();
                    }
                }
                let mut decoded_second = vec![0.0; q * q * p];
                for a in 2..q {
                    for b in 2..q {
                        let same_atom = match (primaries[a], primaries[b]) {
                            (
                                SaeRowJetPrimary::Coordinate { atom: left, .. },
                                SaeRowJetPrimary::Coordinate { atom: right, .. },
                            ) => left == right,
                            _ => false,
                        };
                        if same_atom {
                            for c in 0..p {
                                decoded_second[(a * q + b) * p + c] =
                                    ((row * 23 + a * 7 + b * 3 + c + 1) as f64 * 0.03).cos();
                            }
                        }
                    }
                }
                let beta_atoms = vec![0, 1, 2];
                let n_beta = beta_atoms.len();
                let beta_basis_values = vec![0.8, -0.3, 0.5];
                let mut beta_basis_first = vec![0.0; q * n_beta];
                beta_basis_first[2 * n_beta] = 0.2;
                beta_basis_first[3 * n_beta + 1] = -0.4;
                beta_basis_first[4 * n_beta + 1] = 0.7;
                beta_basis_first[5 * n_beta + 2] = -0.1;
                SaeSoftmaxRowJetInput {
                    n_atoms: k,
                    out_dim: p,
                    coordinate_slots: SaeSoftmaxRowJetInput::coordinate_slots_for(&primaries),
                    primaries: primaries.clone(),
                    gate_values,
                    active_atoms: vec![true, true, row % 2 == 0],
                    sqrt_row_weight: (1.0 + row as f64 * 0.1).sqrt(),
                    decoded,
                    decoded_first,
                    decoded_second,
                    beta_atoms: beta_atoms.into(),
                    beta_basis_values,
                    beta_basis_first,
                    beta_outputs: vec![1.0, 0.2, -0.5, 0.8, 0.3, -0.7].into(),
                }
            })
            .collect()
    }

    #[test]
    fn complete_cpu_rowjet_contains_coordinate_mixed_and_beta_channels_2304() {
        let rows = complete_fixture(2);
        let out = execute_softmax_row_jet_tile(&rows, 1.0, SaeRowJetPath::Cpu)
            .expect("complete CPU row jet");
        assert_eq!(out.q, 6);
        assert!(
            out.first[2 * out.p..3 * out.p]
                .iter()
                .any(|value| *value != 0.0)
        );
        let mixed = (0 * out.q * out.q + 0 * out.q + 2) * out.p;
        assert!(
            out.second[mixed..mixed + out.p]
                .iter()
                .any(|value| *value != 0.0)
        );
        let transpose = (0 * out.q * out.q + 2 * out.q) * out.p;
        assert_eq!(
            &out.second[mixed..mixed + out.p],
            &out.second[transpose..transpose + out.p]
        );
        assert!(out.beta.iter().any(|value| *value != 0.0));
        assert!(out.beta_mixed.iter().any(|value| *value != 0.0));
    }

    // Exercises the Linux-only tile-planner ledger method, so it is gated with it;
    // stacked attributes read as AND.
    #[cfg(target_os = "linux")]
    #[test]
    fn memory_ledger_counts_coordinate_and_mixed_tensors_2304() {
        fn assert_exact_ledger(
            k: usize,
            q: usize,
            p: usize,
            n_beta: usize,
            staging_is_host_peak: bool,
        ) -> SaeRowJetMemoryLedger {
            let f64_bytes = std::mem::size_of::<f64>();
            let i32_bytes = std::mem::size_of::<i32>();

            // Reconstruct every resident allocation independently of
            // `SaeRowJetMemoryLedger::for_shape`. In particular, the complete
            // coordinate Hessian is resident on both sides of the kernel and
            // the single beta-mixed wire channel becomes two scheduled arrays.
            let gate_bytes = k * f64_bytes;
            let sqrt_weight_bytes = f64_bytes;
            let decoded_bytes = k * p * f64_bytes;
            let decoded_first_bytes = q * p * f64_bytes;
            let decoded_second_bytes = q * q * p * f64_bytes;
            let beta_basis_bytes = n_beta * f64_bytes;
            let beta_basis_first_bytes = q * n_beta * f64_bytes;
            let input_f64_bytes = gate_bytes
                + sqrt_weight_bytes
                + decoded_bytes
                + decoded_first_bytes
                + decoded_second_bytes
                + beta_basis_bytes
                + beta_basis_first_bytes;
            let input_i32_bytes = (k + q + q) * i32_bytes;

            let first_output_bytes = q * p * f64_bytes;
            let second_output_bytes = q * q * p * f64_bytes;
            let beta_output_bytes = n_beta * p * f64_bytes;
            let beta_mixed_wire_bytes = q * n_beta * p * f64_bytes;
            let flat_output_bytes = first_output_bytes
                + second_output_bytes
                + beta_output_bytes
                + beta_mixed_wire_bytes;

            assert_eq!(decoded_second_bytes, second_output_bytes);
            let expected_device_bytes_per_row =
                input_f64_bytes + input_i32_bytes + flat_output_bytes;
            let expected_fixed_device_bytes = beta_output_bytes + n_beta * i32_bytes;

            let semantic_input_bytes = input_f64_bytes
                + beta_output_bytes
                + k * std::mem::size_of::<bool>()
                + q * std::mem::size_of::<SaeRowJetPrimary>()
                + q * std::mem::size_of::<SaeCoordinateSlot>()
                + n_beta * std::mem::size_of::<usize>()
                + std::mem::size_of::<SaeSoftmaxRowJetInput>();
            let production_layout_bytes = q * std::mem::size_of::<SaeRowJetPrimary>()
                + std::mem::size_of::<Vec<SaeRowJetPrimary>>();
            let staging_input_bytes = input_f64_bytes + input_i32_bytes;
            let scheduled_output_bytes = first_output_bytes
                + second_output_bytes
                + beta_output_bytes
                + 2 * beta_mixed_wire_bytes
                + std::mem::size_of::<SaeScheduledRowJets>();
            assert_eq!(
                scheduled_output_bytes,
                flat_output_bytes
                    + beta_mixed_wire_bytes
                    + std::mem::size_of::<SaeScheduledRowJets>()
            );
            let shared_host_bytes =
                semantic_input_bytes + production_layout_bytes + flat_output_bytes;
            let expected_cpu_host_bytes_per_row = shared_host_bytes + scheduled_output_bytes;
            let expected_host_bytes_per_row =
                shared_host_bytes + staging_input_bytes.max(scheduled_output_bytes);
            let expected_fixed_host_bytes = gate_bytes
                + beta_output_bytes
                + n_beta * std::mem::size_of::<usize>()
                + n_beta * i32_bytes
                + std::mem::size_of::<SaeRowJetChannels>()
                + 4 * std::mem::size_of::<Vec<()>>();

            let expected = SaeRowJetMemoryLedger {
                fixed_device_bytes: expected_fixed_device_bytes,
                device_bytes_per_row: expected_device_bytes_per_row,
                fixed_host_bytes: expected_fixed_host_bytes,
                cpu_host_bytes_per_row: expected_cpu_host_bytes_per_row,
                host_bytes_per_row: expected_host_bytes_per_row,
            };
            let actual = SaeRowJetMemoryLedger::for_shape(k, q, p, n_beta).expect("memory ledger");
            assert_eq!(actual, expected, "shape K={k}, q={q}, p={p}, beta={n_beta}");

            if staging_is_host_peak {
                assert!(staging_input_bytes > scheduled_output_bytes);
                assert_eq!(
                    actual.host_bytes_per_row,
                    shared_host_bytes + staging_input_bytes
                );
                assert!(actual.host_bytes_per_row > actual.cpu_host_bytes_per_row);
            } else {
                assert!(scheduled_output_bytes > staging_input_bytes);
                assert_eq!(
                    actual.host_bytes_per_row,
                    shared_host_bytes + scheduled_output_bytes
                );
                assert_eq!(actual.host_bytes_per_row, actual.cpu_host_bytes_per_row);
            }
            actual
        }

        // The complete production fixture has a q^2 coordinate channel and a
        // large scheduled expansion, so production expansion is the host peak.
        let ledger = assert_exact_ledger(3, 6, 2, 3, false);
        // A wide atom input with one primary makes CUDA staging the host peak.
        assert_exact_ledger(64, 1, 2, 1, true);

        assert_eq!(
            ledger.maximum_rows(ledger.fixed_device_bytes, usize::MAX),
            0
        );
        assert_eq!(
            ledger.maximum_rows(
                ledger.fixed_device_bytes + ledger.device_bytes_per_row,
                ledger.fixed_host_bytes + ledger.host_bytes_per_row
            ),
            1
        );
    }

    #[cfg(target_os = "linux")]
    #[test]
    fn complete_device_matches_cpu_every_channel_when_admitted_2304() {
        if !row_jet_device_gate("complete row-jet elementwise parity #2304") {
            return;
        }
        // A 37-row smoke fixture launches each kernel for too little time to
        // establish that the admitted production path actually occupies the
        // device. Keep a complete, memory-bounded workload large enough for
        // external utilization/profiler sampling, warm the NVRTC/module path
        // once, then require every measured pass to match the one CPU oracle.
        const ROW_COUNT: usize = 1 << 17;
        const MEASURED_PASSES: usize = 8;
        let rows = complete_fixture(ROW_COUNT);
        let cpu = execute_softmax_row_jet_tile(&rows, 1.0, SaeRowJetPath::Cpu).expect("CPU oracle");

        execute_softmax_row_jet_tile(&rows, 1.0, SaeRowJetPath::Device)
            .expect("admitted device warm-up must execute without a host retry");

        let mut max_error = 0.0_f64;
        for pass in 0..MEASURED_PASSES {
            let device = execute_softmax_row_jet_tile(&rows, 1.0, SaeRowJetPath::Device)
                .unwrap_or_else(|error| {
                    panic!("admitted device pass {pass} must execute without a host retry: {error}")
                });
            max_error = cpu
                .first
                .iter()
                .chain(&cpu.second)
                .chain(&cpu.beta)
                .chain(&cpu.beta_mixed)
                .zip(
                    device
                        .first
                        .iter()
                        .chain(&device.second)
                        .chain(&device.beta)
                        .chain(&device.beta_mixed),
                )
                .fold(max_error, |maximum, (left, right)| {
                    maximum.max((left - right).abs())
                });
        }
        let outputs_per_pass =
            cpu.first.len() + cpu.second.len() + cpu.beta.len() + cpu.beta_mixed.len();
        eprintln!(
            "SAE_ROWJET_GPU_ACCEPT rows={ROW_COUNT} measured_passes={MEASURED_PASSES} outputs_per_pass={outputs_per_pass} max_abs_error={max_error:.17e}"
        );
        assert!(
            max_error <= 1.0e-12,
            "complete SAE device/CPU row-jet error {max_error:e} exceeds 1e-12"
        );
    }

    /// Deterministic per-row contraction vectors matched to `complete_fixture`.
    fn contraction_vectors(
        n: usize,
        q: usize,
        p: usize,
        n_beta: usize,
    ) -> (Vec<f64>, Vec<f64>, Vec<f64>) {
        let probe: Vec<f64> = (0..n * p)
            .map(|index| ((index * 29 + 5) as f64 * 0.011).sin())
            .collect();
        let v_t: Vec<f64> = (0..n * q)
            .map(|index| ((index * 31 + 7) as f64 * 0.013).cos())
            .collect();
        let v_beta: Vec<f64> = (0..n * n_beta)
            .map(|index| ((index * 37 + 11) as f64 * 0.017).sin())
            .collect();
        (probe, v_t, v_beta)
    }

    /// The CPU contracted seam must equal reducing the materialized CPU
    /// channels with the production consumers' own dot/accumulation order —
    /// bit for bit, because both run the identical row program and the
    /// identical f64 reduction order.
    #[test]
    fn contracted_cpu_tile_equals_materialized_channel_reduction_2304() {
        let rows = complete_fixture(5);
        let channels = execute_softmax_row_jet_tile(&rows, 1.0, SaeRowJetPath::Cpu)
            .expect("complete CPU row jet");
        let (n, q, p, n_beta) = (channels.n_rows, channels.q, channels.p, channels.n_beta);
        let (probe, v_t, v_beta) = contraction_vectors(n, q, p, n_beta);
        let scheduled = channels.into_scheduled_rows();
        let dot =
            |a: &[f64], b: &[f64]| -> f64 { a.iter().zip(b.iter()).map(|(&x, &y)| x * y).sum() };

        let linear = execute_softmax_row_jet_tile_contracted(
            &rows,
            1.0,
            SaeRowJetPath::Cpu,
            SaeRowJetContraction::Linear { probe: &probe },
        )
        .expect("CPU linear contraction");
        assert_eq!((linear.n_rows, linear.q, linear.n_beta), (n, q, n_beta));
        for (row, jets) in scheduled.iter().enumerate() {
            let probe_row = &probe[row * p..(row + 1) * p];
            for slot in 0..q {
                let expected = dot(jets.first(slot), probe_row);
                assert_eq!(
                    linear.t[row * q + slot],
                    expected,
                    "linear t mismatch at row={row}, slot={slot}"
                );
            }
            for border in 0..n_beta {
                let expected = dot(jets.beta(border), probe_row);
                assert_eq!(
                    linear.beta[row * n_beta + border],
                    expected,
                    "linear beta mismatch at row={row}, border={border}"
                );
            }
        }

        let bilinear = execute_softmax_row_jet_tile_contracted(
            &rows,
            1.0,
            SaeRowJetPath::Cpu,
            SaeRowJetContraction::Bilinear {
                probe: &probe,
                v_t: &v_t,
                v_beta: &v_beta,
            },
        )
        .expect("CPU bilinear contraction");
        for (row, jets) in scheduled.iter().enumerate() {
            let probe_row = &probe[row * p..(row + 1) * p];
            let v_t_row = &v_t[row * q..(row + 1) * q];
            let v_beta_row = &v_beta[row * n_beta..(row + 1) * n_beta];
            for a in 0..q {
                let mut expected = 0.0_f64;
                for b in 0..q {
                    expected += dot(probe_row, jets.second(a, b)) * v_t_row[b];
                }
                for border in 0..n_beta {
                    expected += dot(probe_row, jets.beta_deriv(a, border)) * v_beta_row[border];
                }
                assert_eq!(
                    bilinear.t[row * q + a],
                    expected,
                    "bilinear t mismatch at row={row}, slot={a}"
                );
            }
            for border in 0..n_beta {
                let mut expected = 0.0_f64;
                for a in 0..q {
                    expected += dot(probe_row, jets.beta_deriv(a, border)) * v_t_row[a];
                }
                assert_eq!(
                    bilinear.beta[row * n_beta + border],
                    expected,
                    "bilinear beta mismatch at row={row}, border={border}"
                );
            }
        }
    }

    /// Non-trivial-value guard: on the complete fixture the contracted outputs
    /// must be generically nonzero in every block (t logit, t coordinate, and
    /// β), so the parity assertions above cannot pass on an all-zeros bug.
    #[test]
    fn contracted_cpu_tile_outputs_are_generically_nonzero_2304() {
        let rows = complete_fixture(3);
        let (n, q, p, n_beta) = (3, 6, 2, 3);
        let (probe, v_t, v_beta) = contraction_vectors(n, q, p, n_beta);
        let linear = execute_softmax_row_jet_tile_contracted(
            &rows,
            1.0,
            SaeRowJetPath::Cpu,
            SaeRowJetContraction::Linear { probe: &probe },
        )
        .expect("CPU linear contraction");
        // Slots 0-1 are logits, slots 2+ are coordinates (see complete_fixture).
        assert!(linear.t[0] != 0.0, "logit-slot contraction must engage");
        assert!(
            linear.t[2] != 0.0,
            "coordinate-slot contraction must engage"
        );
        assert!(linear.beta.iter().any(|&value| value != 0.0));
        let bilinear = execute_softmax_row_jet_tile_contracted(
            &rows,
            1.0,
            SaeRowJetPath::Cpu,
            SaeRowJetContraction::Bilinear {
                probe: &probe,
                v_t: &v_t,
                v_beta: &v_beta,
            },
        )
        .expect("CPU bilinear contraction");
        assert!(bilinear.t.iter().any(|&value| value != 0.0));
        assert!(bilinear.beta.iter().any(|&value| value != 0.0));
    }

    /// Resident-contraction acceptance (#2304): on an admitted device, both
    /// contraction shapes must reproduce the CPU reduction at the same
    /// workload scale as the elementwise gate, while transferring only the
    /// `n·q`/`n·n_beta` reduced outputs.
    #[cfg(target_os = "linux")]
    #[test]
    fn contracted_device_matches_cpu_reduction_when_admitted_2304() {
        if !row_jet_device_gate("contracted row-jet reduction parity #2304") {
            return;
        }
        const ROW_COUNT: usize = 1 << 17;
        const MEASURED_PASSES: usize = 4;
        let rows = complete_fixture(ROW_COUNT);
        let (q, p, n_beta) = (
            rows[0].n_primaries(),
            rows[0].out_dim,
            rows[0].n_beta_borders(),
        );
        let (probe, v_t, v_beta) = contraction_vectors(ROW_COUNT, q, p, n_beta);
        let shapes: [(&str, SaeRowJetContraction<'_>); 2] = [
            ("linear", SaeRowJetContraction::Linear { probe: &probe }),
            (
                "bilinear",
                SaeRowJetContraction::Bilinear {
                    probe: &probe,
                    v_t: &v_t,
                    v_beta: &v_beta,
                },
            ),
        ];
        for (label, contraction) in shapes {
            let cpu = execute_softmax_row_jet_tile_contracted(
                &rows,
                1.0,
                SaeRowJetPath::Cpu,
                contraction,
            )
            .expect("CPU contraction oracle");
            execute_softmax_row_jet_tile_contracted(&rows, 1.0, SaeRowJetPath::Device, contraction)
                .expect("admitted contracted device warm-up must execute without a host retry");
            let mut max_error = 0.0_f64;
            for pass in 0..MEASURED_PASSES {
                let device = execute_softmax_row_jet_tile_contracted(
                    &rows,
                    1.0,
                    SaeRowJetPath::Device,
                    contraction,
                )
                .unwrap_or_else(|error| {
                    panic!(
                        "admitted contracted device pass {pass} must execute without a host retry: {error}"
                    )
                });
                max_error = cpu
                    .t
                    .iter()
                    .chain(&cpu.beta)
                    .zip(device.t.iter().chain(&device.beta))
                    .fold(max_error, |maximum, (left, right)| {
                        maximum.max((left - right).abs())
                    });
            }
            let outputs_per_pass = cpu.t.len() + cpu.beta.len();
            eprintln!(
                "SAE_ROWJET_CONTRACT_GPU_ACCEPT shape={label} rows={ROW_COUNT} measured_passes={MEASURED_PASSES} outputs_per_pass={outputs_per_pass} max_abs_error={max_error:.17e}"
            );
            assert!(
                max_error <= 1.0e-12,
                "contracted SAE device/CPU {label} reduction error {max_error:e} exceeds 1e-12"
            );
        }
    }

    /// Representative per-row selected-inverse weights for the Trace shape: a
    /// symmetric `E_tt` and `beta_inv` (as the true deflation-folded selected
    /// inverse is) and an arbitrary `inv_vbeta`. Shared by the manual-reduction
    /// oracle and the device parity gate so both drive the identical operands.
    fn trace_weights(n: usize, q: usize, n_beta: usize) -> (Vec<f64>, Vec<f64>, Vec<f64>) {
        let mut e_tt = vec![0.0_f64; n * q * q];
        for row in 0..n {
            for a in 0..q {
                for b in a..q {
                    let value = ((row * 13 + a * 7 + b * 3 + 1) as f64 * 0.02).cos();
                    e_tt[row * q * q + a * q + b] = value;
                    e_tt[row * q * q + b * q + a] = value;
                }
            }
        }
        let inv_vbeta: Vec<f64> = (0..n * q * n_beta)
            .map(|index| ((index * 17 + 3) as f64 * 0.01).sin())
            .collect();
        let mut beta_inv = vec![0.0_f64; n_beta * n_beta];
        for i in 0..n_beta {
            for j in i..n_beta {
                let value = ((i * 5 + j * 3 + 2) as f64 * 0.03).cos();
                beta_inv[i * n_beta + j] = value;
                beta_inv[j * n_beta + i] = value;
            }
        }
        (e_tt, inv_vbeta, beta_inv)
    }

    /// Device parity for the θ-adjoint Trace reduction at the same 131072-row
    /// scale as the elementwise/linear/bilinear gate. The materialize-then-reduce
    /// device path must reproduce the authoritative CPU Trace oracle to ≤1e-12,
    /// transferring only the `n·q` / `n·n_beta` reduced coefficients. Skips
    /// cleanly when no device admits (a runtime-absent skip does not count as
    /// proof; the exact-SHA GPU gate is what closes the device claim).
    #[cfg(target_os = "linux")]
    #[test]
    fn contracted_trace_device_matches_cpu_reduction_when_admitted_2304() {
        if !row_jet_device_gate("contracted Trace row-jet reduction parity #2304") {
            return;
        }
        const ROW_COUNT: usize = 1 << 17;
        const MEASURED_PASSES: usize = 4;
        let inv_tau = 1.0;
        let rows = complete_fixture(ROW_COUNT);
        let (q, n_beta) = (rows[0].n_primaries(), rows[0].n_beta_borders());
        let (e_tt, inv_vbeta, beta_inv) = trace_weights(ROW_COUNT, q, n_beta);
        let contraction = SaeRowJetContraction::Trace {
            e_tt: &e_tt,
            inv_vbeta: &inv_vbeta,
            beta_inv: &beta_inv,
        };
        let cpu = execute_softmax_row_jet_tile_contracted(
            &rows,
            inv_tau,
            SaeRowJetPath::Cpu,
            contraction,
        )
        .expect("CPU trace oracle");
        execute_softmax_row_jet_tile_contracted(&rows, inv_tau, SaeRowJetPath::Device, contraction)
            .expect("admitted trace device warm-up must execute without a host retry");
        let mut max_error = 0.0_f64;
        for pass in 0..MEASURED_PASSES {
            let device = execute_softmax_row_jet_tile_contracted(
                &rows,
                inv_tau,
                SaeRowJetPath::Device,
                contraction,
            )
            .unwrap_or_else(|error| {
                panic!(
                    "admitted trace device pass {pass} must execute without a host retry: {error}"
                )
            });
            max_error = cpu
                .t
                .iter()
                .chain(&cpu.beta)
                .zip(device.t.iter().chain(&device.beta))
                .fold(max_error, |maximum, (left, right)| {
                    maximum.max((left - right).abs())
                });
        }
        let outputs_per_pass = cpu.t.len() + cpu.beta.len();
        eprintln!(
            "SAE_ROWJET_TRACE_GPU_ACCEPT rows={ROW_COUNT} measured_passes={MEASURED_PASSES} outputs_per_pass={outputs_per_pass} max_abs_error={max_error:.17e}"
        );
        assert!(
            max_error <= 1.0e-12,
            "Trace SAE device/CPU reduction error {max_error:e} exceeds 1e-12"
        );
    }

    /// The `Trace` seam must equal the dense θ-adjoint reduction of the
    /// materialized channels against a per-row selected inverse — bit for bit,
    /// because both run the identical row program and identical f64 reduction.
    /// Uses a symmetric `E_tt` / `beta_inv` (as the true deflation-folded
    /// selected inverse is), an arbitrary `inv_vbeta`, and guards that every
    /// output block engages so an all-zeros bug cannot pass.
    #[test]
    fn contracted_trace_matches_manual_selected_inverse_reduction_2304() {
        let rows = complete_fixture(4);
        let inv_tau = 1.0;
        let channels =
            execute_softmax_row_jet_tile(&rows, inv_tau, SaeRowJetPath::Cpu).expect("CPU row jet");
        let (n, q, n_beta) = (channels.n_rows, channels.q, channels.n_beta);
        let scheduled = channels.into_scheduled_rows();
        let dot =
            |a: &[f64], b: &[f64]| -> f64 { a.iter().zip(b.iter()).map(|(&x, &y)| x * y).sum() };

        let (e_tt, inv_vbeta, beta_inv) = trace_weights(n, q, n_beta);

        let trace = execute_softmax_row_jet_tile_contracted(
            &rows,
            inv_tau,
            SaeRowJetPath::Cpu,
            SaeRowJetContraction::Trace {
                e_tt: &e_tt,
                inv_vbeta: &inv_vbeta,
                beta_inv: &beta_inv,
            },
        )
        .expect("CPU trace contraction");
        assert_eq!((trace.n_rows, trace.q, trace.n_beta), (n, q, n_beta));

        let mut any_logit = false;
        let mut any_coord = false;
        let mut any_beta = false;
        for (row, jets) in scheduled.iter().enumerate() {
            let e_row = &e_tt[row * q * q..(row + 1) * q * q];
            let vbeta_row = &inv_vbeta[row * q * n_beta..(row + 1) * q * n_beta];
            for w in 0..q {
                let (w_is_logit, atom_w) = primary_kind_atom(rows[row].primaries[w]);
                let mut expected = 0.0_f64;
                for a in 0..q {
                    let (a_is_logit, atom_a) = primary_kind_atom(rows[row].primaries[a]);
                    for b in 0..q {
                        let (b_is_logit, atom_b) = primary_kind_atom(rows[row].primaries[b]);
                        let dh = if w_is_logit && !a_is_logit && !b_is_logit {
                            dot(jets.first(a), jets.first(b))
                                * softmax_data_weight_product_logit_factor(
                                    &rows[row].gate_values,
                                    atom_a,
                                    atom_b,
                                    atom_w,
                                    inv_tau,
                                )
                        } else {
                            dot(jets.second(a, w), jets.first(b))
                                + dot(jets.first(a), jets.second(b, w))
                        };
                        expected += e_row[a * q + b] * dh;
                    }
                    for border in 0..n_beta {
                        let dh = dot(jets.second(a, w), jets.beta(border))
                            + dot(jets.first(a), jets.beta_deriv(w, border));
                        expected += 2.0 * vbeta_row[a * n_beta + border] * dh;
                    }
                }
                for i in 0..n_beta {
                    for j in 0..n_beta {
                        let dh = dot(jets.beta_deriv(w, i), jets.beta(j))
                            + dot(jets.beta(i), jets.beta_deriv(w, j));
                        expected += beta_inv[i * n_beta + j] * dh;
                    }
                }
                assert_eq!(
                    trace.t[row * q + w],
                    expected,
                    "trace t mismatch row={row}, w={w}"
                );
                if trace.t[row * q + w] != 0.0 {
                    if w_is_logit {
                        any_logit = true;
                    } else {
                        any_coord = true;
                    }
                }
            }
            for w_beta in 0..n_beta {
                let mut expected = 0.0_f64;
                for a in 0..q {
                    for b in 0..q {
                        let dh = dot(jets.beta_l_deriv(a, w_beta), jets.first(b))
                            + dot(jets.first(a), jets.beta_l_deriv(b, w_beta));
                        expected += e_row[a * q + b] * dh;
                    }
                    for border in 0..n_beta {
                        let dh = dot(jets.beta_l_deriv(a, w_beta), jets.beta(border));
                        expected += 2.0 * vbeta_row[a * n_beta + border] * dh;
                    }
                }
                assert_eq!(
                    trace.beta[row * n_beta + w_beta],
                    expected,
                    "trace beta mismatch row={row}, w_beta={w_beta}"
                );
                if trace.beta[row * n_beta + w_beta] != 0.0 {
                    any_beta = true;
                }
            }
        }
        assert!(any_logit, "logit-direction trace must engage");
        assert!(any_coord, "coordinate-direction trace must engage");
        assert!(any_beta, "beta-direction trace must engage");
    }

    /// Independent finite-difference oracle for the θ-adjoint's softmax-logit
    /// substitution — the novel #2304 surface that
    /// `contracted_trace_matches_manual_selected_inverse_reduction_2304` cannot
    /// validate because that test re-implements the production reduction formula
    /// (a change-detector, not a correctness oracle).
    ///
    /// The whole point of the `Trace` shape is `Γ = tr(H⁻¹ ∂H/∂θ)`, where for a
    /// logit direction `w` over a coordinate pair `(a,b)` the row Hessian's data
    /// curvature block is the reconstruction Gram `G[a][b] = ⟨J_a, J_b⟩` and the
    /// code differentiates it through the softmax assignment weights (the
    /// `softmax_data_weight_product_logit_factor` substitution) rather than
    /// through second jets. With the coordinate first jet
    /// `J_a = z_{atom_a}·√w·da` (da = decoded-first for slot a) the Gram is
    /// `G[a][b] = z_{atom_a} z_{atom_b} · w · ⟨da,db⟩`, an explicit function of
    /// the softmax gates alone. Choosing the row-local t–t weight `E_tt` to
    /// couple only always-active coordinate slots (and zeroing the border
    /// inverses) collapses the logit-direction output to
    /// `t[w] = Σ_{a,b} E_tt[a][b]·∂G[a][b]/∂ℓ_w = ∂/∂ℓ_w ⟨E_tt, G(ℓ)⟩`.
    ///
    /// This test recomputes that scalar `Φ(ℓ) = ⟨E_tt, G(ℓ)⟩` directly from the
    /// softmax gates and the decoder inner products — sharing no code with the
    /// production reduction, the substitution factor, or the row program's
    /// jets — and central-differences it. A sign error, a missing/extra `inv_tau`
    /// factor, a wrong assignment index in the substitution, or a
    /// reduction-bookkeeping bug on the logit rows all break agreement. Run at
    /// two temperatures so a dropped `inv_tau` cannot hide.
    #[test]
    fn contracted_trace_logit_adjoint_matches_softmax_finite_difference_2304() {
        for &inv_tau in &[1.0_f64, 1.3_f64] {
            let rows = complete_fixture(4);
            let n = rows.len();
            let q = rows[0].primaries.len();
            let p = rows[0].out_dim;
            let n_beta = rows[0].beta_atoms.len();

            // Atoms 0 and 1 carry `active_atoms = true` on every row of
            // `complete_fixture`; atom 2 does not. Restricting `E_tt` to their
            // coordinate slots keeps every first jet in play nonzero, so the
            // production substitution and the independent Gram agree row by row.
            let coord_slots: Vec<usize> = rows[0]
                .primaries
                .iter()
                .enumerate()
                .filter_map(|(slot, primary)| match primary {
                    SaeRowJetPrimary::Coordinate { atom, .. } if *atom == 0 || *atom == 1 => {
                        Some(slot)
                    }
                    _ => None,
                })
                .collect();
            assert!(
                coord_slots.len() >= 2,
                "fixture must expose at least two always-active coordinate slots"
            );
            let coord_atom = |slot: usize| -> usize {
                match rows[0].primaries[slot] {
                    SaeRowJetPrimary::Coordinate { atom, .. } => atom,
                    SaeRowJetPrimary::Logit { .. } => {
                        panic!("coord_slots must only contain coordinate primaries")
                    }
                }
            };

            // Deterministic symmetric weight on the coupled coordinate slots.
            let raw_weight =
                |a: usize, b: usize| -> f64 { ((a * 7 + b * 3 + 1) as f64 * 0.05).cos() };
            let mut e_tt = vec![0.0_f64; n * q * q];
            for row in 0..n {
                for &a in &coord_slots {
                    for &b in &coord_slots {
                        let value = 0.5 * (raw_weight(a, b) + raw_weight(b, a));
                        e_tt[row * q * q + a * q + b] = value;
                    }
                }
            }
            // Zero borders isolate the coordinate-pair data-curvature term.
            let inv_vbeta = vec![0.0_f64; n * q * n_beta];
            let beta_inv = vec![0.0_f64; n_beta * n_beta];

            let trace = execute_softmax_row_jet_tile_contracted(
                &rows,
                inv_tau,
                SaeRowJetPath::Cpu,
                SaeRowJetContraction::Trace {
                    e_tt: &e_tt,
                    inv_vbeta: &inv_vbeta,
                    beta_inv: &beta_inv,
                },
            )
            .expect("CPU trace contraction");

            let dot = |x: &[f64], y: &[f64]| -> f64 { x.iter().zip(y).map(|(&a, &b)| a * b).sum() };
            // z(ℓ) = softmax(inv_tau·ℓ), stable.
            let softmax = |logits: &[f64]| -> Vec<f64> {
                let shift = logits.iter().copied().fold(f64::NEG_INFINITY, f64::max);
                let exps: Vec<f64> = logits
                    .iter()
                    .map(|&l| ((l - shift) * inv_tau).exp())
                    .collect();
                let sum: f64 = exps.iter().sum();
                exps.iter().map(|&e| e / sum).collect()
            };

            let h = 1e-6;
            let mut max_err = 0.0_f64;
            let mut scale = 0.0_f64;
            let mut checked = 0usize;
            for row in 0..n {
                let input = &rows[row];
                let row_weight = input.sqrt_row_weight * input.sqrt_row_weight;
                let da = |slot: usize| -> &[f64] { &input.decoded_first[slot * p..(slot + 1) * p] };
                // Logits consistent with the stored gates: softmax(inv_tau·ℓ)=z
                // for ℓ_k = ln(z_k)/inv_tau (softmax is shift-invariant and Σz=1).
                let base_logits: Vec<f64> = input
                    .gate_values
                    .iter()
                    .map(|&z| z.ln() / inv_tau)
                    .collect();
                let phi = |logits: &[f64]| -> f64 {
                    let z = softmax(logits);
                    let mut acc = 0.0_f64;
                    for &a in &coord_slots {
                        for &b in &coord_slots {
                            let weight = e_tt[row * q * q + a * q + b];
                            acc += weight
                                * z[coord_atom(a)]
                                * z[coord_atom(b)]
                                * row_weight
                                * dot(da(a), da(b));
                        }
                    }
                    acc
                };
                for w in 0..q {
                    let SaeRowJetPrimary::Logit { atom: atom_w } = input.primaries[w] else {
                        continue;
                    };
                    let mut plus = base_logits.clone();
                    plus[atom_w] += h;
                    let mut minus = base_logits.clone();
                    minus[atom_w] -= h;
                    let finite_difference = (phi(&plus) - phi(&minus)) / (2.0 * h);
                    let produced = trace.t[row * q + w];
                    max_err = max_err.max((finite_difference - produced).abs());
                    scale = scale.max(produced.abs()).max(finite_difference.abs());
                    checked += 1;
                }
            }
            assert!(checked >= 2, "must exercise at least two logit directions");
            assert!(
                scale > 1e-6,
                "softmax-substitution trace outputs must be non-trivial (inv_tau={inv_tau}, scale={scale})"
            );
            assert!(
                max_err <= 1e-6 * (1.0 + scale),
                "logit θ-adjoint vs softmax finite difference disagree \
                 (inv_tau={inv_tau}, max_err={max_err}, scale={scale})"
            );
        }
    }

    /// The contracted ledger must charge the reduced download + probe upload
    /// and NEVER the `q²·p` second channel or `q·n_beta·p` mixed channel that
    /// dominates the elementwise tile. Pinned by exact byte arithmetic — not
    /// weakened to an inequality.
    #[test]
    fn contracted_ledger_drops_tower_charges_reduced_download_2304() {
        let (k, q, p, n_beta) = (3usize, 6usize, 2usize, 3usize);
        let f = std::mem::size_of::<f64>();
        let i = std::mem::size_of::<i32>();

        let full = SaeRowJetMemoryLedger::for_shape(k, q, p, n_beta).expect("full ledger");
        let contracted = SaeRowJetMemoryLedger::for_contracted_shape(k, q, p, n_beta)
            .expect("contracted ledger");

        let input_f64 = (k + 1 + k * p + q * p + q * q * p + n_beta + q * n_beta) * f;
        let input_i32 = (k + q + q) * i;
        // Reduced device state: phase-one dots (k + q + n_beta + q²), reduced
        // outputs (q + n_beta), probe (p), and v_t/v_beta (q + n_beta).
        let device_reduced = (k + q + n_beta + q * q + q + n_beta + p + q + n_beta) * f;
        assert_eq!(
            contracted.device_bytes_per_row,
            input_f64 + input_i32 + device_reduced,
            "contracted device per-row bytes"
        );

        let semantic_input = input_f64
            + n_beta * p * f
            + k * std::mem::size_of::<bool>()
            + q * std::mem::size_of::<SaeRowJetPrimary>()
            + q * std::mem::size_of::<SaeCoordinateSlot>()
            + n_beta * std::mem::size_of::<usize>()
            + std::mem::size_of::<SaeSoftmaxRowJetInput>();
        let production_layout = q * std::mem::size_of::<SaeRowJetPrimary>()
            + std::mem::size_of::<Vec<SaeRowJetPrimary>>();
        let reduced_operands = (q + n_beta + p + q + n_beta) * f;
        assert_eq!(
            contracted.cpu_host_bytes_per_row,
            semantic_input + production_layout + reduced_operands,
            "contracted CPU host per-row bytes"
        );

        // The contracted device output is exactly the elementwise output tower
        // (`q·p + q²·p + n_beta·p + q·n_beta·p`) swapped for the reduced state,
        // so the `q²·p` second channel and `q·n_beta·p` mixed channel are gone.
        let elementwise_output = (q * p + q * q * p + n_beta * p + q * n_beta * p) * f;
        assert_eq!(
            full.device_bytes_per_row - contracted.device_bytes_per_row,
            elementwise_output - device_reduced,
            "contracted must drop exactly the elementwise output tower"
        );
        assert!(
            device_reduced < q * q * p * f,
            "reduced device state must be smaller than the q^2 p second channel alone"
        );
        assert!(
            contracted.cpu_host_bytes_per_row < full.cpu_host_bytes_per_row,
            "contracted host residency must be smaller than the elementwise tile"
        );
    }
}