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
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
use super::*;
/// Active-set layout override for [`SaeManifoldTerm::assemble_arrow_schur_inner`].
///
/// `None` is the production path: the layout is derived from the assignment mode
/// and `sparse_active_plan`. `Some(layout_opt)` pins a specific layout — dense
/// (`Some(None)`) or a chosen compact `SaeRowLayout` (`Some(Some(..))`) — so the
/// compact-vs-dense Riemannian-geometry equality regression can drive both code
/// paths on identical data without depending on the host/device memory budget
/// that gates the compact path in production.
pub(crate) type ForcedRowLayout = Option<Option<SaeRowLayout>>;
/// #1154 — base co-training weight for the amortized-encoder reconstruction
/// consistency penalty, as a fraction of the REML criterion magnitude. The
/// effective weight is `COTRAIN_RECON_WEIGHT · max(|REML|, 1)`, so the penalty
/// is a bounded, scale-free share of the objective and needs no caller knob.
pub(crate) const COTRAIN_RECON_WEIGHT: f64 = 0.1;
/// #1154 — base co-training weight for the encoder's certifiable-coverage
/// penalty (the fraction of (row, atom) encodes the Kantorovich certificate
/// rejected). Scaled like [`COTRAIN_RECON_WEIGHT`].
pub(crate) const COTRAIN_CERT_WEIGHT: f64 = 0.05;
/// #1154 — amortized-encoder consistency of a fitted dictionary against its own
/// fit-time target. The co-training signal of the joint amortized-encoder +
/// REML loop: how faithfully (and how certifiably) the cheap one-mat-vec
/// encoder inverts the dictionary the inner solve converged to.
#[derive(Debug, Clone, Copy)]
pub struct AmortizedEncoderConsistency {
/// Mean per-element squared gap between the amortized reconstruction and the
/// exact fitted reconstruction (`‖x̂_amortized − x̂_exact‖² / (n·p)`). Zero ⇒
/// the IFT predictor reproduces the encode map exactly to first order.
pub recon_consistency: f64,
/// Fraction of (row, atom) amortized encodes whose Kantorovich certificate
/// failed (`h > ½`) and fell back to the certified Newton encode.
pub uncertified_fraction: f64,
/// Count of uncertified (row, atom) encodes (numerator of the fraction).
pub n_uncertified: usize,
/// Total (row, atom) encodes scored (`n · K`).
pub n_encodes: usize,
}
impl SaeManifoldTerm {
#[must_use = "build error must be handled"]
pub fn new(atoms: Vec<SaeManifoldAtom>, assignment: SaeAssignment) -> Result<Self, String> {
if atoms.is_empty() {
return Err("SaeManifoldTerm::new: at least one atom required".into());
}
let n = atoms[0].n_obs();
let p = atoms[0].output_dim();
if assignment.n_obs() != n || assignment.k_atoms() != atoms.len() {
return Err(format!(
"SaeManifoldTerm::new: assignment shape ({}, {}) does not match atoms ({n}, {})",
assignment.n_obs(),
assignment.k_atoms(),
atoms.len()
));
}
for (k, atom) in atoms.iter().enumerate() {
if atom.n_obs() != n {
return Err(format!(
"SaeManifoldTerm::new: atom {k} has n_obs={} but atom 0 has {n}",
atom.n_obs()
));
}
if atom.output_dim() != p {
return Err(format!(
"SaeManifoldTerm::new: atom {k} output_dim={} but atom 0 has {p}",
atom.output_dim()
));
}
if atom.latent_dim != assignment.coords[k].latent_dim() {
return Err(format!(
"SaeManifoldTerm::new: atom {k} latent_dim={} but assignment coord has {}",
atom.latent_dim,
assignment.coords[k].latent_dim()
));
}
}
Ok(Self {
atoms,
assignment,
temperature_schedule: None,
last_row_layout: None,
row_metric: None,
collapse_events: Vec::new(),
row_loss_weights: None,
last_frames_active: false,
border_hbb_workspace: Array2::<f64>::zeros((0, 0)),
certificate_dispersion: None,
curvature_walk_report: None,
expected_evidence_gauge_deflated_directions: None,
evidence_gauge_deflation_reanchors: 0,
evidence_gauge_deflation_last_delta_sign: 0,
dictionary_cocollapse_reseeds: 0,
hybrid_split_report: None,
atom_inner_fits: None,
oos_linear_images: None,
})
}
/// Install the fitted reconstruction dispersion used by
/// [`dictionary_incoherence_report`]. This is a pure diagnostic scalar and
/// does not feed any loss, criterion, penalty, or optimizer state.
pub fn set_certificate_dispersion(&mut self, dispersion: f64) -> Result<(), String> {
if !dispersion.is_finite() || dispersion <= 0.0 {
return Err(format!(
"SaeManifoldTerm::set_certificate_dispersion: dispersion must be finite and positive, got {dispersion}"
));
}
self.certificate_dispersion = Some(dispersion);
Ok(())
}
/// Harvest the per-atom inner-decoder-smooth byproducts (#1097 / #1103) the
/// residual-gauge certificate's post-PIRLS atom inference reports consume.
///
/// This is the post-fit harness seam: it needs the reconstruction target `Z`
/// (`target`) and the fitted dispersion `φ` (`dispersion`), both available
/// only after the joint fit converges and the engine has discarded `Z` from
/// the objective. For each atom `k` it captures the Gaussian-identity
/// penalized smooth of the atom's leading decoder output channel `j`
/// (largest column 2-norm of `B_k`) against its partial residual
/// `e_{i} = z_i − fitted_i + a_{ik} g_k(t_i)` on channel `j`, holding all
/// other atoms and the assignment fixed at the fitted optimum — exactly the
/// fixed snapshot ([`crate::terms::sae::identifiability::AtomInnerFit`]) the Riesz
/// debiasing and split-LRT smooth-structure e-value read.
///
/// A pure read of the fitted state: it mutates only the diagnostic
/// `atom_inner_fits` field, never a loss / criterion / penalty / optimizer
/// state. Atoms with no active rows or a degenerate (rank-deficient,
/// non-SPD) inner Hessian get a `None` slot — the genuine prerequisite (an
/// SPD penalized inner Hessian on a non-empty active set) is absent there.
pub fn set_atom_inner_fits(
&mut self,
target: ArrayView2<'_, f64>,
rho: &SaeManifoldRho,
dispersion: f64,
) -> Result<(), String> {
if !dispersion.is_finite() || dispersion <= 0.0 {
return Err(format!(
"SaeManifoldTerm::set_atom_inner_fits: dispersion must be finite and positive, got {dispersion}"
));
}
let n = self.n_obs();
let p = self.output_dim();
let k_atoms = self.k_atoms();
if target.dim() != (n, p) {
return Err(format!(
"SaeManifoldTerm::set_atom_inner_fits: target {:?} != ({n}, {p})",
target.dim()
));
}
// Settled per-row assignments and per-(row, atom) decoded outputs, so the
// per-atom partial residual is `e_k = (z − fitted) + a_k decoded_k`.
let mut assignments = Vec::with_capacity(n);
for row in 0..n {
assignments.push(self.assignment.try_assignments_row_for_rho(row, rho)?);
}
let mut decoded = Array3::<f64>::zeros((n, k_atoms, p));
let mut dbuf = vec![0.0_f64; p];
for row in 0..n {
for atom_idx in 0..k_atoms {
self.atoms[atom_idx].fill_decoded_row(row, &mut dbuf);
for c in 0..p {
decoded[[row, atom_idx, c]] = dbuf[c];
}
}
}
let mut fitted = Array2::<f64>::zeros((n, p));
for row in 0..n {
for atom_idx in 0..k_atoms {
let a = assignments[row][atom_idx];
if a == 0.0 {
continue;
}
for c in 0..p {
fitted[[row, c]] += a * decoded[[row, atom_idx, c]];
}
}
}
let mut inner_fits: Vec<Option<crate::terms::sae::identifiability::AtomInnerFit>> =
Vec::with_capacity(k_atoms);
for atom_idx in 0..k_atoms {
inner_fits.push(self.build_atom_inner_fit(
atom_idx,
target,
&assignments,
decoded.view(),
fitted.view(),
dispersion,
)?);
}
self.atom_inner_fits = Some(inner_fits);
Ok(())
}
/// Build one atom's fixed inner-smooth snapshot for the post-PIRLS atom
/// inference reports, or `None` when the atom has no active rows or the
/// penalized inner Hessian is not SPD. Returns `Err` only on a structural
/// inconsistency (shape mismatch), never on a benign degenerate atom.
pub(crate) fn build_atom_inner_fit(
&self,
atom_idx: usize,
target: ArrayView2<'_, f64>,
assignments: &[Array1<f64>],
decoded: ArrayView3<'_, f64>,
fitted: ArrayView2<'_, f64>,
dispersion: f64,
) -> Result<Option<crate::terms::sae::identifiability::AtomInnerFit>, String> {
let atom = &self.atoms[atom_idx];
let n = atom.n_obs();
let m = atom.basis_size();
let p = atom.output_dim();
if m == 0 || p == 0 {
return Ok(None);
}
// Leading decoder output channel j = argmax_j ‖B_k[:, j]‖, the channel
// that carries the atom's signal.
let mut j_lead = 0usize;
let mut best_norm = -1.0_f64;
for col in 0..p {
let mut norm = 0.0_f64;
for r in 0..m {
let v = atom.decoder_coefficients[[r, col]];
norm += v * v;
}
if norm > best_norm {
best_norm = norm;
j_lead = col;
}
}
let beta = atom.decoder_coefficients.column(j_lead).to_owned();
// Active rows: a_{ik} > 0.
let active: Vec<usize> = (0..n)
.filter(|&row| assignments[row][atom_idx] > 0.0)
.collect();
let n_active = active.len();
// The penalized smooth needs at least as many active rows as it has
// basis columns to give a non-degenerate data Gram; below that the inner
// fit's SPD prerequisite is genuinely unmet.
if n_active == 0 {
return Ok(None);
}
let mut design = Array2::<f64>::zeros((n_active, m));
let mut derivative_design = Array2::<f64>::zeros((n_active, m));
let mut row_scores = Array2::<f64>::zeros((n_active, m));
let mut weights = Array1::<f64>::zeros(n_active);
for (slot, &row) in active.iter().enumerate() {
let a_ik = assignments[row][atom_idx];
let w_i = a_ik * a_ik;
weights[slot] = w_i;
for col in 0..m {
design[[slot, col]] = atom.basis_values[[row, col]];
// Leading latent axis (axis 0) is the atom's primary coordinate;
// it is the one the average-derivative functional integrates.
derivative_design[[slot, col]] = atom.basis_jacobian[[row, col, 0]];
}
// Partial residual on channel j, then the inner-smooth working
// response z_i = e_i / a_ik so that w_i (z_i − Φᵀβ) = a_ik r_i.
let e_i = target[[row, j_lead]] - fitted[[row, j_lead]]
+ a_ik * decoded[[row, atom_idx, j_lead]];
let mu_hat = design.row(slot).dot(&beta);
let z_i = e_i / a_ik;
let res_i = z_i - mu_hat;
// Gaussian-identity score s_i = −w_i res_i Φ_i / φ.
let scale = -w_i * res_i / dispersion;
for col in 0..m {
row_scores[[slot, col]] = scale * design[[slot, col]];
}
}
// Penalized inner Hessian H = ΦᵀWΦ + S̃_k.
let mut xtwx = Array2::<f64>::zeros((m, m));
for slot in 0..n_active {
let w_i = weights[slot];
for a in 0..m {
let xa = design[[slot, a]];
if xa == 0.0 {
continue;
}
for b in 0..m {
xtwx[[a, b]] += w_i * xa * design[[slot, b]];
}
}
}
let penalty = atom.smooth_penalty.clone();
if penalty.dim() != (m, m) {
return Err(format!(
"build_atom_inner_fit: atom {atom_idx} smooth penalty {:?} != ({m}, {m})",
penalty.dim()
));
}
let penalized_hessian = &xtwx + &penalty;
// SPD prerequisite: the inner penalized Hessian must factor, else the
// atom's inner-smooth fit is degenerate and no report is producible.
if penalized_hessian.cholesky(Side::Lower).is_err() {
return Ok(None);
}
// Peak (largest fitted |g_k| on channel j) and mode (largest assignment
// mass) design rows, over the active set.
let mut peak_slot = 0usize;
let mut peak_val = -1.0_f64;
let mut mode_slot = 0usize;
let mut mode_mass = -1.0_f64;
for (slot, &row) in active.iter().enumerate() {
let g_val = design.row(slot).dot(&beta).abs();
if g_val > peak_val {
peak_val = g_val;
peak_slot = slot;
}
let mass = assignments[row][atom_idx];
if mass > mode_mass {
mode_mass = mass;
mode_slot = slot;
}
}
let peak_design_row = design.row(peak_slot).to_owned();
let mode_design_row = design.row(mode_slot).to_owned();
Ok(Some(crate::terms::sae::identifiability::AtomInnerFit {
design,
derivative_design,
beta,
penalty,
penalized_hessian,
row_scores,
weights,
dispersion,
peak_design_row,
mode_design_row,
}))
}
/// Profile the Gaussian reconstruction dispersion at the current seed
/// state. This is the scale used to make SAE penalty seeds dimensionless
/// before the outer rho search starts.
pub fn seed_reconstruction_dispersion(
&self,
target: ArrayView2<'_, f64>,
) -> Result<f64, String> {
let fitted = self.try_fitted()?;
if fitted.dim() != target.dim() {
return Err(format!(
"SaeManifoldTerm::seed_reconstruction_dispersion: fitted {:?} != target {:?}",
fitted.dim(),
target.dim()
));
}
let n_scalar = (target.nrows() * target.ncols()).max(1) as f64;
let mut rss = 0.0_f64;
for row in 0..target.nrows() {
for col in 0..target.ncols() {
let r = target[[row, col]] - fitted[[row, col]];
rss += r * r;
}
}
if !rss.is_finite() || rss < 0.0 {
return Err(format!(
"SaeManifoldTerm::seed_reconstruction_dispersion: non-finite seed RSS {rss}"
));
}
Ok((rss / n_scalar).max(SAE_SEED_DISPERSION_FLOOR))
}
/// Install per-row design honesty weights (#991) — the `1/π` inclusion
/// corrections of a designed corpus subsample (see the field docs on
/// `row_loss_weights` for exactly where they enter the objective).
///
/// Weights must be finite and strictly positive, one per term row. They
/// are self-normalized to mean `1.0` here (only the *relative* design
/// correction matters at the fitted sample size; the absolute `n/budget`
/// scale would silently inflate the dispersion estimate against the
/// sample-sized dof). Weights that are identically equal after
/// normalization (an exact full pass, or any uniform design) are stored
/// as `None`, so the unweighted path stays bit-for-bit identical rather
/// than "multiplied by 1.0".
pub fn set_row_loss_weights(&mut self, weights: Vec<f64>) -> Result<(), String> {
if weights.len() != self.n_obs() {
return Err(format!(
"SaeManifoldTerm::set_row_loss_weights: {} weights for {} rows",
weights.len(),
self.n_obs()
));
}
if weights.is_empty() {
self.row_loss_weights = None;
return Ok(());
}
if !weights.iter().all(|w| w.is_finite() && *w > 0.0) {
return Err(
"SaeManifoldTerm::set_row_loss_weights: weights must be finite and strictly \
positive"
.to_string(),
);
}
let first = weights[0];
if weights.iter().all(|w| *w == first) {
// Uniform design (full pass, or flat measure): the normalized
// weight is exactly 1 everywhere — take the unweighted path.
self.row_loss_weights = None;
return Ok(());
}
let mean = weights.iter().sum::<f64>() / weights.len() as f64;
self.row_loss_weights = Some(weights.into_iter().map(|w| w / mean).collect());
Ok(())
}
/// The installed (mean-1 normalized) design honesty weights, `None` on the
/// exact unweighted path.
pub fn row_loss_weights(&self) -> Option<&[f64]> {
self.row_loss_weights.as_deref()
}
/// Drop any installed per-row reconstruction weights, returning the term to
/// the exact unweighted (full-pass) path. Used by the #997 structure-search
/// wiring to clear the internal estimation/evaluation mask off the adopted
/// term before the payload reconstruction is read over all rows.
pub fn clear_row_loss_weights(&mut self) {
self.row_loss_weights = None;
}
/// Install the single per-row [`RowMetric`](crate::inference::row_metric::RowMetric)
/// that both the reconstruction likelihood and the isometry gauge read.
/// Installing per-row output-Fisher factors here flips the provenance to
/// `OutputFisher` *and* is the only way the gauge acquires a non-identity
/// weight, so the two inner products cannot diverge. Passing a Euclidean
/// metric (or never calling this) keeps the bit-identical isotropic path.
///
/// The metric's row count and output dimension must match the term.
pub fn set_row_metric(
&mut self,
metric: crate::inference::row_metric::RowMetric,
) -> Result<(), String> {
if metric.n_rows() != self.n_obs() {
return Err(format!(
"SaeManifoldTerm::set_row_metric: metric has {} rows but term has {}",
metric.n_rows(),
self.n_obs()
));
}
if metric.p_out() != self.output_dim() {
return Err(format!(
"SaeManifoldTerm::set_row_metric: metric output dim {} but term has {}",
metric.p_out(),
self.output_dim()
));
}
self.row_metric = Some(metric);
Ok(())
}
/// The installed per-row metric, if any. `None` ⇒ Euclidean / isotropic.
/// Consumed by the gauge wiring (to build the matching `WeightField`) and by
/// Object 4 (to read the [`MetricProvenance`](crate::inference::row_metric::MetricProvenance)).
pub fn row_metric(&self) -> Option<&crate::inference::row_metric::RowMetric> {
self.row_metric.as_ref()
}
/// The per-row inner product the additive diagnostics read through: the
/// installed [`RowMetric`](crate::inference::row_metric::RowMetric) when one
/// was set (output-Fisher harvest present), otherwise a freshly-built
/// Euclidean metric of the term's own `(n_obs, output_dim)` shape. Either way
/// a metric always exists, so the diagnostics are never gated by a flag — the
/// Euclidean fallback is the bit-identical isotropic path.
pub(crate) fn diagnostic_metric(
&self,
) -> Result<crate::inference::row_metric::RowMetric, String> {
match self.row_metric() {
Some(metric) => Ok(metric.clone()),
None => {
crate::inference::row_metric::RowMetric::euclidean(self.n_obs(), self.output_dim())
}
}
}
/// Build the additive post-fit diagnostic report for this fitted term: the
/// two-score per-atom [`AtomTwoLensReport`](crate::inference::atom_lens::AtomTwoLensReport)
/// (presence / behavioral coupling / discrepancy) and the residual-gauge
/// [`ResidualGaugeReport`](crate::terms::sae::identifiability::ResidualGaugeReport)
/// certificate.
///
/// Both reports are read through the same single metric
/// ([`Self::diagnostic_metric`]): under a Euclidean / no-harvest provenance
/// the lens coupling is `None` and the gauge is certified under Euclidean
/// provenance — never an error, never gated by a flag (magic-by-default,
/// mirroring the metric selection itself).
///
/// `per_atom_ard_variances`, when supplied, is one ARD variance vector per
/// atom (length = `latent_dim_k`), threaded into the certificate's
/// equal-ARD-rotation detection. `None` (or a per-atom `None`) ⇒ no ARD prior
/// on that atom. `isometry_pin_active` records whether an isometry gauge
/// penalty was installed on the fit: `false` escalates the certificate to the
/// `diffeomorphism-unpinned` verdict (the honest "no metric pin" statement),
/// exactly as the certificate's own escalation flag specifies.
///
/// Pure read: it never mutates the term, never touches a loss / criterion /
/// penalty / optimizer state.
pub fn fit_diagnostics_report(
&self,
per_atom_ard_variances: Option<&[Option<Array1<f64>>]>,
isometry_pin_active: bool,
reconstruction_dispersion: Option<f64>,
assignments_override: Option<ArrayView2<'_, f64>>,
) -> Result<SaeManifoldFitDiagnostics, String> {
if let Some(view) = assignments_override {
let n = self.n_obs();
let k = self.k_atoms();
if view.dim() != (n, k) {
return Err(format!(
"fit_diagnostics_report: assignments_override shape {:?} must be ({n}, {k})",
view.dim()
));
}
}
let metric = self.diagnostic_metric()?;
let atom_two_lens =
crate::inference::atom_lens::atom_two_lens(self, &metric, assignments_override);
let (certificate_model, streamed_curvature) =
self.to_residual_gauge_model(metric, per_atom_ard_variances, isometry_pin_active)?;
// #998: within-atom gauge families are certified on their EXACT orbits
// in the model's own (decoder, coordinate) parameter space — compensated
// symmetries are data-nulls by construction there, no lowering-error
// calibration involved. This now holds whether or not an isometry pin is
// active:
// * pin INACTIVE ⇒ the orbit verdict is the data residual alone (no
// penalty operator);
// * pin ACTIVE ⇒ the orbit verdict adds the isometry pin's orbit-space
// curvature through an [`OrbitPenaltyOperator`] lowered from the
// atom's second jet `Φ''` (the pullback-metric change along the orbit
// differentiates `J = Φ'B` through `t`). A model-class symmetry that
// preserves the metric stays a certified freedom; a non-isometric
// orbit (a basis not closed under the action) is genuinely pinned.
// The relative-curvature fraction `cost/stiffness²` is invariant to the
// pin strength μ (both faces scale with μ), so the operator is built at a
// canonical unit weight. An atom whose basis exposes no analytic second
// jet supplies no operator and falls back to the data residual — never an
// error. Magic-by-default either way: the choice is derived from the fit,
// never a flag.
let views = self.atom_parameter_views();
let ops: Vec<Option<crate::terms::sae::identifiability::OrbitPenaltyOperator>> =
if isometry_pin_active {
views
.iter()
.map(|view| {
view.as_ref().and_then(|v| {
crate::terms::sae::identifiability::isometry_orbit_penalty_operator(
v, 1.0,
)
})
})
.collect()
} else {
(0..self.k_atoms()).map(|_| None).collect()
};
let residual_gauge = if isometry_pin_active {
// The pin-active path consumes the per-row Jacobian curvature
// directly (the certificate_model retains it under a pin), so route
// through the non-streamed exact entry point.
crate::terms::sae::identifiability::residual_gauge_exact(
&certificate_model,
&views,
&ops,
)?
} else {
let (curvature_gram, root_rows) = streamed_curvature.ok_or_else(|| {
"fit_diagnostics_report: missing streamed residual-gauge curvature for unpinned exact path"
.to_string()
})?;
crate::terms::sae::identifiability::residual_gauge_exact_from_curvature_gram(
&certificate_model,
&views,
&ops,
curvature_gram,
root_rows,
)?
};
// #1097 / #1103: per-atom Riesz-debiased functionals and the any-n-valid
// split-LRT smooth-structure e-value (non-constant vs constant inner
// decoder), read straight off the certificate model — which carries
// each atom's `inner_fit` snapshot when the caller harvested it via
// [`Self::set_atom_inner_fits`] before this report. Atoms without a
// harvested inner fit degrade their inference fields to `None` inside
// `atom_inference_reports`, so this is always populated (one entry per
// atom) and never gated by a flag.
let atom_inference =
crate::terms::sae::identifiability::atom_inference_reports(&certificate_model);
Ok(SaeManifoldFitDiagnostics {
atom_two_lens,
residual_gauge,
incoherence_report: match reconstruction_dispersion.or(self.certificate_dispersion) {
Some(dispersion) => Some(dictionary_incoherence_report_with_dispersion(
self, dispersion,
)?),
None => None,
},
atom_inference,
})
}
/// Build the trust-diagnostics producer for the Python `diagnostics` block.
///
/// `assignments` is supplied by the payload assembly site so top-k projection,
/// when requested, is reflected in coverage/frequency and in the tangent
/// spectra. The active threshold is shared with the atom lens so all
/// assignment-support diagnostics agree on what "active" means.
pub fn trust_diagnostics_report(
&self,
assignments: ArrayView2<'_, f64>,
) -> Result<SaeTrustDiagnostics, String> {
let n = self.n_obs();
let k_atoms = self.k_atoms();
if assignments.dim() != (n, k_atoms) {
return Err(format!(
"trust_diagnostics_report: assignments shape {:?} must be ({n}, {k_atoms})",
assignments.dim()
));
}
if !assignments.iter().all(|v| v.is_finite()) {
return Err("trust_diagnostics_report: assignments must be finite".to_string());
}
let metric = self.diagnostic_metric()?;
let active_threshold = crate::inference::atom_lens::SAE_TRUST_ACTIVE_MASS_FLOOR;
let mut atoms = Vec::with_capacity(k_atoms);
let mut atom_trust = Vec::with_capacity(k_atoms);
for (atom_idx, atom) in self.atoms.iter().enumerate() {
let mut active_token_count = 0usize;
let mut activation_sum = 0.0_f64;
for row in 0..n {
let mass = assignments[[row, atom_idx]];
activation_sum += mass;
if mass > active_threshold {
active_token_count += 1;
}
}
let coverage = if n > 0 {
active_token_count as f64 / n as f64
} else {
0.0
};
let activation_frequency = if n > 0 {
activation_sum / n as f64
} else {
0.0
};
let (sigma_min_tangent, sigma_max_tangent) = self
.atom_tangent_spectrum_from_assignments(
atom_idx,
assignments,
&metric,
active_threshold,
)?;
let tangent_condition_score = if sigma_max_tangent > 0.0 {
(sigma_min_tangent / sigma_max_tangent).clamp(0.0, 1.0)
} else {
0.0
};
let trust_score = tangent_condition_score;
atom_trust.push(trust_score);
atoms.push(SaeAtomTrustDiagnostics {
trust_score,
sigma_min_tangent,
sigma_max_tangent,
tangent_condition_score,
coverage,
activation_frequency,
untyped: matches!(atom.basis_kind, SaeAtomBasisKind::Precomputed(_)),
active_token_count,
});
}
Ok(SaeTrustDiagnostics { atom_trust, atoms })
}
pub(crate) fn atom_tangent_spectrum_from_assignments(
&self,
atom_idx: usize,
assignments: ArrayView2<'_, f64>,
metric: &crate::inference::row_metric::RowMetric,
active_threshold: f64,
) -> Result<(f64, f64), String> {
let atom = &self.atoms[atom_idx];
let d = atom.latent_dim;
let p = self.output_dim();
if d == 0 || p == 0 {
return Ok((0.0, 0.0));
}
let mut gram = Array2::<f64>::zeros((d, d));
let mut active_mass_sum = 0.0_f64;
let mut jac_row = vec![0.0_f64; p * d];
for row in 0..self.n_obs() {
let mass = assignments[[row, atom_idx]];
if !(mass > active_threshold) {
continue;
}
active_mass_sum += mass;
for axis in 0..d {
let start = axis;
let mut tangent = vec![0.0_f64; p];
atom.fill_decoded_derivative_row(row, axis, &mut tangent);
for out in 0..p {
jac_row[out * d + start] = tangent[out];
}
}
let row_pullback = metric.pullback(row, &jac_row, d);
for axis_a in 0..d {
for axis_b in 0..=axis_a {
gram[[axis_a, axis_b]] += mass * row_pullback[[axis_a, axis_b]];
}
}
jac_row.fill(0.0);
}
if !(active_mass_sum > 0.0) {
return Ok((0.0, 0.0));
}
let inv_mass = 1.0 / active_mass_sum;
for axis_a in 0..d {
for axis_b in 0..=axis_a {
let value = gram[[axis_a, axis_b]] * inv_mass;
gram[[axis_a, axis_b]] = value;
gram[[axis_b, axis_a]] = value;
}
}
let (evals, _) = gram.eigh(Side::Lower).map_err(|e| {
format!(
"trust_diagnostics_report: atom {atom_idx} tangent eigendecomposition failed: {e}"
)
})?;
let mut sigma_min = f64::INFINITY;
let mut sigma_max = 0.0_f64;
for value in evals.iter().copied() {
let clamped = value.max(0.0);
let sigma = clamped.sqrt();
sigma_min = sigma_min.min(sigma);
sigma_max = sigma_max.max(sigma);
}
if sigma_min.is_finite() {
Ok((sigma_min, sigma_max))
} else {
Ok((0.0, 0.0))
}
}
/// Per-atom exact parameter-space views for the #998 certificate path:
/// the basis values / first-derivative jet, decoder coefficients, latent
/// coordinates, and assignment mass each atom was actually fitted with.
/// Sphere atoms get `None` (their chart's group action is nonlinear, so
/// the exact-orbit realisation does not apply and they stay on the frame
/// path), as does any atom whose coordinate chart width disagrees with its
/// latent dimension (a structurally inconsistent atom must not masquerade
/// as exactly certified).
pub(crate) fn atom_parameter_views(
&self,
) -> Vec<Option<crate::terms::sae::identifiability::AtomParameterView>> {
let assignments = self.assignment.assignments();
let n = self.n_obs();
self.atoms
.iter()
.enumerate()
.map(|(k, atom)| {
if matches!(atom.basis_kind, SaeAtomBasisKind::Sphere) {
return None;
}
let coords = self.assignment.coords[k].as_matrix().to_owned();
if coords.nrows() != n || coords.ncols() != atom.latent_dim {
return None;
}
let mut activations = Array1::<f64>::zeros(n);
for row in 0..n {
activations[row] = assignments[[row, k]];
}
// Second jet Φ'' (#998): supplied when the atom's evaluator
// exposes an analytic Hessian, so a pin-active fit can lower its
// orbit-space isometry penalty operator (the metric-change of the
// pullback gram differentiates Φ' through t). Absent ⇒ the orbit
// verdict stays on the data residual / no-pin path, never an
// error.
let basis_second_jet = atom
.basis_evaluator
.as_ref()
.and_then(|evaluator| evaluator.second_jet_dyn(coords.view()))
.and_then(|res| res.ok());
Some(crate::terms::sae::identifiability::AtomParameterView {
basis_values: atom.basis_values.clone(),
basis_jacobian: atom.basis_jacobian.clone(),
decoder: atom.decoder_coefficients.clone(),
coords,
activations,
basis_second_jet,
})
})
.collect()
}
/// Lower this fitted term into the self-contained
/// [`FittedSaeManifold`](crate::terms::sae::identifiability::FittedSaeManifold) the
/// residual-gauge certificate consumes.
///
/// The certificate's parameter space is the per-atom decoder **frame** — the
/// `(output_dim, latent_dim)` image of the atom's latent axes in output space.
/// We realise it as the active-mass-weighted mean decoder tangent
/// `frame_k[:, a] = (Σ_n a_{nk} · ∂g_k/∂t_a(n)) / Σ_n a_{nk}` over the atom's
/// active rows (the centroid decoder Jacobian columns the certificate docs
/// name). The per-row pinning Jacobian block `J_n ∈ ℝ^{p × param_dim}` is the
/// assignment-weighted per-row decoder tangent placed at each atom's frame
/// slot: column `(k, i, a)` of `J_n` is `a_{nk} · ∂g_k/∂t_a(n)[i]` — exactly
/// the directions the reconstruction data gives cost to, in the same metric
/// the fit used (whitened by the certificate through `RowMetric`).
///
/// The flattened frame layout matches the certificate's
/// `vec(frame_0) ⊕ vec(frame_1) ⊕ …`, row-major within each frame
/// (`frame_k[i, a]` at offset `atom_offset(k) + i·latent_dim_k + a`).
pub(crate) fn to_residual_gauge_model(
&self,
metric: crate::inference::row_metric::RowMetric,
per_atom_ard_variances: Option<&[Option<Array1<f64>>]>,
isometry_pin_active: bool,
) -> Result<
(
crate::terms::sae::identifiability::FittedSaeManifold,
Option<(Array2<f64>, usize)>,
),
String,
> {
use crate::terms::sae::identifiability::{AtomTopology, FittedAtom, FittedSaeManifold};
let n = self.n_obs();
let p = self.output_dim();
let k = self.k_atoms();
let assignments = self.assignment.assignments();
// Per-atom frame `(p, d)` = active-mass-weighted mean decoder tangent,
// and the flattened-frame column offset bookkeeping for the joint
// parameter vector (`vec(frame_0) ⊕ …`, row-major within each frame).
let mut fitted_atoms: Vec<FittedAtom> = Vec::with_capacity(k);
let mut atom_offsets: Vec<usize> = Vec::with_capacity(k);
let mut atom_axis_dim: Vec<usize> = Vec::with_capacity(k);
let mut cursor = 0usize;
for (atom_idx, atom) in self.atoms.iter().enumerate() {
let d = atom.latent_dim;
let topology = match (&atom.basis_kind, d) {
(SaeAtomBasisKind::Periodic, 1) | (SaeAtomBasisKind::Torus, 1) => {
AtomTopology::Circle
}
(SaeAtomBasisKind::Periodic, _) | (SaeAtomBasisKind::Torus, _) => {
AtomTopology::Torus { latent_dim: d }
}
(SaeAtomBasisKind::Sphere, _) => AtomTopology::Sphere,
// `Cylinder` (`S¹ × ℝ`) has exactly one continuous gauge: the
// rotation (shift) of the periodic axis. The unbounded line axis
// carries no rotational gauge, and its translation is already
// pinned by the design's constant column — so the identifiability
// gauge is that of a single circle. Fixing it as `Torus` would
// over-impose a second (nonexistent) circle shift; fixing it as
// `EuclideanPatch { 2 }` would over-impose a frame rotation
// mixing the periodic and linear axes. `Circle` fixes the one
// real continuous gauge and leaves the linear axis ungauged.
(SaeAtomBasisKind::Cylinder, _) => AtomTopology::Circle,
(
SaeAtomBasisKind::Linear
| SaeAtomBasisKind::Duchon
| SaeAtomBasisKind::EuclideanPatch
| SaeAtomBasisKind::Poincare
| SaeAtomBasisKind::Precomputed(_),
_,
) => AtomTopology::EuclideanPatch { latent_dim: d },
};
let mut frame = Array2::<f64>::zeros((p, d));
let mut active_mass = 0.0_f64;
let mut tangent = vec![0.0_f64; p];
for row in 0..n {
let a_nk = assignments[[row, atom_idx]];
if !(a_nk > 0.0) {
continue;
}
active_mass += a_nk;
for axis in 0..d {
atom.fill_decoded_derivative_row(row, axis, &mut tangent);
for i in 0..p {
frame[[i, axis]] += a_nk * tangent[i];
}
}
}
if active_mass > 0.0 {
let inv = 1.0 / active_mass;
frame.mapv_inplace(|v| v * inv);
}
// #995 lowering-error scale: mass-weighted relative dispersion of
// the per-row tangents around the mean frame just built,
// Σ_n a_n Σ_ax ‖t_ax(n) − frame[:,ax]‖² / Σ_n a_n Σ_ax ‖t_ax(n)‖².
// 0 ⇒ the frame represents every active row exactly (flat
// decoder); → 1 ⇒ the tangent field disperses so strongly (e.g. a
// full circle, whose tangents average out) that the mean-frame
// compression cannot distinguish gauge motion from curvature. The
// certificate calibrates its per-generator verdict tolerance to
// this scale so it never claims a pin it cannot resolve.
let mut disp_num = 0.0_f64;
let mut disp_den = 0.0_f64;
for row in 0..n {
let a_nk = assignments[[row, atom_idx]];
if !(a_nk > 0.0) {
continue;
}
for axis in 0..d {
atom.fill_decoded_derivative_row(row, axis, &mut tangent);
for i in 0..p {
let dev = tangent[i] - frame[[i, axis]];
disp_num += a_nk * dev * dev;
disp_den += a_nk * tangent[i] * tangent[i];
}
}
}
let lowering_error = if disp_den > 0.0 {
(disp_num / disp_den).clamp(0.0, 1.0)
} else {
0.0
};
let ard_variances = per_atom_ard_variances
.and_then(|all| all.get(atom_idx))
.and_then(|opt| opt.clone())
.filter(|v| v.len() == d);
fitted_atoms.push(FittedAtom {
name: atom.name.clone(),
topology,
frame,
ard_variances,
lowering_error,
// #1019: post-fit chart canonicalization (arc length for
// d = 1, isometry-flow for d = 2 torus, flat-reference
// isometry-flow for d = 2 free/patch, round-sphere
// conformal-boost flow for d = 2 sphere atoms) pins the chart;
// the certificate downgrades this atom's chart freedom to the
// finite isometry group with PinnedByCanonicalization
// provenance.
chart_canonicalized: atom.chart_canonicalized
&& (d == 1
|| (d == 2
&& matches!(
atom.basis_kind,
SaeAtomBasisKind::Torus
| SaeAtomBasisKind::Linear
| SaeAtomBasisKind::Duchon
| SaeAtomBasisKind::EuclideanPatch
| SaeAtomBasisKind::Sphere
))),
// #1097 / #1103: the per-atom inner-decoder-smooth snapshot,
// attached when the post-fit harness has run
// [`Self::set_atom_inner_fits`] (it needs the reconstruction
// target Z, dropped from the objective at fit end). `None` on a
// bare certificate-only model, or for a degenerate atom whose
// inner Hessian was not SPD.
inner_fit: self
.atom_inner_fits
.as_ref()
.and_then(|fits| fits.get(atom_idx))
.and_then(|slot| slot.clone()),
});
atom_offsets.push(cursor);
atom_axis_dim.push(d);
cursor += p * d;
}
let param_dim = cursor;
// Per-row pinning Jacobian `J_n ∈ ℝ^{p × param_dim}` flattened row-major
// (`J_n[i, c] = jacobian_rows[n][i · param_dim + c]`). Column `(k, i', a)`
// of `J_n` is `a_{nk} · ∂g_k/∂t_a(n)[i']` placed at the atom-k frame slot
// and read out on output coordinate `i = i'` (a frame perturbation of
// output `i'` moves only the row's output coordinate `i'`).
//
// The pinned certificate still consumes the legacy row-block contract.
// The unpinned exact path consumes only `RᵀR`, so stream each transient
// row Jacobian through the metric whitening and discard it immediately.
let (jacobian_rows, streamed_curvature) = if isometry_pin_active {
let mut jacobian_rows: Vec<Vec<f64>> = Vec::with_capacity(n);
let mut tangent = vec![0.0_f64; p];
for row in 0..n {
let mut j_flat = vec![0.0_f64; p * param_dim];
for (atom_idx, atom) in self.atoms.iter().enumerate() {
let a_nk = assignments[[row, atom_idx]];
if !(a_nk > 0.0) {
continue;
}
let d = atom_axis_dim[atom_idx];
let base = atom_offsets[atom_idx];
for axis in 0..d {
atom.fill_decoded_derivative_row(row, axis, &mut tangent);
for i in 0..p {
// Frame coordinate `(k, i, axis)` sits at column
// `base + i·d + axis`; it sources output coordinate `i`.
j_flat[i * param_dim + base + i * d + axis] += a_nk * tangent[i];
}
}
}
jacobian_rows.push(j_flat);
}
(jacobian_rows, None)
} else {
let streamed = self.residual_gauge_streamed_data_curvature(
&metric,
&atom_offsets,
&atom_axis_dim,
param_dim,
)?;
(Vec::new(), Some(streamed))
};
// Isometry-penalty curvature root over the frame parameter space. When
// the isometry gauge pin is active it gives curvature along every fitted
// frame direction (it resists deviation of the decoder image from its
// arc-length parameterization), so its row space is the span of the
// per-atom frame columns: one root row per `(k, axis)` carrying that
// atom's frame column at the atom's frame slot. Empty (`0 × param_dim`)
// when the pin is inactive — exactly the certificate's escalation
// condition to `diffeomorphism-unpinned`.
let isometry_penalty_root = if isometry_pin_active && param_dim > 0 {
let mut root_rows: Vec<Array1<f64>> = Vec::new();
for (atom_idx, fitted) in fitted_atoms.iter().enumerate() {
let d = atom_axis_dim[atom_idx];
let base = atom_offsets[atom_idx];
for axis in 0..d {
let mut r = Array1::<f64>::zeros(param_dim);
let mut any = false;
for i in 0..p {
let v = fitted.frame[[i, axis]];
if v != 0.0 {
any = true;
}
r[base + i * d + axis] = v;
}
if any {
root_rows.push(r);
}
}
}
let mut root = Array2::<f64>::zeros((root_rows.len(), param_dim));
for (ri, r) in root_rows.iter().enumerate() {
root.row_mut(ri).assign(r);
}
root
} else {
Array2::<f64>::zeros((0, param_dim))
};
Ok((
FittedSaeManifold {
atoms: fitted_atoms,
jacobian_rows,
isometry_penalty_root,
metric,
},
streamed_curvature,
))
}
pub(crate) fn residual_gauge_streamed_data_curvature(
&self,
metric: &crate::inference::row_metric::RowMetric,
atom_offsets: &[usize],
atom_axis_dim: &[usize],
param_dim: usize,
) -> Result<(Array2<f64>, usize), String> {
let n = self.n_obs();
let p = self.output_dim();
if metric.p_out() != p {
return Err(format!(
"residual_gauge_streamed_data_curvature: metric output dim {} but term has {p}",
metric.p_out()
));
}
let rank = metric.metric_rank();
let mut gram = Array2::<f64>::zeros((param_dim, param_dim));
if param_dim == 0 || n == 0 || rank == 0 {
return Ok((gram, n * rank));
}
let assignments = self.assignment.assignments();
let mut tangent = vec![0.0_f64; p];
let mut j_flat = vec![0.0_f64; p * param_dim];
let mut root_row = Array1::<f64>::zeros(param_dim);
for row in 0..n {
j_flat.fill(0.0);
for (atom_idx, atom) in self.atoms.iter().enumerate() {
let a_nk = assignments[[row, atom_idx]];
if !(a_nk > 0.0) {
continue;
}
let d = atom_axis_dim[atom_idx];
let base = atom_offsets[atom_idx];
for axis in 0..d {
atom.fill_decoded_derivative_row(row, axis, &mut tangent);
for i in 0..p {
j_flat[i * param_dim + base + i * d + axis] += a_nk * tangent[i];
}
}
}
if metric.drives_gauge() {
for r in 0..rank {
root_row.fill(0.0);
for c in 0..param_dim {
let mut acc = 0.0_f64;
for i in 0..p {
acc += metric.factor_entry(row, i, r) * j_flat[i * param_dim + c];
}
root_row[c] = acc;
}
let row_slice = root_row.as_slice().ok_or_else(|| {
"residual_gauge_streamed_data_curvature: non-contiguous root row"
.to_string()
})?;
Self::accumulate_residual_gauge_gram_row(&mut gram, row_slice);
}
} else {
for i in 0..p {
let start = i * param_dim;
let end = start + param_dim;
Self::accumulate_residual_gauge_gram_row(&mut gram, &j_flat[start..end]);
}
}
}
for a in 0..param_dim {
for b in 0..a {
gram[[b, a]] = gram[[a, b]];
}
}
Ok((gram, n * rank))
}
pub(crate) fn accumulate_residual_gauge_gram_row(gram: &mut Array2<f64>, row: &[f64]) {
for a in 0..row.len() {
let va = row[a];
if va == 0.0 {
continue;
}
for b in 0..=a {
let vb = row[b];
if vb != 0.0 {
gram[[a, b]] += va * vb;
}
}
}
}
pub fn set_temperature_schedule(
&mut self,
sched: GumbelTemperatureSchedule,
) -> Result<(), String> {
sched.validate()?;
self.assignment
.mode
.set_temperature(sched.current_tau(sched.iter_count))?;
self.temperature_schedule = Some(sched);
Ok(())
}
pub(crate) fn advance_temperature_schedule(&mut self) -> Result<Option<f64>, String> {
let Some(schedule) = self.temperature_schedule.as_mut() else {
return Ok(None);
};
schedule.validate()?;
let tau = schedule.step();
self.assignment.mode.set_temperature(tau)?;
Ok(Some(tau))
}
pub fn n_obs(&self) -> usize {
self.assignment.n_obs()
}
pub fn k_atoms(&self) -> usize {
self.atoms.len()
}
/// Auto-derived in-core vs streaming plan for SAE Arrow-Schur work.
///
/// This is intentionally not user-configurable: the route follows the
/// retained full-batch working-set estimate and the currently selected GPU
/// memory budget when CUDA is usable, otherwise a conservative host budget.
pub fn streaming_plan(&self) -> SaeStreamingPlan {
let n_obs = self.n_obs();
let total_basis: usize = self.atoms.iter().map(|atom| atom.basis_size()).sum();
let d_max = self
.atoms
.iter()
.map(|atom| atom.latent_dim)
.max()
.unwrap_or(0);
let border_dim = if self.any_frame_active() {
self.factored_border_dim()
} else {
self.beta_dim()
};
sae_streaming_plan_for_shape(n_obs, total_basis, self.k_atoms(), d_max, border_dim)
}
/// Construction-time validation: every Psi-tier analytic penalty in the
/// registry must be dispatchable into the SAE arrow-Schur row layout.
///
/// Two invariants are enforced upfront so the dispatch loop in
/// `add_sae_analytic_penalty_contributions` is total (no runtime
/// "unsupported penalty" fallthrough, no per-call K-gating):
///
/// 1. Every Psi-tier penalty is either in [`sae_penalty_is_row_block_supported`],
/// or `NuclearNorm` (which is redirected to the per-atom decoder (β) block
/// rather than the coord "t" row block). Assignment sparsity penalties
/// (`IBPAssignment`, `SoftmaxAssignmentSparsity`) are refused because the SAE
/// term already owns them through its built-in assignment path
/// (`loss.assignment_sparsity`). Penalty kinds with cross-row structure
/// (`TotalVariation`, `Monotonicity`, `BlockSparsity`,
/// `IvaeRidgeMeanGauge`, `Orthogonality`, `NestedPrefix`,
/// `SheafConsistency`) cannot be expressed in the SAE row-block layout
/// and are refused here.
///
/// 2. If any Psi-tier row-block penalty is present, every atom shares
/// the same coord latent dim. The current registry model carries one
/// `latent_dim` per descriptor (the "t" latent block declares one
/// `d` value); per-atom dispatch with heterogeneous `d_k` would
/// require per-atom registry entries or per-kind in-place
/// reshaping. Mixed-d row-block fits are rejected with an actionable
/// error pointing at the configuration mismatch.
///
/// The K=1 case trivially satisfies (2). Beta-tier and rho-tier
/// penalties are not constrained here.
pub(crate) fn validate_analytic_penalty_registry(
&self,
registry: &AnalyticPenaltyRegistry,
) -> Result<(), String> {
let mut row_block_penalty_present = false;
for penalty in ®istry.penalties {
if penalty.tier() != PenaltyTier::Psi {
continue;
}
if matches!(
penalty,
AnalyticPenaltyKind::IBPAssignment(_)
| AnalyticPenaltyKind::SoftmaxAssignmentSparsity(_)
) {
return Err(format!(
"SAE-manifold term refuses analytic penalty {:?}: assignment sparsity \
is owned by the built-in SAE assignment path (loss.assignment_sparsity). \
Registering it would double-count the objective and gradient",
penalty.name()
));
}
// NuclearNorm is redirected to the per-atom decoder (β) block in
// `add_sae_beta_penalty` (it penalizes each atom's decoder matrix
// singular spectrum, i.e. its embedding rank), so it bypasses the
// coord "t" row-block requirement below.
if matches!(penalty, AnalyticPenaltyKind::NuclearNorm(_)) {
continue;
}
if !sae_penalty_is_row_block_supported(penalty) {
return Err(format!(
"SAE-manifold term refuses analytic penalty {:?}: this kind \
has cross-row structure and cannot be expressed in the \
arrow-Schur row layout. Use only row-block-supported \
coord penalties (ARD, BlockOrthogonality, \
Sparsity/TopK/JumpReLU, RowPrecisionPrior, \
ParametricRowPrecisionPrior, ScadMcp, Isometry) on the \
coord latent block, or move the penalty to a non-SAE \
term",
penalty.name()
));
}
row_block_penalty_present = true;
}
if row_block_penalty_present {
let mut dims = self.assignment.coords.iter().map(|c| c.latent_dim());
if let Some(first) = dims.next() {
if let Some(mismatch) = dims.find(|d| *d != first) {
return Err(format!(
"SAE-manifold term refuses row-block analytic penalty: \
atoms have heterogeneous coord latent dims (saw {first} \
and {mismatch}). Row-block penalties (ARD, \
BlockOrthogonality, ...) target the unified \"t\" \
latent block whose declared `d` matches one shape; \
per-atom dispatch with mixed `d_k` would silently \
truncate or expand axes. Configure all atoms with the \
same `atom_dim`, or split the row-block penalty into \
per-atom descriptors keyed to per-atom latent blocks"
));
}
}
}
Ok(())
}
pub fn output_dim(&self) -> usize {
self.atoms[0].output_dim()
}
pub fn beta_dim(&self) -> usize {
let p = self.output_dim();
self.atoms.iter().map(|a| a.basis_size() * p).sum()
}
pub(crate) fn take_border_hbb_workspace(&mut self, border_dim: usize) -> Array2<f64> {
let mut workspace =
std::mem::replace(&mut self.border_hbb_workspace, Array2::<f64>::zeros((0, 0)));
if workspace.dim() != (border_dim, border_dim) {
workspace = Array2::<f64>::zeros((border_dim, border_dim));
} else {
workspace.fill(0.0);
}
workspace
}
pub(crate) fn reclaim_border_hbb_workspace(&mut self, sys: &mut ArrowSchurSystem) {
let workspace = std::mem::replace(&mut sys.hbb, Array2::<f64>::zeros((0, 0)));
self.border_hbb_workspace = workspace;
}
/// Factored arrow-Schur border dimension `Σ_k M_k · r_k` (issue #972): the
/// number of decoder coordinates the border actually carries once the
/// low-rank Grassmann frames are profiled out. Atoms with no active frame
/// contribute their full `M_k · p` (`r_k == p`), so on the all-full-`B` path
/// this equals [`Self::beta_dim`]. The border Cholesky / evidence log-det
/// scale with THIS count, not `beta_dim`.
pub fn factored_border_dim(&self) -> usize {
self.atoms.iter().map(|a| a.border_coeff_count()).sum()
}
/// Total profiled-out Grassmann manifold dimension `Σ_k r_k·(p − r_k)` across
/// all active frames (issue #972). This is the count of decoder-frame degrees
/// of freedom estimated OUTSIDE the border by closed-form polar steps, and it
/// must enter the Laplace evidence dimension accounting (evidence honesty):
/// the profiled frame is a MAP point on `∏_k Gr(r_k, p)`, contributing this
/// many free dimensions to the model. `0` when every atom is on the full-`B`
/// path. Threaded into [`Self::reml_occam_term`].
pub fn grassmann_evidence_dimension(&self) -> usize {
self.atoms
.iter()
.map(|a| a.frame_manifold_dimension())
.sum()
}
/// True iff any atom has an active low-rank Grassmann frame (issue #972).
pub fn frames_active(&self) -> bool {
self.atoms.iter().any(|a| a.decoder_frame.is_some())
}
/// Alias of [`Self::frames_active`] (issue #972 / #977 T1): the predicate the
/// assembly / step-lift branch on to decide whether the β-tier is built in
/// the factored coordinate layout. Named to read as the question
/// "is the factored path engaged?" at its call sites.
pub fn any_frame_active(&self) -> bool {
self.frames_active()
}
/// Per-atom column offsets of the *factored* border (issue #972 / #977 T1):
/// the running prefix sum of `M_k · r_k`, one entry per atom (the same
/// convention as [`Self::beta_offsets`]). This is the start of each atom's
/// `C_k` block in the reduced border vector; on the all-full-`B` path it
/// equals `beta_offsets`. Distinct from [`Self::factored_border_offsets`]
/// only in name (both compute the identical prefix sum) — this method is the
/// one the frame transform reads, mirroring `beta_offsets` at the call site.
pub fn factored_beta_offsets(&self) -> Vec<usize> {
self.factored_border_offsets()
}
/// Frame output matrix `U_k ∈ St(p, r_k)` for atom `k` (issue #972 / #977 T1).
/// Returns the active frame `U_k` (`p × r_k`) when atom `k` is framed, else
/// the identity `I_p` (the `r_k == p`, `U_k == I_p` full-`B` special case) so
/// the projection / lift code is uniform across a mixed dictionary.
pub fn frame_output_matrix(&self, atom_idx: usize) -> Array2<f64> {
let atom = &self.atoms[atom_idx];
match &atom.decoder_frame {
Some(frame) => frame.frame().to_owned(),
None => Array2::<f64>::eye(atom.output_dim()),
}
}
/// Per-pair frame factor `W_{ij} = U_iᵀ U_j` (`r_i × r_j`) used as the output
/// factor of the factored data β-Hessian block `G_{ij} ⊗ W_{ij}` (issue #972
/// / #977 T1). When both atoms are framed this is the dense principal-angle
/// cosine matrix between the two frames; for `i == j` with an orthonormal
/// frame it is exactly `I_{r_i}`; for any un-framed atom the corresponding
/// `U` is `I_p`, so a same-atom un-framed pair gives `I_p` (the clean full-`B`
/// `G ⊗ I_p` collapse) and a framed/un-framed cross pair gives the rectangular
/// `U_iᵀ` / `U_j` overlap.
pub fn frame_cross_factor(&self, atom_i: usize, atom_j: usize) -> Array2<f64> {
let ui = self.frame_output_matrix(atom_i);
let uj = self.frame_output_matrix(atom_j);
// `U_iᵀ U_j`: `(r_i × p) · (p × r_j)`. `fast_atb` forms `U_iᵀ U_j` directly.
fast_atb(&ui, &uj)
}
/// Per-atom column offsets of the *factored* border (issue #972): the
/// running prefix sum of `M_k · r_k`. The analogue of [`Self::beta_offsets`]
/// for the reduced coordinate layout — atom `k`'s `C_k` occupies
/// `[factored_border_offsets()[k] .. + M_k·r_k)`. On the full-`B` path this
/// equals `beta_offsets`.
pub fn factored_border_offsets(&self) -> Vec<usize> {
let mut out = Vec::with_capacity(self.k_atoms());
let mut cursor = 0usize;
for atom in &self.atoms {
out.push(cursor);
cursor += atom.border_coeff_count();
}
out
}
/// Assemble the factored border coordinate vector `C = [vec(C_1); …; vec(C_K)]`
/// in row-major `C_k[m, j] → C[off_k + m·r_k + j]` layout (issue #972).
///
/// This is the reduced state the arrow-Schur border carries when frames are
/// active: its length is [`Self::factored_border_dim`] (`Σ M_k·r_k`), the
/// border-size invariant verified by [`grassmann_assert_border_dim_invariant`].
/// Atoms
/// without an active frame contribute their full `vec(B_k)` (their `r_k == p`
/// coordinates are the decoder itself), so on the all-full-`B` path this
/// reproduces [`Self::flatten_beta`].
pub fn flatten_factored_border(&self) -> Result<Array1<f64>, String> {
let offsets = self.factored_border_offsets();
let mut out = Array1::<f64>::zeros(self.factored_border_dim());
for (atom_idx, atom) in self.atoms.iter().enumerate() {
let off = offsets[atom_idx];
let r = atom.border_frame_rank();
let m = atom.basis_size();
let coords = match atom.factored_coordinates()? {
Some(c) => c,
// Full-`B` path: the decoder itself is the coordinate matrix.
None => atom.decoder_coefficients.clone(),
};
for basis_col in 0..m {
for j in 0..r {
out[off + basis_col * r + j] = coords[[basis_col, j]];
}
}
}
Ok(out)
}
/// Scatter a factored border coordinate vector `C` (length
/// [`Self::factored_border_dim`]) back into the per-atom decoders, refreshing
/// each `decoder_coefficients = C_k · U_kᵀ` so the full-`B` consumers stay
/// consistent after a factored border solve (issue #972). The inverse of
/// [`Self::flatten_factored_border`].
pub fn scatter_factored_border(&mut self, border: ArrayView1<'_, f64>) -> Result<(), String> {
let expected = self.factored_border_dim();
if border.len() != expected {
return Err(format!(
"SaeManifoldTerm::scatter_factored_border: border length {} must equal \
factored border dim {expected}",
border.len()
));
}
let offsets = self.factored_border_offsets();
for atom_idx in 0..self.atoms.len() {
let off = offsets[atom_idx];
let (r, m, has_frame) = {
let atom = &self.atoms[atom_idx];
(
atom.border_frame_rank(),
atom.basis_size(),
atom.decoder_frame.is_some(),
)
};
let mut coords = Array2::<f64>::zeros((m, r));
for basis_col in 0..m {
for j in 0..r {
coords[[basis_col, j]] = border[off + basis_col * r + j];
}
}
if has_frame {
self.atoms[atom_idx].set_factored_coordinates(coords.view())?;
} else {
// Full-`B` path: the coordinates ARE the decoder.
self.atoms[atom_idx].decoder_coefficients = coords;
}
}
Ok(())
}
/// Auto-derive and install low-rank Grassmann decoder frames across all
/// atoms (issue #972) — magic-by-default, no flag. Each atom independently
/// activates its frame iff the factorization materially shrinks its border
/// (see [`SaeManifoldAtom::maybe_activate_decoder_frame`]). Returns the
/// number of atoms that activated a frame. Idempotent: re-running re-derives
/// each frame from the current decoder.
///
/// The decision keys on the *frontier* regime the issue targets: at large
/// ambient `p` the full border `Σ M_k · p` reaches `10^7`–`10^8` and the
/// border Cholesky dies, while the decoder's effective column rank `r` stays
/// `≪ p`. Small-`p` atoms (where `r` cannot beat the activation margin)
/// keep the bit-for-bit full-`B` path, so the small-model evidence is
/// unchanged (verified by `factored_evidence_matches_full_b_at_small_p`).
pub fn auto_activate_decoder_frames(&mut self) -> Result<usize, String> {
let mut activated = 0usize;
for atom in &mut self.atoms {
let expected_rank = atom.decoder_frame_activation_rank()?;
match (
expected_rank,
atom.decoder_frame.as_ref().map(GrassmannFrame::rank),
) {
(Some(expected), Some(current)) if expected == current => {
continue;
}
(None, Some(_)) => {
atom.deactivate_decoder_frame();
continue;
}
(None, None) => {
continue;
}
(Some(_), _) => {}
}
if atom.maybe_activate_decoder_frame()?.is_some() {
activated += 1;
}
}
Ok(activated)
}
/// Reconcile decoder-frame activation before a fit entry point. The
/// user-facing `auto_activate_decoder_frames` contract returns only newly
/// installed frames; this helper enforces the stronger invariant the large-p
/// solver needs: every atom whose current decoder satisfies the activation
/// predicate has an active frame after the pass.
pub(crate) fn ensure_decoder_frames_active_for_current_decoder(
&mut self,
) -> Result<(), String> {
self.auto_activate_decoder_frames()?;
for (atom_idx, atom) in self.atoms.iter().enumerate() {
let expected_rank = atom.decoder_frame_activation_rank()?;
if let Some(expected_rank) = expected_rank {
match atom.decoder_frame.as_ref() {
Some(frame) if frame.rank() == expected_rank => {}
Some(frame) => {
return Err(format!(
"SaeManifoldTerm::ensure_decoder_frames_active_for_current_decoder: \
atom {atom_idx} frame rank {} must equal audited rank {expected_rank}",
frame.rank()
));
}
None => {
return Err(format!(
"SaeManifoldTerm::ensure_decoder_frames_active_for_current_decoder: \
atom {atom_idx} has audited rank {expected_rank} but no active frame"
));
}
}
} else if atom.decoder_frame.is_some() {
return Err(format!(
"SaeManifoldTerm::ensure_decoder_frames_active_for_current_decoder: \
atom {atom_idx} kept a frame after the full-B predicate won"
));
}
}
Ok(())
}
/// Closed-form streaming POLAR refresh of every ACTIVE decoder frame from the
/// current data evidence (issue #972 / #977 T1) — the U-block of the
/// alternating block-coordinate ascent that complements the border's
/// C-block Newton step.
///
/// For each framed atom `k` we accumulate the `p × r_k` cross-moment
/// `A_k = Σ_n a_{n,k} · e_{n,k} · ĉ_{n,k}ᵀ`,
/// where `e_{n,k} = z_n − Σ_{k'≠k} a_{n,k'}·decoded_{k'}(n)` is the row's
/// partial reconstruction residual (everything except atom `k`) and
/// `ĉ_{n,k} = Φ_k(t_n)·C_k ∈ ℝ^{r_k}` is atom `k`'s in-span decoded
/// coordinate. The polar factor `U_new = polar(A_k)` is the closed-form MAP
/// frame on `Gr(r_k, p)` given the C-coordinates held fixed — the same
/// `O(p r²)` thin SVD the issue prescribes, run OUTSIDE the border. The frame
/// is then re-installed and the decoder re-projected onto it so the
/// authoritative `B_k = C_k U_newᵀ` and the `(C_k, U_new)` pair stay
/// consistent (a no-op in span for a truly rank-`r` atom). Un-framed atoms
/// are skipped. Returns the number of frames refreshed.
pub(crate) fn refresh_active_frames_from_data(
&mut self,
target: ArrayView2<'_, f64>,
rho: &SaeManifoldRho,
) -> Result<usize, String> {
let n = self.n_obs();
let p = self.output_dim();
let k_atoms = self.k_atoms();
if n == 0 {
return Ok(0);
}
// Per-row assignments and per-(row, atom) decoded outputs, computed once.
let mut assignments = Vec::with_capacity(n);
for row in 0..n {
assignments.push(self.assignment.try_assignments_row_for_rho(row, rho)?);
}
let mut decoded = Array3::<f64>::zeros((n, k_atoms, p));
let mut dbuf = vec![0.0_f64; p];
for row in 0..n {
for atom_idx in 0..k_atoms {
self.atoms[atom_idx].fill_decoded_row(row, &mut dbuf);
for c in 0..p {
decoded[[row, atom_idx, c]] = dbuf[c];
}
}
}
// Full fitted reconstruction `Σ_k a_k decoded_k`, so the per-atom partial
// residual is `e_k = (z − fitted) + a_k decoded_k` (add atom k back in).
let mut fitted = Array2::<f64>::zeros((n, p));
for row in 0..n {
for atom_idx in 0..k_atoms {
let a = assignments[row][atom_idx];
if a == 0.0 {
continue;
}
for c in 0..p {
fitted[[row, c]] += a * decoded[[row, atom_idx, c]];
}
}
}
let mut refreshed = 0usize;
for atom_idx in 0..k_atoms {
// Only atoms with an active frame are refreshed.
let Some(coords_c) = self.atoms[atom_idx].factored_coordinates()? else {
continue;
};
let r = self.atoms[atom_idx].border_frame_rank();
let m = self.atoms[atom_idx].basis_size();
// Accumulate `A_k = Σ_n a_k · e_{n,k} · ĉ_{n,k}ᵀ` directly (p × r).
let mut cross = GrassmannCrossMoment::new(p, r);
// Build per-row p-target `a_k·e_k` and r-coord `a_k·ĉ` batched, then
// accumulate as one outer-product sum. `accumulate` forms
// `targetsᵀ·coords`, so scaling EITHER side by `a_k` once gives the
// `a_k²` weight on the cross-moment that matches the C-block normal
// equations (residual leg carries `a_k`, coordinate leg carries
// `a_k`).
let mut targets = Array2::<f64>::zeros((n, p));
let mut rcoords = Array2::<f64>::zeros((n, r));
for row in 0..n {
let a = assignments[row][atom_idx];
// Partial residual e_{n,k} = z_n − (fitted − a_k decoded_k).
for c in 0..p {
let e = target[[row, c]] - fitted[[row, c]] + a * decoded[[row, atom_idx, c]];
targets[[row, c]] = a * e;
}
// In-span coordinate ĉ_{n,k} = Φ_k(t_n)·C_k ∈ ℝ^r.
for j in 0..r {
let mut acc = 0.0_f64;
for basis_col in 0..m {
acc += self.atoms[atom_idx].basis_values[[row, basis_col]]
* coords_c[[basis_col, j]];
}
rcoords[[row, j]] = a * acc;
}
}
cross.accumulate(targets.view(), rcoords.view())?;
// `polar(A_k)` is well-defined only when the moment is non-trivial;
// a zero moment (e.g. a fully collapsed atom) leaves the frame as-is.
if cross.moment().iter().all(|&v| v == 0.0) {
continue;
}
self.atoms[atom_idx].refresh_frame_from_cross_moment(cross.moment())?;
refreshed += 1;
}
Ok(refreshed)
}
pub fn beta_offsets(&self) -> Vec<usize> {
let p = self.output_dim();
let mut out = Vec::with_capacity(self.k_atoms());
let mut cursor = 0usize;
for atom in &self.atoms {
out.push(cursor);
cursor += atom.basis_size() * p;
}
out
}
/// Per-atom β column ranges for the block-Jacobi Schur preconditioner.
///
/// Returns one `Range<usize>` per atom, covering that atom's decoder
/// coefficients in the flat β vector:
/// `[beta_offsets[k] .. beta_offsets[k] + basis_size[k] * p_out]`.
///
/// Pass to [`ArrowSchurSystem::set_block_offsets`] so that
/// [`crate::solver::arrow_schur::JacobiPreconditioner`] builds one dense
/// Schur sub-block per atom instead of scalar-diagonal inversion.
pub fn beta_block_offsets(&self) -> Arc<[std::ops::Range<usize>]> {
let p = self.output_dim();
let mut ranges: Vec<std::ops::Range<usize>> = Vec::with_capacity(self.k_atoms());
let mut cursor = 0usize;
for atom in &self.atoms {
let width = atom.basis_size() * p;
ranges.push(cursor..cursor + width);
cursor += width;
}
Arc::from(ranges.into_boxed_slice())
}
/// Decide whether the sparse per-row active-set layout is engaged for the
/// dense-weight assignment modes (softmax / IBP-MAP), and if so derive the
/// per-row active-atom cap and magnitude cutoff.
///
/// The decision is auto-derived from the problem size and the
/// device/host working-set budget — never a CLI flag or kwarg. JumpReLU is
/// not handled here (it always uses its structural gate via
/// [`SaeRowLayout::from_jumprelu`]). The dense Gauss-Newton data Gram `G`
/// is `(m_total × m_total)` f64; if its dense form fits the budget we keep
/// the exact full-support solve (every atom active per row), so small-`K`
/// problems are bit-for-bit unchanged. Above that, we cap each row to the
/// `k_active` atoms that make the *sparse* Gram fit the same budget, with a
/// relative magnitude cutoff that drops assignment mass contributing
/// negligible `O(a²)` curvature.
///
/// Returns `Some((k_active_cap, cutoff))` to engage sparsity, or `None` to
/// keep the dense full-support layout.
pub(crate) fn sparse_active_plan(&self) -> Option<(usize, f64)> {
// The per-row Riemannian tangent projection for non-Euclidean atom
// latents is now applied directly on the compact active-set rows (see
// the `Some(layout)` arm in `assemble_arrow_schur`, via
// `compact_row_ext_manifold_and_point`), which rebuilds each row's
// product manifold in its compact column order and applies the SAME
// gt/htt/htbeta + Kronecker-Jacobian projections the dense path uses. So
// the sparse plan may engage on curved ext-coord manifolds (circle /
// torus / sphere atoms) — the affordability lever for manifold-SAE at
// large `K`, where the dense `K²` co-assignment Gram is the cost. (The
// former `is_euclidean()`-only restriction punted every curved atom to
// the dense layout; it is lifted.) The host/device in-core budget is the
// single gate now; it is parameterised in `sparse_active_plan_for_budget`
// so the engagement regression can pin a small budget without allocating
// a multi-GB dense Gram.
let budget = match crate::gpu::device_runtime::GpuRuntime::global() {
// Allow up to one quarter of the AGGREGATE device budget for the dense
// Gram, matching the streaming dispatcher's in-core fraction. The
// per-atom-pair Gram blocks fan out across the whole device pool, so
// the in-core fraction sums every ordinal's budget, not just the
// primary's.
Some(rt) => {
let aggregate: usize = rt
.device_ordinals()
.iter()
.map(|&ord| rt.memory_budget_for(ord))
.sum();
aggregate / 4
}
None => sae_host_in_core_budget_bytes().0,
};
self.sparse_active_plan_for_budget(budget)
}
/// Budget-parameterised core of [`Self::sparse_active_plan`]. The dense data
/// Gram footprint `(m_total · m_total) f64` is compared against `budget`; a
/// term whose dense Gram exceeds the budget engages the compact active-set
/// plan (returns `Some((k_active_cap, cutoff))`), regardless of whether any
/// atom latent is curved. Pulled out so the curved-atom engagement
/// regression can pin a small budget deterministically.
pub(crate) fn sparse_active_plan_for_budget(&self, budget: usize) -> Option<(usize, f64)> {
// Relative magnitude cutoff: assignment mass below this fraction of the
// row's peak `|a_k|` enters the Gram only as `O(a²)` curvature and is
// dropped. Chosen so dropped terms are ~1e-6 of the peak self-coupling.
const RELATIVE_CUTOFF: f64 = 1.0e-3;
let k_atoms = self.k_atoms();
if k_atoms <= 1 {
return None;
}
let p = self.output_dim();
let m_total: usize = self.atoms.iter().map(|a| a.basis_size()).sum();
// Dense data Gram footprint: (m_total · m_total) f64.
let dense_gram_bytes = m_total
.saturating_mul(m_total)
.saturating_mul(SAE_BYTES_PER_F64);
if dense_gram_bytes <= budget {
return None;
}
// Sparse Gram footprint scales with the per-row active basis count
// `k_active · m_atom`. Solve for the largest `k_active` whose sparse
// Gram `(k_active · m_atom)²` still fits the budget.
let m_atom = (m_total as f64 / k_atoms as f64).max(1.0);
let max_active_basis = ((budget as f64 / SAE_BYTES_PER_F64 as f64).sqrt() / m_atom).floor();
let k_active_cap = (max_active_basis as usize).clamp(1, k_atoms);
// p does not enter the Gram dimension (it is carried by the `⊗ I_p`
// structure), but a degenerate `p == 0` term has no decoder columns.
if p == 0 {
return None;
}
Some((k_active_cap, RELATIVE_CUTOFF))
}
pub fn flatten_beta(&self) -> Array1<f64> {
let p = self.output_dim();
let offsets = self.beta_offsets();
let mut out = Array1::<f64>::zeros(self.beta_dim());
for (atom_idx, atom) in self.atoms.iter().enumerate() {
let m = atom.basis_size();
let off = offsets[atom_idx];
for basis_col in 0..m {
for out_col in 0..p {
out[off + basis_col * p + out_col] =
atom.decoder_coefficients[[basis_col, out_col]];
}
}
}
out
}
pub fn set_flat_beta(&mut self, beta: ArrayView1<'_, f64>) -> Result<(), String> {
if beta.len() != self.beta_dim() {
return Err(format!(
"set_flat_beta: beta length {} != expected {}",
beta.len(),
self.beta_dim()
));
}
let p = self.output_dim();
let offsets = self.beta_offsets();
for (atom_idx, atom) in self.atoms.iter_mut().enumerate() {
let m = atom.basis_size();
let off = offsets[atom_idx];
for basis_col in 0..m {
for out_col in 0..p {
atom.decoder_coefficients[[basis_col, out_col]] =
beta[off + basis_col * p + out_col];
}
}
}
Ok(())
}
pub fn refit_decoder_least_squares_at_current_state(
&mut self,
target: ArrayView2<'_, f64>,
rho: Option<&SaeManifoldRho>,
) -> Result<(), String> {
let n = self.n_obs();
let p = self.output_dim();
if target.dim() != (n, p) {
return Err(format!(
"SaeManifoldTerm::refit_decoder_least_squares_at_current_state: target shape {:?} != ({n}, {p})",
target.dim()
));
}
let k_atoms = self.k_atoms();
let offsets = self.beta_offsets();
let m_total = self.beta_dim() / p;
let mut design = Array2::<f64>::zeros((n, m_total));
for row in 0..n {
let assignments = match rho {
Some(rho) => self.assignment.try_assignments_row_for_rho(row, rho)?,
None => self.assignment.try_assignments_row(row)?,
};
for atom_idx in 0..k_atoms {
let atom = &self.atoms[atom_idx];
let weight = assignments[atom_idx];
let m = atom.basis_size();
let off = offsets[atom_idx] / p;
for basis_col in 0..m {
design[[row, off + basis_col]] = weight * atom.basis_values[[row, basis_col]];
}
}
}
let beta = solve_design_least_squares(design.view(), target)?;
if beta.dim() != (m_total, p) {
return Err(format!(
"SaeManifoldTerm::refit_decoder_least_squares_at_current_state: beta shape {:?} != ({m_total}, {p})",
beta.dim()
));
}
for atom_idx in 0..k_atoms {
let m = self.atoms[atom_idx].basis_size();
let off = offsets[atom_idx] / p;
for basis_col in 0..m {
for out_col in 0..p {
self.atoms[atom_idx].decoder_coefficients[[basis_col, out_col]] =
beta[[off + basis_col, out_col]];
}
}
self.atoms[atom_idx].refresh_intrinsic_smooth_penalty();
}
Ok(())
}
pub fn fitted(&self) -> Array2<f64> {
self.try_fitted().expect("assignment logits must be finite")
}
/// The #1026 hybrid-collapse substitution map: `atom_idx → &AtomLinearImage`
/// for every `d = 1` slot whose post-fit verdict selected its straight
/// (`Θ → 0`) sub-model. Empty when no report has been computed
/// (`hybrid_split_report == None`, e.g. mid-fit) or no slot collapsed. The
/// SINGLE source of the collapse policy — every reconstruction path (the
/// rho-keyed `try_fitted_with_rho`, the explicit-assignment
/// [`Self::reconstruct_from_assignments`] used by the top-k projection)
/// reads it so train, OOS, and top-k reconstructions decode collapsed slots
/// identically (#1228, #1233).
pub(crate) fn hybrid_linear_image_map(
&self,
) -> std::collections::HashMap<usize, &crate::terms::sae::hybrid_split::AtomLinearImage> {
// A fitted term carries its collapse policy on the post-fit
// `hybrid_split_report`; an OOS term carries the same trained images on
// `oos_linear_images` (#1228). At most one is `Some` in practice, but
// prefer the report when both are present.
if let Some(report) = self.hybrid_split_report.as_ref() {
return report
.verdicts
.iter()
.filter_map(|v| v.linear_image.as_ref().map(|img| (img.atom_idx, img)))
.collect();
}
if let Some(images) = self.oos_linear_images.as_ref() {
return images.iter().map(|img| (img.atom_idx, img)).collect();
}
std::collections::HashMap::new()
}
/// #1228 — attach the trained dictionary's hybrid-collapsed linear images to
/// this (typically OOS) term so its reconstruction (`fitted` / the top-k
/// assembler) decodes verdict-linear `d = 1` slots by the SAME straight
/// sub-model the training reconstruction used, instead of the original
/// curved decoder. Each image's `atom_idx` must index a real slot; an image
/// whose channel count `p` disagrees with this term's output dim, or whose
/// `atom_idx` is out of range, is rejected so a stale/mismatched payload
/// cannot silently corrupt the reconstruction. Pass an empty slice (or never
/// call this) for an all-curved OOS reconstruction.
pub(crate) fn set_hybrid_linear_images(
&mut self,
images: Vec<crate::terms::sae::hybrid_split::AtomLinearImage>,
) -> Result<(), String> {
let p = self.output_dim();
let k_atoms = self.k_atoms();
for img in &images {
if img.atom_idx >= k_atoms {
return Err(format!(
"set_hybrid_linear_images: atom_idx {} out of range (k_atoms={k_atoms})",
img.atom_idx
));
}
if img.b0.len() != p || img.b1.len() != p {
return Err(format!(
"set_hybrid_linear_images: atom {} linear image has p=({}, {}) != output_dim {p}",
img.atom_idx,
img.b0.len(),
img.b1.len()
));
}
if self.atoms[img.atom_idx].latent_dim != 1 {
return Err(format!(
"set_hybrid_linear_images: atom {} is not d=1; only d=1 slots collapse to a straight image",
img.atom_idx
));
}
}
self.oos_linear_images = if images.is_empty() {
None
} else {
Some(images)
};
Ok(())
}
/// Assemble the reconstruction `Σ_k a[i,k]·g_k(t_{ik})` from an EXPLICIT
/// per-row assignment matrix (e.g. a hard top-k projection of the fitted
/// soft assignments), honouring the #1026 hybrid collapse when `collapse` is
/// set: a verdict-linear `d = 1` slot decodes its straight sub-model image
/// instead of its curved curve, exactly as the production `try_fitted` does.
/// This is the shared assembler the FFI top-k path uses so the projected
/// reconstruction composes with hybrid collapse (#1233) instead of
/// re-deriving the curved image by hand and silently bypassing the verdict.
/// The atom coordinates (`t`) and decoded curves are the term's own fitted
/// ones; only the assignment masses come from `assignments`.
pub fn reconstruct_from_assignments(
&self,
assignments: ArrayView2<'_, f64>,
collapse: bool,
) -> Result<Array2<f64>, String> {
let n = self.n_obs();
let p = self.output_dim();
let k_atoms = self.k_atoms();
if assignments.dim() != (n, k_atoms) {
return Err(format!(
"SaeManifoldTerm::reconstruct_from_assignments: assignments {:?} != ({n}, {k_atoms})",
assignments.dim()
));
}
let linear_images = if collapse {
self.hybrid_linear_image_map()
} else {
std::collections::HashMap::new()
};
let mut out = Array2::<f64>::zeros((n, p));
let mut g_buf = vec![0.0_f64; p];
for row in 0..n {
for atom_idx in 0..k_atoms {
let a_k = assignments[[row, atom_idx]];
if a_k == 0.0 {
continue;
}
if let Some(image) = linear_images.get(&atom_idx) {
let t = self.assignment.coords[atom_idx].as_matrix()[[row, 0]];
image.fill_row(t, &mut g_buf);
} else {
self.atoms[atom_idx].fill_decoded_row(row, &mut g_buf);
}
let mut out_row = out.row_mut(row);
for out_col in 0..p {
out_row[out_col] += a_k * g_buf[out_col];
}
}
}
Ok(out)
}
pub fn try_fitted(&self) -> Result<Array2<f64>, String> {
// Production/user-facing reconstruction: honours the #1026 hybrid-split
// verdict (verdict-linear `d = 1` slots decode their straight sub-model).
self.try_fitted_with_rho(None, true)
}
pub(crate) fn try_fitted_for_rho(&self, rho: &SaeManifoldRho) -> Result<Array2<f64>, String> {
// Internal/fitting reconstruction: the pure CURVED image (the joint fit
// and the #1026 adjudication both require the uncollapsed curve).
self.try_fitted_with_rho(Some(rho), false)
}
pub(crate) fn try_fitted_with_rho(
&self,
rho: Option<&SaeManifoldRho>,
collapse: bool,
) -> Result<Array2<f64>, String> {
let n = self.n_obs();
let p = self.output_dim();
let k_atoms = self.k_atoms();
let mut out = Array2::<f64>::zeros((n, p));
// #1026 — the curved/linear hybrid-split verdict is LOAD-BEARING on the
// production reconstruction, not just a side report. When
// [`Self::compute_hybrid_split_report`] (run post-fit in
// `canonicalize_charts_post_fit`) adjudicated a `d = 1` atom's evidence
// in favour of its straight (Θ→0) sub-model, the model's output
// reconstruction (`fitted()` / `try_fitted` → predict and the user-facing
// output) decodes that slot with its fitted linear image instead of its
// curved decoded curve. The linear images are coordinate-keyed and
// rho-independent (exact weighted-LS lines realised inside the
// adjudication — no re-fit, no #1051 outer continuation).
//
// The collapse engages only when the caller asks for it (`collapse`):
// the production `try_fitted` path and the explicit
// `hybrid_collapsed_reconstruction` entry point. The pure-curved
// `try_fitted_for_rho` opts out — the joint fit's loss/assembly optimise
// the curved decoder coefficients and must see the curved image, and the
// #1026 adjudication itself compares the curved fit against its straight
// sub-model — both require the uncollapsed curve. (During fitting the
// report is `None` regardless; it is only computed post-fit.)
let linear_images = if collapse {
self.hybrid_linear_image_map()
} else {
std::collections::HashMap::new()
};
// Reuse a single scratch buffer across all (row, atom) pairs instead of
// allocating a fresh `Array1<f64>` of length p per call.
let mut g_buf = vec![0.0_f64; p];
for row in 0..n {
let a = match rho {
Some(rho) => self.assignment.try_assignments_row_for_rho(row, rho)?,
None => self.assignment.try_assignments_row(row)?,
};
for atom_idx in 0..k_atoms {
let a_k = a[atom_idx];
if let Some(image) = linear_images.get(&atom_idx) {
// Verdict-linear slot: substitute the straight sub-model image
// at this row's fitted on-atom coordinate.
let t = self.assignment.coords[atom_idx].as_matrix()[[row, 0]];
image.fill_row(t, &mut g_buf);
} else {
self.atoms[atom_idx].fill_decoded_row(row, &mut g_buf);
}
let mut out_row = out.row_mut(row);
for out_col in 0..p {
out_row[out_col] += a_k * g_buf[out_col];
}
}
}
Ok(out)
}
/// Per-atom **leave-one-atom-out (LOAO) explained-variance contribution**
/// (#1026): for each atom `k`, the drop in reconstruction explained variance
/// `ΔEV_k = EV(full) − EV(full ⊖ atom_k)` when that atom's contribution
/// `a[i,k]·g_k(coord[i,k])` is removed from the assembled reconstruction and
/// nothing else is refit. Because every atom adds linearly into the same
/// fitted reconstruction (`fitted[i] = Σ_k a[i,k]·g_k`), zeroing one atom is
/// the exact "this atom withheld" counterfactual, and the EV it was earning
/// is `EV(full) − EV(without k)`. This is the per-atom held-out EV
/// attribution the #1026 roadmap pairs with each atom's fitted turning `Θ`:
/// a `Θ ≈ 0` atom earning a large `ΔEV` is a linear-tail direction; a
/// high-`Θ` atom earning a large `ΔEV` is a genuine curved family carrying
/// reconstruction it would otherwise shatter into `N(ε) ≈ Θ/(2√(2ε))` linear
/// directions. Pure read-only diagnostic — never mutates any atom.
///
/// Returns one `Option<f64>` per atom in atom order; `None` for an atom
/// whose ⊖-reconstruction EV is undefined (degenerate target variance), and
/// `None` for the whole vector if the full-reconstruction EV is undefined.
/// #1026: the load-bearing curved-vs-linear hybrid-split verdict for the
/// fitted dictionary, or `None` until [`Self::canonicalize_charts_post_fit`]
/// has run (or when no `d = 1` atom is eligible). Surfaced in the Python model
/// output so the user sees which atoms genuinely earn their curvature.
pub fn hybrid_split_report(
&self,
) -> Option<&crate::terms::sae::hybrid_split::SaeHybridSplitReport> {
self.hybrid_split_report.as_ref()
}
/// Build the #1026 curved-vs-linear hybrid-split report by adjudicating each
/// eligible `d = 1` atom's fitted curved image against its straight (linear
/// special-case) sub-model on the common rank-aware Laplace evidence scale.
///
/// Both candidates are scored against the SAME data — the atom's
/// leave-this-atom-out response residual `y_resp = target − (full − a_k·γ_k)`
/// (#1202) — over its assigned rows: the curved candidate predicts its actual
/// mass-scaled contribution `a_k·γ_k`, the linear candidate the best
/// mass-weighted straight line fit to `y_resp` (the collapsed linear lane —
/// closed form, NOT the broken euclidean outer fit path of #1051). Linear is
/// the curved family's nested `Θ = 0` sub-model on common data, so the
/// per-slot evidence argmin is a genuine match-or-beat comparison. Eligible
/// atoms are `d = 1` atoms with an installed evaluator at the full curvature
/// dial (`homotopy_eta == 1.0`) whose live coordinate dim still matches the
/// atom's latent dim. Returns `None` when no reconstruction `target` is
/// supplied (there is no data to adjudicate against).
pub fn compute_hybrid_split_report(
&self,
rho: &SaeManifoldRho,
target: Option<ArrayView2<'_, f64>>,
) -> Result<Option<crate::terms::sae::hybrid_split::SaeHybridSplitReport>, String> {
let n = self.n_obs();
let p = self.output_dim();
// Per-atom held-out `ΔEV_k` (leave-one-atom-out explained-variance drop),
// paired with each atom's fitted turning Θ onto the verdict so the report
// carries the #1026 `(Θ, ΔEV)` frontier point as structured data. Absent
// when no reconstruction target is supplied.
let loao_ev: Vec<Option<f64>> = match target {
Some(t) => self.per_atom_loao_explained_variance(t, rho)?,
None => vec![None; self.k_atoms()],
};
let delta_ev_for =
|atom_idx: usize| -> Option<f64> { loao_ev.get(atom_idx).copied().flatten() };
// The common-evidence comparison (#1202) scores both candidates against
// the response data the atom is responsible for. That requires a target;
// with none supplied there is nothing to adjudicate against, so no report.
let Some(target) = target else {
return Ok(None);
};
if target.dim() != (n, p) {
return Err(format!(
"SaeManifoldTerm::compute_hybrid_split_report: target {:?} != ({n}, {p})",
target.dim()
));
}
// Per-row assignment masses (once), so each atom's weighted straight-line
// fit uses the same row weighting the joint reconstruction loss does.
let mut weights: Vec<Array1<f64>> = Vec::with_capacity(n);
for row in 0..n {
weights.push(self.assignment.try_assignments_row_for_rho(row, rho)?);
}
// The full assembled reconstruction `Σ_k a[i,k]·γ_k`, computed once. Each
// atom's leave-this-atom-out response residual is `y_resp = target −
// (full − a_k·γ_k)`, the data both that atom's candidates fit (#1202).
let full = self.try_fitted_for_rho(rho)?;
let eligible: Vec<usize> = (0..self.k_atoms())
.filter(|&atom_idx| {
let atom = &self.atoms[atom_idx];
atom.latent_dim == 1
&& atom.basis_evaluator.is_some()
&& atom.homotopy_eta == 1.0
&& self.assignment.coords[atom_idx].latent_dim() == atom.latent_dim
})
.collect();
// Per-atom fitted decoded image at every row (the curved candidate's
// realized curve, which the linear candidate must approximate).
let coords_for = |atom_idx: usize| -> Array1<f64> {
self.assignment.coords[atom_idx]
.as_matrix()
.column(0)
.to_owned()
};
let assign_for = |atom_idx: usize| -> Array1<f64> {
Array1::from_iter((0..n).map(|row| weights[row][atom_idx]))
};
let decoded_for = |atom_idx: usize| -> Array2<f64> {
let mut decoded = Array2::<f64>::zeros((n, p));
let mut buf = vec![0.0_f64; p];
for row in 0..n {
self.atoms[atom_idx].fill_decoded_row(row, &mut buf);
for col in 0..p {
decoded[[row, col]] = buf[col];
}
}
decoded
};
// The atom's leave-this-atom-out response residual `y_resp = target −
// (full − a_k·γ_k) = (target − full) + a_k·γ_k`. Both the curved and the
// linear candidate are scored against this on common data (#1202).
let target_resid_for = |atom_idx: usize| -> Array2<f64> {
let mut resid = Array2::<f64>::zeros((n, p));
let mut buf = vec![0.0_f64; p];
for row in 0..n {
let a_k = weights[row][atom_idx];
self.atoms[atom_idx].fill_decoded_row(row, &mut buf);
for col in 0..p {
resid[[row, col]] = target[[row, col]] - full[[row, col]] + a_k * buf[col];
}
}
resid
};
let manifold_for = |atom_idx: usize| -> crate::terms::latent::LatentManifold {
self.assignment.coords[atom_idx].manifold().clone()
};
crate::terms::sae::hybrid_split::build_hybrid_split_report(
&self.atoms,
eligible.into_iter(),
coords_for,
assign_for,
decoded_for,
target_resid_for,
manifold_for,
delta_ev_for,
)
}
pub fn per_atom_loao_explained_variance(
&self,
target: ArrayView2<'_, f64>,
rho: &SaeManifoldRho,
) -> Result<Vec<Option<f64>>, String> {
let n = self.n_obs();
let p = self.output_dim();
let k_atoms = self.k_atoms();
if target.dim() != (n, p) {
return Err(format!(
"SaeManifoldTerm::per_atom_loao_explained_variance: target {:?} != ({n}, {p})",
target.dim()
));
}
let full = self.try_fitted_for_rho(rho)?;
let Some(ev_full) = reconstruction_explained_variance(target, full.view()) else {
return Ok(vec![None; k_atoms]);
};
// Cache each row's assignment weights once, then subtract a single
// atom's decoded contribution per LOAO pass instead of reassembling the
// whole dictionary k times.
let mut weights: Vec<Array1<f64>> = Vec::with_capacity(n);
for row in 0..n {
weights.push(self.assignment.try_assignments_row_for_rho(row, rho)?);
}
let mut g_buf = vec![0.0_f64; p];
let mut out = Vec::with_capacity(k_atoms);
for atom_idx in 0..k_atoms {
let mut without = full.clone();
for row in 0..n {
let a_k = weights[row][atom_idx];
if a_k == 0.0 {
continue;
}
self.atoms[atom_idx].fill_decoded_row(row, &mut g_buf);
let mut without_row = without.row_mut(row);
for out_col in 0..p {
without_row[out_col] -= a_k * g_buf[out_col];
}
}
out.push(
reconstruction_explained_variance(target, without.view())
.map(|ev_without| ev_full - ev_without),
);
}
Ok(out)
}
/// #1026 — the LOAD-BEARING collapsed reconstruction: the assembled
/// dictionary output `Σ_k a[i,k]·g_k(coord[i,k])` in which every slot whose
/// hybrid-split verdict selected LINEAR has its curved decoded image replaced
/// by its fitted straight sub-model `b₀ + (t − t̄)·b₁`. This is what makes the
/// verdict *change the reconstruction* instead of merely logging a choice:
/// the linear-collapsed atom no longer pays its `M·p` curved coefficients, it
/// carries a `2·p` straight image whose decoded curve has zero turning.
///
/// The straight images are the exact weighted-least-squares lines already
/// realized inside [`Self::compute_hybrid_split_report`] (no re-fit, no outer
/// continuation, sidestepping #1051). Returns the curved reconstruction
/// unchanged when no verdict selected linear, or when the report has not been
/// computed yet (`hybrid_split_report == None`).
pub fn hybrid_collapsed_reconstruction(
&self,
rho: &SaeManifoldRho,
) -> Result<Array2<f64>, String> {
// #1026 — the hybrid collapse is realised by the SINGLE reconstruction
// path ([`Self::try_fitted_with_rho`]) with the collapse flag set: a
// verdict-linear `d = 1` slot decodes its straight sub-model image
// instead of its curved curve. This replaces the dedicated re-collapse
// loop this method used to carry (a parallel layer). The production
// `try_fitted` shares the identical routine at `rho = None`; this entry
// point keeps the rho-keyed collapse for the #1026 EV-dominance reporting
// (`hybrid_collapsed_explained_variance`) and the regression battery.
self.try_fitted_with_rho(Some(rho), true)
}
/// #1026 — the reconstruction explained variance of the hybrid-collapsed
/// dictionary (every verdict-linear slot decoded by its straight sub-model)
/// against `target`. The companion of [`Self::per_atom_loao_explained_variance`]
/// for the dominance claim: because each linear-collapsed slot is the curved
/// family's `Θ → 0` sub-model and is only kept when its evidence beats the
/// curved candidate's parameter price, the collapsed dictionary match-or-beats
/// the all-curved one on EV-per-parameter — the strict-generalization floor
/// the #1026 hybrid argument rests on. `None` when EV is undefined (degenerate
/// target variance).
pub fn hybrid_collapsed_explained_variance(
&self,
target: ArrayView2<'_, f64>,
rho: &SaeManifoldRho,
) -> Result<Option<f64>, String> {
let n = self.n_obs();
let p = self.output_dim();
if target.dim() != (n, p) {
return Err(format!(
"SaeManifoldTerm::hybrid_collapsed_explained_variance: target {:?} != ({n}, {p})",
target.dim()
));
}
let collapsed = self.hybrid_collapsed_reconstruction(rho)?;
Ok(reconstruction_explained_variance(target, collapsed.view()))
}
/// #1026 ladder item 2/3 — the AMORTIZED ENCODER, wired from the fitted
/// dictionary. Builds the offline certified [`EncodeAtlas`] over this term's
/// frozen atoms and encodes a target corpus `targets` (`n × p`) through the
/// per-chart distilled Jacobian predictor, with the Kantorovich certificate
/// gating each row and an exact-solve fallback for the rows the amortized
/// predictor cannot certify. Returns one [`EncodeResult`] per atom (the
/// per-atom encoded coordinates + per-row certificate mask), in dictionary
/// order.
///
/// This is the thread's "encoder + certificate-gated exact fallback"
/// deployment made reachable from a fit: the distilled map approximates
/// inference at one mat-vec/row, and any row whose amortized prediction fails
/// `h ≤ ½` falls back to the certified IFT-warm-start Newton encode
/// ([`EncodeAtlas::certified_encode_row`]); rows that still cannot be
/// certified ride the [`EncodeResult::encode_uncertified_count`] flag for the
/// upstream exact multi-start solve (honesty, never a silent wrong encode).
///
/// Magic by default: the atlas's worst-case bounds are auto-derived from the
/// fit — `amplitude_bound[k]` is the largest fitted assignment mass `a[i,k]`
/// the encode can produce for atom `k` (the encode recovers `t` from
/// `x ≈ z·γ_k(t)` at amplitude `z = a[i,k]`), and `target_norm_bound` is the
/// largest target row norm — so no caller supplies a knob. Per-row amplitudes
/// are the fitted assignment masses for the same target the dictionary was fit
/// against; an external corpus reuses the per-row masses the assignment
/// produces for it upstream (passed in `amplitudes`, one column per atom).
pub fn amortized_encode_target(
&self,
targets: ArrayView2<'_, f64>,
amplitudes: ArrayView2<'_, f64>,
) -> Result<Vec<crate::terms::sae::encode::EncodeResult>, String> {
let p = self.output_dim();
let k_atoms = self.k_atoms();
let n = targets.nrows();
if targets.ncols() != p {
return Err(format!(
"SaeManifoldTerm::amortized_encode_target: targets have {} cols but output_dim is {p}",
targets.ncols()
));
}
if amplitudes.dim() != (n, k_atoms) {
return Err(format!(
"SaeManifoldTerm::amortized_encode_target: amplitudes {:?} must be (n={n}, K={k_atoms})",
amplitudes.dim()
));
}
// Magic-by-default offline bounds, auto-derived from the fit so no caller
// supplies a knob. `target_norm_bound` is the largest target row L2 norm
// (bounds `‖x‖` over the corpus); `amplitude_bound[k]` is the largest
// fitted assignment mass for atom `k` (bounds `|z_k|`), with a strictly
// positive floor so a near-inactive atom still certifies a finite radius.
let mut target_norm_bound = 0.0_f64;
for row in 0..n {
let norm = targets.row(row).dot(&targets.row(row)).sqrt();
if norm.is_finite() && norm > target_norm_bound {
target_norm_bound = norm;
}
}
let mut amplitude_bound = vec![0.0_f64; k_atoms];
for atom_idx in 0..k_atoms {
let mut bound = 0.0_f64;
for row in 0..n {
let z = amplitudes[[row, atom_idx]].abs();
if z.is_finite() && z > bound {
bound = z;
}
}
// A strictly positive amplitude floor keeps the offline Lipschitz
// scaling finite for atoms with no active row in this corpus (those
// rows encode to the chart center via the certificate anyway).
amplitude_bound[atom_idx] = bound.max(1.0);
}
let atlas = crate::terms::sae::encode::EncodeAtlas::build(
&self.atoms,
&litude_bound,
target_norm_bound,
crate::terms::sae::encode::AtlasConfig::default(),
)?;
// Per-atom amortized encode with a certificate-gated exact-solve fallback:
// a row whose distilled prediction fails `h ≤ ½` is retried through the
// certified IFT-warm-start Newton path; a row that still cannot be
// certified stays flagged for the upstream multi-start solve.
// (The atlas is rho-free; the per-row amplitudes already carry the
// rho-resolved assignment masses the caller produced upstream.)
let mut results = Vec::with_capacity(k_atoms);
for atom_idx in 0..k_atoms {
let atom = &self.atoms[atom_idx];
let amp_col = amplitudes.column(atom_idx).to_owned();
let amortized =
atlas.amortized_encode_batch(atom, atom_idx, targets, amp_col.view())?;
let mut coords = amortized.coords;
let mut certified = amortized.certified;
for row in 0..n {
if certified[row] {
continue;
}
let (t, cert) =
atlas.certified_encode_row(atom, atom_idx, targets.row(row), amp_col[row])?;
if cert.certified() {
coords.row_mut(row).assign(&t);
certified[row] = true;
}
}
results.push(crate::terms::sae::encode::EncodeResult::from_rows(
coords, certified,
));
}
Ok(results)
}
/// #1026 — the fitted per-row assignment masses `a[i,k]` (the activation
/// amplitudes `z_k` the amortized encode recovers `t` against), as an
/// `n × K` matrix. These are exactly the masses
/// [`Self::try_fitted_with_rho`] assembles the reconstruction from, so
/// feeding them to [`Self::amortized_encode_target`] re-encodes the SAME
/// inference the dictionary was fit against — the self-consistency the
/// distilled encoder is supervised to approximate.
pub fn fitted_assignment_amplitudes(
&self,
rho: &SaeManifoldRho,
) -> Result<Array2<f64>, String> {
let n = self.n_obs();
let k_atoms = self.k_atoms();
let mut amplitudes = Array2::<f64>::zeros((n, k_atoms));
for row in 0..n {
let a = self.assignment.try_assignments_row_for_rho(row, rho)?;
for atom_idx in 0..k_atoms {
amplitudes[[row, atom_idx]] = a[atom_idx];
}
}
Ok(amplitudes)
}
/// #1026 — encode the dictionary's own fit-time target with the amortized
/// encoder, deriving the per-row amplitudes from the fitted assignment so the
/// caller supplies neither bounds nor amplitudes (magic by default). The
/// end-to-end "fit → distilled encoder → certificate-gated encode" path.
pub fn amortized_encode_fitted(
&self,
targets: ArrayView2<'_, f64>,
rho: &SaeManifoldRho,
) -> Result<Vec<crate::terms::sae::encode::EncodeResult>, String> {
let amplitudes = self.fitted_assignment_amplitudes(rho)?;
self.amortized_encode_target(targets, amplitudes.view())
}
/// #1154 — amortized-encoder consistency of the CURRENT dictionary against
/// its own fit-time target. This is the co-training signal of the joint
/// amortized-encoder + REML loop (Design A): the amortized (one-mat-vec)
/// encode is built from the *current* fitted decoder, run on `targets`, and
/// scored on two principled axes —
///
/// * `recon_consistency` (the bilinear part of the co-training loss): the
/// mean per-element squared gap between the **amortized** reconstruction
/// `Σ_k z_k · Φ_k(t̂_k) B_k` (decode the amortized coords) and the
/// **exact** fitted reconstruction `Σ_k z_k · Φ_k(t_k^*) B_k` the inner
/// solve converged to. A dictionary whose encode map is well-approximated
/// to first order by the per-chart IFT predictor scores near zero; a
/// dictionary the amortized encoder *cannot* invert faithfully (sharp
/// curvature, poorly-charted regions) scores high. Minimising this jointly
/// with REML steers the fit toward dictionaries that admit a fast,
/// faithful amortized encode — the architectural co-adaptation #1154 adds.
/// * `uncertified_fraction`: the share of (row, atom) encodes whose
/// Kantorovich certificate failed (`h > ½`), i.e. that fell back to the
/// certified IFT-warm-start Newton. This is the encoder's *certifiable coverage*
/// of the dictionary; co-training rewards dictionaries the cheap encode
/// certifies, not just ones it happens to land.
///
/// The certificate keeps every accepted amortized coord honest (uncertified
/// rows already ride the exact fallback inside `amortized_encode_target`), so
/// this metric never silently trusts a wrong encode — it MEASURES how much of
/// the dictionary the cheap encoder can faithfully and certifiably invert.
pub fn amortized_encoder_consistency(
&self,
targets: ArrayView2<'_, f64>,
rho: &SaeManifoldRho,
) -> Result<AmortizedEncoderConsistency, String> {
let n = self.n_obs();
let p = self.output_dim();
let k_atoms = self.k_atoms();
if targets.dim() != (n, p) {
return Err(format!(
"SaeManifoldTerm::amortized_encoder_consistency: targets {:?} must be (n={n}, p={p})",
targets.dim()
));
}
let amplitudes = self.fitted_assignment_amplitudes(rho)?;
let encodes = self.amortized_encode_target(targets, amplitudes.view())?;
// The EXACT fitted reconstruction the inner solve converged to (pure
// curved image, rho-keyed) is the supervision target for the amortized
// reconstruction. Both are n×p ambient, so the comparison is layout-free.
let exact_recon = self.try_fitted_for_rho(rho)?;
// Build the amortized reconstruction Σ_k z_k · Φ_k(t̂_k) B_k by decoding
// each atom's amortized coords through that atom's own basis evaluator.
let mut amortized_recon = Array2::<f64>::zeros((n, p));
let mut uncertified = 0usize;
for atom_idx in 0..k_atoms {
let atom = &self.atoms[atom_idx];
let result = &encodes[atom_idx];
// An atom with no basis evaluator cannot decode an amortized
// reconstruction; every one of its rows is necessarily uncertified
// (the encode flagged them all), so it contributes nothing to the
// amortized recon and its full row-count to the uncertified tally.
// Count it and skip the decode rather than erroring — the consistency
// fold stays a bounded penalty, never a hard abort of the criterion.
let Some(evaluator) = atom.basis_evaluator.as_ref() else {
uncertified += n;
continue;
};
uncertified += result.encode_uncertified_count;
// Decode the amortized coords: Φ_k(t̂) is (n × M_k); B_k is (M_k × p).
let (phi, _jac) = evaluator.evaluate(result.coords.view())?;
let decoded = phi.dot(&atom.decoder_coefficients); // (n × p)
for row in 0..n {
let z = amplitudes[[row, atom_idx]];
if z == 0.0 {
continue;
}
for col in 0..p {
amortized_recon[[row, col]] += z * decoded[[row, col]];
}
}
}
let mut sse = 0.0_f64;
for row in 0..n {
for col in 0..p {
let gap = amortized_recon[[row, col]] - exact_recon[[row, col]];
sse += gap * gap;
}
}
let denom = (n.max(1) * p.max(1)) as f64;
let recon_consistency = sse / denom;
let total_encodes = (n * k_atoms).max(1) as f64;
let uncertified_fraction = uncertified as f64 / total_encodes;
Ok(AmortizedEncoderConsistency {
recon_consistency,
uncertified_fraction,
n_uncertified: uncertified,
n_encodes: n * k_atoms,
})
}
/// #1154 — the co-trained REML criterion: the exact REML criterion at `rho`
/// PLUS the amortized-encoder consistency penalty, so the outer optimizer
/// co-adapts the dictionary + smoothing parameters λ TOWARD a dictionary the
/// fast amortized encoder can faithfully and certifiably invert.
///
/// This is Design A of #1154. The inner solve still converges the `(t, β)`
/// system to stationarity at the engine's current ρ (so the implicit-function
/// REML λ-gradient `dβ̂/dλ = −(H+S_λ)⁻¹(dS_λ/dλ)β̂` stays EXACT — the encoder
/// only warm-starts/co-adapts, it never replaces the stationary point). The
/// added term
///
/// ```text
/// J_cotrain(ρ) = REML(ρ) + w · ‖x̂_amortized − x̂_exact‖²/(n·p)
/// + w_cert · uncertified_fraction
/// ```
///
/// folds the post-fit amortized-encode quality into the ranked objective. The
/// weights are auto-scaled to the REML criterion magnitude (magic by default:
/// no caller knob) so the consistency term is a meaningful but non-dominant
/// fraction of the objective regardless of problem scale.
pub fn reml_criterion_cotrained(
&mut self,
target: ArrayView2<'_, f64>,
rho: &SaeManifoldRho,
registry: Option<&AnalyticPenaltyRegistry>,
inner_max_iter: usize,
learning_rate: f64,
ridge_ext_coord: f64,
ridge_beta: f64,
) -> Result<(f64, SaeManifoldLoss, AmortizedEncoderConsistency), String> {
// #1154: always attempt the amortized warm-start first inside
// `reml_criterion_cotrained` (the encode/warm path for the cotrained
// objective). Good warm-starts from the running dictionary land the
// inner solve closer to the stationary point used for the fold.
// Advisory only (0 or err falls back to cold); telemetry recorded by
// outer objective callers when present.
let warm_n = self
.warm_start_latents_from_amortized_encoder(target, rho)
.unwrap_or(0);
if warm_n > 0 {
eprintln!(
"SAE-INNER: warm-started {warm_n} rows for reml_criterion (inner_max_iter={inner_max_iter})"
);
}
let (reml, loss) = self.reml_criterion_with_refine_policy(
target,
rho,
registry,
inner_max_iter,
learning_rate,
ridge_ext_coord,
ridge_beta,
true,
)?;
let consistency = self.amortized_encoder_consistency(target, rho)?;
// Auto-scale the co-training weights to the REML magnitude so the
// consistency penalty is a bounded, scale-free fraction of the objective
// (magic by default: no caller knob). `reml_scale` floors at 1 so a
// near-zero criterion still admits a meaningful consistency contribution.
let cotrained = Self::fold_cotrain_consistency(reml, &consistency);
Ok((cotrained, loss, consistency))
}
/// #1154 — the single source of the co-training fold arithmetic: add the
/// auto-scaled amortized-encoder consistency penalty to an already-computed
/// REML criterion at the converged dictionary. Both the public
/// [`Self::reml_criterion_cotrained`] entry point and the outer-loop value /
/// gradient lanes (`SaeManifoldOuterObjective::fold_cotrain_consistency`)
/// route through THIS function, so the folded objective cannot drift between
/// the criterion and the cascade-ranked cost (the objective↔gradient desync
/// bug class). The weights are auto-scaled to the REML magnitude (`max(|REML|,
/// 1)`) so the penalty is a bounded, scale-free fraction of the objective
/// regardless of problem scale; the fold carries no analytic gradient (under
/// Design A the REML λ-gradient stays the exact implicit-function path).
#[must_use]
pub fn fold_cotrain_consistency(
reml_cost: f64,
consistency: &AmortizedEncoderConsistency,
) -> f64 {
let reml_scale = reml_cost.abs().max(1.0);
reml_cost
+ COTRAIN_RECON_WEIGHT * reml_scale * consistency.recon_consistency
+ COTRAIN_CERT_WEIGHT * reml_scale * consistency.uncertified_fraction
}
/// #1154 item 2 — warm-start the inner latent coordinates from the amortized
/// encoder (Design A). Builds the per-chart IFT-Jacobian atlas from the
/// CURRENT dictionary, runs the one-mat-vec amortized encode of `target`
/// against each atom at the rho-resolved assignment masses, and overwrites
/// each atom's stored latent coords with the predicted `t̂` ON THE ROWS THE
/// KANTOROVICH CERTIFICATE ACCEPTS. Uncertified rows are left at their
/// current coords (the previous-iterate start), so the
/// warm-start can only HELP — a row the cheap predictor cannot certify never
/// corrupts the seed. The subsequent inner Newton refines from this seed to
/// the SAME stationary point (the warm-start changes only the basin entry,
/// not the root), so the REML λ-gradient stays exactly the implicit-function
/// path and the criterion is unchanged at convergence — the amortized encoder
/// only accelerates/co-adapts the inner solve, it never replaces the
/// stationary point.
///
/// Returns the number of (row, atom) coords actually warm-started (the
/// certified-prediction count), for instrumentation / tests. A first-build
/// dictionary with no usable charts simply warm-starts nothing and returns 0
/// (the cold path is byte-for-byte unchanged).
pub fn warm_start_latents_from_amortized_encoder(
&mut self,
target: ArrayView2<'_, f64>,
rho: &SaeManifoldRho,
) -> Result<usize, String> {
let n = self.n_obs();
let k_atoms = self.k_atoms();
if n == 0 || k_atoms == 0 {
return Ok(0);
}
let amplitudes = self.fitted_assignment_amplitudes(rho)?;
let encodes = self.amortized_encode_target(target, amplitudes.view())?;
let mut warm_started = 0usize;
for atom_idx in 0..k_atoms {
let d = self.atoms[atom_idx].latent_dim;
if d == 0 {
continue;
}
let result = &encodes[atom_idx];
// Start from the atom's CURRENT coords so uncertified rows are left
// exactly as they were; overwrite only the certified predictions.
let mut coords = self.assignment.coords[atom_idx].as_matrix();
if coords.dim() != (n, d) {
return Err(format!(
"warm_start_latents_from_amortized_encoder: atom {atom_idx} coords {:?} != (n={n}, d={d})",
coords.dim()
));
}
for row in 0..n {
if !result.certified[row] {
continue;
}
for axis in 0..d {
coords[[row, axis]] = result.coords[[row, axis]];
}
warm_started += 1;
}
// `as_matrix` lays coords out row-major (`[[row, axis]]`), exactly the
// `values[row*d + axis]` order `set_flat` expects, so a plain
// row-major iterator reconstructs the flat vector.
let flat = Array1::from_iter(coords.iter().copied());
self.assignment.coords[atom_idx].set_flat(flat.view());
}
// The basis caches must follow the freshly-seeded coords so the next
// inner solve evaluates Φ at the warm-started t̂, not the stale coords.
self.refresh_basis_from_current_coords()?;
Ok(warm_started)
}
pub fn loss(
&self,
target: ArrayView2<'_, f64>,
rho: &SaeManifoldRho,
) -> Result<SaeManifoldLoss, String> {
self.loss_scaled(target, rho, 1.0)
}
/// Penalized objective with a `penalty_scale` applied to the β-tier
/// (decoder smoothness) penalty, mirroring
/// [`Self::assemble_arrow_schur_scaled`]. The streaming line search sums
/// per-chunk `loss_scaled(..., n_chunk / N)` so that the global smoothness
/// penalty is counted exactly once across a pass while the per-row data,
/// assignment-prior, and ARD terms sum naturally. `penalty_scale == 1.0`
/// recovers the full-batch objective.
pub fn loss_scaled(
&self,
target: ArrayView2<'_, f64>,
rho: &SaeManifoldRho,
penalty_scale: f64,
) -> Result<SaeManifoldLoss, String> {
if !(penalty_scale.is_finite() && penalty_scale > 0.0) {
return Err(format!(
"SaeManifoldTerm::loss_scaled: penalty_scale must be finite and positive; got {penalty_scale}"
));
}
if target.dim() != (self.n_obs(), self.output_dim()) {
return Err(format!(
"SaeManifoldTerm::loss: Z must be ({}, {}); got {:?}",
self.n_obs(),
self.output_dim(),
target.dim()
));
}
// The likelihood whitens through the RowMetric **only** when the metric
// is a genuinely estimated noise model (`metric.whitens_likelihood()`,
// i.e. `WhitenedStructured` — the #974 residual-covariance seam). For
// Euclidean (default `None`) and for the OutputFisher *gauge* metric the
// reconstruction data-fit stays the isotropic `0.5 * Σ r²`: a gauge /
// output-Fisher inner product must NOT silently replace the
// reconstruction loss with a Fisher pullback (#980). It only drives the
// gauge (see `analytic_penalties::corrected_isometry_penalty`). The
// producer of `WhitenedStructured` is
// `inference::residual_factor::StructuredResidualModel::row_metric`; the
// SAME metric whitens the assembled gradient/Hessian in
// `assemble_arrow_schur` (the single #974 seam), so this value and that
// gradient cannot desync. Without a whitening metric this path is
// bit-for-bit the historical isotropic data-fit.
let whitens = self
.row_metric
.as_ref()
.is_some_and(|metric| metric.whitens_likelihood());
// #991 design honesty weights: the reconstruction channel of row `i`
// is weighted by `w_i` (mean-1 HT inclusion correction). The assembly
// applies the same `w_i` via a `√w_i` scaling of the row residual /
// Jacobian / β load at its single seam, so this value and that
// gradient/Hessian carry the identical per-row factor. `None` ⇒ the
// historical unweighted sum, bit-for-bit.
let row_loss_w = self.row_loss_weights.as_deref();
let n = self.n_obs();
let p = self.output_dim();
let k_atoms = self.k_atoms();
// #1017: the data-fit is the dominant per-line-search-trial cost (it
// re-runs every Armijo halving × every inner Newton iteration × every
// outer ρ evaluation). The old path materialised the whole `n × p`
// fitted matrix (`try_fitted_for_rho`) and then walked it AGAIN to form
// the residual sum — two sequential `n·p` passes plus an `n·p`
// allocation per trial. Fuse the reconstruction and the residual reduce
// into ONE row-parallel pass that never materialises the fitted matrix:
// each row decodes its atoms into per-worker scratch, differences
// against the target, and contributes its scalar `0.5·w·‖r‖²` to a
// chunk-ordered fold (bit-identical run-to-run). Per-worker scratch
// (`map_init`) keeps the only allocations one `g_buf`/`fitted_row` pair
// per rayon thread rather than per row. Stay sequential inside a worker
// (the topology race owns the outer pool) to avoid nested
// oversubscription.
let parallel = n >= SAE_LOSS_PARALLEL_ROW_MIN && rayon::current_thread_index().is_none();
let row_data_fit =
|row: usize, g_buf: &mut [f64], fitted_row: &mut [f64]| -> Result<f64, String> {
let a = self.assignment.try_assignments_row_for_rho(row, rho)?;
for slot in fitted_row.iter_mut() {
*slot = 0.0;
}
for atom_idx in 0..k_atoms {
self.atoms[atom_idx].fill_decoded_row(row, g_buf);
let a_k = a[atom_idx];
for out_col in 0..p {
fitted_row[out_col] += a_k * g_buf[out_col];
}
}
for out_col in 0..p {
fitted_row[out_col] = target[[row, out_col]] - fitted_row[out_col];
}
let w_row = row_loss_w.map_or(1.0, |w| w[row]);
let mut acc = 0.0_f64;
match self.row_metric.as_ref() {
Some(metric) if whitens => {
let resid = ArrayView1::from(&fitted_row[..p]);
for w in metric.whiten_residual_row(row, resid) {
acc += 0.5 * w_row * w * w;
}
}
_ => {
for &r in fitted_row[..p].iter() {
acc += 0.5 * w_row * r * r;
}
}
}
Ok(acc)
};
let data_fit = if parallel {
use rayon::prelude::*;
const CHUNK: usize = 32;
let partials: Vec<Result<f64, String>> = (0..n)
.into_par_iter()
.chunks(CHUNK)
.map_init(
|| (vec![0.0_f64; p], vec![0.0_f64; p]),
|(g_buf, fitted_row), idxs| {
let mut acc = 0.0_f64;
for row in idxs {
acc += row_data_fit(row, g_buf, fitted_row)?;
}
Ok(acc)
},
)
.collect();
let mut total = 0.0_f64;
for partial in partials {
total += partial?;
}
total
} else {
let mut g_buf = vec![0.0_f64; p];
let mut fitted_row = vec![0.0_f64; p];
let mut total = 0.0_f64;
for row in 0..n {
total += row_data_fit(row, &mut g_buf, &mut fitted_row)?;
}
total
};
let assignment_sparsity = assignment_prior_value(&self.assignment, rho);
let smoothness = penalty_scale * self.decoder_smoothness_value(rho.lambda_smooth());
let ard = self.ard_value(rho)?;
Ok(SaeManifoldLoss {
data_fit,
assignment_sparsity,
smoothness,
ard,
evidence_gauge_deflated_directions: 0,
})
}
/// Reconstruction data-fit `0.5·Σ_i w_i·‖whiten(Z_i − R_i)‖²` for an EXPLICIT
/// reconstruction matrix `R` (e.g. the hard top-k–projected `fitted`), using
/// the SAME per-row metric and design-honesty weights as [`Self::loss_scaled`]
/// (the soft-assignment data-fit). The only difference is the residual source:
/// `loss_scaled` decodes the soft assignments on the fly, this consumes a
/// reconstruction the caller already assembled (so the projected loss and the
/// returned projected `fitted` describe one and the same model). The penalty
/// terms (`assignment_sparsity`/`smoothness`/`ard`) are decoder/ρ properties
/// the top-k gate does not change, so the caller keeps them from the soft
/// `loss_scaled` and only swaps this data-fit in — see #1232.
pub fn data_fit_for_reconstruction(
&self,
target: ArrayView2<'_, f64>,
reconstruction: ArrayView2<'_, f64>,
) -> Result<f64, String> {
let n = self.n_obs();
let p = self.output_dim();
if target.dim() != (n, p) {
return Err(format!(
"SaeManifoldTerm::data_fit_for_reconstruction: Z must be ({n}, {p}); got {:?}",
target.dim()
));
}
if reconstruction.dim() != (n, p) {
return Err(format!(
"SaeManifoldTerm::data_fit_for_reconstruction: reconstruction must be ({n}, {p}); got {:?}",
reconstruction.dim()
));
}
let whitens = self
.row_metric
.as_ref()
.is_some_and(|metric| metric.whitens_likelihood());
let row_loss_w = self.row_loss_weights.as_deref();
let mut resid = vec![0.0_f64; p];
let mut total = 0.0_f64;
for row in 0..n {
for out_col in 0..p {
resid[out_col] = target[[row, out_col]] - reconstruction[[row, out_col]];
}
let w_row = row_loss_w.map_or(1.0, |w| w[row]);
match self.row_metric.as_ref() {
Some(metric) if whitens => {
let r = ArrayView1::from(&resid[..p]);
for w in metric.whiten_residual_row(row, r) {
total += 0.5 * w_row * w * w;
}
}
_ => {
for &r in resid[..p].iter() {
total += 0.5 * w_row * r * r;
}
}
}
}
Ok(total)
}
pub fn analytic_penalty_value_total(
&self,
registry: &AnalyticPenaltyRegistry,
penalty_scale: f64,
) -> Result<f64, ArrowSchurError> {
if !(penalty_scale.is_finite() && penalty_scale > 0.0) {
return Err(ArrowSchurError::SchurFactorFailed {
reason: format!(
"SaeManifoldTerm::analytic_penalty_value_total: penalty_scale must be finite \
and positive; got {penalty_scale}"
),
});
}
let rho_global = Array1::<f64>::zeros(registry.total_rho_count());
let layout = registry.rho_layout();
let beta = self.flatten_beta();
let mut value = 0.0_f64;
for (penalty, (rho_slice, tier, name)) in registry.penalties.iter().zip(layout.iter()) {
let rho_local = rho_global.slice(s![rho_slice.clone()]);
// Skip the registry `ARDPenalty` here for the same reason it is
// skipped in `add_sae_analytic_penalty_contributions`: the coordinate
// ARD energy is already counted by `loss.ard` (the von-Mises
// `ard_value`), and the registry penalty's legacy Gaussian `½λt²` is
// period-discontinuous. Including it would double-count the energy and
// make this line-search objective jump across the branch cut while the
// assembled gradient (von-Mises only, after the assembly fix) stays
// continuous — i.e. a near-zero step would change the objective by a
// finite amount and Armijo would wrongly reject it.
if matches!(penalty, AnalyticPenaltyKind::Ard(_)) {
continue;
}
match tier {
PenaltyTier::Psi => {
if let AnalyticPenaltyKind::NuclearNorm(base) = penalty {
for (per_atom, start, end) in self.live_nuclear_norm_penalties(base) {
value += penalty_scale
* per_atom.value(beta.slice(s![start..end]), rho_local);
}
} else {
if !sae_penalty_is_row_block_supported(penalty) {
return Err(ArrowSchurError::SchurFactorFailed {
reason: format!(
"validate_analytic_penalty_registry should have refused \
non-row-block Psi-tier penalty {:?} (registry layout name \
{name:?})",
penalty.name()
),
});
}
for atom_idx in 0..self.k_atoms() {
let coord = &self.assignment.coords[atom_idx];
if let AnalyticPenaltyKind::Isometry(iso) = penalty {
let corrected_kind =
self.corrected_isometry_penalty(iso, atom_idx, coord)?;
value += corrected_kind.value(coord.as_flat().view(), rho_local);
} else if sae_coord_penalty_is_origin_anchored_magnitude(penalty) {
// Origin-anchored magnitude shrinkage (SCAD/MCP) is
// restricted to the Euclidean axes; periodic axes have
// no chart origin and would make this energy
// period-discontinuous (issue #795). This must mirror
// the gradient/curvature assembly in
// `add_sae_coord_penalty` exactly.
match sae_coord_penalty_euclidean_restriction(coord) {
Some((_axes, compacted)) => {
value += penalty.value(compacted.view(), rho_local);
}
None => {
value += penalty.value(coord.as_flat().view(), rho_local);
}
}
} else {
value += penalty.value(coord.as_flat().view(), rho_local);
}
}
}
}
PenaltyTier::Beta => {
if let AnalyticPenaltyKind::DecoderIncoherence(base) = penalty {
if let Some(per_fit) = self.live_decoder_incoherence_penalty(base) {
value += penalty_scale * per_fit.value(beta.view(), rho_local);
}
} else if let AnalyticPenaltyKind::MechanismSparsity(base) = penalty {
for (per_atom, start, end) in self.live_mechanism_sparsity_penalties(base) {
if start < end {
value += penalty_scale * per_atom.value(beta.view(), rho_local);
}
}
} else {
value += penalty_scale * penalty.value(beta.view(), rho_local);
}
}
PenaltyTier::Rho => {}
}
}
Ok(value)
}
/// Energy of the decoder-block analytic penalties that have no native
/// `SaeManifoldLoss` counterpart, evaluated at the current decoder `β` and
/// the converged SAE state. These act on the per-atom decoder coefficient
/// matrices: cross-atom decoder incoherence (#671), mechanism
/// (feature-group) sparsity, and nuclear-norm embedding rank (#672). Each
/// is injected with its live per-atom shape / co-activation before its
/// value is taken, mirroring the assemble path.
///
/// This is deliberately narrower than [`Self::analytic_penalty_value_total`]:
/// it excludes the Psi-tier coordinate / assignment penalties (ARD,
/// Isometry, ScadMcp, BlockOrthogonality, IBP/softmax assignment sparsity).
/// The SAE already carries its own ARD (`loss.ard`) and assignment sparsity
/// (`loss.assignment_sparsity`) energy, so adding the registry ARD /
/// assignment value on top would double-count, and the gauge-only
/// coordinate penalties are not part of the penalized deviance the
/// REML/Laplace criterion scores. The decoder-block penalties, by contrast,
/// are real penalized-energy terms with no `loss.*` representative: the
/// inner solve minimizes them (they enter `gb`/`hbb`) but they were absent
/// from the criterion scalar `v`. This restores that consistency so the
/// ρ-sweep ranks the same objective the inner solve descends — the #671
/// incoherence lever in particular now shapes model selection, not just the
/// Newton step.
///
/// NOTE: the coordinate-block penalties with no native `loss.*` twin
/// (`ScadMcp`, `BlockOrthogonality`) carry the same residual inconsistency
/// (scored in the line search via `penalized_objective_total`, absent from
/// the REML scalar). They are left out here because they share a registry
/// dispatch with the always-on `Isometry` gauge, whose inclusion in the
/// topology-comparison criterion is a separate design question (#673:
/// topology evidence is gauge-conditional). Folding the coord-tier energy in
/// is tracked apart from this #671 decoder fix.
pub fn analytic_decoder_penalty_value_total(
&self,
registry: &AnalyticPenaltyRegistry,
) -> Result<f64, ArrowSchurError> {
// Resolve each penalty's rho slice exactly as `analytic_penalty_value_total`
// does (registry-local rho at zeros), so a learnable decoder-penalty weight
// is honoured rather than indexing into an empty view.
let rho_global = Array1::<f64>::zeros(registry.total_rho_count());
let layout = registry.rho_layout();
let beta = self.flatten_beta();
let mut value = 0.0_f64;
for (penalty, (rho_slice, _tier, _name)) in registry.penalties.iter().zip(layout.iter()) {
let rho_local = rho_global.slice(s![rho_slice.clone()]);
match penalty {
AnalyticPenaltyKind::DecoderIncoherence(base) => {
if let Some(per_fit) = self.live_decoder_incoherence_penalty(base) {
value += per_fit.value(beta.view(), rho_local);
}
}
AnalyticPenaltyKind::MechanismSparsity(base) => {
for (per_atom, start, end) in self.live_mechanism_sparsity_penalties(base) {
if start < end {
value += per_atom.value(beta.view(), rho_local);
}
}
}
AnalyticPenaltyKind::NuclearNorm(base) => {
for (per_atom, start, end) in self.live_nuclear_norm_penalties(base) {
value += per_atom.value(beta.slice(s![start..end]), rho_local);
}
}
_ => {}
}
}
Ok(value)
}
/// Energy of the COORDINATE-tier isometry penalty(ies) at the converged
/// SAE state. This is the per-atom `½μ Σ_n ‖J_n^T W_n J_n / gbar − g_ref‖²`
/// summed over atoms, evaluated through `corrected_isometry_penalty` so the
/// live decoder/coordinate caches drive the value exactly as the assemble
/// path does. It has no `SaeManifoldLoss` twin (the loss carries only
/// data-fit / assignment / smoothness / ARD), so the Laplace/REML criterion
/// must add it explicitly to score the same penalized objective the inner
/// solve descends.
pub fn isometry_penalty_value_total(
&self,
registry: &AnalyticPenaltyRegistry,
) -> Result<f64, ArrowSchurError> {
let rho_global = Array1::<f64>::zeros(registry.total_rho_count());
let layout = registry.rho_layout();
let mut value = 0.0_f64;
for (penalty, (rho_slice, _tier, _name)) in registry.penalties.iter().zip(layout.iter()) {
if let AnalyticPenaltyKind::Isometry(iso) = penalty {
let rho_local = rho_global.slice(s![rho_slice.clone()]);
for atom_idx in 0..self.k_atoms() {
let coord = &self.assignment.coords[atom_idx];
let corrected_kind = self.corrected_isometry_penalty(iso, atom_idx, coord)?;
value += corrected_kind.value(coord.as_flat().view(), rho_local);
}
}
}
Ok(value)
}
/// Extra analytic-penalty energy that has no native `SaeManifoldLoss`
/// component but is part of the penalized objective ranked by the SAE
/// Laplace/REML criterion.
pub fn reml_extra_penalty_value_total(
&self,
registry: &AnalyticPenaltyRegistry,
) -> Result<f64, ArrowSchurError> {
Ok(self.analytic_decoder_penalty_value_total(registry)?
+ self.isometry_penalty_value_total(registry)?)
}
pub fn penalized_objective_total(
&self,
target: ArrayView2<'_, f64>,
rho: &SaeManifoldRho,
registry: Option<&AnalyticPenaltyRegistry>,
penalty_scale: f64,
) -> Result<f64, String> {
let mut total = self.loss_scaled(target, rho, penalty_scale)?.total();
if let Some(analytic_registry) = registry {
total += self
.analytic_penalty_value_total(analytic_registry, penalty_scale)
.map_err(|err| format!("SaeManifoldTerm::penalized_objective_total: {err}"))?;
}
Ok(total)
}
pub(crate) fn decoder_smoothness_value(&self, lambda_smooth: f64) -> f64 {
// Smoothness penalty value is `0.5·λ·Σ_oc B[:,oc]ᵀ S B[:,oc]`. Form the
// `S·B` matrix product once per atom (O(M²·p)) and reduce against `B`
// with a single O(M·p) Hadamard sum, instead of the previous
// four-factor multiply-accumulate inside an `O(M²·p)` triple loop.
// The quadratic form only sees the symmetric part of `S`, so reusing
// the raw (un-symmetrised) `smooth_penalty` here is numerically
// identical to the symmetrised assembly form.
// Per-atom `S_k · B_k` products are independent across atoms, so they ride
// the multi-GPU batched smoothness GEMM (uniform-shape groups tiled across
// every device); `symmetrize = false` because the quadratic form only sees
// the symmetric part of `S` regardless. Exact CPU fallback per atom.
let sb_inputs: Vec<(ArrayView2<'_, f64>, ArrayView2<'_, f64>)> = self
.atoms
.iter()
.map(|atom| (atom.smooth_penalty.view(), atom.decoder_coefficients.view()))
.collect();
let sb_all = batched_smooth_sb(&sb_inputs, false);
let mut acc = 0.0;
for (atom, sb) in self.atoms.iter().zip(sb_all.iter()) {
acc += 0.5 * lambda_smooth * (&atom.decoder_coefficients * sb).sum();
}
acc
}
pub(crate) fn ard_value(&self, rho: &SaeManifoldRho) -> Result<f64, String> {
if rho.log_ard.len() != self.k_atoms() {
return Err(format!(
"ARD rho has {} atoms but term has {}",
rho.log_ard.len(),
self.k_atoms()
));
}
let n = self.n_obs();
let mut acc = 0.0;
for (atom_idx, coord) in self.assignment.coords.iter().enumerate() {
let d = coord.latent_dim();
if rho.log_ard[atom_idx].is_empty() {
continue;
}
if rho.log_ard[atom_idx].len() != d {
return Err(format!(
"ARD rho atom {atom_idx} has len {} but atom dim is {d}",
rho.log_ard[atom_idx].len()
));
}
// Per-axis periodicity selects the smooth von-Mises energy on
// wrapped (Circle) axes and the Gaussian on Euclidean axes.
let periods = coord.effective_axis_periods();
for axis in 0..d {
let log_alpha = rho.log_ard[atom_idx][axis];
// Clamp the log-precision before exponentiating: a raw
// `exp(log_ard)` overflows to `inf` for `log_ard ≳ 709`, and the
// `inf` precision then poisons the ARD energy / curvature with
// `inf · 0.0 = NaN` (#742, Issue 4).
let alpha = SaeManifoldRho::stable_exp_strength(log_alpha);
let period = periods[axis];
let mut energy = 0.0;
for row in 0..n {
let v = coord.row(row)[axis];
energy += ArdAxisPrior::eval(alpha, v, period).value;
}
// Negative-log prior for precision alpha. The data-dependent
// energy is the (Gaussian or von-Mises) coordinate prior; the
// accompanying normaliser is the precision log-partition.
//
// Euclidean axes keep the Gaussian normaliser `-0.5 n log α`.
// Periodic (von-Mises) axes use the EXACT von-Mises precision
// log-partition `n[-η + log I0(η)]`, η = α/κ², κ = 2π/P, rather
// than the Gaussian surrogate: the von-Mises partition function
// is `2π I0(η)` (up to the κ Jacobian), so the per-observation
// normaliser is `-η + log I0(η)` and is exact across the cut.
match period {
None => {
acc += energy - 0.5 * (n as f64) * log_alpha;
}
Some(p) => {
let kappa = std::f64::consts::TAU / p;
let eta = alpha / (kappa * kappa);
// Overflow-free `log I0(η)`; `bessel_i0(η).ln()` would be
// `+inf` for `η ≳ 709` (#1113).
let log_i0 = bessel_i0_log_and_ratio(eta).0;
acc += energy + (n as f64) * (-eta + log_i0);
}
}
}
}
Ok(acc)
}
/// Assemble the enlarged `(logits, t)` row-local Arrow-Schur system.
///
/// Full-batch entry point: a single chunk covering all rows, with the
/// β-tier penalties (decoder smoothness, ARD, analytic β penalties) carrying
/// their full strength. The streaming driver calls
/// [`Self::assemble_arrow_schur_scaled`] directly with a `penalty_scale`
/// equal to the minibatch fraction `n_chunk / N`, so that the sum of the
/// per-chunk β-tier contributions over a full pass reconstructs exactly the
/// single global β penalty (the smoothness/ARD/β terms are functions of `B`
/// and the global coordinates, not of the chunk's rows).
pub fn assemble_arrow_schur(
&mut self,
target: ArrayView2<'_, f64>,
rho: &SaeManifoldRho,
analytic_penalties: Option<&AnalyticPenaltyRegistry>,
) -> Result<ArrowSchurSystem, String> {
self.assemble_arrow_schur_scaled(target, rho, analytic_penalties, 1.0)
}
/// Assemble the row-local Arrow-Schur system with a `penalty_scale` applied
/// to the β-tier (decoder smoothness, ARD prior, analytic β penalties).
///
/// `penalty_scale == 1.0` recovers the full-batch assembly. The streaming
/// driver passes the minibatch fraction `n_chunk / N` so that the β-tier
/// reduced-Schur and gradient contributions of the chunks sum to exactly one
/// global copy across a full pass (data-fit, assignment-prior, and per-row
/// coord/logit analytic terms are *not* scaled — they are genuine per-row
/// sums).
pub fn assemble_arrow_schur_scaled(
&mut self,
target: ArrayView2<'_, f64>,
rho: &SaeManifoldRho,
analytic_penalties: Option<&AnalyticPenaltyRegistry>,
penalty_scale: f64,
) -> Result<ArrowSchurSystem, String> {
self.assemble_arrow_schur_scaled_with_beta_penalty_probe_threshold(
target,
rho,
analytic_penalties,
penalty_scale,
SAE_DENSE_BETA_PENALTY_PROBE_MAX_DIM,
)
}
pub(crate) fn assemble_arrow_schur_scaled_with_beta_penalty_probe_threshold(
&mut self,
target: ArrayView2<'_, f64>,
rho: &SaeManifoldRho,
analytic_penalties: Option<&AnalyticPenaltyRegistry>,
penalty_scale: f64,
dense_beta_penalty_probe_max_dim: usize,
) -> Result<ArrowSchurSystem, String> {
self.assemble_arrow_schur_inner(
target,
rho,
analytic_penalties,
penalty_scale,
dense_beta_penalty_probe_max_dim,
None,
)
}
/// Innermost assembly entry. `forced_layout` overrides the budget-derived
/// active-set layout so a caller can pin the dense (`Forced(None)`) or a
/// specific compact (`Forced(Some(layout))`) path — used by the
/// compact-vs-dense Riemannian-geometry equality regression test to drive
/// both layouts on identical data. `Computed` is the production path:
/// the layout is derived from the assignment mode + `sparse_active_plan`.
pub(crate) fn assemble_arrow_schur_inner(
&mut self,
target: ArrayView2<'_, f64>,
rho: &SaeManifoldRho,
analytic_penalties: Option<&AnalyticPenaltyRegistry>,
penalty_scale: f64,
dense_beta_penalty_probe_max_dim: usize,
forced_layout: ForcedRowLayout,
) -> Result<ArrowSchurSystem, String> {
if !(penalty_scale.is_finite() && penalty_scale > 0.0) {
return Err(format!(
"SaeManifoldTerm::assemble_arrow_schur_scaled: penalty_scale must be finite and positive; got {penalty_scale}"
));
}
if target.dim() != (self.n_obs(), self.output_dim()) {
return Err(format!(
"SaeManifoldTerm::assemble_arrow_schur: Z must be ({}, {}); got {:?}",
self.n_obs(),
self.output_dim(),
target.dim()
));
}
if rho.log_ard.len() != self.k_atoms() {
return Err(format!(
"SaeManifoldTerm::assemble_arrow_schur: log_ard length {} != K {}",
rho.log_ard.len(),
self.k_atoms()
));
}
for (atom_idx, coord) in self.assignment.coords.iter().enumerate() {
let ard_len = rho.log_ard[atom_idx].len();
let d = coord.latent_dim();
if ard_len != 0 && ard_len != d {
return Err(format!(
"SaeManifoldTerm::assemble_arrow_schur: log_ard atom {atom_idx} \
has len {ard_len}; expected 0 (disabled) or atom dim {d}"
));
}
}
// Reparameterize each atom's roughness Gram into arc length at the
// current decoder/coordinates (issue #673). This is the single
// chokepoint for both the inner Newton assembly and the undamped
// evidence factorization, so freezing the pullback-metric weight here
// (lagged-diffusivity) keeps the smoothness value, gradient, Kronecker
// Hessian, and REML log-det mutually consistent within each assembly
// and makes the converged penalty — hence the topology evidence —
// gauge-invariant. Constant-speed (periodic) atoms are unaffected.
for atom in &mut self.atoms {
atom.refresh_intrinsic_smooth_penalty();
}
let n = self.n_obs();
let p = self.output_dim();
let k_atoms = self.k_atoms();
let assignment_dim = self.assignment.assignment_coord_dim();
let q = self.assignment.row_block_dim();
let beta_dim = self.beta_dim();
let frame_projection = FrameProjection::new(self);
let beta_offsets = frame_projection.beta_offsets.clone();
let coord_offsets = self.assignment.coord_offsets();
// β-tier decoder smoothness is a global (B-only) penalty; under a
// minibatch pass it is scaled by the chunk fraction so the per-chunk
// contributions sum to one global copy.
let lambda_smooth = rho.lambda_smooth() * penalty_scale;
let (assignment_grad, assignment_hdiag) =
assignment_prior_grad_hdiag(&self.assignment, rho)?;
// #1038 softmax entropy: the exact per-row Hessian in logits is dense
// (`H_kj = (λ/τ²) a_k[δ_kj(m−L_k−1)+a_j(L_k+L_j+1−2m)]`), not just the
// `assignment_hdiag` diagonal. Build the shared penalty + `scale = λ/τ²`
// once here so the dense row block written into `block.htt` below, the
// criterion's `log|H|`, and the #1006 θ-adjoint all differentiate the
// SAME operator. JumpReLU / IBP keep their (separately exact) diagonal /
// cross-row channels and leave this `None`. The block is gauge-null in
// isolation (`H·𝟙 = 0`); it is only ever summed onto the gauge-breaking
// data-fit row block before the Cholesky factor, never factored alone.
let softmax_dense: Option<(
crate::terms::analytic_penalties::SoftmaxAssignmentSparsityPenalty,
f64,
)> = match self.assignment.mode {
AssignmentMode::Softmax {
temperature,
sparsity,
} if k_atoms > 1 => {
let inv_tau = 1.0 / temperature;
let scale = rho.lambda_sparse() * sparsity * inv_tau * inv_tau;
Some((
crate::terms::analytic_penalties::SoftmaxAssignmentSparsityPenalty::new(
k_atoms,
temperature,
),
scale,
))
}
_ => None,
};
// Decoder smoothness penalty: build one KroneckerPenaltyOp per atom
// (structure = λ·S_k ⊗ I_p, offset = beta_offsets[k]) instead of
// materialising the dense K×K block. The gradient is a dense K-vector
// accumulated into `smooth_grad_gb` and written into sys.gb after sys
// is constructed (#296).
let mut smooth_ops: Vec<Arc<dyn BetaPenaltyOp>> = Vec::with_capacity(self.atoms.len());
// #972 / #977 T1: retain each atom's symmetrised `λ S_k` (`M_k × M_k`) so
// the frame transform can rebuild the smooth penalty in the factored
// coordinate space as `λ S_k ⊗ I_{r_k}` (the `tr(C_kᵀ S_k C_k)` form,
// using `U_kᵀU_k = I`). Unused — and not even read — on the full-`B`
// path, so this is a zero-cost capture there.
let mut smooth_scaled_s: Vec<Array2<f64>> = Vec::with_capacity(self.atoms.len());
let mut smooth_grad_gb = vec![0.0_f64; beta_dim];
// #1117 — rank deficiency is handled at the basis layer: any
// rank-deficient atom was reparametrized onto its data-supported subspace
// at fit entry (`reduce_atoms_to_data_supported_rank`), so the β-tier here
// always sees a full-rank design and needs no step-time data-null
// deflation operator. The well-conditioned (full-rank) path is unchanged.
// Per-atom smoothness-gradient GEMMs `½(S_k+S_kᵀ)·B_k` are independent
// across atoms; batch them across ALL GPUs (uniform-shape tiles) and
// scale by `lambda_smooth` below. `symmetrize = true` reproduces the
// per-atom symmetrised `scaled_s/λ` used by the Kronecker op. Exact CPU
// fallback per atom keeps the result bit-for-bit with the all-CPU path.
let sym_sb_inputs: Vec<(ArrayView2<'_, f64>, ArrayView2<'_, f64>)> = self
.atoms
.iter()
.map(|atom| (atom.smooth_penalty.view(), atom.decoder_coefficients.view()))
.collect();
let sym_sb_all = batched_smooth_sb(&sym_sb_inputs, true);
for (atom_idx, atom) in self.atoms.iter().enumerate() {
let m = atom.basis_size();
let off = beta_offsets[atom_idx];
// Symmetrise and scale the smoothness penalty matrix.
let mut scaled_s = Array2::<f64>::zeros((m, m));
for i in 0..m {
for j in 0..m {
let s_ij = 0.5 * (atom.smooth_penalty[[i, j]] + atom.smooth_penalty[[j, i]]);
scaled_s[[i, j]] = lambda_smooth * s_ij;
}
}
// Gradient: g[beta_i] += (λ S_k B_k)[i, out_col]. The (m×m)·(m×p)
// GEMM `½(S+Sᵀ)·B_k` was computed in the multi-GPU batch above; here
// we only apply the scalar `lambda_smooth`.
let sb = &sym_sb_all[atom_idx] * lambda_smooth;
for out_col in 0..p {
for i in 0..m {
let beta_i = off + i * p + out_col;
smooth_grad_gb[beta_i] += sb[[i, out_col]];
}
}
// IdentityRightKroneckerPenaltyOp: factor_a = λ·S_k (m×m), factor_b = I_p.
smooth_ops.push(Arc::new(IdentityRightKroneckerPenaltyOp {
factor_a: scaled_s.clone(),
p,
global_offset: off,
k: beta_dim,
}));
// Retain `λ S_k` for the factored rebuild (no-op cost on full-`B`).
smooth_scaled_s.push(scaled_s);
}
// Per-row active-set layout. Engaged for two regimes:
// * JumpReLU — structural gate plus the smooth prior's
// machine-precision support: atoms with
// `(logit - threshold)/tau > -36` enter the compact solve
// ([`jumprelu_in_optimization_band`]). Strictly gated-off atoms
// (logit ≤ threshold) carry zero assignment mass so their data-fit
// reconstruction contribution and data-fit logit JVP are zero, but
// supported atoms keep value-consistent prior gradient in the row block.
// * IBP-MAP at large `K` — the dense `(m_total · p)²` data
// Gram is infeasible, so each row is truncated to its
// top-`k_active` atoms above a relative magnitude cutoff
// ([`Self::sparse_active_plan`]). Small-`K` problems return `None`
// and keep the exact full-support layout.
// The compact row block is sized `q_active = |active| + Σ_{k∈active}
// d_k` instead of the full `q`.
let coord_dims: Vec<usize> = self
.assignment
.coords
.iter()
.map(|c| c.latent_dim())
.collect();
let row_layout: Option<SaeRowLayout> = match forced_layout {
Some(layout) => layout,
None => match self.assignment.mode {
AssignmentMode::JumpReLU {
threshold,
temperature,
} => Some(SaeRowLayout::from_jumprelu(
n,
k_atoms,
threshold,
temperature,
&self.assignment.logits,
coord_dims.clone(),
self.assignment.coord_offsets(),
)),
AssignmentMode::Softmax { .. } => None,
AssignmentMode::IBPMap { .. } => {
match self.sparse_active_plan() {
Some((k_active_cap, relative_cutoff)) => {
// Build per-row dense assignments once to derive the
// active set; the row loop re-derives `assignments`
// (cheap gate map at the same rho) and reuses these
// active sets.
let mut assignments_all = Vec::with_capacity(n);
for row in 0..n {
assignments_all
.push(self.assignment.try_assignments_row_for_rho(row, rho)?);
}
// Absolute cutoff = relative_cutoff · max row peak, so a
// single threshold drops sub-1e-3 mass across all rows.
let peak = assignments_all
.iter()
.flat_map(|a| a.iter())
.fold(0.0_f64, |m, &v| m.max(v.abs()));
let cutoff = relative_cutoff * peak;
Some(SaeRowLayout::from_dense_weights(
&assignments_all,
k_active_cap,
cutoff,
coord_dims.clone(),
self.assignment.coord_offsets(),
))
}
None => None,
}
}
},
};
// #974 likelihood-whitening seam. The single per-row decision: when the
// installed `RowMetric` is a genuinely estimated noise model
// (`whitens_likelihood()` — only `WhitenedStructured`), the
// reconstruction data-fit, its t-block Gauss-Newton row block, AND the
// β-tier data-fit gradient are all assembled through the SAME per-row
// metric `M_n = U_n U_nᵀ = Σ_n^{-1}`. There is exactly ONE construction
// site (the `whiten_rows` closure below), so the value the line-search
// sums and the gradient/Hessian the Newton step solves cannot drift apart
// (the objective↔gradient-desync cure). For Euclidean / OutputFisher /
// no-metric the closure is the identity and every downstream loop is
// byte-identical to the historical isotropic path.
let whitens_likelihood = self
.row_metric
.as_ref()
.is_some_and(|metric| metric.whitens_likelihood());
// #972 / #977 T1: engage the FACTORED Grassmann-coordinate β-tier when
// any atom has an active decoder frame. The closed-form factorization
// `Φᵀ(G ⊗ I_p)Φ = G ⊗ (U_iᵀU_j)` is EXACT only for the isotropic
// likelihood; under an active whitening metric (`whitens_likelihood()`,
// only `WhitenedStructured`) the per-row output factor would be
// `U_iᵀ M_n U_j` and does NOT factor out of the basis Gram, so we fall
// back to the full-`B` path there (frames + whitening is out of scope —
// see #974). The common Euclidean / OutputFisher / no-metric case factors
// cleanly. When `frames_engaged` is false, EVERY β-tier object below is
// assembled bit-for-bit as the historical full-`B` path.
let frames_engaged = self.any_frame_active() && !whitens_likelihood;
let admission_plan = self
.streaming_plan()
.admitted_or_error(self.n_obs(), self.output_dim(), self.k_atoms())
.map_err(|err| format!("SaeManifoldTerm::assemble_arrow_schur: {err}"))?;
let dense_beta_curvature = admission_plan.direct_admitted
&& !(frames_engaged && beta_dim > dense_beta_penalty_probe_max_dim);
let row_htbeta_dim = if frames_engaged {
self.factored_border_dim()
} else {
beta_dim
};
// Build the Arrow-Schur system: heterogeneous row dims when a compact
// layout is active, uniform `q` otherwise.
let mut sys = if let Some(ref layout) = row_layout {
let per_row_dims: Vec<usize> = (0..n).map(|row| layout.row_q_active(row)).collect();
if dense_beta_curvature {
let hbb_workspace = self.take_border_hbb_workspace(beta_dim);
ArrowSchurSystem::new_with_per_row_dims_and_hbb_and_htbeta_cols(
per_row_dims,
beta_dim,
hbb_workspace,
row_htbeta_dim,
)
} else {
self.border_hbb_workspace = Array2::<f64>::zeros((0, 0));
ArrowSchurSystem::new_with_per_row_dims_empty_hbb_and_htbeta_cols(
per_row_dims,
beta_dim,
row_htbeta_dim,
)
}
} else if dense_beta_curvature {
let hbb_workspace = self.take_border_hbb_workspace(beta_dim);
ArrowSchurSystem::new_with_hbb_and_htbeta_cols(
n,
q,
beta_dim,
hbb_workspace,
row_htbeta_dim,
)
} else {
self.border_hbb_workspace = Array2::<f64>::zeros((0, 0));
ArrowSchurSystem::new_with_empty_hbb_and_htbeta_cols(n, q, beta_dim, row_htbeta_dim)
};
// Apply accumulated smoothness-penalty gradients into sys.gb.
for (i, g) in smooth_grad_gb.iter().enumerate() {
sys.gb[i] += g;
}
// `w_dim` is the whitened output dimension: `rank` of the metric factor
// when whitening, else `p` (identity). `error_white` is the whitened
// residual `U_nᵀ r_n ∈ ℝ^{w_dim}` whose squared norm is `r_nᵀ M_n r_n`,
// shared by the value path, the t-block GN, and (lifted back to p-space)
// the β-tier gradient.
let w_dim = match self.row_metric.as_ref() {
Some(metric) if whitens_likelihood => metric.metric_rank(),
_ => p,
};
// Data-fit Gauss-Newton β-Hessian is block-diagonal across the `p`
// output channels and identical in each: with the flat β layout
// `β[μ·p + oc] = B[μ, oc]` (μ enumerating (atom, basis_col)) the GN
// outer product `Jβᵀ Jβ` couples only equal `oc`, with the same
// `(M_total × M_total)` block `G[μ, μ'] = Σ_rows (a_k φ_k[m])(a_{k'} φ_{k'}[m'])`
// for every channel. So `H_data = G ⊗ I_p`. The `μ` index of an `a_phi`
// entry whose global β base is `beta_base` is `beta_base / p` (every
// `beta_offset` and the `basis_col·p` stride are multiples of `p`).
//
// `G` is only non-zero on `(atom_i, atom_j)` pairs that co-occur in
// some row's active set, so we accumulate it as a sparse map of dense
// per-atom-pair `(m_i × m_j)` blocks keyed by `(atom_i, atom_j)` rather
// than as a dense `(m_total × m_total)` matrix. At `K = 100K` with
// per-row active sets of size `k_active ≪ K`, only `O(N · k_active²)`
// pairs are ever touched, so the data Gram (and every matvec /
// diagonal pass over it via `SparseBlockKroneckerPenaltyOp`) tracks the
// active atoms instead of `K²`. In the dense full-support layout the
// map degenerates to every co-occurring pair, reproducing the dense
// Gram exactly. A `BTreeMap` key order keeps the installed op's
// fingerprint deterministic. The `μ`-space offset of atom `k` is
// `beta_offsets[k] / p`.
type SaeGBlocks = std::collections::BTreeMap<(usize, usize), Array2<f64>>;
let m_total: usize = self.atoms.iter().map(|a| a.basis_size()).sum();
let mu_offsets: Vec<usize> = beta_offsets.iter().map(|&off| off / p).collect();
// Stick-breaking prior for IBP-MAP depends only on (k_atoms, alpha_eff)
// which are constant across rows for the current rho; precompute once.
let ibp_prior_vec = match self.assignment.mode {
AssignmentMode::IBPMap { .. } => {
let alpha = self
.assignment
.mode
.resolved_ibp_alpha(rho)
.ok_or_else(|| "IBP assignment alpha resolution failed".to_string())?;
Some(ibp_stick_breaking_prior(k_atoms, alpha).to_vec())
}
_ => None,
};
let ibp_prior_slice = ibp_prior_vec.as_deref();
// #991 design honesty weights (mean-1 HT inclusion corrections); see
// the seam comment at the per-row residual below.
let row_loss_w = self.row_loss_weights.as_deref();
// Dense full-support index `[0, k_atoms)`, used by the row loop when no
// compact layout is engaged so the active-atom iteration is uniform.
let all_atoms_index: Vec<usize> = (0..k_atoms).collect();
// Per-atom per-axis periodicity, hoisted out of the row loop. Selects
// the smooth von-Mises coordinate prior on wrapped (Circle) axes and
// the Gaussian prior on Euclidean axes; see `ArdAxisPrior`.
let ard_axis_periods: Vec<Vec<Option<f64>>> = self
.assignment
.coords
.iter()
.map(|coord| coord.effective_axis_periods())
.collect();
struct SaeAssemblyRow {
pub(crate) row: usize,
pub(crate) block: ArrowRowBlock,
pub(crate) gb_delta: Vec<(usize, f64)>,
pub(crate) g_blocks: SaeGBlocks,
pub(crate) kron_a_phi: Option<Vec<(usize, f64)>>,
pub(crate) kron_jac: Option<Vec<f64>>,
}
// Per-row scratch reused across all rows a rayon worker processes
// (#1017). The assembly closure is re-run every inner Newton iteration ×
// every outer ρ evaluation; allocating these eight loop-invariant-sized
// buffers (`k_atoms·p`, several `p`, one `q·max(w_dim,p)`) once per
// worker via `map_init` — rather than once per (row × assembly) inside
// the closure — removes the dominant small-allocation traffic the
// eu-stack profile attributed to allocator/barrier spin at the SAE LLM
// shape (p≈5120). Every buffer is fully filled (or `.fill(0.0)`'d) before
// it is read each row, so reuse is bit-identical to the fresh-alloc path;
// `gb_delta`/`g_blocks` are NOT scratch (they move into the returned
// `SaeAssemblyRow`) and stay allocated per row.
struct RowScratch {
pub(crate) decoded: Array2<f64>,
pub(crate) dg_buf: Vec<f64>,
pub(crate) fitted: Array1<f64>,
pub(crate) error: Array1<f64>,
pub(crate) error_white: Vec<f64>,
pub(crate) error_metric: Array1<f64>,
pub(crate) jac_white: Vec<f64>,
pub(crate) decoded_scratch: Vec<f64>,
}
use rayon::iter::{IntoParallelIterator, ParallelIterator};
let row_results: Vec<SaeAssemblyRow> = (0..n)
.into_par_iter()
.map_init(
|| RowScratch {
decoded: Array2::<f64>::zeros((k_atoms, p)),
dg_buf: vec![0.0_f64; p],
fitted: Array1::<f64>::zeros(p),
error: Array1::<f64>::zeros(p),
error_white: vec![0.0_f64; w_dim],
error_metric: Array1::<f64>::zeros(p),
jac_white: vec![0.0_f64; q * w_dim.max(p)],
decoded_scratch: vec![0.0_f64; p],
},
|scratch, row| -> Result<SaeAssemblyRow, String> {
let RowScratch {
decoded,
dg_buf,
fitted,
error,
error_white,
error_metric,
jac_white,
decoded_scratch,
} = scratch;
let mut gb_delta: Vec<(usize, f64)> = Vec::new();
let mut g_blocks: SaeGBlocks = std::collections::BTreeMap::new();
let assignments = self.assignment.try_assignments_row_for_rho(row, rho)?;
// Reconstruction uses the row's active support: for the dense
// full-support layout this is all atoms (exact); for a compact
// layout the dropped atoms carry negligible `O(a)` reconstruction
// mass and zero curvature, so excluding them keeps `fitted`,
// `error`, and the logit-JVP cross term `(decoded[k] − fitted)`
// mutually consistent with the curvature actually assembled.
fitted.fill(0.0);
let row_active_owned: Option<&[usize]> =
row_layout.as_ref().map(|l| l.active_atoms[row].as_slice());
match row_active_owned {
Some(active) => {
for &atom_idx in active {
let a_k = assignments[atom_idx];
self.atoms[atom_idx]
.fill_decoded_row(row, decoded_scratch.as_mut_slice());
for out_col in 0..p {
decoded[[atom_idx, out_col]] = decoded_scratch[out_col];
fitted[out_col] += a_k * decoded_scratch[out_col];
}
}
}
None => {
for atom_idx in 0..k_atoms {
let a_k = assignments[atom_idx];
self.atoms[atom_idx]
.fill_decoded_row(row, decoded_scratch.as_mut_slice());
for out_col in 0..p {
decoded[[atom_idx, out_col]] = decoded_scratch[out_col];
fitted[out_col] += a_k * decoded_scratch[out_col];
}
}
}
}
for out_col in 0..p {
error[out_col] = fitted[out_col] - target[[row, out_col]];
}
// #991 design-honesty seam: a per-row scalar weight `w_row` on the
// reconstruction channel is exactly the metric `w_row · I_p`, so it
// is realized as a `√w_row` scaling of the THREE row-local data
// quantities at their construction sites — this residual, the
// latent Jacobian (below), and the β basis load `a·φ` (below).
// Every downstream data object then carries exactly one factor of
// `w_row` (gt, htt, htbeta, the β Gram `G`, and the β gradient),
// matching the `w_row`-weighted value `loss_scaled` sums; the
// per-row latent priors (assignment / ARD, added to `gt`/`htt`
// further down) are deliberately unweighted — see the
// `row_loss_weights` field docs. `None` ⇒ `sqrt_row_w == 1.0` and
// no multiply is applied (bit-identical unweighted path).
let sqrt_row_w = row_loss_w.map_or(1.0, |w| w[row].sqrt());
if sqrt_row_w != 1.0 {
for out_col in 0..p {
error[out_col] *= sqrt_row_w;
}
}
// #974 seam (step 1/2): whiten the per-row residual ONCE.
// * not whitening ⇒ `error_white == error` (length p) and
// `error_metric == error`; every downstream loop is the
// historical isotropic path bit-for-bit.
// * whitening ⇒ `error_white = U_nᵀ r_n ∈ ℝ^{w_dim}` (its squared
// norm is `r_nᵀ M_n r_n`, the value the data-fit sums) and
// `error_metric = U_n (U_nᵀ r_n) = M_n r_n ∈ ℝ^p` (the p-space
// metric-applied residual the β-tier gradient contracts).
match self.row_metric.as_ref() {
Some(metric) if whitens_likelihood => {
let wr = metric.whiten_residual_row(row, error.view());
for (slot, &v) in error_white.iter_mut().zip(wr.iter()) {
*slot = v;
}
let mr = metric.apply_metric_row(row, error.view());
for (slot, &v) in error_metric.iter_mut().zip(mr.iter()) {
*slot = v;
}
}
_ => {
for out_col in 0..p {
error_white[out_col] = error[out_col];
error_metric[out_col] = error[out_col];
}
}
}
// Determine whether this row uses the compact active-set layout.
// * JumpReLU: gated atoms plus the smooth prior's
// machine-precision support enter.
// * IBP-MAP at large K: only the top-`k_active` atoms.
// * Otherwise (small K): the dense uniform-q layout.
let (q_row, mut local_jac_row) = if let Some(layout) = row_layout.as_ref() {
let active = &layout.active_atoms[row];
let starts = &layout.coord_starts[row];
let q_active = layout.row_q_active(row);
let mut jac_compact = Array2::<f64>::zeros((q_active, p));
// Logit JVP rows for active atoms only, using the per-mode
// assignment sensitivity `da_k/dl_k` contracted into the
// decoded / fitted-corrected output direction.
let logits_row = self.assignment.logits.row(row);
for (j, &k) in active.iter().enumerate() {
fill_active_atom_logit_jvp(
ActiveAtomLogitJvp {
mode: self.assignment.mode,
k,
logit_k: logits_row[k],
a_k: assignments[k],
decoded_k: decoded.row(k),
fitted: fitted.view(),
ibp_prior: ibp_prior_slice,
compact_index: j,
},
&mut jac_compact,
);
}
// Coordinate JVP rows for active atoms only.
for (j, &k) in active.iter().enumerate() {
let d = self.atoms[k].latent_dim;
let a_k = assignments[k];
let coord_start = starts[j];
for axis in 0..d {
self.atoms[k].fill_decoded_derivative_row(
row,
axis,
dg_buf.as_mut_slice(),
);
for out_col in 0..p {
jac_compact[[coord_start + axis, out_col]] =
a_k * dg_buf[out_col];
}
}
}
(q_active, jac_compact)
} else {
// Fresh per-row Jacobian, structurally identical to the
// JumpReLU branch: every (q × p) element is unconditionally
// overwritten below (assignment-chart JVP rows + coordinate rows), so the
// `Array2::zeros` allocation needs no separate `fill(0.0)` and
// the populated buffer is returned by move without a clone.
let mut jac_row = Array2::<f64>::zeros((q, p));
fill_assignment_logit_jvp_rows(
self.assignment.mode,
self.assignment.logits.row(row),
assignments.view(),
decoded.view(),
fitted.view(),
ibp_prior_slice,
&mut jac_row,
);
// Coordinate columns for all atoms.
for atom_idx in 0..k_atoms {
let d = self.atoms[atom_idx].latent_dim;
let off = coord_offsets[atom_idx];
let a_k = assignments[atom_idx];
for axis in 0..d {
self.atoms[atom_idx].fill_decoded_derivative_row(
row,
axis,
dg_buf.as_mut_slice(),
);
for out_col in 0..p {
jac_row[[off + axis, out_col]] = a_k * dg_buf[out_col];
}
}
}
(q, jac_row)
};
// #991 design-honesty seam, Jacobian leg: scale the row's latent
// Jacobian by `√w_row` BEFORE the whitening / Kronecker capture so
// htt (= J̃J̃ᵀ), the data part of gt (= J̃ẽ, the residual already
// carries its own √w_row), and the htbeta cross block (J paired
// with the √w_row-scaled β load below) each carry exactly one
// factor of `w_row`. No-op on the unweighted path.
if sqrt_row_w != 1.0 {
for a in 0..q_row {
for out_col in 0..p {
local_jac_row[[a, out_col]] *= sqrt_row_w;
}
}
}
// #974 seam (step 2/2): whiten the per-row Jacobian through the SAME
// metric the residual was whitened by. `jac_white[a*w_dim + k]` holds
// `J̃[a, k] = Σ_out U_n[out, k] · J_n[a, out]` so the t-block
// Gauss-Newton row block is `htt = J̃ J̃ᵀ = J_n M_n J_nᵀ` and
// `gt = J̃ ẽ = J_nᵀ M_n r_n`. When not whitening, `w_dim == p` and the
// whitened jac equals the raw Jacobian, so htt/gt are byte-identical
// to the historical isotropic assembly. Because the SAME `error_white`
// feeds both the value-path data-fit (Σ½ ẽ²) and this gradient
// (J̃ ẽ), the objective and its t-block gradient share one whitening
// — they cannot desync.
if whitens_likelihood {
if let Some(metric) = self.row_metric.as_ref() {
for a in 0..q_row {
for k in 0..w_dim {
let mut acc = 0.0;
// U_n[out, k] read through the metric's factor layout.
for out_col in 0..p {
acc += metric.factor_entry(row, out_col, k)
* local_jac_row[[a, out_col]];
}
jac_white[a * w_dim + k] = acc;
}
}
}
} else {
for a in 0..q_row {
for out_col in 0..p {
jac_white[a * w_dim + out_col] = local_jac_row[[a, out_col]];
}
}
}
// Build the per-row Arrow-Schur block at the row's active dim.
let mut block = ArrowRowBlock::new(q_row, row_htbeta_dim);
for a in 0..q_row {
let jac_a = &jac_white[a * w_dim..(a + 1) * w_dim];
let g = jac_a
.iter()
.zip(error_white.iter())
.map(|(&j, &e)| j * e)
.sum::<f64>();
block.gt[a] += g;
for b in 0..q_row {
let jac_b = &jac_white[b * w_dim..(b + 1) * w_dim];
let h = jac_a
.iter()
.zip(jac_b.iter())
.map(|(&ja, &jb)| ja * jb)
.sum::<f64>();
block.htt[[a, b]] += h;
}
}
// Assignment prior in logit space.
// For compact layout: position `j` = active_atoms index.
// For dense layout: position `atom_idx` directly.
//
// H-consistency note (#1006 audit). This `assignment_hdiag` is the
// assignment channel's raw diagonal curvature, added un-majorized. It
// is exact for JumpReLU and exact within each IBP row/column diagonal,
// but it is a deliberate diagonal approximation for two full-Hessian
// structures that the current factorization does not yet carry (#1038):
//
// * softmax entropy has dense within-row Hessian
// H_kj = (λ/τ²) a_k[δ_kj(m-L_k-1) + a_j(L_k+L_j+1-2m)];
// this block stores only its diagonal.
// * IBP empirical-π has cross-row rank-one terms per column
// H_(i,k),(j,k) = w score_derivative_k z'_ik z'_jk for i != j;
// this row-local block stores only the diagonal/self-row part.
// The exact scalar `D`-coefficient `d_k = w·s'_k` is now
// surfaced as `IbpHessianDiagThirdChannels::cross_row_d`
// (FD-verified against ∂²value/∂ℓ_ik∂ℓ_jk in
// `ibp_cross_row_woodbury_d_matches_full_off_diagonal_hessian`),
// and `z_jac` carries `u_k`'s entries `z'_ik`. The exact
// determinant-lemma consumer is
// log det(I_K + D UᵀH₀'⁻¹U) on the NO-SELF base
// H₀' = H₀ − Σ_k d_k diag(z'_ik²) — which requires re-factoring
// the per-row logit-slot diagonal (a factorization-side change
// in `solver::arrow_schur`, outside this assembly chokepoint).
//
// The criterion's log|H| and Γ adjoint differentiate this same
// assembled diagonal/quasi-Laplace Hessian, so value and gradient stay
// on one branch. A future dense-row softmax or IBP Woodbury correction
// must update both assembly and the θ-adjoint together.
let assignment_base = row * k_atoms;
if let Some(layout) = row_layout.as_ref() {
let active = &layout.active_atoms[row];
for (j, &k) in active.iter().enumerate() {
block.gt[j] += assignment_grad[assignment_base + k];
block.htt[[j, j]] += assignment_hdiag[assignment_base + k];
}
} else {
for free_idx in 0..assignment_dim {
block.gt[free_idx] += assignment_grad[assignment_base + free_idx];
}
if let Some((penalty, scale)) = softmax_dense.as_ref() {
// #1190: write the PSD softmax Fisher-information metric
// `G = scale·(diag(a) − a aᵀ)` onto the row's logit block in
// place of the EXACT entropy Hessian. The entropy Hessian is
// INDEFINITE (concave directions on long-tailed rows), which
// drove the per-row evidence block non-PD and forced the
// downstream Faddeev–Popov deflation to flatten data-relevant
// logit directions (under-identifying the atoms) while leaving
// value/adjoint on two branches (log|H| read the deflated
// factor; the θ-adjoint differentiated the raw dense Hessian).
// `G` is a covariance/Gram, hence exactly PSD and smooth in the
// logits, so the block is PD by construction and the deflation
// no longer fires on the entropy block. Because the entropy
// penalty is a FIXED prior whose stationary point is set by its
// (unchanged) EXACT gradient, substituting its curvature with
// the Fisher metric only conditions the Newton step and the
// Laplace normalizer's curvature operator — it does NOT move the
// optimum (a fixed-prior curvature majorization, exactly like
// the ARD `prior.hess.max(0.0)` precedent ~line 3848).
//
// Softmax uses the REDUCED K−1 free-logit chart (the last
// reference logit is fixed at 0, `assignment_coord_dim() = K−1`).
// Holding z_{K-1} fixed, the reduced curvature over the free
// logits 0..K−1 is exactly the top-left (K−1)×(K−1) submatrix of
// the full K×K metric (the fixed logit contributes no row/column
// to the free curvature). The criterion's `log|H|` and the
// #1006 θ-adjoint differentiate this SAME `G` (see the
// `row_fisher_metric_logit_derivative` site below), so value and
// adjoint stay on one exact branch.
let row_logits: Vec<f64> = (0..k_atoms)
.map(|k| self.assignment.logits[[row, k]])
.collect();
let h_dense = penalty.row_fisher_metric(&row_logits, *scale);
for ki in 0..assignment_dim {
for kj in 0..assignment_dim {
block.htt[[ki, kj]] += h_dense[[ki, kj]];
}
}
} else {
for free_idx in 0..assignment_dim {
block.htt[[free_idx, free_idx]] +=
assignment_hdiag[assignment_base + free_idx];
}
}
}
// ARD on each on-atom coordinate.
// For compact layout: only active atoms; coord positions use compact starts.
// For dense layout: all atoms; coord positions use coord_offsets.
if let Some(layout) = row_layout.as_ref() {
let active = &layout.active_atoms[row];
let starts = &layout.coord_starts[row];
for (j, &k) in active.iter().enumerate() {
let coord = &self.assignment.coords[k];
let d = coord.latent_dim();
if rho.log_ard[k].is_empty() {
continue;
}
if rho.log_ard[k].len() != d {
return Err(format!(
"ARD rho atom {k} has len {} but atom dim is {d}",
rho.log_ard[k].len()
));
}
let row_t = coord.row(row);
let periods = &ard_axis_periods[k];
for axis in 0..d {
// ARD on coords is a genuine per-row prior (each row
// contributes the per-axis prior energy), so it is NOT
// minibatch-scaled — the per-chunk row sums already
// reconstruct the full coordinate prior across a pass.
// The value (`ard_value`/`loss.ard`) and the gradient
// both come from the SAME `ArdAxisPrior` energy, so they
// stay FD-consistent on periodic axes. The exact
// von-Mises curvature `V'' = α·cos(κt)` is INDEFINITE —
// it goes negative for |t| past a quarter period — so
// writing it raw into the Newton/Schur `htt` diagonal
// makes that PSD curvature block indefinite and the Schur
// Cholesky (used both for the Newton step and the exact
// log-det) fails on a non-PD pivot. Accumulate the PSD
// majorizer `max(V'', 0)` instead, exactly as
// `add_sae_coord_penalty` does for the registry coord
// penalties: the positive part keeps `htt` PSD so the
// factorization succeeds, and majorizing the curvature of
// a fixed prior only damps the Newton step — it does not
// move the stationary point (the gradient, which sets the
// fixed point, stays the exact `V'`).
let alpha =
SaeManifoldRho::stable_exp_strength(rho.log_ard[k][axis]);
let prior = ArdAxisPrior::eval(alpha, row_t[axis], periods[axis]);
block.gt[starts[j] + axis] += prior.grad;
block.htt[[starts[j] + axis, starts[j] + axis]] +=
prior.hess.max(0.0);
}
}
} else {
for atom_idx in 0..k_atoms {
let coord = &self.assignment.coords[atom_idx];
let d = coord.latent_dim();
if rho.log_ard[atom_idx].is_empty() {
continue;
}
if rho.log_ard[atom_idx].len() != d {
return Err(format!(
"ARD rho atom {atom_idx} has len {} but atom dim is {d}",
rho.log_ard[atom_idx].len()
));
}
let off = coord_offsets[atom_idx];
let row_t = coord.row(row);
let periods = &ard_axis_periods[atom_idx];
for axis in 0..d {
// PSD-majorize the (possibly negative) von-Mises curvature
// into the Newton/Schur `htt` block; see the compact-layout
// branch above for why `max(V'', 0)` is required to keep
// `htt` PD (the exact `V'' = α·cos κt` is indefinite past a
// quarter period and breaks the Schur/log-det Cholesky).
let alpha = SaeManifoldRho::stable_exp_strength(
rho.log_ard[atom_idx][axis],
);
let prior = ArdAxisPrior::eval(alpha, row_t[axis], periods[axis]);
block.gt[off + axis] += prior.grad;
block.htt[[off + axis, off + axis]] += prior.hess.max(0.0);
}
}
}
// Beta gradient/Hessian — Kronecker form J_β = φᵀ ⊗ I_p.
//
// The per-row beta Jacobian is
// J_β[out_col, beta_idx] = a_k · phi_k[basis_col] if out_col == out_col(beta_idx)
// 0 otherwise
// so the data-fit Gauss-Newton beta-Hessian factors as a rank-`p`
// sum of outer products. We pre-compute the per-(atom, basis_col)
// scalar `a_k · phi_k` once and reuse it across the `out_col`
// and inner `(atom_j, basis_col2)` loops.
//
// Full-B rows keep the matrix-free Kronecker path below. Factored
// rows write the `q_i × Σ M_k r_k` C-space cross slab directly by
// folding each output-channel contribution through the atom frame,
// so no `q_i × β_dim` slab is ever materialized.
//
// Only the row's active atoms contribute `a_phi` support and data
// curvature: in a compact layout (JumpReLU gate or large-K
// top-`k_active` truncation) the inactive atoms carry zero (gated)
// or sub-cutoff assignment mass and are excluded — this is what
// keeps both the htbeta support and the `G` accumulation
// `O(k_active)` rather than `O(K)`. In the dense full-support
// layout `row_active` spans all atoms.
let row_active: &[usize] = match row_layout.as_ref() {
Some(layout) => layout.active_atoms[row].as_slice(),
None => &all_atoms_index,
};
let mut a_phi: Vec<(usize, f64)> = Vec::with_capacity(row_active.len() * 4);
// Per-active-atom weighted basis row `a_k · φ_k[·]`, retained so the
// data Gram blocks can be accumulated as clean per-atom-pair outer
// products `(a_k φ_k) (a_{k'} φ_{k'})ᵀ`.
let mut weighted_phi: Vec<(usize, Vec<f64>)> =
Vec::with_capacity(row_active.len());
for &atom_idx in row_active {
let atom = &self.atoms[atom_idx];
let atom_beta_off = beta_offsets[atom_idx];
let m = atom.basis_size();
let a_k = assignments[atom_idx];
let mut wphi = Vec::with_capacity(m);
for basis_col in 0..m {
let phi = atom.basis_values[[row, basis_col]];
// #991 design-honesty seam, β leg: the `√w_row` here pairs
// with the `√w_row` on the residual (β gradient =
// `a·φ · M r` ⇒ w_row) and with itself (β Gram `G` and the
// htbeta Kronecker capture ⇒ w_row). `1.0` when unweighted.
let w = a_k * phi * sqrt_row_w;
a_phi.push((atom_beta_off + basis_col * p, w));
wphi.push(w);
}
weighted_phi.push((atom_idx, wphi));
}
// β data-fit gradient `gᵦ += J_βᵀ M_n r_n`. The β-Jacobian is
// `J_β = φ_nᵀ ⊗ I_p`, so `J_βᵀ M_n r_n = φ_n ⊗ (M_n r_n)` —
// contract the basis weight `a·φ` against the p-space metric-applied
// residual `error_metric` (= `M_n r_n`), the SAME whitening the value
// path and t-block share. When not whitening, `error_metric == error`
// and this is byte-identical to the historical `J_βᵀ r`.
for &(beta_base_i, j_beta_i) in a_phi.iter() {
if j_beta_i == 0.0 {
continue;
}
for out_col in 0..p {
gb_delta
.push((beta_base_i + out_col, j_beta_i * error_metric[out_col]));
// No dense hbb write — the sparse `G ⊗ I_p` op installed
// after the loop carries the data-fit GN β-Hessian.
}
}
if frames_engaged {
for &atom_idx in row_active {
let atom = &self.atoms[atom_idx];
let m = atom.basis_size();
let a_k = assignments[atom_idx];
for basis_col in 0..m {
let phi = atom.basis_values[[row, basis_col]];
let w = a_k * phi * sqrt_row_w;
if w == 0.0 {
continue;
}
let c_base = frame_projection.border_offsets[atom_idx]
+ basis_col * frame_projection.ranks[atom_idx];
for c in 0..q_row {
let mut hrow = block.htbeta.row_mut(c);
let hrow_slice =
hrow.as_slice_mut().expect("htbeta row is contiguous");
for out_col in 0..p {
let value = local_jac_row[[c, out_col]] * w;
frame_projection.accumulate_output_project(
atom_idx, c_base, out_col, value, hrow_slice,
);
}
}
}
}
}
// Data-fit GN β-Hessian: accumulate the channel-independent block
// `G[μ_i, μ_j] += (a_k φ_k)[μ_i] (a_{k'} φ_{k'})[μ_j]` into the
// sparse per-atom-pair map (the `out_col` dimension is carried by
// `I_p`). Only co-occurring `(atom_i, atom_j)` pairs are touched.
for ai in 0..weighted_phi.len() {
let (atom_i, ref wphi_i) = weighted_phi[ai];
let m_i = wphi_i.len();
for aj in 0..weighted_phi.len() {
let (atom_j, ref wphi_j) = weighted_phi[aj];
let m_j = wphi_j.len();
let blk = g_blocks
.entry((atom_i, atom_j))
.or_insert_with(|| Array2::<f64>::zeros((m_i, m_j)));
for li in 0..m_i {
let wi = wphi_i[li];
if wi == 0.0 {
continue;
}
for lj in 0..m_j {
blk[[li, lj]] += wi * wphi_j[lj];
}
}
}
}
let (kron_a_phi, kron_jac) = if !frames_engaged {
// Flatten local_jac_row row-major into a plain Vec<f64> (q_row * p entries).
let mut jac_flat = vec![0.0_f64; q_row * p];
for c in 0..q_row {
for j in 0..p {
jac_flat[c * p + j] = local_jac_row[[c, j]];
}
}
(Some(a_phi), Some(jac_flat))
} else {
(None, None)
};
Ok(SaeAssemblyRow {
row,
block,
gb_delta,
g_blocks,
kron_a_phi,
kron_jac,
})
},
)
.collect::<Result<Vec<_>, String>>()?;
let mut g_blocks: SaeGBlocks = std::collections::BTreeMap::new();
let mut kron_a_phi: Vec<Vec<(usize, f64)>> = Vec::with_capacity(n);
let mut kron_jac: Vec<Vec<f64>> = Vec::with_capacity(n);
for (row, row_result) in row_results.into_iter().enumerate() {
assert_eq!(
row, row_result.row,
"parallel SAE row assembly returned rows out of order"
);
for (idx, value) in row_result.gb_delta {
sys.gb[idx] += value;
}
for ((atom_i, atom_j), data) in row_result.g_blocks {
let m_i = data.nrows();
let m_j = data.ncols();
let blk = g_blocks
.entry((atom_i, atom_j))
.or_insert_with(|| Array2::<f64>::zeros((m_i, m_j)));
for li in 0..m_i {
for lj in 0..m_j {
blk[[li, lj]] += data[[li, lj]];
}
}
}
if !frames_engaged {
kron_a_phi.push(
row_result
.kron_a_phi
.expect("full-B SAE row assembly must return a_phi rows"),
);
kron_jac.push(
row_result
.kron_jac
.expect("full-B SAE row assembly must return local Jacobian rows"),
);
}
sys.rows[row] = row_result.block;
}
// Apply Riemannian geometry to the per-row row blocks (htt, gt) and
// also to the per-row Kronecker local Jacobians stored in kron_jac.
// When the SAE ext-coord manifold is non-Euclidean (any atom latent
// on sphere / circle / interval), the local Jacobian rows that map
// into the t-block tangent space must be projected via the per-row
// tangent projector P_i. This mirrors what
// `apply_riemannian_latent_geometry` does to `row.htbeta`, applied
// here to the (q × p) kron_jac so the Kronecker htbeta_matvec uses
// the Riemannian-projected form.
// Apply Riemannian geometry only for the dense uniform-q layout. Any
// compact active-set layout (JumpReLU gate or large-K softmax/IBP
// truncation) has heterogeneous q_i; the Riemannian projector path
// requires a uniform latent dimension. The sparse plan only engages on
// Euclidean ext-coord manifolds (see `sparse_active_plan`), so skipping
// the projector here is correct — there is nothing to project.
match row_layout.as_ref() {
None => {
let raw_gt_rows: Vec<Array1<f64>> =
sys.rows.iter().map(|row| row.gt.clone()).collect();
self.apply_sae_riemannian_geometry(&mut sys);
let manifold = self.ext_coord_manifold();
if !frames_engaged && !manifold.is_euclidean() {
let ext = self.ext_coord_matrix();
// Project the local Jacobian columns onto the tangent space at
// each row's ext-coord point. Each column `j` of the row's
// (q_row × p) Jacobian is an ambient-space vector of length
// `q_row`; the manifold projector acts on one such column at a
// time. Working directly on the row-major `jac_flat` storage via
// a single reusable `col_buf` avoids the two dense (q × p) copies
// (flatten→Array2, project, unflatten→Vec) that previously fired
// per row. `t_buf` still holds the row's ext-coord vector.
let mut t_buf = vec![0.0_f64; q];
let mut col_buf = Array1::<f64>::zeros(q);
for row_idx in 0..n {
let ext_row = ext.row(row_idx);
for (slot, &v) in t_buf.iter_mut().zip(ext_row.iter()) {
*slot = v;
}
let t_i = ArrayView1::from(t_buf.as_slice());
let raw_gt = raw_gt_rows[row_idx].view();
let jac_flat = &mut kron_jac[row_idx];
let q_row = jac_flat.len() / p;
for j in 0..p {
for c in 0..q_row {
col_buf[c] = jac_flat[c * p + j];
}
let projected_col = manifold.project_vector_to_gradient_tangent(
t_i,
raw_gt.slice(ndarray::s![..q_row]),
col_buf.slice(ndarray::s![..q_row]),
);
for c in 0..q_row {
jac_flat[c * p + j] = projected_col[c];
}
}
}
}
}
Some(layout) => {
// Compact active-set layout (#1117 follow-up): the dense
// `ext_coord_manifold()` is keyed to the uniform full-`q` block
// ordering, so it cannot be applied to the heterogeneous compact
// rows directly. Instead we rebuild, PER ROW, the product manifold
// and ext-coord point in that row's compact column order (see
// `compact_row_ext_manifold_and_point`) and apply the SAME three
// per-row Riemannian operations the dense
// `apply_riemannian_latent_geometry` applies — gradient tangent
// projection of `gt`, the Riemannian Hessian correction of `htt`,
// and the column tangent projection of `htbeta` — plus the
// identical Kronecker `kron_jac` column projection. On the shared
// active support this is byte-identical to slicing the dense
// product manifold, so engaging the sparse plan on a non-Euclidean
// ext manifold is now correct (the former
// `is_euclidean()`-only guard in `sparse_active_plan` is lifted).
//
// Euclidean ext manifolds still skip all of this (every
// per-row manifold is a product of Euclidean parts whose
// projector is the identity); we early-out so those rows stay
// byte-for-byte the historical compact path.
if !self.ext_coord_manifold().is_euclidean() {
for row_idx in 0..n {
let (manifold_i, point_i) =
self.compact_row_ext_manifold_and_point(row_idx, layout);
let t_i = point_i.view();
// gt / htt / htbeta on the compact ArrowRowBlock, exactly
// as `apply_riemannian_latent_geometry` does for dense
// uniform-q rows.
let gt_e = sys.rows[row_idx].gt.clone();
let htt_e = sys.rows[row_idx].htt.clone();
let htbeta_e = sys.rows[row_idx].htbeta.clone();
sys.rows[row_idx].gt =
manifold_i.project_gradient_to_tangent(t_i, gt_e.view());
sys.rows[row_idx].htt =
manifold_i.riemannian_hessian_matrix(t_i, gt_e.view(), htt_e.view());
sys.rows[row_idx].htbeta = manifold_i
.project_matrix_columns_to_gradient_tangent(
t_i,
gt_e.view(),
htbeta_e.view(),
);
// Kronecker local-Jacobian column projection (full-B path
// only), using the SAME pre-projection gradient `gt_e` so
// the cross-block geometry matches the dense branch.
if !frames_engaged {
let jac_flat = &mut kron_jac[row_idx];
let q_row = jac_flat.len() / p;
let mut col_buf = Array1::<f64>::zeros(q_row);
for j in 0..p {
for c in 0..q_row {
col_buf[c] = jac_flat[c * p + j];
}
let projected_col = manifold_i.project_vector_to_gradient_tangent(
t_i,
gt_e.view(),
col_buf.view(),
);
for c in 0..q_row {
jac_flat[c * p + j] = projected_col[c];
}
}
}
}
}
}
}
// Build and install the full-B Kronecker htbeta_matvec.
//
// `SaeKroneckerRows` holds per-row `(a_phi, local_jac)` and implements
// the cross-block operator without ever materialising the dense
// `(q × K·p)` slab. The cross-block factorises as `H_tβ = L · J_β`,
// where `J_β = φᵀ ⊗ I_p` projects a length-`K` β vector onto the
// `p`-dimensional decoded output space (`apply_jbeta`) and `L_i` is
// the per-row `(q_i × p)` assignment+coordinate Jacobian that lifts
// that p-vector into the row's `q_i`-dim tangent block (`apply_l`).
// Both factors are required: the contract of `set_row_htbeta_operator`
// is `out.len() == d` (= `q_i`), so writing `apply_jbeta`'s p-vector
// output directly into a length-`q_i` buffer overflows whenever
// `p > q_i` (the common case once `p` reflects real feature width).
// Symmetric for the transpose: `H_βt = J_βᵀ · Lᵀ`, so apply `Lᵀ`
// first to map the q_i-vector back to p-space, then scatter through
// the support.
let device_rows = if frames_engaged {
None
} else {
Some((kron_a_phi.clone(), kron_jac.clone()))
};
if !frames_engaged {
let kron = Arc::new(SaeKroneckerRows::new(p, kron_a_phi, kron_jac));
let kron_t = Arc::clone(&kron);
let p_dim = p;
sys.set_row_htbeta_operator(
move |row_idx, x, out| {
// out = L_i · (J_β · x). Allocate a length-p scratch buffer
// for the intermediate decoded-output vector; both factors
// overwrite their output buffers (`apply_jbeta` zeroes
// before accumulating, `apply_l` writes per-row), so no
// pre-zeroing of `u_p`/`out` is needed.
let out_slice = out.as_slice_mut().expect("out is always standard-layout");
let mut u_p = vec![0.0_f64; p_dim];
if let Some(xs) = x.as_slice() {
kron.apply_jbeta(row_idx, xs, &mut u_p);
} else {
let x_vec: Vec<f64> = x.iter().copied().collect();
kron.apply_jbeta(row_idx, &x_vec, &mut u_p);
}
kron.apply_l(row_idx, &u_p, out_slice);
},
move |row_idx, v, out| {
// out += J_βᵀ · (Lᵀ · v). `apply_l_t` accumulates into a
// zero-initialised length-p buffer to produce the p-vector
// `Lᵀ v`; `scatter_jbeta_t` then adds φ_i[s] · u_p[j] into
// the length-K β accumulator at each active `(s, j)`.
let out_slice = out.as_slice_mut().expect("out is always standard-layout");
let mut u_p = vec![0.0_f64; p_dim];
if let Some(vs) = v.as_slice() {
kron_t.apply_l_t(row_idx, vs, &mut u_p);
} else {
let v_vec: Vec<f64> = v.iter().copied().collect();
kron_t.apply_l_t(row_idx, &v_vec, &mut u_p);
}
kron_t.scatter_jbeta_t(row_idx, &u_p, out_slice);
},
);
}
let mut beta_penalty_assembly = SaeBetaPenaltyAssembly::default();
let factored_row_projection = if frames_engaged && analytic_penalties.is_some() {
Some(&frame_projection)
} else {
None
};
if let Some(registry) = analytic_penalties {
// Upfront validation: refuse penalty kinds the SAE row layout
// cannot host, and refuse mixed-d row-block configurations.
// This makes the dispatch loop below total — no runtime
// "unsupported penalty" fallthrough, no K-gating.
self.validate_analytic_penalty_registry(registry)
.map_err(|err| format!("SaeManifoldTerm::assemble_arrow_schur: {err}"))?;
beta_penalty_assembly = self
.add_sae_analytic_penalty_contributions(
&mut sys,
registry,
penalty_scale,
row_layout.as_ref(),
dense_beta_curvature,
factored_row_projection,
)
.map_err(|err| format!("SaeManifoldTerm::assemble_arrow_schur: {err}"))?;
}
if frames_engaged {
// ── #972 / #977 T1 — FACTORED β-tier transform ──────────────────
//
// The entire β-tier above was assembled in the full-`B` (p-wide)
// layout: `sys.gb` is `g_B` (length `beta_dim`), `sys.hbb` carries
// any analytic Beta-tier penalty, and `g_blocks` is the
// FRAME-INDEPENDENT basis Gram. We now rebuild the β-tier in the
// factored coordinate space `C` (width `factored_border_dim`), the
// full-`B` system sandwiched by `Φ = blkdiag(I_{M_k} ⊗ U_k)`:
// * gradient `g_C = Φᵀ g_B` (per atom `(g_B U_k)`),
// * data H `Φᵀ(G⊗I_p)Φ = G_{ij}⊗(U_iᵀU_j)`,
// * smooth `λ S_k ⊗ I_{r_k}` (since `U_kᵀU_k = I`),
// * analytic `Φᵀ hbb Φ` (dense, only if written).
// Un-framed atoms ride the `r_k = p, U_k = I_p` identity special case.
let off_c = &frame_projection.border_offsets;
let ranks = &frame_projection.ranks;
let basis_sizes = &frame_projection.basis_sizes;
let border_dim = frame_projection.border_dim();
let gb_c = frame_projection.project_border_vec(sys.gb.view());
// Data β-Hessian: `G_{ij} ⊗ W_{ij}` with `W_{ij} = U_iᵀU_j`. The
// basis Gram `g_blocks` is unchanged; only the output factor is the
// per-pair frame overlap (`I_{r_k}` within a framed atom, `I_p` for
// un-framed).
let mut frame_blocks: Vec<FactoredFrameGBlock> = Vec::with_capacity(g_blocks.len());
for ((atom_i, atom_j), data) in g_blocks.into_iter() {
if data.iter().all(|&v| v == 0.0) {
continue;
}
// `W_{ij} = U_iᵀ U_j` from the precomputed per-atom frames.
let w = self.frame_cross_factor(atom_i, atom_j);
frame_blocks.push(FactoredFrameGBlock {
atom_i,
atom_j,
g: data,
w,
});
}
let data_op =
FactoredFrameKroneckerOp::new(ranks.clone(), basis_sizes.clone(), frame_blocks)?;
// Smooth penalty in factored space: `λ S_k ⊗ I_{r_k}` at `off_C[k]`.
let mut ops: Vec<Arc<dyn BetaPenaltyOp>> = Vec::with_capacity(self.atoms.len() + 2);
for k in 0..self.atoms.len() {
let r = ranks[k];
ops.push(Arc::new(IdentityRightKroneckerPenaltyOp {
factor_a: smooth_scaled_s[k].clone(),
p: r,
global_offset: off_c[k],
k: border_dim,
}));
}
ops.push(Arc::new(data_op));
// Analytic Beta-tier penalty: project the dense full-`B` `hbb` block
// `Φᵀ hbb Φ` into the factored space. Only present when a Beta-tier
// penalty actually wrote `hbb` (else `hbb` is all-zero and the dense
// `(border_dim)²` op is skipped entirely, exactly as full-`B`).
if beta_penalty_assembly.dense_written {
let hbb_c =
self.project_dense_penalty_to_factored(sys.hbb.view(), &frame_projection);
ops.push(Arc::new(DensePenaltyOp(hbb_c)));
} else if beta_penalty_assembly.deferred_factored {
let registry =
analytic_penalties.expect("deferred beta curvature requires registry");
let hbb_c = self.build_factored_beta_penalty_curvature(
registry,
penalty_scale,
&frame_projection,
);
ops.push(Arc::new(DensePenaltyOp(hbb_c)));
}
// Re-point the system's β-tier to the factored width. The t-tier
// (per-row `htt`, `gt`) is frame-independent and untouched; row
// cross-block slabs were allocated and assembled directly in
// factored coordinates, so analytic row supplements and data-fit
// cross terms already share shape `(q_i × factored_border_dim)`.
sys.k = border_dim;
sys.gb = gb_c;
self.reclaim_border_hbb_workspace(&mut sys);
// Factored per-atom block ranges for the block-Jacobi Schur
// preconditioner: `[off_C[k] .. off_C[k] + M_k·r_k]`.
let mut block_ranges: Vec<std::ops::Range<usize>> =
Vec::with_capacity(self.atoms.len());
for k in 0..self.atoms.len() {
let start = off_c[k];
block_ranges.push(start..start + basis_sizes[k] * ranks[k]);
}
sys.set_block_offsets(Arc::from(block_ranges.into_boxed_slice()));
sys.set_penalty_op(Arc::new(CompositePenaltyOp { k: border_dim, ops }));
} else {
let (device_a_phi, device_local_jac) =
device_rows.expect("full-beta SAE PCG rows are cloned before row operator install");
// Wire per-atom β block ranges so the Jacobi preconditioner builds one
// dense Schur sub-block per atom (block-Jacobi) instead of scalar-diagonal
// inversion. Each atom's decoder coefficients form a natural block:
// `[beta_offsets[k] .. beta_offsets[k] + basis_size[k] * p_out]`.
sys.set_block_offsets(self.beta_block_offsets());
// Install the composite BetaPenaltyOp (#296): smoothness contributions
// via per-atom KroneckerPenaltyOp (avoid dense K×K materialisation), the
// data-fit Gauss-Newton β-Hessian as the structured `G ⊗ I_p`
// SparseBlockKroneckerPenaltyOp (block-sparse over co-occurring
// `(atom, atom')` pairs, block-diagonal across the `p` output channels,
// identical per channel), plus — only when a Beta-tier analytic penalty
// was written — the dense `sys.hbb` residual contribution. When no beta
// penalty fired, `sys.hbb` is all-zero and the dense `(K·p)²` operator
// is skipped entirely. The sparse data op tracks only the active-atom
// couplings, so its storage and matvec cost scale with `k_active`, not
// `K`, at `K = 100K`.
// Convert the per-atom-pair coupling map into `SparseGBlock`s keyed
// by μ-space offsets. Empty blocks (no co-occurrence) are simply
// absent from the map.
let g_sparse_blocks: Vec<SparseGBlock> = g_blocks
.into_iter()
.filter_map(|((atom_i, atom_j), data)| {
if data.iter().all(|&v| v == 0.0) {
None
} else {
Some(SparseGBlock {
row_off: mu_offsets[atom_i],
col_off: mu_offsets[atom_j],
data,
})
}
})
.collect();
let device_smooth_blocks = smooth_scaled_s
.iter()
.enumerate()
.map(|(atom_idx, factor_a)| {
// #1117 — rank deficiency is removed at the basis layer, so the
// device PCG smooth block is just `λ S_k ⊗ I_p` (full-rank
// design); no data-null deflation is folded in here.
DeviceSaeSmoothBlock {
global_offset: beta_offsets[atom_idx],
factor_a: factor_a.clone(),
}
})
.collect();
sys.set_device_sae_pcg_data(DeviceSaePcgData {
p,
beta_dim,
a_phi: device_a_phi,
local_jac: device_local_jac,
smooth_blocks: device_smooth_blocks,
sparse_g_blocks: g_sparse_blocks.clone(),
});
let mut ops: Vec<Arc<dyn BetaPenaltyOp>> = smooth_ops;
ops.push(Arc::new(SparseBlockKroneckerPenaltyOp {
p,
dim_a: m_total,
k: beta_dim,
blocks: g_sparse_blocks,
}));
if beta_penalty_assembly.dense_written {
ops.push(Arc::new(DensePenaltyOp(sys.hbb.clone())));
}
sys.set_penalty_op(Arc::new(CompositePenaltyOp { k: beta_dim, ops }));
self.reclaim_border_hbb_workspace(&mut sys);
}
if let Some(deflation) = self.row_gauge_deflation_for_layout(row_layout.as_ref()) {
sys.set_row_gauge_deflation(deflation);
}
// #1038 IBP cross-row Woodbury source. The exact IBP Hessian has the
// per-column rank-one cross-row block `H_(i,k),(j,k) = w·s'_k·z'_ik·z'_jk`
// (for ALL `i,j`, including the `i=j` self term) that couples DISTINCT
// latent rows through the shared empirical mass `M_k = Σ_i z_ik`. The
// assembled row-block-diagonal `htt` already carries the `i=j` self term
// `w·s'_k·z'_ik²` — it is the first summand of `assignment_hdiag`'s
// `hessian_diag` value `w·(score_derivative·z_jac² + score·c_ik)` written
// at the logit diagonal above. So the consumer (`solver::arrow_schur`,
// #1038 `IbpCrossRowSource`/`CrossRowWoodbury`) DOWNDATES exactly
// `Σ_k d_k·z'_ik²` (`self_term_downdate`) to recover the NO-SELF base
// `H₀'`, then re-adds the FULL rank-one `U D Uᵀ` via the determinant
// lemma — so value, the evidence log-determinant, and the θ/ρ-adjoint all
// differentiate the SAME `H_full = H₀' + U D Uᵀ`.
//
// The source is built from the SAME `ibp_assignment_third_channels`
// operator the #1006 θ-adjoint consumes:
// * `d[k] = cross_row_d[k] = w·s'_k = w·score_derivative_k` (the column
// `D`-coefficient — NOT sign-definite, hence the consumer's
// indefinite-capacitance LU);
// * `entries[(i,k)] = (global_t_index, k, z'_ik)` with `z'_ik =
// z_jac[i·K + k]` and `global_t_index = sys.row_offsets[i] + k`. IBP
// is a DENSE assignment mode (`assignment_coord_dim() = K`,
// `last_row_layout = None`), so atom `k`'s logit slot is local
// position `k` of row `i`'s block — exactly the `(base + pos)` index
// the gradient path records in `ibp_logit_sites`
// (`row_vars_for_cache_row` maps `vars[atom] = Logit { atom }`). This
// pins the `U`-column convention bit-for-bit to the consumer.
if let Some(channels) = ibp_assignment_third_channels(&self.assignment, rho)? {
let mut entries: Vec<(usize, usize, f64)> = Vec::with_capacity(n * k_atoms);
for row in 0..n {
let start = row * k_atoms;
let g_base = sys.row_offsets[row];
for k in 0..k_atoms {
let z_prime = channels.z_jac[start + k];
entries.push((g_base + k, k, z_prime));
}
}
let source = IbpCrossRowSource {
r: k_atoms,
d: channels.cross_row_d.clone(),
entries,
};
sys.set_ibp_cross_row_source(source);
}
// Store the active-set layout for `apply_newton_step`.
self.last_row_layout = row_layout;
// Record whether `delta_beta` from this system is a factored ΔC (needs a
// frame lift) or a full-`B` ΔB. Read by `apply_newton_step_impl`.
self.last_frames_active = frames_engaged;
Ok(sys)
}
/// Project a dense full-`B` Beta-tier penalty Hessian `hbb` (`beta_dim ×
/// beta_dim`, the analytic `∂²P/∂B∂B` block) into the factored coordinate
/// space `Φᵀ hbb Φ` (`border_dim × border_dim`) for the #972 / #977 T1
/// frame transform. `Φ = blkdiag(I_{M_k} ⊗ U_k)` maps C-space → B-space, so
/// the projected block contracts both index legs through the per-atom frames.
///
/// The projection is done in two passes to stay `O(beta_dim · border_dim +
/// border_dim²)` instead of forming the dense `Φ` explicitly: first
/// `T = hbb · Φ` (right multiply, columns fold `U`), then `Φᵀ · T` (left
/// multiply, rows fold `U`). Analytic Beta-tier penalties are rare and small,
/// so this only fires when one is actually installed.
pub(crate) fn project_dense_penalty_to_factored(
&self,
hbb: ArrayView2<'_, f64>,
projection: &FrameProjection,
) -> Array2<f64> {
projection.project_block(hbb)
}
pub(crate) fn build_factored_beta_penalty_curvature(
&self,
registry: &AnalyticPenaltyRegistry,
penalty_scale: f64,
projection: &FrameProjection,
) -> Array2<f64> {
let rho_global = Array1::<f64>::zeros(registry.total_rho_count());
let layout = registry.rho_layout();
let target_beta = self.flatten_beta();
let mut hbb_c = Array2::<f64>::zeros((projection.border_dim(), projection.border_dim()));
for (penalty, (rho_slice, tier, _name)) in registry.penalties.iter().zip(layout.iter()) {
if matches!(penalty, AnalyticPenaltyKind::Ard(_)) {
continue;
}
let rho_local = rho_global.slice(s![rho_slice.clone()]);
match tier {
PenaltyTier::Psi if matches!(penalty, AnalyticPenaltyKind::NuclearNorm(_)) => {
self.add_factored_beta_penalty_curvature_for_penalty(
&mut hbb_c,
penalty,
target_beta.view(),
rho_local,
penalty_scale,
projection,
);
}
PenaltyTier::Beta => {
self.add_factored_beta_penalty_curvature_for_penalty(
&mut hbb_c,
penalty,
target_beta.view(),
rho_local,
penalty_scale,
projection,
);
}
_ => {}
}
}
hbb_c
}
pub(crate) fn add_factored_beta_penalty_curvature_for_penalty(
&self,
hbb_c: &mut Array2<f64>,
penalty: &AnalyticPenaltyKind,
target_beta: ArrayView1<'_, f64>,
rho_local: ArrayView1<'_, f64>,
penalty_scale: f64,
projection: &FrameProjection,
) {
let p = self.output_dim();
if let AnalyticPenaltyKind::DecoderIncoherence(base) = penalty {
let Some(per_fit) = self.live_decoder_incoherence_penalty(base) else {
return;
};
let beta_dim = self.beta_dim();
let mut probe = Array1::<f64>::zeros(beta_dim);
for k in 0..self.atoms.len() {
for basis_col in 0..projection.basis_sizes[k] {
for frame_col in 0..projection.ranks[k] {
probe.fill(0.0);
projection.lift_axis_into(&mut probe, k, basis_col, frame_col);
let col = projection.border_offsets[k]
+ basis_col * projection.ranks[k]
+ frame_col;
let hv = per_fit.psd_majorizer_hvp(target_beta, rho_local, probe.view());
projection
.project_border_vec(hv.view())
.iter()
.enumerate()
.for_each(|(row, &v)| hbb_c[[row, col]] += penalty_scale * v);
}
}
}
return;
}
if let AnalyticPenaltyKind::MechanismSparsity(base) = penalty {
for (per_atom, start, end) in self.live_mechanism_sparsity_penalties(base) {
let atom_idx = projection
.beta_offsets
.iter()
.position(|&offset| offset == start)
.expect("live mechanism-sparsity offset must match an SAE atom");
let block_len = end - start;
let mut local_penalty = per_atom.clone();
local_penalty.target = PsiSlice {
range: 0..block_len,
latent_dim: Some(projection.basis_sizes[atom_idx]),
};
let block = target_beta.slice(s![start..end]);
let mut probe = Array1::<f64>::zeros(block_len);
for basis_col in 0..projection.basis_sizes[atom_idx] {
for frame_col in 0..projection.ranks[atom_idx] {
probe.fill(0.0);
projection.lift_local_axis_into(&mut probe, atom_idx, basis_col, frame_col);
let col = projection.border_offsets[atom_idx]
+ basis_col * projection.ranks[atom_idx]
+ frame_col;
let hv = local_penalty.psd_majorizer_hvp(block, rho_local, probe.view());
projection.project_local_atom_vec_into(
atom_idx,
hv.view(),
hbb_c.column_mut(col),
penalty_scale,
);
}
}
}
return;
}
if let AnalyticPenaltyKind::NuclearNorm(base) = penalty {
for (per_atom, start, end) in self.live_nuclear_norm_penalties(base) {
let atom_idx = projection
.beta_offsets
.iter()
.position(|&offset| offset == start)
.expect("live nuclear-norm offset must match an SAE atom");
let block = target_beta.slice(s![start..end]);
let block_len = end - start;
let mut probe = Array1::<f64>::zeros(block_len);
for basis_col in 0..projection.basis_sizes[atom_idx] {
for frame_col in 0..projection.ranks[atom_idx] {
probe.fill(0.0);
projection.lift_local_axis_into(&mut probe, atom_idx, basis_col, frame_col);
let col = projection.border_offsets[atom_idx]
+ basis_col * projection.ranks[atom_idx]
+ frame_col;
let hv = per_atom.psd_majorizer_hvp(block, rho_local, probe.view());
projection.project_local_atom_vec_into(
atom_idx,
hv.view(),
hbb_c.column_mut(col),
penalty_scale,
);
}
}
}
return;
}
let beta_dim = self.beta_dim();
let mut probe = Array1::<f64>::zeros(beta_dim);
for k in 0..self.atoms.len() {
for basis_col in 0..projection.basis_sizes[k] {
for frame_col in 0..projection.ranks[k] {
probe.fill(0.0);
projection.lift_axis_into(&mut probe, k, basis_col, frame_col);
let col =
projection.border_offsets[k] + basis_col * projection.ranks[k] + frame_col;
let hv = penalty.psd_majorizer_hvp(target_beta, rho_local, probe.view());
projection
.project_border_vec(hv.view())
.iter()
.enumerate()
.for_each(|(row, &v)| hbb_c[[row, col]] += penalty_scale * v);
}
}
}
assert_eq!(p, self.output_dim());
}
pub(crate) fn ext_coord_matrix(&self) -> Array2<f64> {
let n = self.n_obs();
let q = self.assignment.row_block_dim();
let flat = self.assignment.flatten_ext_coords();
let mut out = Array2::<f64>::zeros((n, q));
for row in 0..n {
for col in 0..q {
out[[row, col]] = flat[row * q + col];
}
}
out
}
pub(crate) fn ext_coord_manifold(&self) -> LatentManifold {
let mut parts = Vec::with_capacity(self.assignment.row_block_dim());
for _ in 0..self.assignment.assignment_coord_dim() {
parts.push(LatentManifold::Euclidean);
}
let mut any_constrained = false;
for coord in &self.assignment.coords {
if coord.manifold().is_euclidean() {
for _ in 0..coord.latent_dim() {
parts.push(LatentManifold::Euclidean);
}
} else {
any_constrained = true;
parts.push(coord.manifold().clone());
}
}
if any_constrained {
LatentManifold::Product(parts)
} else {
LatentManifold::Euclidean
}
}
pub(crate) fn apply_sae_riemannian_geometry(&self, sys: &mut ArrowSchurSystem) {
let manifold = self.ext_coord_manifold();
if manifold.is_euclidean() {
return;
}
let ext = self.ext_coord_matrix();
let latent =
LatentCoordValues::from_matrix_with_manifold(ext.view(), LatentIdMode::None, manifold);
sys.apply_riemannian_latent_geometry(&latent);
}
/// Build the compact-layout ext-coord product manifold and point for one row.
///
/// The dense `ext_coord_manifold()` is keyed to the full-`q` block ordering
/// `[assignment parts (all Euclidean for IBP-MAP / JumpReLU), then per-atom
/// coord blocks in atom order]`. A compact active-set row instead lays its
/// `q_active` columns out as `[one Euclidean logit slot per active atom,
/// then each active atom's coord block in `active` order]` (see
/// [`SaeRowLayout::from_active_atoms`] / `coord_starts`). To reuse the exact
/// per-row Riemannian projector on the compact block we rebuild a product
/// manifold and the matching ext-coord point in that compact order: the
/// `active.len()` logit slots are `Euclidean` (the assignment channel is
/// always Euclidean for the modes that engage sparsity — `assignment_coord_dim
/// == k_atoms`), and each active atom contributes its own coordinate
/// manifold. On the shared active support this is byte-identical to slicing
/// the dense full-`q` product manifold, so the compact projection matches the
/// dense path exactly — it only drops the inactive atoms' (negligible-mass)
/// coordinate blocks the compact layout already excludes from curvature.
///
/// Returns `(manifold, t_compact)` where `t_compact` has length `q_active`.
/// The logit-slot entries of `t_compact` are filled from the row logits (the
/// Euclidean projector ignores the point, so any finite value is equivalent;
/// using the true logits keeps the point well-defined and finite).
pub(crate) fn compact_row_ext_manifold_and_point(
&self,
row: usize,
layout: &SaeRowLayout,
) -> (LatentManifold, Array1<f64>) {
let active = &layout.active_atoms[row];
let q_active = layout.row_q_active(row);
let mut parts: Vec<LatentManifold> = Vec::with_capacity(active.len() + active.len());
let mut point = Array1::<f64>::zeros(q_active);
// Logit slots: one Euclidean part per active atom, in `active` order.
let logits_row = self.assignment.logits.row(row);
for (j, &k) in active.iter().enumerate() {
parts.push(LatentManifold::Euclidean);
point[j] = logits_row[k];
}
// Coordinate blocks: each active atom's coordinate manifold + point, at
// the compact coord start the layout assigned it.
for (j, &k) in active.iter().enumerate() {
let coord = &self.assignment.coords[k];
let d = coord.latent_dim();
let coord_start = layout.coord_starts[row][j];
let manifold_k = coord.manifold();
// A `d`-dim coordinate whose manifold is a product (e.g. a torus =
// Circle×Circle) already carries its `d` parts; a scalar manifold is
// one part. Either way the manifold's ambient width must equal `d`,
// matching the `d` compact columns at `coord_start`.
parts.push(manifold_k.clone());
let coord_point = coord.row(row);
for axis in 0..d {
point[coord_start + axis] = coord_point[axis];
}
}
(LatentManifold::Product(parts), point)
}
/// Numerical rank of a symmetric matrix: the count of eigenvalues
/// exceeding `tol · max_eig`, with `tol = 1e-9` (the conventional
/// relative spectral cutoff used elsewhere in the codebase).
///
/// Used to count the penalised dimension of each atom's `smooth_penalty`
/// `S_k` so the REML criterion's `−½·p·rank(S)·log λ_smooth` Occam term
/// uses the *effective* penalty rank rather than the ambient basis size
/// (a thin-plate / B-spline penalty has a non-trivial null space).
pub(crate) fn symmetric_rank(s: &Array2<f64>) -> Result<usize, String> {
if s.nrows() != s.ncols() {
return Err(format!(
"SaeManifoldTerm::symmetric_rank: matrix must be square, got {}x{}",
s.nrows(),
s.ncols()
));
}
let m = s.ncols();
if m == 0 {
return Ok(0);
}
// Symmetrize defensively through the shared ndarray helper. The SAE
// rank cutoff is intentionally local to the SAE evidence contract; only
// the symmetric cleanup is shared with the other construction modules.
let mut sym = s.clone();
crate::matrix::symmetrize_in_place(&mut sym);
let (evals, _evecs) = sym
.eigh(Side::Lower)
.map_err(|e| format!("SaeManifoldTerm::symmetric_rank: eigh failed: {e}"))?;
let max_eig = evals.iter().fold(0.0_f64, |acc, &v| acc.max(v));
if !(max_eig > 0.0) {
return Ok(0);
}
let tol = SAE_MANIFOLD_SPECTRAL_RANK_CUTOFF * max_eig;
Ok(evals.iter().filter(|&&v| v > tol).count())
}
/// True REML criterion for the SAE term at a FIXED ρ.
///
/// Runs the inner `(t, β)` arrow-Schur Newton solve to convergence at the
/// supplied ρ (with NO in-loop ARD update — ρ is owned by the engine),
/// then forms the Laplace/REML cost
///
/// ```text
/// V(ρ) = ℓ_pen(t̂, β̂; ρ) + ½ log|H(t̂, β̂; ρ)|
/// − ½ · p · (Σ_k rank S_k) · log λ_smooth
/// ```
///
/// where `ℓ_pen = loss.total()` is the penalised objective at the inner
/// optimum and `½ log|H|` is the Laplace normaliser. `H` is the joint
/// `(t, β)` Hessian assembled by the arrow-Schur system; its `H_tt` block
/// carries `α = exp(log_ard)` on its diagonal, so as α grows `½ log|H|`
/// rises while the `−½·n·log α` already inside `loss.ard` falls — their
/// balance IS the effective-dof term that the deleted `α = n/‖t‖²` rule
/// dropped, which is why the criterion needs no clamp to stay finite on a
/// collapsing axis.
///
/// The final `−½·p·rank(S)·log λ_smooth` term is the smoothing-penalty
/// normaliser `−½ log|λ S|_+` restricted to its ρ-dependent part: `S_k` is
/// shared across all `p` decoder output channels (the `⊗ I_p` Kronecker
/// structure), so `log|λ S|_+ = p·rank(S)·log λ + p·log|S|_+`, and the
/// `½ p·log|S|_+` piece is ρ-independent. ALL ρ-independent additive
/// constants (the `2π` Laplace constant, the base `½ p·log|S|_+` penalty
/// logdet, the assignment-prior normaliser) are DROPPED here: they shift
/// `V` by a constant and do not affect the ρ-argmin the engine seeks.
///
/// Returns `(V, loss)` so the engine can both rank ρ and surface the inner
/// loss breakdown.
pub fn reml_criterion(
&mut self,
target: ArrayView2<'_, f64>,
rho: &SaeManifoldRho,
registry: Option<&AnalyticPenaltyRegistry>,
inner_max_iter: usize,
learning_rate: f64,
ridge_ext_coord: f64,
ridge_beta: f64,
) -> Result<(f64, SaeManifoldLoss), String> {
self.reml_criterion_with_refine_policy(
target,
rho,
registry,
inner_max_iter,
learning_rate,
ridge_ext_coord,
ridge_beta,
true,
)
}
pub(crate) fn reml_criterion_with_refine_policy(
&mut self,
target: ArrayView2<'_, f64>,
rho: &SaeManifoldRho,
registry: Option<&AnalyticPenaltyRegistry>,
inner_max_iter: usize,
learning_rate: f64,
ridge_ext_coord: f64,
ridge_beta: f64,
refine_progress_extension: bool,
) -> Result<(f64, SaeManifoldLoss), String> {
let plan = self.streaming_plan().admitted_or_error(
self.n_obs(),
self.output_dim(),
self.k_atoms(),
)?;
if plan.streaming {
// #1225: streaming and dense MUST optimize the SAME mathematical
// objective — the full REML criterion `loss.total() + extra_penalty +
// ½ log|H| − Occam`. The streaming branch previously returned only
// `loss.total() + extra_penalty_energy`, dropping the Laplace
// normalizer `½ log|H|` and the Occam term, so large shapes (exactly
// where streaming is needed) were ranked by penalized loss rather than
// REML — and dense vs streaming disagreed on the objective. Route
// through the streaming exact-logdet path, which assembles the same
// chunk-by-chunk-bit-identical `½ log|H|_stream` and the same
// `−Occam`/extra-penalty terms as the dense `reml_criterion_with_cache`
// (different memory strategy, same objective).
self.reml_criterion_streaming_exact(
target,
rho,
registry,
inner_max_iter,
learning_rate,
ridge_ext_coord,
ridge_beta,
)
} else {
let (v, loss, _cache) = self.reml_criterion_with_cache_refine_policy(
target,
rho,
registry,
inner_max_iter,
learning_rate,
ridge_ext_coord,
ridge_beta,
refine_progress_extension,
)?;
Ok((v, loss))
}
}
/// As [`Self::reml_criterion`], but also returns the converged undamped
/// `ArrowFactorCache` so callers (the EFS fixed-point step) can read the
/// selected-inverse traces `(H⁻¹)_tt` / `(H⁻¹)_ββ` without re-factoring.
/// The cache is the single shared O(K³) Direct factor; both the
/// log-determinant criterion and the Fellner-Schall ρ-step consume it.
pub fn reml_criterion_with_cache(
&mut self,
target: ArrayView2<'_, f64>,
rho: &SaeManifoldRho,
registry: Option<&AnalyticPenaltyRegistry>,
inner_max_iter: usize,
learning_rate: f64,
ridge_ext_coord: f64,
ridge_beta: f64,
) -> Result<(f64, SaeManifoldLoss, ArrowFactorCache), String> {
self.reml_criterion_with_cache_refine_policy(
target,
rho,
registry,
inner_max_iter,
learning_rate,
ridge_ext_coord,
ridge_beta,
true,
)
}
pub(crate) fn reml_criterion_with_cache_refine_policy(
&mut self,
target: ArrayView2<'_, f64>,
rho: &SaeManifoldRho,
registry: Option<&AnalyticPenaltyRegistry>,
inner_max_iter: usize,
learning_rate: f64,
ridge_ext_coord: f64,
ridge_beta: f64,
refine_progress_extension: bool,
) -> Result<(f64, SaeManifoldLoss, ArrowFactorCache), String> {
let admission_plan = self.streaming_plan().admitted_or_error(
self.n_obs(),
self.output_dim(),
self.k_atoms(),
)?;
if !admission_plan.direct_logdet_admitted() {
return Err(format!(
"SaeManifoldTerm::reml_criterion_with_cache: predicted working set {} bytes exceeds budget {} bytes for dense evidence cache; shape n={},p={},K={}; cost-only streaming route is required",
admission_plan.estimated_direct_peak_bytes,
admission_plan.in_core_budget_bytes,
self.n_obs(),
self.output_dim(),
self.k_atoms()
));
}
// 1. Run the inner (t, β) Newton solve to convergence at FIXED ρ.
// `run_joint_fit_arrow_schur` no longer touches ρ.
let mut rho_fixed = rho.clone();
let mut loss = self.run_joint_fit_arrow_schur(
target,
&mut rho_fixed,
registry,
inner_max_iter,
learning_rate,
ridge_ext_coord,
ridge_beta,
)?;
// 2. Drive the inner (t, β) solve to the KKT/step-converged optimum and
// take one final UNDAMPED factor there to obtain the joint Hessian
// log-determinant. We force ridge = 0 and the dense `Direct` Schur
// mode so `arrow_log_det_from_cache` returns the exact
// `log|H| = Σ_i log|H_tt^(i)| + log|Schur_β|` (it rejects damped
// factors and InexactPCG caches, which have no dense Schur factor).
// This is the same evidence convention the main GAM REML path uses.
// The shared `converge_inner_for_undamped_logdet` driver guarantees
// the per-row `H_tt^(i)` blocks are PD at the converged optimum so
// the undamped (`ridge = 0`) factorization succeeds — the streaming
// log-det path reuses the identical driver so both rank the same
// converged Laplace optimum and stay bit-identical.
let options = ArrowSolveOptions::direct().with_ill_conditioning_tolerated();
let cache = self.converge_inner_for_undamped_logdet(
target,
rho,
&mut rho_fixed,
registry,
inner_max_iter,
learning_rate,
ridge_ext_coord,
ridge_beta,
&mut loss,
&options,
refine_progress_extension,
)?;
self.record_evidence_gauge_deflation_count(cache.gauge_deflated_directions)?;
loss.evidence_gauge_deflated_directions = cache.gauge_deflated_directions;
let log_det = arrow_log_det_from_cache(&cache).ok_or_else(|| {
"SaeManifoldTerm::reml_criterion: arrow_log_det_from_cache returned None at \
ridge=0 Direct mode (no dense Schur factor); the joint Hessian log-det is \
required for the Laplace normaliser"
.to_string()
})?;
// 3. Smoothing-penalty Occam term `−½·Σ_k r_k·rank(S_k)·log λ_smooth`
// plus the profiled-frame evidence-dimension correction
// `+½·Σ_k r_k·(p−r_k)·log λ_smooth` (issue #972). On the full-`B` path
// (`r_k == p`, no frames) this is exactly the historical
// `½·p·(Σ rank S_k)·log λ_smooth`, so the small-model criterion is
// unchanged. The single seam is `reml_occam_term`, shared with the
// streaming path so both rank the identical Laplace dimension count.
let occam = self.reml_occam_term(rho)?;
// Decoder-block analytic-penalty energy (#671/#672). The inner solve
// descended this energy (it enters `gb`/`hbb`) but it had no native
// `loss.*` representative, so the Laplace criterion `v` was scoring a
// different objective than the one minimized. Add the converged
// decoder-penalty value so the ρ-sweep ranks the same penalized
// deviance. Excludes the Psi-tier ARD/assignment penalties already
// accounted for in `loss.total()` (see
// `analytic_decoder_penalty_value_total`).
// Extra analytic-penalty energy (#671/#737). Decoder-block penalties and
// coordinate-tier isometry enter the inner solve but have no `loss.*`
// representative, so the Laplace criterion must add them explicitly to
// rank the same penalized deviance the Newton solve descends.
let extra_penalty_energy = match registry {
Some(reg) => self
.reml_extra_penalty_value_total(reg)
.map_err(|err| format!("SaeManifoldTerm::reml_criterion: {err}"))?,
None => 0.0,
};
let v = loss.total() + extra_penalty_energy + 0.5 * log_det - occam;
Ok((v, loss, cache))
}
/// The #1037 quotient-dimension invariant: a Laplace normalizer `½log|H|` is
/// only comparable across ρ at a COMMON quotient (gauge-deflation) dimension.
/// The first observation pins the expected count; a later match is a no-op.
///
/// A later observation that DIFFERS is, under the K>1 fit, a LEGITIMATE
/// quotient-dimension event — an atom born, reseeded (the #976 collapse
/// guards), or rank-reduced moves the number of gauge-flat rows. Because a
/// deflated direction is lifted to unit stiffness and contributes the
/// ρ-independent `log 1 = 0` to the evidence, re-anchoring the comparison to
/// the new dimension is exactly evidence-preserving and keeps every future
/// cross-ρ comparison consistent — the principled response, not an abort.
///
/// The genuine pathology the guard still catches is a count that NEVER
/// STABILIZES: re-anchors are bounded by the per-atom structural-event budget
/// (`k·(reseed_budget+1)+1`), and a runaway quotient dimension past that
/// bound refuses loudly. This supersedes the prior strict-constant guard and
/// its ±1 flicker band (#1117) at root — the band was masking exactly the
/// legitimate K>1 dimension changes this re-anchoring now handles.
pub(crate) fn record_evidence_gauge_deflation_count(
&mut self,
count: usize,
) -> Result<(), String> {
match self.expected_evidence_gauge_deflated_directions {
Some(expected) if expected == count => Ok(()),
Some(expected) => {
// A change in the gauge-deflation count between two evidence
// factorizations is a legitimate quotient-dimension event under
// the K>1 fit: an atom can be born, reseeded (the #976 collapse
// guards), or rank-reduced across the ρ-walk, and each such event
// moves the number of gauge-flat rows. The #1037 invariant is
// NOT "the count never changes" — it is "two Laplace normalizers
// are only comparable at a COMMON quotient dimension". The
// principled response to a legitimate change is therefore to
// RE-ANCHOR the comparison to the new dimension (so every future
// cross-ρ comparison within the optimization is consistent), not
// to abort the fit. This is exactly evidence-preserving: each
// gauge-deflated direction is lifted to unit stiffness and
// contributes the ρ-independent `log 1 = 0` to `½log|H|`, so the
// converged criterion value is identical whether a given row is
// counted as deflated or not — only the BOOKKEEPING dimension
// must agree across a comparison, and re-anchoring restores that.
//
// The genuine pathology the guard must still catch is a count
// that NEVER STABILIZES — an OSCILLATING quotient dimension that
// re-anchors without converging, signalling a truly ill-posed
// evidence surface. But the deflation count is NOT a discrete
// dictionary-level event count: it is the per-ROW-summed number of
// near-null evidence directions across all N rows (#1217). On real
// K≥2 activations it is an O(N) quantity that drifts SMOOTHLY and
// monotonically as the conditioning improves over the ρ-walk
// (e.g. 171→156→…→113 as smoothing increases) — a benign,
// evidence-neutral change (each deflated direction contributes the
// ρ-independent `log 1 = 0` to `½log|H|`, so re-anchoring never
// moves the criterion value). Charging such a monotone drift
// against a `k`-sized "structural event" budget was wrong: it
// counts threshold crossings of a continuous per-row quantity, not
// atom births/reseeds, so the budget tripped on a perfectly healthy
// converging K=2 fit (#1217 regression from the #1189/#1190
// basin-escape fixes, which shifted which rows sit near the
// deflation floor).
//
// The principled discriminator is DIRECTION REVERSALS: a count
// that drifts one way and settles is benign; a count that bounces
// up and down without settling is the oscillating-quotient
// pathology. We therefore charge the re-anchor budget ONLY on a
// reversal of the change direction, and size the budget by the
// number of distinct dictionary structural events (births/reseeds)
// that can each legitimately flip the drift direction. A monotone
// drift of any length re-anchors freely (it is consistently
// re-anchored and evidence-neutral); a genuinely oscillating count
// exhausts the reversal budget and refuses loudly.
let delta_sign: i8 = if count > expected { 1 } else { -1 };
let is_reversal = self.evidence_gauge_deflation_last_delta_sign != 0
&& delta_sign != self.evidence_gauge_deflation_last_delta_sign;
self.evidence_gauge_deflation_last_delta_sign = delta_sign;
if is_reversal {
self.evidence_gauge_deflation_reanchors += 1;
}
let reversal_budget = self
.k_atoms()
.saturating_mul(
SAE_ATOM_COLLAPSE_RESEED_BUDGET
+ SAE_DICTIONARY_COCOLLAPSE_RESEED_BUDGET
+ 1,
)
.saturating_add(1);
if self.evidence_gauge_deflation_reanchors > reversal_budget {
return Err(format!(
"SaeManifoldTerm::reml_criterion: row-gauge evidence deflation count \
oscillated (reversed direction {} times, last {expected}->{count}) within \
one optimization, exceeding the {reversal_budget}-reversal budget for {} \
atoms; the quotient dimension is not stabilizing, refusing to compare \
Laplace normalizers",
self.evidence_gauge_deflation_reanchors,
self.k_atoms()
));
}
log::debug!(
"SaeManifoldTerm::reml_criterion: per-row evidence deflation count changed \
{expected}->{count} (a benign per-row conditioning drift across the ρ-walk; \
reversal {}/{reversal_budget}); re-anchoring the Laplace normalizer comparison \
to the new dimension",
self.evidence_gauge_deflation_reanchors
);
self.expected_evidence_gauge_deflated_directions = Some(count);
Ok(())
}
None => {
self.expected_evidence_gauge_deflated_directions = Some(count);
Ok(())
}
}
}
pub(crate) fn is_undamped_evidence_row_non_pd(err: &ArrowSchurError) -> bool {
matches!(
err,
ArrowSchurError::PerRowFactorFailed { reason, .. }
if reason.contains("H_tt is non-PD at base ridge")
&& reason.contains("evidence mode preserves the genuine Cholesky")
)
}
/// Drive the inner `(t, β)` Newton solve to the KKT/step-converged optimum
/// and return the final UNDAMPED (`ridge = 0`) joint-Hessian factor cache.
///
/// The Laplace normaliser `½log|H|` is only the correct REML criterion at
/// the inner optimum `(t̂, β̂)`, so the criterion must refine the inner state
/// until either the KKT gradient or the undamped Newton step meets tolerance
/// before factoring. Crucially, **at the converged optimum the per-row
/// `H_tt^(i)` blocks are PD**, so the undamped (`ridge = 0`) factorization
/// succeeds; an off-optimum iterate (e.g. the initial seed, or a state
/// stopped after only `inner_max_iter` steps) can have an indefinite /
/// rank-deficient per-row block (`p_out = 1` → rank-1 `JᵀJ`, softmax
/// assignment-sparsity negative logit curvature) that surfaces
/// `PerRowFactorFailed` from the undamped `factor_one_row`. Both the dense
/// (`reml_criterion_with_cache`) and the streaming
/// (`reml_criterion_streaming_exact`) evidence paths route through this same
/// driver, so they converge to the identical inner state and their
/// `ridge = 0` log-determinants stay bit-identical (#847).
pub(crate) fn converge_inner_for_undamped_logdet(
&mut self,
target: ArrayView2<'_, f64>,
rho: &SaeManifoldRho,
rho_fixed: &mut SaeManifoldRho,
registry: Option<&AnalyticPenaltyRegistry>,
inner_max_iter: usize,
learning_rate: f64,
ridge_ext_coord: f64,
ridge_beta: f64,
loss: &mut SaeManifoldLoss,
options: &ArrowSolveOptions,
refine_progress_extension: bool,
) -> Result<ArrowFactorCache, String> {
// `inner_max_iter == 0` is a genuine FREEZE of the inner `(t, β)` state
// — a verbatim warm-start reuse, not a convergence request (gam#577/#579,
// #850). The convergence/refinement loop below MUST NOT run even one
// Newton step in that case (the old `inner_max_iter.max(1)` floor moved
// β off the seed), so we factor exactly once at the frozen iterate and
// return that undamped cache without invoking the stationarity gate.
// The caller has already run `run_joint_fit_arrow_schur(..., 0, ...)`,
// which left the seed untouched, so `self` is at the warm-start β here.
if inner_max_iter == 0 {
let sys = self
.assemble_arrow_schur(target, rho, registry)
.map_err(|err| format!("SaeManifoldTerm::reml_criterion: {err}"))?;
let factored = solve_arrow_newton_step_with_options(&sys, 0.0, 0.0, options)
.map_err(|err| format!("SaeManifoldTerm::reml_criterion: {err}"))?;
// The frozen-state Newton step (factored.0, factored.1) is discarded
// — only the undamped factor cache (factored.2) is consumed for the
// log-det / selected-inverse traces; β stays at the warm-start seed.
return Ok(factored.2);
}
let mut total_inner_iter = inner_max_iter;
let accepted_base_refine_iter = inner_max_iter.max(1).saturating_mul(16).max(64);
let value_probe_base_refine_iter = inner_max_iter.max(1).saturating_mul(4).max(16);
let base_refine_iter = if refine_progress_extension {
accepted_base_refine_iter
} else {
value_probe_base_refine_iter
};
let progress_refine_iter = if refine_progress_extension {
inner_max_iter.max(1).saturating_mul(64).max(256)
} else {
base_refine_iter
};
let mut previous_refine_grad_norm: Option<f64> = None;
let mut saw_refine_progress = false;
// #1051 — objective-stagnation convergence. On an ill-conditioned
// penalised bilinear fit (the euclidean / Duchon decoder × latent
// coordinate system on a trivial shape), the inner Newton crawls: each
// refine round lowers the penalised objective by a shrinking amount while
// the KKT gradient and the undamped step stay above their relative
// tolerances (the near-singular Schur amplifies the step in the
// weakly-identified decoder direction). The grad-OR-step gate then never
// fires and the solve is rejected as "did not converge" — the 1e12
// sentinel. A Newton/LM iterate whose objective has stopped decreasing
// to within `√εmach` of its scale IS the numerical inner optimum; ranking
// the Laplace criterion there is correct. We accept that fixed point
// instead of grinding the budget.
let entry_loss_total = loss.total();
let mut previous_loss_total = entry_loss_total;
let mut refine_rounds: usize = 0;
// Consecutive stall rounds: counts how many successive refine rounds
// ended in a stall AND a failed undamped factor. Once this reaches
// `SAE_MANIFOLD_INNER_OBJECTIVE_STALL_MIN_ROUNDS` the iterate is at
// its numerical fixed point and cannot be improved further; returning
// `Err` here is the same "did not converge" signal that
// `is_recoverable_value_probe_refusal` already handles, so the outer
// BFGS treats it as an INFINITY probe and tries a different ρ instead
// of looping forever burning the extended progress budget. Without
// this counter the stagnation handler fell through when the undamped
// factor failed and the loop kept extending via `saw_refine_progress`
// from earlier rounds, accumulating minutes of wasted work (#1094).
let mut consecutive_stall_factor_fail: usize = 0;
loop {
let sys = self
.assemble_arrow_schur(target, rho, registry)
.map_err(|err| format!("SaeManifoldTerm::reml_criterion: {err}"))?;
// Evidence-only factorization: the Newton step (Δt, Δβ) is discarded
// and only the factor cache is consumed — the exact undamped log-det
// and the selected-inverse traces. As ρ sweeps to extremes (e.g. a
// wide ARD-α sweep), H_tt is genuinely PD but can be ill-conditioned;
// the standard Direct guard rejects that to protect Newton-step
// accuracy, but the log-det is exact from diag(L) regardless of the
// condition number and the traces only need the (PD) factor. So
// tolerate the ill-conditioning rejection here (a genuine non-PD pivot
// still errors). The cache stays undamped at ridge=0, so
// `arrow_log_det_from_cache` remains exact.
// The exact KKT stationarity residual is the joint gradient
// ‖g‖ = √(Σ_i ‖g_t^(i)‖² + ‖g_β‖²), read straight off the assembled
// system. Unlike the Newton step Δ = H⁻¹g, the gradient is
// factorisation-independent: it is NOT amplified by an inverse, so a
// genuinely stationary but ill-conditioned fit (tiny g, possibly large
// Δ in a flat direction) is correctly recognised as converged. The
// `with_ill_conditioning_tolerated` Direct factor below documents that
// its Δ may be inaccurate in exactly those flat directions, so using Δ
// alone as the convergence gate would falsely reject healthy fits.
let grad_norm_sq: f64 = sys
.rows
.iter()
.map(|row| row.gt.iter().map(|&v| v * v).sum::<f64>())
.sum::<f64>()
+ sys.gb.iter().map(|&v| v * v).sum::<f64>();
let grad_norm = grad_norm_sq.sqrt();
// Quotient KKT-gradient (#1117): the raw joint gradient retains a
// persistent small component in the chart-gauge orbit and the
// rank-deficient decoder β-null even at a stationary fit, so the raw
// grad gate never clears on a rank-deficient circle and the inner
// refine loop crawls until the (large) progress budget dies — the
// 2-min stall. Measure the gradient on the SAME identified quotient
// the step gate already uses: a fit whose only remaining gradient
// lives in those flat directions is stationary on the quotient, so
// ranking the Laplace criterion there is correct. The dense per-row
// g_t is laid into the `n·q` coordinate layout the gauge basis spans;
// non-dense/heterogeneous systems fall back to the raw norm.
let quotient_grad_norm = {
let n = self.n_obs();
let q = self.assignment.row_block_dim();
let dense_len = n.saturating_mul(q);
let mut grad_ext_coord = Array1::<f64>::zeros(dense_len);
let mut dense_layout_ok = sys.rows.len() == n;
if dense_layout_ok {
for (row_idx, row) in sys.rows.iter().enumerate() {
let base = sys.row_offsets[row_idx];
let di = sys.row_dims[row_idx];
if base + di > dense_len || row.gt.len() < di {
dense_layout_ok = false;
break;
}
for axis in 0..di {
grad_ext_coord[base + axis] = row.gt[axis];
}
}
}
if dense_layout_ok {
self.quotient_gradient_norm_sq(
grad_ext_coord.view(),
sys.gb.view(),
grad_norm_sq,
rho_fixed.lambda_smooth(),
)
.map(|v| v.sqrt())
.unwrap_or(grad_norm)
} else {
grad_norm
}
};
let iterate_scale = self.inner_iterate_scale();
// Relative parameter-step tolerance for Δ (well-conditioned charts)
// and a scaled KKT-gradient tolerance. Convergence is accepted on
// EITHER a small KKT gradient OR a small undamped Newton step: SAE
// manifold fits contain gauge-like coordinate/decoder directions (the
// circle's rotation gauge, decoder column-space rotations) where the
// shared-block Hessian is near-singular, so the undamped step can stay
// large in that flat direction even at a genuine stationary point; the
// gradient, which is not amplified by the inverse, recognises it. With
// the isometry Gauss-Newton block now a coherent PSD pullback (no
// indefinite Schur pivot), the inner solve reaches true stationarity,
// so the gradient tolerance is a standard relative KKT residual rather
// than the 0.1.154-regression band-aid (3e-3) that masked the
// non-convergence the indefinite curvature caused.
let step_tolerance = SAE_MANIFOLD_INNER_STEP_REL_TOL * iterate_scale;
let grad_tolerance = SAE_MANIFOLD_INNER_GRAD_REL_TOL * iterate_scale;
if !grad_norm_sq.is_finite() {
return Err(format!(
"SaeManifoldTerm::reml_criterion: undamped inner KKT residual is non-finite \
at the inner optimum (‖g‖²={grad_norm_sq}); the joint Hessian \
factorisation is degenerate at this ρ"
));
}
let (delta_t, delta_beta, cache): (Array1<f64>, Array1<f64>, ArrowFactorCache) =
match solve_arrow_newton_step_with_options(&sys, 0.0, 0.0, options) {
Ok(factored) => factored,
Err(err) if Self::is_undamped_evidence_row_non_pd(&err) => {
if grad_norm <= grad_tolerance || quotient_grad_norm <= grad_tolerance {
// K>1: the softmax/IBP logit–coordinate Gauss-Newton
// cross-terms (H_zt = J_z^T J_t, assembled row-locally from
// the assignment JVP × basis JVP) can make a per-row H_tt
// indefinite at the TRUE KKT stationary point — when two
// atoms' decoders specialise in opposite directions the
// Schur complement of the logit block goes negative even
// though the priors and the full-joint GN term are PSD.
//
// The undamped evidence factor already conditions that
// block the PRINCIPLED way: `factor_spectral_deflated_
// evidence_row` discovers the negative/flat eigen-direction
// and stiffens it to UNIT curvature (eigenvalue → +1), so it
// contributes a ρ-INDEPENDENT log 1 = 0 to the evidence —
// the same quotient pseudo-determinant convention the gauge
// (#1037) and data-null (#1117) deflations use. Reaching
// THIS arm at stationarity therefore means even the spectral
// deflation declined (a non-finite block or a failed
// eigendecomposition): the state is genuinely broken, so we
// surface the hard refusal and let the outer BFGS treat this
// ρ as an INFINITY probe (`is_recoverable_value_probe_
// refusal`). We must NOT ridge-damp here: a `+ridge·I`
// fallback injects a ρ-dependent ½·log|I + ridge·H_tt⁻¹|
// bias into the VALUE that the analytic ρ-gradient (built
// for the undamped Laplace log-det) never sees, desyncing
// the outer line-search — the multi-atom non-convergence
// this fix (#1117) removes.
return Err(format!(
"SaeManifoldTerm::reml_criterion: stationary undamped \
evidence factorization has a non-PD per-row H_tt block \
that spectral unit-stiffness deflation could not \
condition (‖g‖={grad_norm:.6e}, tol {grad_tolerance:.6e}); \
{err}"
));
}
let refine_limit = Self::refine_iteration_limit(
total_inner_iter,
base_refine_iter,
progress_refine_iter,
previous_refine_grad_norm,
grad_norm,
saw_refine_progress,
);
if total_inner_iter >= refine_limit {
// #1117/#1118 — pre-stationarity genuinely-indefinite
// non-gauge H_tt under K>1 IBP/softmax row-sharing. The
// logit × coordinate Gauss-Newton cross term H_zt = J_zᵀJ_t
// can drive a shared row's H_tt Schur complement NEGATIVE off
// the gauge orbit; the LM-escalated refinement above cannot
// always cross the indefinite basin into the PD region within
// the descent-extended budget.
//
// The undamped (ridge=0) evidence factor already conditions
// that block the PRINCIPLED way: `factor_spectral_deflated_
// evidence_row` discovers the negative/flat eigen-direction
// and stiffens it to UNIT curvature (eigenvalue → +1), a
// ρ-INDEPENDENT log 1 = 0 evidence contribution — so the
// `Ok(factored)` arm above accepts the indefinite block and
// returns a finite, monotone-comparable value to the outer
// BFGS WITHOUT a ρ-dependent bias. Reaching THIS arm means
// even that spectral deflation declined (a non-finite block
// or a failed eigendecomposition): the iterate is genuinely
// broken, so we surface the hard refusal and let the outer
// BFGS treat this ρ as an INFINITY probe.
//
// We must NOT ridge-damp here: a `+ridge·I` evidence
// fallback injects a ρ-dependent ½·log|I + ridge·H_tt⁻¹|
// bias into the VALUE that the analytic ρ-gradient (built
// for the undamped Laplace log-det) never sees, desyncing
// the outer line-search — the multi-atom non-convergence this
// fix removes. K=1 (and any already-PD or spectral-deflatable
// K>1 row) never reaches this branch.
return Err(format!(
"SaeManifoldTerm::reml_criterion: undamped evidence \
factorization hit a non-PD per-row H_tt block before KKT \
stationarity (‖g‖={grad_norm:.6e}, tol {grad_tolerance:.6e}) \
and the refinement budget was exhausted after \
{total_inner_iter} inner iterations; {err}"
));
}
let remaining = refine_limit - total_inner_iter;
let refine_iter = inner_max_iter.max(1).min(remaining);
saw_refine_progress |=
Self::refine_round_made_progress(previous_refine_grad_norm, grad_norm);
previous_refine_grad_norm = Some(grad_norm);
*loss = self.run_joint_fit_arrow_schur(
target,
rho_fixed,
registry,
refine_iter,
learning_rate,
ridge_ext_coord,
ridge_beta,
)?;
total_inner_iter += refine_iter;
continue;
}
Err(err) => {
return Err(format!("SaeManifoldTerm::reml_criterion: {err}"));
}
};
// The Laplace normaliser ½log|H| is only the correct REML criterion at
// the inner optimum (t̂, β̂). Convergence is judged by EITHER a small
// gradient (KKT stationarity) OR a small undamped Newton step; the
// solve is only rejected as non-converged when BOTH are large, i.e.
// the iterate is neither stationary nor about to move negligibly. That
// disjunction is what keeps an ill-conditioned-but-stationary fit
// (small g, large Δ) from being rejected while still refusing to rank
// an off-optimum Laplace criterion that is genuinely mid-flight.
let step_norm_sq: f64 = delta_t.iter().map(|&v| v * v).sum::<f64>()
+ delta_beta.iter().map(|&v| v * v).sum::<f64>();
if !step_norm_sq.is_finite() {
return Err(format!(
"SaeManifoldTerm::reml_criterion: undamped inner residual is non-finite at \
the inner optimum (‖Δ‖²={step_norm_sq}, ‖g‖²={grad_norm_sq}); the joint \
Hessian factorisation is degenerate at this ρ"
));
}
let step_norm = step_norm_sq.sqrt();
let quotient_step_norm_sq = self.quotient_newton_step_norm_sq(
delta_t.view(),
delta_beta.view(),
step_norm_sq,
rho_fixed.lambda_smooth(),
)?;
let quotient_step_norm = quotient_step_norm_sq.sqrt();
// Converge on ANY of: the raw KKT gradient (well-conditioned fit),
// the QUOTIENT KKT gradient (#1117 — rank-deficient fit whose only
// residual gradient is gauge/null flat-direction crawl), or the
// quotient Newton step. The quotient-gradient disjunct is what lets
// a rank-deficient K=1 circle terminate in budget instead of crawling
// the weakly-identified valley until the refine budget dies.
if grad_norm <= grad_tolerance
|| quotient_grad_norm <= grad_tolerance
|| quotient_step_norm <= step_tolerance
{
let scale = self.inner_iterate_scale();
eprintln!(
"SAE-INNER-OK: ‖g‖={grad_norm:.6e} (quot_g={quotient_grad_norm:.6e}) ‖qstep‖={quotient_step_norm:.6e} \
iters~{total_inner_iter} scale={scale:.3e}"
);
return Ok(cache);
}
let refine_limit = Self::refine_iteration_limit(
total_inner_iter,
base_refine_iter,
progress_refine_iter,
previous_refine_grad_norm,
grad_norm,
saw_refine_progress,
);
if total_inner_iter >= refine_limit {
// Permanent useful diagnostic for inner solve non-convergence in reml_criterion
// (especially relevant for co-training / warm-started paths on difficult rho or manifolds).
let scale = self.inner_iterate_scale();
eprintln!(
"SAE-INNER-FAIL: ‖g‖={grad_norm:.6e} (quot_g={quotient_grad_norm:.6e} tol={grad_tolerance:.6e}) \
‖qstep‖={quotient_step_norm:.6e} (raw‖Δ‖={step_norm:.6e} tol={step_tolerance:.6e}) \
after {total_inner_iter} iters, iterate_scale={scale:.3e}, refine_ext={refine_progress_extension}"
);
return Err(format!(
"SaeManifoldTerm::reml_criterion: inner solve did not converge at fixed ρ; \
neither the KKT gradient ‖g‖={grad_norm:.6e} (tol {grad_tolerance:.6e}) nor \
the quotient Newton step ‖Π⊥gauge Δ‖={quotient_step_norm:.6e} \
(raw ‖Δ‖={step_norm:.6e}, tol {step_tolerance:.6e}) met \
tolerance after {total_inner_iter} inner iterations. Refusing to rank an \
off-optimum Laplace criterion."
));
}
let remaining = refine_limit - total_inner_iter;
let refine_iter = inner_max_iter.max(1).min(remaining);
saw_refine_progress |=
Self::refine_round_made_progress(previous_refine_grad_norm, grad_norm);
previous_refine_grad_norm = Some(grad_norm);
*loss = self.run_joint_fit_arrow_schur(
target,
rho_fixed,
registry,
refine_iter,
learning_rate,
ridge_ext_coord,
ridge_beta,
)?;
total_inner_iter += refine_iter;
refine_rounds += 1;
// #1051 — objective-stagnation fixed point. A whole refine round that
// failed to lower the penalised objective by a meaningful FRACTION of
// the total since-entry reduction means the Newton/LM iterate is at
// its numerical optimum: the remaining KKT residual lives in the
// weakly-identified decoder / gauge directions the near-singular Schur
// cannot resolve. Ranking the Laplace criterion at this fixed point is
// correct (the only further motion is cosmetic flat-valley crawl), so
// accept the current cache instead of refining until the budget dies.
// Requires a few completed refine rounds (so the fraction baseline is
// meaningful) but is NOT gated behind the full refine budget — the
// whole point is to terminate the crawl long before that.
let new_loss_total = loss.total();
// Two stagnation signals, both required: (1) the latest refine round
// contributed a negligible FRACTION of the total objective reduction
// achieved since entry — the fit has captured essentially all the
// achievable improvement and is now crawling cosmetically along the
// weakly-identified valley; (2) the absolute relative decrease is
// itself tiny. The fraction test is scale- and rate-free (it fires
// whether the crawl decays fast or slow), so it recognises the
// over-smoothed / rank-deficient fixed point the bare relative floor
// misses, while still never firing on a fit that is materially
// improving round over round.
let total_improvement = (entry_loss_total - new_loss_total).max(0.0);
let round_improvement = (previous_loss_total - new_loss_total).max(0.0);
let objective_scale = previous_loss_total.abs().max(new_loss_total.abs()) + 1.0;
let relative_decrease = round_improvement / objective_scale;
let captured_fraction = if total_improvement > 0.0 {
round_improvement / total_improvement
} else {
0.0
};
let stalled = new_loss_total.is_finite()
&& relative_decrease.is_finite()
&& (relative_decrease < SAE_MANIFOLD_INNER_OBJECTIVE_STALL_REL_TOL
|| captured_fraction < SAE_MANIFOLD_INNER_OBJECTIVE_STALL_FRACTION);
previous_loss_total = new_loss_total;
if stalled && refine_rounds >= SAE_MANIFOLD_INNER_OBJECTIVE_STALL_MIN_ROUNDS {
let stationary_sys = self
.assemble_arrow_schur(target, rho_fixed, registry)
.map_err(|err| format!("SaeManifoldTerm::reml_criterion: {err}"))?;
if let Ok((_dt, _db, stationary_cache)) =
solve_arrow_newton_step_with_options(&stationary_sys, 0.0, 0.0, options)
{
eprintln!(
"SAE-INNER-STALL-OK: accepted at numerical fixed point after objective stall, ‖g‖={grad_norm:.6e}"
);
return Ok(stationary_cache);
}
// Stagnated AND the undamped factor still fails: this is the
// numerical fixed point of the inner solve under rank-deficient
// or ill-conditioned geometry (e.g. multi-atom euclidean with
// near-zero initial latent coords, #1094). The iterate cannot
// be improved further at this ρ. Treat it as "inner solve did
// not converge" — the same signal `is_recoverable_value_probe_refusal`
// already handles, causing the outer BFGS to return INFINITY for
// this ρ probe and try a different one. Without this early
// return the stagnation handler fell through and the loop kept
// burning the extended `progress_refine_iter` budget indefinitely.
consecutive_stall_factor_fail += 1;
if consecutive_stall_factor_fail >= SAE_MANIFOLD_INNER_OBJECTIVE_STALL_MIN_ROUNDS {
return Err(format!(
"SaeManifoldTerm::reml_criterion: inner solve did not converge at fixed ρ; \
objective stalled for {consecutive_stall_factor_fail} consecutive refine \
rounds (‖g‖={grad_norm:.6e}, tol {grad_tolerance:.6e}) and the undamped \
evidence factorization failed at each stall point — the iterate is at the \
numerical fixed point under rank-deficient geometry (#{consecutive_stall_factor_fail} \
stall-factor-fail rounds; refusing to rank an off-optimum Laplace criterion)"
));
}
} else {
consecutive_stall_factor_fail = 0;
}
}
}
pub(crate) fn refine_iteration_limit(
total_inner_iter: usize,
base_refine_iter: usize,
progress_refine_iter: usize,
previous_grad_norm: Option<f64>,
grad_norm: f64,
saw_refine_progress: bool,
) -> usize {
// Flat affine-gauge valleys can keep crawling productively after the
// historical base budget. Extend only when the measured KKT residual has
// shown a real finite round-to-round drop; true stalls end at the base
// work budget (#968/#1029). Value-order probes pass the base budget as
// their progress budget, so this branch cannot make probes expensive.
if total_inner_iter < base_refine_iter {
return base_refine_iter;
}
let making_progress =
saw_refine_progress || Self::refine_round_made_progress(previous_grad_norm, grad_norm);
if making_progress && grad_norm.is_finite() {
progress_refine_iter
} else {
base_refine_iter
}
}
pub(crate) fn refine_round_made_progress(
previous_grad_norm: Option<f64>,
grad_norm: f64,
) -> bool {
previous_grad_norm
.is_some_and(|prev| prev.is_finite() && grad_norm.is_finite() && grad_norm < prev)
}
pub(crate) fn outer_gradient_arrow_solver<'a>(
&'a self,
cache: &'a ArrowFactorCache,
) -> Result<DeflatedArrowSolver<'a>, String> {
let Err(conditioning_err) = Self::outer_gradient_conditioning_error(cache) else {
return Ok(DeflatedArrowSolver::plain(cache));
};
let Some(max_pivot) = arrow_factor_max_pivot(cache) else {
return Err(conditioning_err);
};
if !(max_pivot.is_finite() && max_pivot > 0.0) {
return Err(conditioning_err);
}
let full_len = cache.delta_t_len() + cache.k;
let mut raw_gauges = Vec::new();
for gauge in self.dense_step_gauge_vectors()? {
if gauge.len() != full_len {
continue;
}
let norm_sq = gauge.iter().map(|v| v * v).sum::<f64>();
if !(norm_sq.is_finite() && norm_sq > 1.0e-24) {
continue;
}
raw_gauges.push(gauge);
}
// #1051: admit the β (decoder) coordinate basis as additional deflation
// candidates when the block is small enough to eigendecompose cheaply.
// A rank-deficient decoder design (e.g. a euclidean-1D line in a p=2
// ambient: decoder column rank 1 of 3) puts a genuine near-null
// direction of the joint Hessian in the β block, OUTSIDE the closed-form
// chart gauge orbit. Feeding the β basis into the same Rayleigh
// eigendecomposition below lets that flat direction be identified and
// Faddeev-Popov-deflated exactly like a chart gauge, so the analytic
// outer gradient becomes well-defined instead of rejecting the trial ρ.
// The Rayleigh floor still keeps only genuinely flat (sub-floor)
// directions, so a well-conditioned decoder is unaffected.
let delta_t_len = cache.delta_t_len();
if cache.k > 0 && cache.k <= SAE_OUTER_GRADIENT_BETA_NULL_PROBE_MAX_DIM {
for beta_idx in 0..cache.k {
let mut unit = Array1::<f64>::zeros(full_len);
unit[delta_t_len + beta_idx] = 1.0;
raw_gauges.push(unit);
}
}
if raw_gauges.is_empty() {
return Err(conditioning_err);
}
let mut gauge_span: Vec<Array1<f64>> = Vec::new();
for mut gauge in raw_gauges {
for basis in &gauge_span {
let coeff = gauge.dot(basis);
for i in 0..gauge.len() {
gauge[i] -= coeff * basis[i];
}
}
let norm_sq = gauge.iter().map(|v| v * v).sum::<f64>();
if !(norm_sq.is_finite() && norm_sq > 1.0e-24) {
continue;
}
let inv_norm = norm_sq.sqrt().recip();
for value in gauge.iter_mut() {
*value *= inv_norm;
}
gauge_span.push(gauge);
}
if gauge_span.is_empty() {
return Err(conditioning_err);
}
let span_rank = gauge_span.len();
let mut h_span = Array2::<f64>::zeros((span_rank, span_rank));
for col in 0..span_rank {
let h_gauge = match apply_cached_arrow_hessian(
cache,
gauge_span[col].slice(s![..cache.delta_t_len()]),
gauge_span[col].slice(s![cache.delta_t_len()..]),
) {
Ok(value) => value,
Err(_) => return Err(conditioning_err),
};
let h_flat = flatten_arrow_parts(h_gauge.t.view(), h_gauge.beta.view());
for row in 0..span_rank {
h_span[[row, col]] = gauge_span[row].dot(&h_flat);
}
}
for row in 0..span_rank {
for col in 0..row {
let sym = 0.5 * (h_span[[row, col]] + h_span[[col, row]]);
h_span[[row, col]] = sym;
h_span[[col, row]] = sym;
}
}
let (evals, evecs) = h_span
.eigh(Side::Lower)
.map_err(|_| conditioning_err.clone())?;
let strict_gauge_floor = SAE_OUTER_GRADIENT_GAUGE_RAYLEIGH_FACTOR * max_pivot;
let fallback_gauge_floor = SAE_OUTER_GRADIENT_PIVOT_RATIO_FLOOR.sqrt() * max_pivot;
let mut orthonormal: Vec<Array1<f64>> = Vec::new();
for eig_idx in 0..evals.len() {
let rayleigh = evals[eig_idx];
if !(rayleigh.is_finite() && rayleigh <= strict_gauge_floor) {
continue;
}
let mut direction = Array1::<f64>::zeros(full_len);
for basis_idx in 0..span_rank {
let coeff = evecs[[basis_idx, eig_idx]];
for row in 0..full_len {
direction[row] += coeff * gauge_span[basis_idx][row];
}
}
let norm_sq = direction.iter().map(|v| v * v).sum::<f64>();
if !(norm_sq.is_finite() && norm_sq > 1.0e-24) {
continue;
}
let inv_norm = norm_sq.sqrt().recip();
for value in direction.iter_mut() {
*value *= inv_norm;
}
orthonormal.push(direction);
}
if orthonormal.is_empty() {
let mut best_idx = None;
let mut best_rayleigh = f64::INFINITY;
for eig_idx in 0..evals.len() {
let rayleigh = evals[eig_idx];
if rayleigh.is_finite()
&& rayleigh < best_rayleigh
&& rayleigh <= fallback_gauge_floor
{
best_idx = Some(eig_idx);
best_rayleigh = rayleigh;
}
}
if let Some(eig_idx) = best_idx {
let mut direction = Array1::<f64>::zeros(full_len);
for basis_idx in 0..span_rank {
let coeff = evecs[[basis_idx, eig_idx]];
for row in 0..full_len {
direction[row] += coeff * gauge_span[basis_idx][row];
}
}
let norm_sq = direction.iter().map(|v| v * v).sum::<f64>();
if norm_sq.is_finite() && norm_sq > 1.0e-24 {
let inv_norm = norm_sq.sqrt().recip();
for value in direction.iter_mut() {
*value *= inv_norm;
}
orthonormal.push(direction);
}
}
}
if orthonormal.is_empty() {
return Err(conditioning_err);
}
// Quotient-geometry gauge fixing: add stiffness only along the closed-form
// gauge orbit (Faddeev-Popov style). Components orthogonal to that orbit
// are identical to the original inverse solve, while gauge components are
// bounded at the Hessian scale `max_pivot`.
DeflatedArrowSolver::from_orthonormal_gauges(cache, orthonormal, max_pivot)
.map_err(|_| conditioning_err)
}
pub(crate) fn outer_gradient_conditioning_error(
cache: &ArrowFactorCache,
) -> Result<(), String> {
let pivot = arrow_factor_min_pivot(cache);
let Some(min_pivot) = pivot.min_pivot else {
return Err(
"analytic outer gradient undefined at this rho: joint Hessian numerically \
singular (no cached Cholesky pivots)"
.to_string(),
);
};
let Some(max_pivot) = arrow_factor_max_pivot(cache) else {
return Err(
"analytic outer gradient undefined at this rho: joint Hessian numerically \
singular (no cached Cholesky pivot scale)"
.to_string(),
);
};
let ratio = min_pivot / max_pivot;
if min_pivot.is_finite()
&& max_pivot.is_finite()
&& max_pivot > 0.0
&& ratio.is_finite()
&& ratio >= SAE_OUTER_GRADIENT_PIVOT_RATIO_FLOOR
{
return Ok(());
}
Err(format!(
"analytic outer gradient undefined at this rho: joint Hessian numerically singular \
(min/max pivot ratio {ratio:.3e} < floor {floor:.3e}; min pivot {min_pivot:.3e}, \
max pivot {max_pivot:.3e})",
floor = SAE_OUTER_GRADIENT_PIVOT_RATIO_FLOOR,
))
}
/// Smoothing-penalty Occam normalizer `−½ Σ_k r_k·rank(S_k)·log λ_smooth`
/// PLUS the profiled-frame evidence-dimension term `½ Σ_k r_k·(p−r_k)·log
/// λ_smooth` (issue #972).
///
/// On the full-`B` path every atom's frame rank `r_k == p`, so the first
/// piece reduces to the historical `½ p·(Σ rank S_k)·log λ_smooth` and the
/// Grassmann term is zero — bit-for-bit unchanged. When a frame is active the
/// decoder coordinates `C_k` carry the `⊗ I_{r_k}` Kronecker structure (the
/// smoothing penalty `S_k` now acts on `r_k` channels, not `p`), so the
/// penalty-logdet normalizer uses `r_k·rank(S_k)`; and the `r_k·(p−r_k)`
/// frame degrees of freedom profiled OUT of the border are counted explicitly
/// in the Laplace dimension accounting (evidence honesty) so the criterion
/// cannot buy a free evidence boost by hiding decoder freedom in the frame.
pub(crate) fn reml_occam_term(&self, rho: &SaeManifoldRho) -> Result<f64, String> {
let mut penalized_channel_dim = 0usize;
for atom in &self.atoms {
let rank_s = Self::symmetric_rank(&atom.smooth_penalty)?;
// Penalized decoder dimension: `r_k` coordinate channels carry the
// `S_k` roughness penalty (full-`B` path ⇒ `r_k == p`).
penalized_channel_dim += atom.border_frame_rank() * rank_s;
}
// Profiled Grassmann dimensions enter the Laplace evidence dimension
// count with the OPPOSITE sign of the penalty Occam term (they are
// free, unpenalized-by-`S` profiled directions), so `−occam` adds
// `+½ Σ r(p−r) log λ` to the criterion `V` — the honesty correction.
let grassmann_dim = self.grassmann_evidence_dimension();
let occam_penalty = 0.5 * (penalized_channel_dim as f64) * rho.log_lambda_smooth;
let frame_dim_term = 0.5 * (grassmann_dim as f64) * rho.log_lambda_smooth;
// `V = … − occam`, so we want the net occam to SUBTRACT the penalty
// normalizer and ADD the frame-dimension count. Returning
// `occam_penalty − frame_dim_term` achieves that after the caller's
// `− occam`.
Ok(occam_penalty - frame_dim_term)
}
pub(crate) fn reml_occam_log_lambda_smooth_derivative(&self) -> Result<f64, String> {
let mut penalized_channel_dim = 0usize;
for atom in &self.atoms {
let rank_s = Self::symmetric_rank(&atom.smooth_penalty)?;
penalized_channel_dim += atom.border_frame_rank() * rank_s;
}
let grassmann_dim = self.grassmann_evidence_dimension();
Ok(0.5 * ((penalized_channel_dim as f64) - (grassmann_dim as f64)))
}
pub fn reml_criterion_streaming_exact(
&mut self,
target: ArrayView2<'_, f64>,
rho: &SaeManifoldRho,
registry: Option<&AnalyticPenaltyRegistry>,
inner_max_iter: usize,
learning_rate: f64,
ridge_ext_coord: f64,
ridge_beta: f64,
) -> Result<(f64, SaeManifoldLoss), String> {
let mut rho_fixed = rho.clone();
let mut loss = self.run_joint_fit_arrow_schur(
target,
&mut rho_fixed,
registry,
inner_max_iter,
learning_rate,
ridge_ext_coord,
ridge_beta,
)?;
// Drive the inner (t, β) state to the SAME KKT/step-converged optimum the
// dense `reml_criterion_with_cache` reaches before factoring. At that
// optimum the per-row `H_tt^(i)` blocks are PD, so the undamped
// (`ridge_t = 0`) streaming factorization in `streaming_exact_arrow_log_det`
// succeeds — without this, a state stopped after only `inner_max_iter`
// steps can leave a rank-deficient / indefinite row block (`p_out = 1` →
// rank-1 `JᵀJ`, softmax negative-logit curvature) that surfaces
// `PerRowFactorFailed` at base ridge 0. Sharing the driver also keeps the
// streaming and dense log-determinants bit-identical (#847).
let options = ArrowSolveOptions::direct().with_ill_conditioning_tolerated();
// The dense factor cache from convergence is surplus here — the streaming
// path recomputes the (bit-identical) log-determinant chunk-by-chunk in
// `streaming_exact_arrow_log_det` to bound peak memory — so it is dropped.
let converged_cache = self.converge_inner_for_undamped_logdet(
target,
rho,
&mut rho_fixed,
registry,
inner_max_iter,
learning_rate,
ridge_ext_coord,
ridge_beta,
&mut loss,
&options,
true,
)?;
drop(converged_cache);
let log_det = self.streaming_exact_arrow_log_det(target, rho, registry)?;
let occam = self.reml_occam_term(rho)?;
// Extra analytic-penalty energy (#671/#737), matching the full-batch
// `reml_criterion_with_cache` path so streaming and dense criteria rank
// the identical penalized objective.
let extra_penalty_energy = match registry {
Some(reg) => self
.reml_extra_penalty_value_total(reg)
.map_err(|err| format!("SaeManifoldTerm::reml_criterion_streaming_exact: {err}"))?,
None => 0.0,
};
Ok((
loss.total() + extra_penalty_energy + 0.5 * log_det - occam,
loss,
))
}
pub fn streaming_exact_arrow_log_det(
&mut self,
target: ArrayView2<'_, f64>,
rho: &SaeManifoldRho,
registry: Option<&AnalyticPenaltyRegistry>,
) -> Result<f64, String> {
if target.dim() != (self.n_obs(), self.output_dim()) {
return Err(format!(
"SaeManifoldTerm::streaming_exact_arrow_log_det: target must be ({}, {}); got {:?}",
self.n_obs(),
self.output_dim(),
target.dim()
));
}
let plan = self.streaming_plan().admitted_or_error(
self.n_obs(),
self.output_dim(),
self.k_atoms(),
)?;
if plan.estimated_dense_schur_bytes > plan.in_core_budget_bytes {
return Err(format!(
"SaeManifoldTerm::streaming_exact_arrow_log_det: predicted dense reduced Schur {} bytes exceeds budget {} bytes; cost-only matrix-free route is required",
plan.estimated_dense_schur_bytes, plan.in_core_budget_bytes
));
}
let n_total = self.n_obs();
let chunk_size = plan.chunk_size.min(n_total.max(1));
// #972 / #977 T1: the reduced β-Schur is over the FACTORED border when
// frames are active (each chunk inherits the frames via
// `materialize_chunk`, so every `chunk_schur` is `border_dim²`), matching
// the dense path's factored log-det. Full-`B` ⇒ `border_dim == beta_dim`.
let border_dim = if self.frames_active() {
self.factored_border_dim()
} else {
self.beta_dim()
};
let mut schur_acc = Array2::<f64>::zeros((border_dim, border_dim));
let mut log_det_tt = 0.0_f64;
let options = ArrowSolveOptions::direct().with_ill_conditioning_tolerated();
let mut start = 0usize;
while start < n_total {
let end = (start + chunk_size).min(n_total);
let penalty_scale = (end - start) as f64 / n_total as f64;
let chunk_logits = self.assignment.logits.slice(s![start..end, ..]).to_owned();
let chunk_coords: Vec<Array2<f64>> = self
.assignment
.coords
.iter()
.map(|coord| coord.as_matrix().slice(s![start..end, ..]).to_owned())
.collect();
let mut chunk = self.materialize_chunk(chunk_logits, chunk_coords)?;
// #1117 — rank deficiency is removed at the basis layer at fit entry
// (`reduce_atoms_to_data_supported_rank`), so each chunk inherits the
// already-reduced full-rank atoms via `materialize_chunk`; there are
// no global deflation projectors to propagate.
// #991: chunk terms inherit the row's design honesty weight slice
// (global mean-1 normalization preserved — NOT re-normalized per
// chunk — so the per-chunk sums reconstruct the global weighted
// objective exactly).
if let Some(w) = self.row_loss_weights.as_deref() {
chunk.row_loss_weights = Some(w[start..end].to_vec());
}
let z_chunk = target.slice(s![start..end, ..]);
let sys = chunk
.assemble_arrow_schur_scaled(z_chunk, rho, registry, penalty_scale)
.map_err(|err| format!("SaeManifoldTerm::streaming_exact_arrow_log_det: {err}"))?;
let mut streaming = StreamingArrowSchur::from_system(&sys, sys.rows.len().max(1));
let (chunk_log_det_tt, chunk_schur) = streaming
.reduced_schur_and_log_det_tt(0.0, 0.0, &options)
.map_err(|err| format!("SaeManifoldTerm::streaming_exact_arrow_log_det: {err}"))?;
log_det_tt += chunk_log_det_tt;
for row in 0..border_dim {
for col in 0..border_dim {
schur_acc[[row, col]] += chunk_schur[[row, col]];
}
}
start = end;
}
let log_det_schur = StreamingArrowSchur::reduced_schur_log_det(&schur_acc, &options)
.map_err(|err| format!("SaeManifoldTerm::streaming_exact_arrow_log_det: {err}"))?;
Ok(log_det_tt + log_det_schur)
}
/// Per-atom, per-axis coordinate sum-of-squares `‖t_kj‖² = Σ_i t_{i,k,j}²`.
///
/// This is the data-fit sufficient statistic for the ARD precision update
/// (the numerator-side `‖t‖²` of the deleted `α = n/‖t‖²` rule). Returned
/// per atom as an `Array1` of length `d_k`.
///
/// On a *periodic* (Circle) axis the relevant statistic is the von-Mises
/// energy-equivalent `Σ_i 2/α·V(t_i) = Σ_i (2/κ²)(1−cos κ t_i)` (independent
/// of α), so that `½·α·sumsq == Σ_i V(t_i)` matches `ard_value`. This keeps
/// the Mackay/Fellner–Schall fixed point `α ← n / (sumsq + tr H⁻¹)`
/// consistent with the actual periodic prior energy rather than the
/// origin-dependent raw `t²`.
pub(crate) fn ard_coord_sumsq(&self) -> Vec<Array1<f64>> {
let mut out = Vec::with_capacity(self.k_atoms());
for coord in &self.assignment.coords {
let d = coord.latent_dim();
let periods = coord.effective_axis_periods();
let mut sq = Array1::<f64>::zeros(d);
for row in 0..coord.n_obs() {
let t = coord.row(row);
for axis in 0..d {
// `sq_equiv` is independent of `alpha`; pass 1.0.
sq[axis] += ArdAxisPrior::eval(1.0, t[axis], periods[axis]).sq_equiv;
}
}
out.push(sq);
}
out
}
/// Per-atom, per-axis posterior-variance trace `tr_kj(H⁻¹) =
/// Σ_i [(H⁻¹)_tt]_{(i,k,j),(i,k,j)}` from the converged factor cache.
///
/// `cache.latent_block_inverse_diagonal()` returns the diagonal of the
/// latent block `(H⁻¹)_tt` in the cache's compact per-row `delta_t`
/// layout (length `row_offsets[N]`); each per-row block is laid out as
/// `[logit scalars…, then per-active-atom coord axes…]`. This routine
/// sums those diagonal entries over the coord positions belonging to each
/// `(atom k, axis j)` across all observation rows where atom `k` is active.
///
/// `self.last_row_layout` must be the layout from the *same* assemble that
/// produced `cache`:
/// - `Some(layout)`: compact active-set mode (JumpReLU / large-K
/// softmax-IBP truncation). For row `i`, atom `k`'s position in the
/// active list gives its compact coord-block start `coord_starts[i][pos]`;
/// inactive atoms contribute 0 (the prior dominates there anyway).
/// - `None`: dense full-support layout, uniform row dim
/// `q = assignment_dim + Σ d_k`; atom `k`'s coord block sits at the
/// fixed full-row offset `coord_offsets[k]` after the assignment chart.
///
/// This `tr_kj(H⁻¹)` is exactly the posterior-variance term the deleted
/// `α = n/‖t‖²` rule dropped; the corrected Mackay/Fellner-Schall fixed
/// point is `α_new = n / (‖t_kj‖² + tr_kj(H⁻¹))`.
pub(crate) fn ard_inverse_traces(
&self,
cache: &ArrowFactorCache,
) -> Result<Vec<Array1<f64>>, ArrowSchurError> {
let inv_diag = cache.latent_block_inverse_diagonal()?;
let n = self.n_obs();
let coord_offsets = self.assignment.coord_offsets();
let mut traces: Vec<Array1<f64>> = self
.assignment
.coords
.iter()
.map(|c| Array1::<f64>::zeros(c.latent_dim()))
.collect();
for row in 0..n {
let row_base = cache.row_offsets[row];
match self.last_row_layout {
Some(ref layout) => {
let active = &layout.active_atoms[row];
let starts = &layout.coord_starts[row];
for (pos, &k) in active.iter().enumerate() {
let d = self.assignment.coords[k].latent_dim();
let block_start = starts[pos];
for axis in 0..d {
traces[k][axis] += inv_diag[row_base + block_start + axis];
}
}
}
None => {
for k in 0..self.k_atoms() {
let d = self.assignment.coords[k].latent_dim();
let block_start = coord_offsets[k];
for axis in 0..d {
traces[k][axis] += inv_diag[row_base + block_start + axis];
}
}
}
}
}
Ok(traces)
}
pub(crate) fn ard_log_precision_explicit_derivatives(
&self,
rho: &SaeManifoldRho,
) -> Result<Vec<Array1<f64>>, String> {
if rho.log_ard.len() != self.k_atoms() {
return Err(format!(
"ARD rho has {} atoms but term has {}",
rho.log_ard.len(),
self.k_atoms()
));
}
let n = self.n_obs() as f64;
let mut out = Vec::with_capacity(self.k_atoms());
for (atom_idx, coord) in self.assignment.coords.iter().enumerate() {
let d = coord.latent_dim();
let mut atom_out = Array1::<f64>::zeros(rho.log_ard[atom_idx].len());
if rho.log_ard[atom_idx].is_empty() {
out.push(atom_out);
continue;
}
if rho.log_ard[atom_idx].len() != d {
return Err(format!(
"ARD rho atom {atom_idx} has len {} but atom dim is {d}",
rho.log_ard[atom_idx].len()
));
}
let periods = coord.effective_axis_periods();
for axis in 0..d {
let alpha = SaeManifoldRho::stable_exp_strength(rho.log_ard[atom_idx][axis]);
let period = periods[axis];
let mut energy_deriv = 0.0_f64;
for row in 0..coord.n_obs() {
let t = coord.row(row)[axis];
energy_deriv += ArdAxisPrior::eval(alpha, t, period).value;
}
let normalizer_deriv = match period {
None => -0.5 * n,
Some(p) => {
let kappa = std::f64::consts::TAU / p;
let eta = alpha / (kappa * kappa);
// d/d(log α) of `n[-η + log I0(η)]` = `n η (I1/I0 - 1)`.
// The ratio is computed without forming `e^{η}`, so it
// stays finite for large `η` instead of the `inf/inf =
// NaN` that `bessel_i1(η)/bessel_i0(η)` produces (#1113).
let ratio = bessel_i0_log_and_ratio(eta).1;
n * eta * (-1.0 + ratio)
}
};
atom_out[axis] = energy_deriv + normalizer_deriv;
}
out.push(atom_out);
}
Ok(out)
}
pub(crate) fn ard_log_precision_hessian_trace(
&self,
rho: &SaeManifoldRho,
cache: &ArrowFactorCache,
solver: &DeflatedArrowSolver<'_>,
) -> Result<Vec<Array1<f64>>, ArrowSchurError> {
let inv_diag = solver
.latent_inverse_diagonal()
.map_err(|err| ArrowSchurError::SchurFactorFailed { reason: err })?;
let n = self.n_obs();
let coord_offsets = self.assignment.coord_offsets();
let ard_axis_periods: Vec<Vec<Option<f64>>> = self
.assignment
.coords
.iter()
.map(LatentCoordValues::effective_axis_periods)
.collect();
let mut traces: Vec<Array1<f64>> = self
.assignment
.coords
.iter()
.enumerate()
.map(|(k, c)| {
if rho.log_ard[k].is_empty() {
Array1::<f64>::zeros(0)
} else {
Array1::<f64>::zeros(c.latent_dim())
}
})
.collect();
for row in 0..n {
let row_base = cache.row_offsets[row];
match self.last_row_layout {
Some(ref layout) => {
let active = &layout.active_atoms[row];
let starts = &layout.coord_starts[row];
for (pos, &k) in active.iter().enumerate() {
if rho.log_ard[k].is_empty() {
continue;
}
let coord = &self.assignment.coords[k];
let d = coord.latent_dim();
let block_start = starts[pos];
for axis in 0..d {
let alpha = SaeManifoldRho::stable_exp_strength(rho.log_ard[k][axis]);
let t = coord.row(row)[axis];
let prior = ArdAxisPrior::eval(alpha, t, ard_axis_periods[k][axis]);
traces[k][axis] +=
0.5 * inv_diag[row_base + block_start + axis] * prior.hess.max(0.0);
}
}
}
None => {
for k in 0..self.k_atoms() {
if rho.log_ard[k].is_empty() {
continue;
}
let coord = &self.assignment.coords[k];
let d = coord.latent_dim();
let block_start = coord_offsets[k];
for axis in 0..d {
let alpha = SaeManifoldRho::stable_exp_strength(rho.log_ard[k][axis]);
let t = coord.row(row)[axis];
let prior = ArdAxisPrior::eval(alpha, t, ard_axis_periods[k][axis]);
traces[k][axis] +=
0.5 * inv_diag[row_base + block_start + axis] * prior.hess.max(0.0);
}
}
}
}
}
Ok(traces)
}
/// Decoder smoothness penalty quadratic form `Σ_k Σ_oc B_k[:,oc]ᵀ S_k B_k[:,oc]`.
///
/// This is `βᵀ (⊕_k S_k ⊗ I_p) β` — the un-scaled (λ-free) penalty energy
/// in the flat β layout, the denominator of the λ_smooth Fellner-Schall
/// update. `S_k` is symmetrised defensively (as the assembler does).
pub(crate) fn decoder_smoothness_quadratic_form(&self) -> f64 {
// `Σ_k Σ_oc B_k[:,oc]ᵀ ½(S_k+S_kᵀ) B_k[:,oc]` = `Σ_k <B_k, ½(S_k+S_kᵀ)·B_k>`.
// The per-atom `½(S+Sᵀ)·B_k` GEMMs are independent, so they ride the
// multi-GPU batched smoothness GEMM (uniform-shape tiles across every
// device) with an exact per-atom CPU fallback.
let sb_inputs: Vec<(ArrayView2<'_, f64>, ArrayView2<'_, f64>)> = self
.atoms
.iter()
.map(|atom| (atom.smooth_penalty.view(), atom.decoder_coefficients.view()))
.collect();
let sb_all = batched_smooth_sb(&sb_inputs, true);
let mut acc = 0.0_f64;
for (atom, sb) in self.atoms.iter().zip(sb_all.iter()) {
acc += (&atom.decoder_coefficients * sb).sum();
}
acc
}
/// Effective penalized dof of the decoder smoothness penalty:
/// `tr(S_β⁻¹ · M)` with `M = ⊕_k (λ_smooth · S_k) ⊗ I_p` embedded in the
/// flat β layout, where `S_β⁻¹ = (H⁻¹)_ββ` is the Schur-complement inverse.
///
/// Built per keystone's documented pattern on
/// [`ArrowFactorCache::schur_inverse_apply`]:
/// `tr(S_β⁻¹ M) = Σ_col e_colᵀ S_β⁻¹ M e_col`. Column `(k, μ, oc)` of `M`
/// (global index `off_k + μ·p + oc`) is `λ·S_k[:,μ] ⊗ e_oc` — nonzero only
/// at `off_k + ν·p + oc` for `ν in 0..M_k` — so we materialise just that
/// sparse K-vector, apply `S_β⁻¹`, and read back `result[col]`. The
/// `⊗ I_p` only couples equal `oc`, but `S_β` itself couples channels
/// through the data-fit block, so all `p` channels are summed (no
/// channel-block-identity shortcut). Total cost `beta_dim` Schur solves.
pub(crate) fn decoder_smoothness_effective_dof(
&self,
cache: &ArrowFactorCache,
lambda_smooth: f64,
) -> Result<f64, ArrowSchurError> {
let p = self.output_dim();
let frames_active = self.frames_active();
let (offsets, out_dim): (Vec<usize>, Box<dyn Fn(usize) -> usize>) = if frames_active {
let ranks: Vec<usize> = self.atoms.iter().map(|a| a.border_frame_rank()).collect();
(
self.factored_beta_offsets(),
Box::new(move |k: usize| ranks[k]),
)
} else {
(self.beta_offsets(), Box::new(move |_k: usize| p))
};
let k = cache.k;
let mut trace = 0.0_f64;
let mut m_col = Array1::<f64>::zeros(k);
for (atom_idx, atom) in self.atoms.iter().enumerate() {
let s = &atom.smooth_penalty;
let m = atom.basis_size();
let off = offsets[atom_idx];
let r = out_dim(atom_idx);
for mu in 0..m {
for oc in 0..r {
let col = off + mu * r + oc;
m_col.fill(0.0);
for nu in 0..m {
let s_nu_mu = 0.5 * (s[[nu, mu]] + s[[mu, nu]]);
m_col[off + nu * r + oc] = lambda_smooth * s_nu_mu;
}
let z = cache.schur_inverse_apply(m_col.view())?;
trace += z[col];
}
}
}
Ok(trace)
}
pub(crate) fn decoder_smoothness_effective_dof_with_solver(
&self,
cache: &ArrowFactorCache,
solver: &DeflatedArrowSolver<'_>,
lambda_smooth: f64,
) -> Result<f64, String> {
let p = self.output_dim();
// #972 / #977 T1: the cache's β block is the FACTORED border when frames
// are active (`cache.k == factored_border_dim`), so the smoothness edf
// trace `tr((H⁻¹)_ββ · M)` is taken over the same factored layout, with
// `M = ⊕_k (λ S_k) ⊗ I_{r_k}` at the factored offsets (the `U_kᵀU_k = I`
// collapse means the per-coordinate-channel penalty is `λ S_k`, exactly
// as in the full-`B` `⊗ I_p` case but with `r_k` channels). On the
// full-`B` path `frames_active` is false: `out_dim_k = p`, the offsets
// are `beta_offsets`, and this is bit-for-bit the historical trace.
let frames_active = self.frames_active();
let (offsets, out_dim): (Vec<usize>, Box<dyn Fn(usize) -> usize>) = if frames_active {
let ranks: Vec<usize> = self.atoms.iter().map(|a| a.border_frame_rank()).collect();
(
self.factored_beta_offsets(),
Box::new(move |k: usize| ranks[k]),
)
} else {
(self.beta_offsets(), Box::new(move |_k: usize| p))
};
let k = cache.k;
let mut trace = 0.0_f64;
let mut m_col = Array1::<f64>::zeros(k);
for (atom_idx, atom) in self.atoms.iter().enumerate() {
let s = &atom.smooth_penalty;
let m = atom.basis_size();
let off = offsets[atom_idx];
let r = out_dim(atom_idx);
for mu in 0..m {
for oc in 0..r {
let col = off + mu * r + oc;
// M[:,col] = λ · S_k[:,mu] ⊗ e_oc (nonzero at off+ν·r+oc).
m_col.fill(0.0);
for nu in 0..m {
let s_nu_mu = 0.5 * (s[[nu, mu]] + s[[mu, nu]]);
m_col[off + nu * r + oc] = lambda_smooth * s_nu_mu;
}
let zero_t = Array1::<f64>::zeros(cache.delta_t_len());
let z = solver.solve(zero_t.view(), m_col.view())?.beta;
trace += z[col];
}
}
}
Ok(trace)
}
pub(crate) fn assignment_log_strength_hessian_trace(
&self,
rho: &SaeManifoldRho,
cache: &ArrowFactorCache,
solver: &DeflatedArrowSolver<'_>,
) -> Result<f64, String> {
let k_atoms = self.k_atoms();
// #1038 softmax: `H` carries the DENSE entropy block, and since the
// entropy curvature scales linearly with `λ_sparse = exp(ρ)`,
// `∂H/∂ρ = H_entropy` (the full dense per-row block, not just its
// diagonal). The trace `½ tr(H⁻¹ ∂H/∂ρ)` must therefore contract the
// dense `∂H/∂ρ` against the per-row selected-inverse BLOCK, mirroring the
// dense `log|H|` and θ-adjoint — a diagonal-only contraction would
// desync the ρ-gradient from the criterion. (Softmax uses the dense
// `None` layout, so logit positions index atoms directly.)
if let AssignmentMode::Softmax {
temperature,
sparsity,
} = self.assignment.mode
{
if k_atoms <= 1 {
return Ok(0.0);
}
let inv_tau = 1.0 / temperature;
let scale = rho.lambda_sparse() * sparsity * inv_tau * inv_tau;
let penalty = crate::terms::analytic_penalties::SoftmaxAssignmentSparsityPenalty::new(
k_atoms,
temperature,
);
// Softmax uses the reduced K−1 free-logit chart: only positions
// 0..K−1 are free logit coordinates (last reference logit fixed), and
// the reduced `∂H/∂ρ` over the free logits is the top-left
// (K−1)×(K−1) submatrix of the full dense block. Contract it against
// the matching per-row selected-inverse block.
let assignment_dim = self.assignment.assignment_coord_dim();
let total_t = cache.delta_t_len();
let mut trace = 0.0_f64;
for row in 0..self.n_obs() {
let row_base = cache.row_offsets[row];
let q = cache.row_dims[row];
let logit_dim = assignment_dim.min(q);
let row_logits: Vec<f64> = (0..k_atoms)
.map(|k| self.assignment.logits[[row, k]])
.collect();
// ∂H/∂ρ over this row's free-logit block (position j ↔ atom j).
// #1190: the assembled curvature block is the softmax Fisher metric
// `G = scale·(diag(a) − a aᵀ)` (the PSD operator that replaced the
// indefinite entropy Hessian). `scale = λ_sparse·sparsity/τ²` is
// linear in `λ_sparse = exp(ρ)`, so `∂(scale·G)/∂ρ = scale·G = G`
// evaluated at the current scale — differentiate the SAME operator
// the assembly and θ-adjoint use so the ρ-gradient stays on one
// branch.
let dh_rho = penalty.row_fisher_metric(&row_logits, scale);
for kj in 0..logit_dim {
let mut rhs_t = Array1::<f64>::zeros(total_t);
let rhs_beta = Array1::<f64>::zeros(cache.k);
rhs_t[row_base + kj] = 1.0;
let solved = solver
.solve(rhs_t.view(), rhs_beta.view())
.map_err(|err| format!("assignment_log_strength_hessian_trace: {err}"))?;
for ki in 0..logit_dim {
// trace += (H⁻¹)_{ki,kj} · (∂H/∂ρ)_{kj,ki}; dh_rho symmetric.
trace += solved.t[row_base + ki] * dh_rho[[kj, ki]];
}
}
}
return Ok(0.5 * trace);
}
let hdiag = assignment_prior_log_strength_hdiag(&self.assignment, rho)?;
if hdiag.is_empty() {
return Ok(0.0);
}
let inv_diag = solver
.latent_inverse_diagonal()
.map_err(|err| format!("assignment_log_strength_hessian_trace: {err}"))?;
let assignment_dim = self.assignment.assignment_coord_dim();
let mut trace = 0.0_f64;
for row in 0..self.n_obs() {
let row_base = cache.row_offsets[row];
let assignment_base = row * k_atoms;
match self.last_row_layout {
Some(ref layout) => {
for (pos, &atom) in layout.active_atoms[row].iter().enumerate() {
trace += inv_diag[row_base + pos] * hdiag[assignment_base + atom];
}
}
None => {
for free_idx in 0..assignment_dim {
trace += inv_diag[row_base + free_idx] * hdiag[assignment_base + free_idx];
}
}
}
}
Ok(0.5 * trace)
}
pub(crate) fn learnable_ibp_forward_alpha_data_derivative(
&self,
rho: &SaeManifoldRho,
target: ArrayView2<'_, f64>,
) -> Result<f64, String> {
let AssignmentMode::IBPMap {
temperature: _,
learnable_alpha: true,
..
} = self.assignment.mode
else {
return Ok(0.0);
};
let alpha = self
.assignment
.mode
.resolved_ibp_alpha(rho)
.ok_or_else(|| "learnable IBP alpha resolution failed".to_string())?;
let k_atoms = self.k_atoms();
let prior = ibp_stick_breaking_prior(k_atoms, alpha);
let mut dprior = Array1::<f64>::zeros(k_atoms);
for k in 0..k_atoms {
dprior[k] = prior[k] * k as f64 / (alpha + 1.0);
}
let n = self.n_obs();
let p = self.output_dim();
let row_loss_w = self.row_loss_weights.as_deref();
let whitens = self
.row_metric
.as_ref()
.is_some_and(|metric| metric.whitens_likelihood());
let mut decoded = vec![0.0_f64; p];
let mut fitted = Array1::<f64>::zeros(p);
let mut f_rho = Array1::<f64>::zeros(p);
let mut residual = Array1::<f64>::zeros(p);
let mut total = 0.0_f64;
for row in 0..n {
let assignments = self.assignment.try_assignments_row_for_rho(row, rho)?;
fitted.fill(0.0);
f_rho.fill(0.0);
for k in 0..k_atoms {
self.atoms[k].fill_decoded_row(row, &mut decoded);
let sigma = assignments[k] / prior[k];
let da_rho = sigma * dprior[k];
for out_col in 0..p {
fitted[out_col] += assignments[k] * decoded[out_col];
f_rho[out_col] += da_rho * decoded[out_col];
}
}
for out_col in 0..p {
residual[out_col] = fitted[out_col] - target[[row, out_col]];
}
let residual_metric = match self.row_metric.as_ref() {
Some(metric) if whitens => metric.apply_metric_row(row, residual.view()),
_ => residual.to_vec(),
};
let row_weight = row_loss_w.map_or(1.0, |w| w[row]);
let mut row_dot = 0.0_f64;
for out_col in 0..p {
row_dot += residual_metric[out_col] * f_rho[out_col];
}
total += row_weight * row_dot;
}
Ok(total)
}
pub(crate) fn add_learnable_ibp_forward_alpha_data_rhs(
&self,
rho: &SaeManifoldRho,
target: ArrayView2<'_, f64>,
cache: &ArrowFactorCache,
t: &mut Array1<f64>,
beta: &mut Array1<f64>,
) -> Result<(), String> {
let AssignmentMode::IBPMap {
temperature,
learnable_alpha: true,
..
} = self.assignment.mode
else {
return Ok(());
};
let alpha = self
.assignment
.mode
.resolved_ibp_alpha(rho)
.ok_or_else(|| "learnable IBP alpha resolution failed".to_string())?;
let k_atoms = self.k_atoms();
let p = self.output_dim();
let prior = ibp_stick_breaking_prior(k_atoms, alpha);
let mut dprior = Array1::<f64>::zeros(k_atoms);
for k in 0..k_atoms {
dprior[k] = prior[k] * k as f64 / (alpha + 1.0);
}
let inv_tau = 1.0 / temperature;
let row_loss_w = self.row_loss_weights.as_deref();
let whitens = self
.row_metric
.as_ref()
.is_some_and(|metric| metric.whitens_likelihood());
let border = self.border_channels_for_cache(cache)?;
let mut decoded_rows = vec![vec![0.0_f64; p]; k_atoms];
let mut decoded_deriv = vec![0.0_f64; p];
let mut fitted = Array1::<f64>::zeros(p);
let mut f_rho = Array1::<f64>::zeros(p);
let mut residual = Array1::<f64>::zeros(p);
for row in 0..self.n_obs() {
let assignments = self.assignment.try_assignments_row_for_rho(row, rho)?;
fitted.fill(0.0);
f_rho.fill(0.0);
for k in 0..k_atoms {
self.atoms[k].fill_decoded_row(row, &mut decoded_rows[k]);
let sigma = assignments[k] / prior[k];
let da_rho = sigma * dprior[k];
for out_col in 0..p {
fitted[out_col] += assignments[k] * decoded_rows[k][out_col];
f_rho[out_col] += da_rho * decoded_rows[k][out_col];
}
}
for out_col in 0..p {
residual[out_col] = fitted[out_col] - target[[row, out_col]];
}
let residual_metric = match self.row_metric.as_ref() {
Some(metric) if whitens => metric.apply_metric_row(row, residual.view()),
_ => residual.to_vec(),
};
let f_metric = match self.row_metric.as_ref() {
Some(metric) if whitens => metric.apply_metric_row(row, f_rho.view()),
_ => f_rho.to_vec(),
};
let row_weight = row_loss_w.map_or(1.0, |w| w[row]);
let row_vars = self.row_vars_for_cache_row(row, cache)?;
let row_base = cache.row_offsets[row];
for (pos, var) in row_vars.iter().enumerate() {
let mut contribution = 0.0_f64;
match *var {
SaeLocalRowVar::Logit { atom } => {
let sigma = assignments[atom] / prior[atom];
let sigma_jac = sigma * (1.0 - sigma) * inv_tau;
let da_dl = sigma_jac * prior[atom];
let d_da_rho_dl = sigma_jac * dprior[atom];
for out_col in 0..p {
contribution += da_dl * decoded_rows[atom][out_col] * f_metric[out_col];
contribution += d_da_rho_dl
* decoded_rows[atom][out_col]
* residual_metric[out_col];
}
}
SaeLocalRowVar::Coord { atom, axis } => {
let sigma = assignments[atom] / prior[atom];
let da_rho = sigma * dprior[atom];
self.atoms[atom].fill_decoded_derivative_row(row, axis, &mut decoded_deriv);
for out_col in 0..p {
contribution +=
assignments[atom] * decoded_deriv[out_col] * f_metric[out_col];
contribution +=
da_rho * decoded_deriv[out_col] * residual_metric[out_col];
}
}
}
t[row_base + pos] += row_weight * contribution;
}
for channel in &border {
let phi = self.atoms[channel.atom].basis_values[[row, channel.basis_col]];
let sigma = assignments[channel.atom] / prior[channel.atom];
let da_rho = sigma * dprior[channel.atom];
let mut contribution = 0.0_f64;
for out_col in 0..p {
let output = channel.output[out_col];
contribution += assignments[channel.atom] * phi * output * f_metric[out_col];
contribution += da_rho * phi * output * residual_metric[out_col];
}
beta[channel.index] += row_weight * contribution;
}
}
Ok(())
}
pub(crate) fn border_channels_for_cache(
&self,
cache: &ArrowFactorCache,
) -> Result<Vec<SaeBorderChannel>, String> {
let p = self.output_dim();
let frames_active = self.last_frames_active && cache.k == self.factored_border_dim();
let offsets = if frames_active {
self.factored_beta_offsets()
} else {
self.beta_offsets()
};
let mut channels = Vec::with_capacity(cache.k);
for (atom_idx, atom) in self.atoms.iter().enumerate() {
let m = atom.basis_size();
let frame = if frames_active {
self.frame_output_matrix(atom_idx)
} else {
Array2::<f64>::eye(p)
};
let r = frame.ncols();
for basis_col in 0..m {
for channel in 0..r {
let mut output = vec![0.0_f64; p];
for out_col in 0..p {
output[out_col] = frame[[out_col, channel]];
}
channels.push(SaeBorderChannel {
atom: atom_idx,
basis_col,
index: offsets[atom_idx] + basis_col * r + channel,
output,
});
}
}
}
if channels.len() != cache.k {
return Err(format!(
"border channel layout has {} entries but cache border has {}",
channels.len(),
cache.k
));
}
Ok(channels)
}
pub(crate) fn row_vars_for_cache_row(
&self,
row: usize,
cache: &ArrowFactorCache,
) -> Result<Vec<SaeLocalRowVar>, String> {
let q_row = cache.row_dims[row];
let mut vars: Vec<Option<SaeLocalRowVar>> = vec![None; q_row];
match self.last_row_layout {
Some(ref layout) => {
for (pos, &atom) in layout.active_atoms[row].iter().enumerate() {
vars[pos] = Some(SaeLocalRowVar::Logit { atom });
let start = layout.coord_starts[row][pos];
let d = self.assignment.coords[atom].latent_dim();
for axis in 0..d {
vars[start + axis] = Some(SaeLocalRowVar::Coord { atom, axis });
}
}
}
None => {
let assignment_dim = self.assignment.assignment_coord_dim();
let coord_offsets = self.assignment.coord_offsets();
for atom in 0..assignment_dim {
vars[atom] = Some(SaeLocalRowVar::Logit { atom });
}
for atom in 0..self.k_atoms() {
let start = coord_offsets[atom];
let d = self.assignment.coords[atom].latent_dim();
for axis in 0..d {
vars[start + axis] = Some(SaeLocalRowVar::Coord { atom, axis });
}
}
}
}
vars.into_iter()
.enumerate()
.map(|(idx, v)| {
v.ok_or_else(|| {
format!("row_vars_for_cache_row: row {row} position {idx} was not mapped")
})
})
.collect()
}
pub(crate) fn atom_second_jets(&self) -> Result<Vec<Array4<f64>>, String> {
let mut out = Vec::with_capacity(self.k_atoms());
for (atom_idx, atom) in self.atoms.iter().enumerate() {
let coords = self.assignment.coords[atom_idx].as_matrix();
let jet = if let Some(second) = atom.basis_second_jet.as_ref() {
second.second_jet(coords.view())?
} else {
let evaluator = atom.basis_evaluator.as_ref().ok_or_else(|| {
format!(
"logdet_theta_adjoint: atom '{}' has no basis evaluator for second jets",
atom.name
)
})?;
evaluator
.second_jet_dyn(coords.view())
.ok_or_else(|| {
format!(
"logdet_theta_adjoint: atom '{}' basis does not expose analytic second jets",
atom.name
)
})??
};
let expected = (
atom.n_obs(),
atom.basis_size(),
atom.latent_dim,
atom.latent_dim,
);
if jet.dim() != expected {
return Err(format!(
"logdet_theta_adjoint: atom '{}' second jet shape {:?}, expected {:?}",
atom.name,
jet.dim(),
expected
));
}
out.push(jet);
}
Ok(out)
}
pub(crate) fn gate_first_derivatives_for_row(
&self,
rho: &SaeManifoldRho,
row: usize,
assignments: ArrayView1<'_, f64>,
vars: &[SaeLocalRowVar],
) -> Result<Vec<Vec<f64>>, String> {
let k_atoms = self.k_atoms();
let q = vars.len();
let mut dz = vec![vec![0.0_f64; k_atoms]; q];
match self.assignment.mode {
AssignmentMode::Softmax { temperature, .. } => {
let inv_tau = 1.0 / temperature;
for (a_idx, var_a) in vars.iter().enumerate() {
let SaeLocalRowVar::Logit { atom: j } = *var_a else {
continue;
};
for k in 0..k_atoms {
let indicator = if k == j { 1.0 } else { 0.0 };
dz[a_idx][k] = assignments[k] * (indicator - assignments[j]) * inv_tau;
}
}
}
AssignmentMode::IBPMap {
temperature, alpha, ..
} => {
let effective_alpha = self
.assignment
.mode
.resolved_ibp_alpha(rho)
.unwrap_or(alpha);
let prior = ibp_stick_breaking_prior(k_atoms, effective_alpha);
let inv_tau = 1.0 / temperature;
for (idx, var) in vars.iter().enumerate() {
let SaeLocalRowVar::Logit { atom } = *var else {
continue;
};
let (_z, d1, _d2) =
sae_sigmoid_derivatives_from_value(assignments[atom], inv_tau, prior[atom]);
dz[idx][atom] = d1;
}
}
AssignmentMode::JumpReLU {
temperature,
threshold,
} => {
let inv_tau = 1.0 / temperature;
let logits = self.assignment.logits.row(row);
for (idx, var) in vars.iter().enumerate() {
let SaeLocalRowVar::Logit { atom } = *var else {
continue;
};
if logits[atom] <= threshold {
continue;
}
let (_z, d1, _d2) =
sae_sigmoid_derivatives_from_value(assignments[atom], inv_tau, 1.0);
dz[idx][atom] = d1;
}
}
}
Ok(dz)
}
fn reconstruction_row_program_for_logdet(
&self,
rho: &SaeManifoldRho,
row: usize,
vars: &[SaeLocalRowVar],
assignments: ArrayView1<'_, f64>,
second_jets: &[Array4<f64>],
) -> Result<crate::terms::sae::row_jet_program::SaeReconstructionRowProgram, String> {
use crate::terms::sae::row_jet_program::{
AtomRowBasisJet, RowGate, SAE_FIXED_COORD_SLOT, SaeReconstructionRowProgram,
};
let p = self.output_dim();
let k_atoms = self.k_atoms();
if assignments.len() != k_atoms {
return Err(format!(
"reconstruction_row_program_for_logdet: assignments length {} != K={k_atoms}",
assignments.len()
));
}
if second_jets.len() != k_atoms {
return Err(format!(
"reconstruction_row_program_for_logdet: second_jets length {} != K={k_atoms}",
second_jets.len()
));
}
let mut logit_slot = vec![None; k_atoms];
let mut coord_slot: Vec<Vec<usize>> = self
.atoms
.iter()
.map(|atom| vec![SAE_FIXED_COORD_SLOT; atom.latent_dim])
.collect();
for (slot, var) in vars.iter().enumerate() {
match *var {
SaeLocalRowVar::Logit { atom } => {
if atom >= k_atoms {
return Err(format!(
"reconstruction_row_program_for_logdet: logit atom {atom} outside K={k_atoms}"
));
}
logit_slot[atom] = Some(slot);
}
SaeLocalRowVar::Coord { atom, axis } => {
if atom >= k_atoms || axis >= coord_slot[atom].len() {
return Err(format!(
"reconstruction_row_program_for_logdet: coord ({atom},{axis}) outside atom layout"
));
}
coord_slot[atom][axis] = slot;
}
}
}
let atoms: Vec<AtomRowBasisJet> = self
.atoms
.iter()
.enumerate()
.map(|(atom_idx, atom)| {
let m = atom.basis_size();
let d = atom.latent_dim;
let second = &second_jets[atom_idx];
AtomRowBasisJet {
phi: (0..m)
.map(|basis_col| atom.basis_values[[row, basis_col]])
.collect(),
d_phi: (0..m)
.map(|basis_col| {
(0..d)
.map(|axis| atom.basis_jacobian[[row, basis_col, axis]])
.collect()
})
.collect(),
d2_phi: (0..m)
.map(|basis_col| {
(0..d)
.map(|axis_a| {
(0..d)
.map(|axis_b| second[[row, basis_col, axis_a, axis_b]])
.collect()
})
.collect()
})
.collect(),
decoder: (0..m)
.map(|basis_col| {
(0..p)
.map(|out_col| atom.decoder_coefficients[[basis_col, out_col]])
.collect()
})
.collect(),
latent_dim: d,
}
})
.collect();
let logits = self.assignment.logits.row(row).to_vec();
let (gate, gate_shift, gate_scale) = match self.assignment.mode {
AssignmentMode::Softmax { temperature, .. } => (
RowGate::Softmax {
inv_tau: 1.0 / temperature,
},
vec![0.0; k_atoms],
vec![1.0; k_atoms],
),
AssignmentMode::IBPMap {
temperature, alpha, ..
} => {
let effective_alpha = self
.assignment
.mode
.resolved_ibp_alpha(rho)
.unwrap_or(alpha);
(
RowGate::PerAtomLogistic {
inv_tau: 1.0 / temperature,
},
vec![0.0; k_atoms],
ibp_stick_breaking_prior(k_atoms, effective_alpha).to_vec(),
)
}
AssignmentMode::JumpReLU {
temperature,
threshold,
} => (
RowGate::PerAtomLogistic {
inv_tau: 1.0 / temperature,
},
vec![threshold; k_atoms],
logits
.iter()
.map(|&logit| if logit > threshold { 1.0 } else { 0.0 })
.collect(),
),
};
Ok(SaeReconstructionRowProgram {
atoms,
gate_value: assignments.to_vec(),
logits,
gate_scale,
gate_shift,
gate,
logit_slot,
coord_slot,
n_primaries: vars.len(),
})
}
fn fill_reconstruction_channels_from_program<const K: usize>(
program: &crate::terms::sae::row_jet_program::SaeReconstructionRowProgram,
sqrt_row_w: f64,
first: &mut [Vec<f64>],
second: &mut [Vec<Vec<f64>>],
) {
for out_col in 0..program.out_dim() {
let tower = program.reconstruction_column::<K>(out_col);
for a in 0..K {
first[a][out_col] = sqrt_row_w * tower.g[a];
for b in 0..K {
second[a][b][out_col] = sqrt_row_w * tower.h[a][b];
}
}
}
}
fn fill_reconstruction_channels_from_program_dynamic(
program: &crate::terms::sae::row_jet_program::SaeReconstructionRowProgram,
sqrt_row_w: f64,
first: &mut [Vec<f64>],
second: &mut [Vec<Vec<f64>>],
) -> Result<(), String> {
macro_rules! dispatch {
($($k:literal),* $(,)?) => {
match program.n_primaries {
$(
$k => {
Self::fill_reconstruction_channels_from_program::<$k>(
program,
sqrt_row_w,
first,
second,
);
Ok(())
}
)*
q => Err(format!(
"SAE row reconstruction Tower4 production path supports at most 16 row primaries, got {q}"
)),
}
};
}
dispatch!(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)
}
pub(crate) fn row_jets_for_logdet(
&self,
rho: &SaeManifoldRho,
row: usize,
vars: Vec<SaeLocalRowVar>,
assignments: ArrayView1<'_, f64>,
second_jets: &[Array4<f64>],
border: &[SaeBorderChannel],
) -> Result<SaeRowJets, String> {
let p = self.output_dim();
let q = vars.len();
let sqrt_row_w = self
.row_loss_weights
.as_deref()
.map_or(1.0, |w| w[row].sqrt());
let dz = self.gate_first_derivatives_for_row(rho, row, assignments, &vars)?;
let mut first = vec![vec![0.0_f64; p]; q];
let mut second = vec![vec![vec![0.0_f64; p]; q]; q];
let program =
self.reconstruction_row_program_for_logdet(rho, row, &vars, assignments, second_jets)?;
Self::fill_reconstruction_channels_from_program_dynamic(
&program,
sqrt_row_w,
&mut first,
&mut second,
)?;
let mut beta = vec![vec![0.0_f64; p]; border.len()];
let mut beta_deriv = vec![vec![vec![0.0_f64; p]; border.len()]; q];
let mut beta_l_deriv = vec![vec![vec![0.0_f64; p]; border.len()]; q];
for (beta_pos, channel) in border.iter().enumerate() {
let atom = channel.atom;
let phi = self.atoms[atom].basis_values[[row, channel.basis_col]];
let base = assignments[atom] * phi * sqrt_row_w;
for out_col in 0..p {
beta[beta_pos][out_col] = base * channel.output[out_col];
}
for (var_idx, var) in vars.iter().enumerate() {
let scalar = match *var {
SaeLocalRowVar::Logit { .. } => dz[var_idx][atom] * phi * sqrt_row_w,
SaeLocalRowVar::Coord {
atom: coord_atom,
axis,
} if coord_atom == atom => {
assignments[atom]
* self.atoms[atom].basis_jacobian[[row, channel.basis_col, axis]]
* sqrt_row_w
}
_ => 0.0,
};
if scalar != 0.0 {
for out_col in 0..p {
beta_deriv[var_idx][beta_pos][out_col] = scalar * channel.output[out_col];
}
}
let scalar_l = match *var {
SaeLocalRowVar::Logit { .. } => {
dz[var_idx][atom]
* self.atoms[atom].basis_values[[row, channel.basis_col]]
* sqrt_row_w
}
SaeLocalRowVar::Coord {
atom: coord_atom,
axis,
} if coord_atom == atom => {
assignments[atom]
* self.atoms[atom].basis_jacobian[[row, channel.basis_col, axis]]
* sqrt_row_w
}
_ => 0.0,
};
if scalar_l != 0.0 {
for out_col in 0..p {
beta_l_deriv[var_idx][beta_pos][out_col] =
scalar_l * channel.output[out_col];
}
}
}
}
Ok(SaeRowJets {
vars,
first,
second,
beta,
beta_deriv,
beta_l_deriv,
})
}
pub(crate) fn assignment_prior_hdiag_derivative_entry(
&self,
rho: &SaeManifoldRho,
row: usize,
diag_atom: usize,
wrt: SaeLocalRowVar,
ibp_channels: Option<&IbpHessianDiagThirdChannels>,
) -> f64 {
let SaeLocalRowVar::Logit { atom: wrt_atom } = wrt else {
return 0.0;
};
match self.assignment.mode {
AssignmentMode::Softmax { .. } => {
// #1038: the softmax entropy Hessian is now stored DENSE in
// `block.htt` and its full θ-derivative `∂H_{k,j}/∂z_w` (diagonal
// AND off-diagonal) is added inline in `logdet_theta_adjoint` from
// the shared `row_dense_hessian_logit_derivative`. Returning the
// diagonal contribution here too would double-count, so this
// primitive is silent for softmax — the dense path is the single
// source for value, logdet, and adjoint.
0.0
}
AssignmentMode::JumpReLU {
temperature,
threshold,
} => {
if diag_atom != wrt_atom {
return 0.0;
}
let logit = self.assignment.logits[[row, diag_atom]];
if !crate::terms::sae::assignment::jumprelu_in_optimization_band(
logit,
threshold,
temperature,
) {
return 0.0;
}
let inv_tau = 1.0 / temperature;
let activation =
crate::linalg::utils::stable_logistic((logit - threshold) * inv_tau);
let slope = activation * (1.0 - activation);
2.0 * rho.lambda_sparse()
* slope
* slope
* (1.0 - 2.0 * activation)
* inv_tau
* inv_tau
* inv_tau
}
AssignmentMode::IBPMap { .. } => {
// The assembled `htt` diagonal consumes
// `IBPAssignmentPenalty::hessian_diag`, whose logit derivative
// splits into a row-local direct-`z` channel and a global
// empirical-`M_k` channel (π_k couples every row in column k).
// This same-row primitive returns only the LOCAL direct-`z`
// channel — and only on the matching logit (`diag_atom == w`),
// since H_ik depends on no other row's z explicitly. The global
// M_k channel is accumulated column-wise in
// `logdet_theta_adjoint` (it needs the per-row selected-inverse
// diagonals), so adding it here would double-count.
if diag_atom != wrt_atom {
return 0.0;
}
match ibp_channels {
Some(ch) => ch.local_logit_third[row * ch.k_max + diag_atom],
None => 0.0,
}
}
}
}
pub(crate) fn ard_majorized_hessian_derivative(
&self,
rho: &SaeManifoldRho,
row: usize,
atom: usize,
axis: usize,
) -> f64 {
if rho.log_ard[atom].is_empty() {
return 0.0;
}
let alpha = SaeManifoldRho::stable_exp_strength(rho.log_ard[atom][axis]);
let periods = self.assignment.coords[atom].effective_axis_periods();
let t = self.assignment.coords[atom].row(row)[axis];
let prior = ArdAxisPrior::eval(alpha, t, periods[axis]);
if prior.hess <= 0.0 {
return 0.0;
}
match periods[axis] {
None => 0.0,
Some(period) => {
let kappa = std::f64::consts::TAU / period;
-alpha * kappa * (kappa * t).sin()
}
}
}
pub fn outer_rho_gradient_ift_rhs(
&self,
rho: &SaeManifoldRho,
target: ArrayView2<'_, f64>,
j: usize,
cache: &ArrowFactorCache,
) -> Result<SaeArrowVector, String> {
let n_params = rho.to_flat().len();
if j >= n_params {
return Err(format!(
"outer_rho_gradient_ift_rhs: coordinate {j} outside rho dim {n_params}"
));
}
let mut t = Array1::<f64>::zeros(cache.delta_t_len());
let mut beta = Array1::<f64>::zeros(cache.k);
if j == 0 {
let assignment_grad =
assignment_prior_log_strength_target_mixed(&self.assignment, rho)?;
let k_atoms = self.k_atoms();
let assignment_dim = self.assignment.assignment_coord_dim();
for row in 0..self.n_obs() {
let base = cache.row_offsets[row];
let assignment_base = row * k_atoms;
match self.last_row_layout {
Some(ref layout) => {
for (pos, &atom) in layout.active_atoms[row].iter().enumerate() {
t[base + pos] = assignment_grad[assignment_base + atom];
}
}
None => {
for free_idx in 0..assignment_dim {
t[base + free_idx] = assignment_grad[assignment_base + free_idx];
}
}
}
}
self.add_learnable_ibp_forward_alpha_data_rhs(rho, target, cache, &mut t, &mut beta)?;
} else if j == 1 {
let lambda = rho.lambda_smooth();
let frames_active = self.last_frames_active && cache.k == self.factored_border_dim();
let offsets = if frames_active {
self.factored_beta_offsets()
} else {
self.beta_offsets()
};
for (atom_idx, atom) in self.atoms.iter().enumerate() {
let m = atom.basis_size();
let coeffs = if frames_active {
match &atom.decoder_frame {
Some(frame) => frame.project_decoder(atom.decoder_coefficients.view())?,
None => atom.decoder_coefficients.clone(),
}
} else {
atom.decoder_coefficients.clone()
};
let r = coeffs.ncols();
let off = offsets[atom_idx];
for mu in 0..m {
for channel in 0..r {
let mut acc = 0.0_f64;
for nu in 0..m {
let s_sym = 0.5
* (atom.smooth_penalty[[mu, nu]] + atom.smooth_penalty[[nu, mu]]);
acc += s_sym * coeffs[[nu, channel]];
}
beta[off + mu * r + channel] = lambda * acc;
}
}
}
} else {
let mut cursor = 2usize;
for atom in 0..rho.log_ard.len() {
for axis in 0..rho.log_ard[atom].len() {
if cursor == j {
let alpha = SaeManifoldRho::stable_exp_strength(rho.log_ard[atom][axis]);
let periods = self.assignment.coords[atom].effective_axis_periods();
for row in 0..self.n_obs() {
let row_t = self.assignment.coords[atom].row(row);
let prior = ArdAxisPrior::eval(alpha, row_t[axis], periods[axis]);
let Some(pos) = sae_coord_penalty_offset(
self.last_row_layout.as_ref(),
self.assignment.coord_offsets()[atom] + axis,
row,
atom,
) else {
continue;
};
t[cache.row_offsets[row] + pos] = prior.grad;
}
return Ok(SaeArrowVector { t, beta });
}
cursor += 1;
}
}
}
Ok(SaeArrowVector { t, beta })
}
pub(crate) fn logdet_theta_adjoint(
&self,
rho: &SaeManifoldRho,
cache: &ArrowFactorCache,
solver: &DeflatedArrowSolver<'_>,
) -> Result<SaeArrowVector, String> {
// Γ_a = tr(H⁻¹ ∂H/∂θ_a) over the inner variables θ (#1006). `H` here is
// the SAME object the evidence factor builds — Gauss-Newton data
// curvature plus the prior majorizers / `hessian_diag` diagonals the
// Newton/Schur Cholesky factorizes — so each block's θ-derivative channel
// is differentiated on the criterion's own branch (no value/gradient
// desync). The IBP-MAP assignment prior is the one block whose
// `hessian_diag` couples every row in a column through the plug-in
// empirical mass `M_k = Σ_i z_ik`; its logit derivative therefore has a
// row-local channel (handled inline via
// `assignment_prior_hdiag_derivative_entry`) and a cross-row channel
// (accumulated column-wise after the row loop, below).
let n = self.n_obs();
let total_t = cache.delta_t_len();
let mut gamma_t = Array1::<f64>::zeros(total_t);
let mut gamma_beta = Array1::<f64>::zeros(cache.k);
let second_jets = self.atom_second_jets()?;
let border = self.border_channels_for_cache(cache)?;
let mut beta_inv = Array2::<f64>::zeros((cache.k, cache.k));
if cache.k > 0 {
let rhs_t = Array1::<f64>::zeros(total_t);
for col in 0..cache.k {
let mut rhs_beta = Array1::<f64>::zeros(cache.k);
rhs_beta[col] = 1.0;
let solved = solver.solve(rhs_t.view(), rhs_beta.view()).map_err(|err| {
format!("logdet_theta_adjoint: beta selected inverse solve: {err}")
})?;
for row in 0..cache.k {
beta_inv[[row, col]] = solved.beta[row];
}
}
}
// IBP `hessian_diag` logit third-derivative channels (#1006), exact for
// the diagonal/quasi-Laplace assignment curvature this assembly actually
// factors. The full IBP Hessian also has per-column cross-row rank-one
// terms; those are omitted from H and therefore from this adjoint until
// the evidence factor grows the matching Woodbury correction.
let ibp_channels = ibp_assignment_third_channels(&self.assignment, rho)?;
let k_atoms = self.k_atoms();
// #1038 softmax entropy: the dense per-row entropy Hessian written into
// `block.htt` has off-diagonal logit terms whose θ-derivative the adjoint
// must contract too (not just the diagonal). Build the SAME penalty +
// `scale = λ/τ²` the assembly uses so value/logdet/adjoint differentiate
// one operator. `None` for non-softmax modes (their diagonal/cross-row
// channels are handled by `assignment_prior_hdiag_derivative_entry` and
// the IBP column pass).
let softmax_dense_adjoint: Option<(
crate::terms::analytic_penalties::SoftmaxAssignmentSparsityPenalty,
f64,
)> = match self.assignment.mode {
AssignmentMode::Softmax {
temperature,
sparsity,
} if k_atoms > 1 => {
let inv_tau = 1.0 / temperature;
let scale = rho.lambda_sparse() * sparsity * inv_tau * inv_tau;
Some((
crate::terms::analytic_penalties::SoftmaxAssignmentSparsityPenalty::new(
k_atoms,
temperature,
),
scale,
))
}
_ => None,
};
// Per active logit position: (row i, column k, global t-index,
// (H⁻¹)_ik,ik) — the inputs to the IBP cross-row empirical-`M_k` channel.
let mut ibp_logit_sites: Vec<(usize, usize, usize, f64)> = Vec::new();
for row in 0..n {
let q = cache.row_dims[row];
let base = cache.row_offsets[row];
let vars = self.row_vars_for_cache_row(row, cache)?;
let assignments = self.assignment.try_assignments_row_for_rho(row, rho)?;
let jets = self.row_jets_for_logdet(
rho,
row,
vars,
assignments.view(),
&second_jets,
&border,
)?;
let mut inv_vv = Array2::<f64>::zeros((q, q));
let mut inv_vbeta = Array2::<f64>::zeros((q, cache.k));
for col in 0..q {
let mut rhs_t = Array1::<f64>::zeros(total_t);
let rhs_beta = Array1::<f64>::zeros(cache.k);
rhs_t[base + col] = 1.0;
let solved = solver.solve(rhs_t.view(), rhs_beta.view()).map_err(|err| {
format!("logdet_theta_adjoint: selected inverse solve: {err}")
})?;
for r in 0..q {
inv_vv[[r, col]] = solved.t[base + r];
}
for b in 0..cache.k {
inv_vbeta[[col, b]] = solved.beta[b];
}
}
// Record each active logit's column, global t-index, and
// selected-inverse diagonal (H⁻¹)_ik,ik for the IBP cross-row pass.
if ibp_channels.is_some() {
for (pos, var) in jets.vars.iter().enumerate() {
if let SaeLocalRowVar::Logit { atom } = *var {
ibp_logit_sites.push((row, atom, base + pos, inv_vv[[pos, pos]]));
}
}
}
// #1190: when `w` is a logit and the assignment is softmax, the per-row
// softmax Fisher-metric `G = scale·(diag(a) − a aᵀ)` is what the assembly
// wrote into `htt` (the PSD curvature operator that replaces the
// indefinite exact entropy Hessian). Its full θ-derivative `∂G_{k,j}/∂z_w`
// (diagonal AND off-diagonal) is the SAME `a`-derived tensor the assembly
// and logdet now differentiate, so value and adjoint stay on ONE exact
// branch. Compute it once per logit `w` and add it at every logit pair
// `(a,b)` below. The diagonal softmax case is therefore handled here, NOT
// in `assignment_prior_hdiag_derivative_entry` (which returns 0 for
// softmax to avoid double-counting).
let row_logits_softmax: Option<Vec<f64>> = softmax_dense_adjoint.as_ref().map(|_| {
(0..k_atoms)
.map(|k| self.assignment.logits[[row, k]])
.collect()
});
for w in 0..q {
let mut gamma = 0.0_f64;
let softmax_dh_w: Option<Array2<f64>> = match (
softmax_dense_adjoint.as_ref(),
row_logits_softmax.as_ref(),
jets.vars[w],
) {
(Some((penalty, scale)), Some(row_logits), SaeLocalRowVar::Logit { atom }) => {
Some(penalty.row_fisher_metric_logit_derivative(row_logits, *scale, atom))
}
_ => None,
};
for a in 0..q {
for b in 0..q {
let mut dh = sae_dot(&jets.second[a][w], &jets.first[b])
+ sae_dot(&jets.first[a], &jets.second[b][w]);
if let (
Some(dh_w),
SaeLocalRowVar::Logit { atom: atom_a },
SaeLocalRowVar::Logit { atom: atom_b },
) = (softmax_dh_w.as_ref(), jets.vars[a], jets.vars[b])
{
dh += dh_w[[atom_a, atom_b]];
}
if a == b {
dh += match jets.vars[a] {
SaeLocalRowVar::Logit { atom } => self
.assignment_prior_hdiag_derivative_entry(
rho,
row,
atom,
jets.vars[w],
ibp_channels.as_ref(),
),
SaeLocalRowVar::Coord { atom, axis } if a == w => {
self.ard_majorized_hessian_derivative(rho, row, atom, axis)
}
_ => 0.0,
};
}
gamma += inv_vv[[b, a]] * dh;
}
}
for a in 0..q {
for (beta_pos, channel) in border.iter().enumerate() {
let dh = sae_dot(&jets.second[a][w], &jets.beta[beta_pos])
+ sae_dot(&jets.first[a], &jets.beta_deriv[w][beta_pos]);
gamma += 2.0 * inv_vbeta[[a, channel.index]] * dh;
}
}
for (beta_i, channel_i) in border.iter().enumerate() {
for (beta_j, channel_j) in border.iter().enumerate() {
let dh = sae_dot(&jets.beta_deriv[w][beta_i], &jets.beta[beta_j])
+ sae_dot(&jets.beta[beta_i], &jets.beta_deriv[w][beta_j]);
gamma += beta_inv[[channel_i.index, channel_j.index]] * dh;
}
}
gamma_t[base + w] = gamma;
}
for (w_beta_pos, w_channel) in border.iter().enumerate() {
let mut gamma = 0.0_f64;
for a in 0..q {
for b in 0..q {
let dh = sae_dot(&jets.beta_l_deriv[a][w_beta_pos], &jets.first[b])
+ sae_dot(&jets.first[a], &jets.beta_l_deriv[b][w_beta_pos]);
gamma += inv_vv[[b, a]] * dh;
}
}
for a in 0..q {
for (beta_pos, channel) in border.iter().enumerate() {
let dh = sae_dot(&jets.beta_l_deriv[a][w_beta_pos], &jets.beta[beta_pos]);
gamma += 2.0 * inv_vbeta[[a, channel.index]] * dh;
}
}
gamma_beta[w_channel.index] += gamma;
}
}
// IBP cross-row empirical-`M_k` channel of Γ (#1006). The assembled
// diagonal H_ik consumes `hessian_diag`, whose dependence on the column
// mass M_k = Σ_i z_ik couples every row in a column. Differentiating
// tr(H⁻¹ ∂H/∂ℓ_wk) on that shared branch:
// Γ_wk += [ Σ_i (H⁻¹)_ik,ik · ∂_M H_ik ] · J_wk = C_k · J_wk,
// where ∂_M H_ik = `m_channel[i*K+k]` and J_wk = `z_jac[w*K+k]`. The
// row-local direct-`z` channel was already added inline above, so this
// pass adds only the cross-row remainder (it spans `w ≠ i` and the
// self-row M_k self-coupling, which the row-local primitive deliberately
// omits to avoid double-counting).
if let Some(channels) = ibp_channels.as_ref() {
let mut col_coeff = vec![0.0_f64; k_atoms];
for &(row, atom, _t_index, inv_diag) in &ibp_logit_sites {
col_coeff[atom] += inv_diag * channels.m_channel[row * k_atoms + atom];
}
for &(row, atom, t_index, _inv_diag) in &ibp_logit_sites {
gamma_t[t_index] += col_coeff[atom] * channels.z_jac[row * k_atoms + atom];
}
}
Ok(SaeArrowVector {
t: gamma_t,
beta: gamma_beta,
})
}
/// Analytic SAE REML outer-ρ gradient components at the already converged
/// inner state represented by `loss` and `cache`.
///
/// The returned gradient is the assembled analytic outer derivative:
/// explicit penalty terms, direct logdet traces, Occam terms, and the #1006
/// implicit-state third-order correction.
pub(crate) fn analytic_outer_rho_gradient_components(
&self,
target: ArrayView2<'_, f64>,
rho: &SaeManifoldRho,
loss: &SaeManifoldLoss,
cache: &ArrowFactorCache,
solver: &DeflatedArrowSolver<'_>,
) -> Result<SaeOuterRhoGradientComponents, String> {
let n_params = rho.to_flat().len();
let mut explicit = Array1::<f64>::zeros(n_params);
let mut logdet_trace = Array1::<f64>::zeros(n_params);
let mut occam = Array1::<f64>::zeros(n_params);
let mut third_order_correction = Array1::<f64>::zeros(n_params);
explicit[0] = assignment_prior_log_strength_derivative(&self.assignment, rho)
+ self.learnable_ibp_forward_alpha_data_derivative(rho, target)?;
logdet_trace[0] = self.assignment_log_strength_hessian_trace(rho, cache, solver)?;
explicit[1] = loss.smoothness;
logdet_trace[1] = 0.5
* self
.decoder_smoothness_effective_dof_with_solver(cache, solver, rho.lambda_smooth())
.map_err(|err| format!("analytic_outer_rho_gradient_components: {err}"))?;
occam[1] = -self.reml_occam_log_lambda_smooth_derivative()?;
let ard_explicit = self.ard_log_precision_explicit_derivatives(rho)?;
let ard_trace = self
.ard_log_precision_hessian_trace(rho, cache, solver)
.map_err(|err| format!("analytic_outer_rho_gradient_components: {err}"))?;
let mut cursor = 2usize;
for k in 0..rho.log_ard.len() {
for axis in 0..rho.log_ard[k].len() {
explicit[cursor] = ard_explicit[k][axis];
logdet_trace[cursor] = ard_trace[k][axis];
cursor += 1;
}
}
let gamma = self.logdet_theta_adjoint(rho, cache, solver)?;
for coord in 0..n_params {
let rhs = self.outer_rho_gradient_ift_rhs(rho, target, coord, cache)?;
let solved = solver.solve(rhs.t.view(), rhs.beta.view()).map_err(|err| {
format!("analytic_outer_rho_gradient_components: full_inverse_apply: {err}")
})?;
let mut dot = 0.0_f64;
for idx in 0..gamma.t.len() {
dot += gamma.t[idx] * solved.t[idx];
}
for idx in 0..gamma.beta.len() {
dot += gamma.beta[idx] * solved.beta[idx];
}
third_order_correction[coord] = -0.5 * dot;
}
Ok(SaeOuterRhoGradientComponents {
explicit,
logdet_trace,
occam,
third_order_correction,
})
}
/// Public analytic outer-ρ gradient at a converged inner state, constructing
/// the deflated arrow solver from the supplied cache. Use this seam from
/// integration tests and external consumers that have a converged
/// `(loss, cache)` from [`Self::reml_criterion_with_cache`] but no access to
/// the crate-private `DeflatedArrowSolver`.
pub fn analytic_outer_rho_gradient_at_converged(
&self,
target: ArrayView2<'_, f64>,
rho: &SaeManifoldRho,
loss: &SaeManifoldLoss,
cache: &ArrowFactorCache,
) -> Result<SaeOuterRhoGradientComponents, String> {
let solver = self.outer_gradient_arrow_solver(cache)?;
self.analytic_outer_rho_gradient_components(target, rho, loss, cache, &solver)
}
/// Compose the SAE LAML criterion as a sum of atoms (#931 SAE pilot).
///
/// This is the single seam that establishes value↔gradient coherence for
/// the SAE objective: it runs the inner solve once via
/// [`Self::reml_criterion_with_cache`], reads the value decomposition
/// (`loss.total() + extra_penalty_energy`, `log|H|`, `occam`) and the
/// matching gradient channels (`SaeOuterRhoGradientComponents`) from the
/// SAME converged cache, and hands them to [`SaeCriterion::assemble`]. The
/// returned criterion's [`SaeCriterion::value`] and
/// [`SaeCriterion::gradient`] are then projections of one factorization —
/// the outer optimizer can no longer evaluate a value path and a gradient
/// path that disagree (the #752/#748/#901 desync class). The
/// implicit-stationarity envelope correction (#1006's Γ term) is its own
/// named atom, so the channel the desync class keeps dropping is visible
/// rather than a silent zero.
pub fn criterion_as_atoms(
&mut self,
target: ArrayView2<'_, f64>,
rho: &SaeManifoldRho,
registry: Option<&AnalyticPenaltyRegistry>,
inner_max_iter: usize,
learning_rate: f64,
ridge_ext_coord: f64,
ridge_beta: f64,
) -> Result<SaeCriterion, String> {
let (_v, loss, cache) = self.reml_criterion_with_cache(
target,
rho,
registry,
inner_max_iter,
learning_rate,
ridge_ext_coord,
ridge_beta,
)?;
let log_det = arrow_log_det_from_cache(&cache).ok_or_else(|| {
"criterion_as_atoms: arrow_log_det_from_cache returned None".to_string()
})?;
let occam = self.reml_occam_term(rho)?;
let extra_penalty_energy = match registry {
Some(reg) => self
.reml_extra_penalty_value_total(reg)
.map_err(|err| format!("SaeManifoldTerm::criterion_as_atoms: {err}"))?,
None => 0.0,
};
let data_fit_priors_value = loss.total() + extra_penalty_energy;
let solver = self.outer_gradient_arrow_solver(&cache)?;
let components =
self.analytic_outer_rho_gradient_components(target, rho, &loss, &cache, &solver)?;
Ok(SaeCriterion::assemble(
data_fit_priors_value,
log_det,
occam,
components.explicit,
components.logdet_trace,
components.occam,
components.third_order_correction,
))
}
/// Gaussian reconstruction dispersion `φ̂`, the scale that turns the
/// unscaled inverse-Hessian β-block `S_β⁻¹` into a posterior covariance
/// `Cov(β) = φ̂·S_β⁻¹` — the same `Vb = φ·H⁻¹` convention the main GAM
/// inference path uses.
///
/// `RSS = Σ_{i,c} (z_{ic} − ẑ_{ic})² = 2·data_fit` (the loss stores the
/// half-sum `½Σr²`). The residual degrees of freedom subtract the effective
/// parameter count from the `N·p` scalar observations:
/// * decoder β: `beta_dim − tr(λ_smooth · S_β⁻¹ · ⊕_k S_k⊗I_p)`, the
/// smoothness effective-dof already assembled for the Fellner-Schall
/// step (penalty-shrunk directions do not cost a full parameter);
/// * latent coordinates: enabled ARD axes use the exact ARD-shrunk trace
/// `Σ_k Σ_j (n_active_k − α_{kj}·tr_{kj}(H⁻¹))`; atoms with disabled
/// native ARD charge the full active coordinate count because those
/// latent variables are estimated without an ARD precision.
///
/// The coordinate term is the **exact** ARD-shrunk effective dof of the
/// latent block: along axis `(k,j)` the MacKay/Fellner-Schall edf is
/// `n_active_k − α_{kj}·tr_{kj}(H⁻¹)`, the well-determined-direction count
/// after the ARD prior `α_{kj}` shrinks each coordinate. `tr_{kj}(H⁻¹)` is
/// the same posterior-variance trace [`Self::ard_inverse_traces`] assembles
/// for the EFS ARD step (reused here, not recomputed), so the dispersion is
/// consistent with the precision update `α_new = n/(‖t‖²+tr(H⁻¹))`. The
/// per-axis scalar count `n_active_k` must match the support the trace sums
/// over: `n` for the dense full-support layout, or the number of rows where
/// atom `k` is active for the compact active-set layout (inactive
/// prior-dominated coordinates contribute 0 to both the trace and the
/// count, hence 0 edf). The residual dof is floored at 1 so `φ̂` stays
/// finite and positive.
pub(crate) fn reconstruction_dispersion(
&self,
loss: &SaeManifoldLoss,
cache: &ArrowFactorCache,
rho: &SaeManifoldRho,
) -> Result<f64, String> {
let n = self.n_obs();
let p = self.output_dim();
let n_scalar = (n * p) as f64;
let rss = 2.0 * loss.data_fit;
let smooth_edf = self
.decoder_smoothness_effective_dof(cache, rho.lambda_smooth())
.map_err(|e| format!("reconstruction_dispersion: smooth edf: {e}"))?;
// #972 / #977 T1: the raw decoder-parameter count is `beta_dim` on the
// full-`B` path, but when frames are active the estimated decoder freedom
// is the factored border `Σ M_k·r_k` PLUS the `Σ r_k·(p−r_k)` Grassmann
// frame degrees profiled out (both are genuinely estimated), which the
// smoothness shrinkage `smooth_edf` (taken over the factored border) then
// discounts. On the full-`B` path `factored_border_dim == beta_dim` and
// `grassmann_evidence_dimension == 0`, so this is exactly `beta_dim`.
let raw_decoder_dof = if self.frames_active() {
(self.factored_border_dim() + self.grassmann_evidence_dimension()) as f64
} else {
self.beta_dim() as f64
};
let beta_edf = (raw_decoder_dof - smooth_edf).max(0.0);
// Exact ARD-shrunk latent-coordinate edf, reusing the EFS trace cache.
let traces = self
.ard_inverse_traces(cache)
.map_err(|e| format!("reconstruction_dispersion: ARD traces: {e}"))?;
if rho.log_ard.len() != self.atoms.len() {
return Err(format!(
"reconstruction_dispersion: ρ has {} ARD atoms but term has {}",
rho.log_ard.len(),
self.atoms.len()
));
}
let mut coord_edf = 0.0_f64;
for (k, atom) in self.atoms.iter().enumerate() {
let d_k = atom.latent_dim;
if traces[k].len() != d_k {
return Err(format!(
"reconstruction_dispersion: trace shape mismatch at atom {k} \
(traces={}, d_k={d_k})",
traces[k].len()
));
}
let ard_len = rho.log_ard[k].len();
if ard_len != 0 && ard_len != d_k {
return Err(format!(
"reconstruction_dispersion: ARD shape mismatch at atom {k} \
(log_ard={ard_len}, d_k={d_k})"
));
}
// Scalar count matched to the trace support (see fn doc).
let n_active_k = match self.last_row_layout {
Some(ref layout) => layout
.active_atoms
.iter()
.filter(|active| active.contains(&k))
.count() as f64,
None => n as f64,
};
if ard_len == 0 {
coord_edf += n_active_k * d_k as f64;
continue;
}
for j in 0..d_k {
let alpha = SaeManifoldRho::stable_exp_strength(rho.log_ard[k][j]);
// edf_kj ∈ [0, n_active_k]; clamp against numerical drift.
let edf_kj = (n_active_k - alpha * traces[k][j]).clamp(0.0, n_active_k);
coord_edf += edf_kj;
}
}
let resid_dof = (n_scalar - beta_edf - coord_edf).max(1.0);
let phi = rss / resid_dof;
if !phi.is_finite() || phi < 0.0 {
return Err(format!(
"reconstruction_dispersion: non-finite/negative φ̂={phi} \
(RSS={rss}, resid_dof={resid_dof}, beta_edf={beta_edf}, coord_edf={coord_edf})"
));
}
Ok(phi.max(f64::MIN_POSITIVE))
}
/// Posterior covariance and ambient shape band for every atom — the
/// user-facing uncertainty of the fitted manifold shapes.
///
/// For atom `k` with decoder-block range `r_k` (see
/// [`Self::beta_block_offsets`]), `Cov(β_k) = φ·S_β⁻¹[r_k, r_k]` is the
/// φ-scaled posterior covariance of its decoder coefficients with the
/// latent coordinates marginalized out. The ambient point at a coordinate
/// `t` is `m_k(t) = Φ_k(t)·B_k`, *linear* in `β_k`, so its per-channel
/// posterior variance is the closed form
/// `Var_c(t) = Σ_{b1,b2} Φ_k(t)[b1] Φ_k(t)[b2] · Cov(β_k)[(b1,c),(b2,c)]`
/// — no sampling. The band is evaluated at up to [`SHAPE_BAND_MAX_POINTS`]
/// evenly-strided of the atom's own on-atom coordinates, reusing the basis
/// values already stored on the atom, so it reports uncertainty exactly
/// where the data lives and needs no basis-kind-specific grid.
///
/// A near-degenerate atom has a near-singular Schur block, so `Cov(β_k)` —
/// and the band — fans out automatically: the band width is a
/// per-coordinate visual of how well each atom is identified.
pub fn assemble_shape_uncertainty(
&self,
cache: &ArrowFactorCache,
dispersion: f64,
) -> Result<SaeShapeUncertainty, String> {
let p = self.output_dim();
// #972 / #977 T1: the cache β block is the FACTORED border when frames
// are active, so each atom's Schur inverse block is the `(M_k·r_k)`
// coordinate covariance `Cov(vec C_k)`. We LIFT it to the full
// `(M_k·p)` decoder covariance `Cov(vec B_k) = (I_{M_k} ⊗ U_k) Cov(vec
// C_k)(I_{M_k} ⊗ U_k)ᵀ` (since `B_k = C_k U_kᵀ`) so the downstream band
// code — which reads the `b·p + c` flat layout — is unchanged. On the
// full-`B` path the block is already `(M_k·p)` and the lift is skipped.
let frames_active = self.frames_active();
let frame_projection = FrameProjection::new(self);
let block_ranges = if frames_active {
(0..self.k_atoms())
.map(|k| frame_projection.atom_border_range(k))
.collect::<Vec<_>>()
} else {
self.beta_block_offsets().to_vec()
};
let mut atoms = Vec::with_capacity(self.k_atoms());
for (k, atom) in self.atoms.iter().enumerate() {
let m = atom.basis_size();
let cov_block = cache
.schur_inverse_block(block_ranges[k].clone())
.map_err(|e| format!("assemble_shape_uncertainty: atom {k}: {e}"))?;
let n_rows = atom.n_obs();
let d = atom.latent_dim;
// Evenly-strided evaluation rows bound the band cost.
let stride = n_rows.div_ceil(SHAPE_BAND_MAX_POINTS).max(1);
let eval_rows: Vec<usize> = (0..n_rows).step_by(stride).collect();
let g = eval_rows.len();
let coords_mat = self.assignment.coords[k].as_matrix();
let mut band_coords = Array2::<f64>::zeros((g, d));
let mut band_mean = Array2::<f64>::zeros((g, p));
let mut band_sd = Array2::<f64>::zeros((g, p));
let mut decoded = vec![0.0_f64; p];
for (gi, &row) in eval_rows.iter().enumerate() {
for axis in 0..d {
band_coords[[gi, axis]] = coords_mat[[row, axis]];
}
atom.fill_decoded_row(row, &mut decoded);
for c in 0..p {
band_mean[[gi, c]] = decoded[c];
}
}
let framed = frames_active && atom.decoder_frame.is_some();
let dense_entries = (m * p).saturating_mul(m * p);
let cov = if framed && dense_entries > SAE_DECODER_COV_PAYLOAD_MAX_ENTRIES {
// LLM-scale ambient `p`: the dense `(M_k·p)²` lift would be
// gigabytes per atom and exists only to export the full
// covariance. Compute the band variance EXACTLY from the
// factored frame covariance instead: with `B_k = C_k·U_kᵀ`,
// Var_c(t) = (φ ⊗ u_c)ᵀ Cov(vec C_k) (φ ⊗ u_c)
// which is the r×r quadratic form `u_cᵀ Y u_c` with
// Y = Σ_{b1,b2} φ[b1] φ[b2] Cov(C)[(b1,·),(b2,·)].
let mut cov_c = cov_block;
cov_c.mapv_inplace(|v| v * dispersion);
for (gi, &row) in eval_rows.iter().enumerate() {
let basis = atom.basis_values.row(row);
for c in 0..p {
let var = frame_projection.output_variance(k, cov_c.view(), basis, c);
band_sd[[gi, c]] = var.max(0.0).sqrt();
}
}
None
} else {
// Lift the factored `(M_k·r_k)` coordinate covariance to the
// full `(M_k·p)` decoder covariance through this atom's frame;
// identity (a plain scaled copy) on the un-framed full-`B` path.
let mut cov = if framed {
frame_projection.lift_block(k, cov_block.view())
} else {
cov_block
};
cov.mapv_inplace(|v| v * dispersion);
for (gi, &row) in eval_rows.iter().enumerate() {
// Var_c = Σ_{b1,b2} Φ[b1]Φ[b2] Cov[(b1,c),(b2,c)]; the flat
// decoder index is basis·p + channel (row-major (M_k, p)).
for c in 0..p {
let var = frame_projection.full_output_variance(
k,
cov.view(),
atom.basis_values.row(row),
c,
);
band_sd[[gi, c]] = var.max(0.0).sqrt();
}
}
Some(cov)
};
atoms.push(SaeAtomShapeUncertainty {
decoder_covariance: cov,
band_coords,
band_mean,
band_sd,
});
}
Ok(SaeShapeUncertainty { dispersion, atoms })
}
/// #977 — complete the per-atom shape band for any atom the pre-search
/// Schur factor could not cover (a structure-search-BORN atom, whose index
/// is ≥ the seed `K` the Schur cache was assembled at), from that atom's OWN
/// fitted penalized inner Hessian.
///
/// The Schur path ([`Self::assemble_shape_uncertainty`]) reads the joint
/// inverse-Hessian β-block per atom, but that factor is assembled ONCE before
/// the structure search runs, so it is indexed by the SEED dictionary. A born
/// atom therefore has no Schur block and would otherwise be reported with NO
/// uncertainty band — a silent gap. This method closes it: every atom carries
/// a band, none is reported without one.
///
/// The principled per-atom band is the Laplace posterior of the atom's inner
/// reconstruction smooth, which [`Self::set_atom_inner_fits`] already fits at
/// the settled state for EVERY atom (born included). With the Gaussian-identity
/// inner smooth, each output channel `c`'s decoder posterior is
/// `Cov(β_{k,c}) = φ · H_k⁻¹`, where `H_k = Φ_kᵀ W_k Φ_k + S̃_k` is the atom's
/// fitted penalized inner Hessian (`AtomInnerFit::penalized_hessian`). The
/// ambient point `m_k(t) = Φ_k(t)·B_k` is linear in `B_k`, so its per-channel
/// posterior variance is the closed form
/// `Var_c(t) = φ · Φ_k(t)ᵀ H_k⁻¹ Φ_k(t)`,
/// which is the SAME for every channel `c` (the inner Hessian is shared across
/// channels; the decoder differs only in the mean). The band is evaluated at
/// the same evenly-strided on-atom coordinate subset the Schur path uses, so a
/// born atom's band is reported exactly where its data lives.
///
/// This is a strict completion: an atom whose band the Schur path already
/// filled (a finite `band_sd`) is left untouched; only atoms with a missing
/// entry (index past the assembled set) or an all-NaN band are filled. An
/// all-NaN band arises either as the no-decoder-covariance fallback OR when
/// the caller deliberately invalidated a stale PRE-search band via
/// [`SaeShapeUncertainty::invalidate_bands_for_recompute`] after a structure
/// move re-converged the dictionary (#1230); in both cases the band is
/// recomputed here against the FINAL model. When a band is (re)filled the
/// whole slot — `band_coords`, `band_mean`, AND `band_sd` — is rebuilt from
/// the current fitted atom, so an atom whose coordinates / decoded mean / row
/// count shifted under a structure-search refit gets a fully consistent band
/// (never a stale-coordinate or shape-mismatched one). An atom whose inner fit
/// is degenerate (`None` — no active rows / non-SPD inner Hessian) is left
/// with its NaN band, faithfully reporting "unidentified" rather than
/// fabricating a number. Requires [`Self::set_atom_inner_fits`] to have run;
/// without it the completion is a no-op (the band stays as the Schur path left
/// it).
pub fn complete_born_atom_shape_bands(
&self,
unc: &mut SaeShapeUncertainty,
) -> Result<(), String> {
let inner_fits = match &self.atom_inner_fits {
Some(fits) => fits,
// No inner fits harvested: nothing to complete from. Leave the bands
// as the Schur path produced them.
None => return Ok(()),
};
let p = self.output_dim();
let dispersion = unc.dispersion;
// Grow the per-atom band list to the post-search atom count so a born
// atom (index past the Schur-assembled set) has a slot. New slots start
// as NaN bands and are filled below from the inner fit.
while unc.atoms.len() < self.k_atoms() {
let k = unc.atoms.len();
let atom = &self.atoms[k];
let n_rows = atom.n_obs();
let d = atom.latent_dim;
let stride = n_rows.div_ceil(SHAPE_BAND_MAX_POINTS).max(1);
let eval_rows: Vec<usize> = (0..n_rows).step_by(stride).collect();
let g = eval_rows.len();
let coords_mat = self.assignment.coords[k].as_matrix();
let mut band_coords = Array2::<f64>::zeros((g, d));
let mut band_mean = Array2::<f64>::zeros((g, p));
let band_sd = Array2::<f64>::from_elem((g, p), f64::NAN);
let mut decoded = vec![0.0_f64; p];
for (gi, &row) in eval_rows.iter().enumerate() {
for axis in 0..d {
band_coords[[gi, axis]] = coords_mat[[row, axis]];
}
atom.fill_decoded_row(row, &mut decoded);
for c in 0..p {
band_mean[[gi, c]] = decoded[c];
}
}
unc.atoms.push(SaeAtomShapeUncertainty {
decoder_covariance: None,
band_coords,
band_mean,
band_sd,
});
}
for (k, atom) in self.atoms.iter().enumerate() {
let band = &mut unc.atoms[k];
// Only complete a MISSING band: an atom the Schur path already filled
// (a finite sd anywhere) keeps its joint-Hessian band untouched.
let already_filled = band.band_sd.iter().any(|v| v.is_finite());
if already_filled {
continue;
}
let inner = match inner_fits.get(k).and_then(|f| f.as_ref()) {
Some(f) => f,
// Degenerate atom (no active rows / non-SPD inner Hessian): leave
// the NaN band — honestly "unidentified", never a fabricated band.
None => continue,
};
let m = atom.basis_size();
if inner.penalized_hessian.dim() != (m, m) {
return Err(format!(
"complete_born_atom_shape_bands: atom {k} inner Hessian {:?} != ({m}, {m})",
inner.penalized_hessian.dim()
));
}
// Factor the atom's own penalized inner Hessian H_k = ΦᵀWΦ + S̃_k. It
// was checked SPD when the inner fit was built; re-factor here to solve
// H_k⁻¹ Φ(t). A factorization failure (numerical drift since the inner
// fit) leaves the NaN band rather than a fabricated number.
let chol = match inner.penalized_hessian.cholesky(Side::Lower) {
Ok(c) => c,
Err(_) => continue,
};
// Evenly-strided on-atom rows, matched to the band the Schur path uses.
let n_rows = atom.n_obs();
let d = atom.latent_dim;
let stride = n_rows.div_ceil(SHAPE_BAND_MAX_POINTS).max(1);
let eval_rows: Vec<usize> = (0..n_rows).step_by(stride).collect();
let g = eval_rows.len();
// Rebuild the ENTIRE band slot (coords / mean / sd) from the CURRENT
// fitted atom rather than only overwriting `band_sd`. #1230 — a seed
// atom whose pre-search band was invalidated for recompute (because
// structure search re-converged the dictionary) may have changed its
// coordinates, decoded mean, AND on-atom row count, so reusing the old
// `band_coords` / `band_mean` (or indexing the old-shaped `band_sd`)
// would mismatch the final model. A born atom whose slot was just
// pushed with the right shape is rebuilt identically — same result.
let coords_mat = self.assignment.coords[k].as_matrix();
let mut band_coords = Array2::<f64>::zeros((g, d));
let mut band_mean = Array2::<f64>::zeros((g, p));
let mut band_sd = Array2::<f64>::from_elem((g, p), f64::NAN);
let mut decoded = vec![0.0_f64; p];
for (gi, &row) in eval_rows.iter().enumerate() {
for axis in 0..d {
band_coords[[gi, axis]] = coords_mat[[row, axis]];
}
atom.fill_decoded_row(row, &mut decoded);
for c in 0..p {
band_mean[[gi, c]] = decoded[c];
}
// Φ_k(t) at this on-atom row.
let phi_t = atom.basis_values.row(row).to_owned();
// H_k⁻¹ Φ(t), then the quadratic form Φ(t)ᵀ H_k⁻¹ Φ(t).
let solved = chol.solvevec(&phi_t);
let quad = phi_t.dot(&solved).max(0.0);
// Var_c(t) = φ · Φ(t)ᵀ H_k⁻¹ Φ(t) — identical across channels (the
// inner Hessian is shared; the decoder differs only in the mean).
let sd = (dispersion * quad).sqrt();
for c in 0..p {
band_sd[[gi, c]] = sd;
}
}
band.band_coords = band_coords;
band.band_mean = band_mean;
band.band_sd = band_sd;
}
Ok(())
}
pub(crate) fn shape_uncertainty_without_decoder_covariance(
&self,
dispersion: f64,
) -> SaeShapeUncertainty {
let p = self.output_dim();
let mut atoms = Vec::with_capacity(self.k_atoms());
for (k, atom) in self.atoms.iter().enumerate() {
let n_rows = atom.n_obs();
let d = atom.latent_dim;
let stride = n_rows.div_ceil(SHAPE_BAND_MAX_POINTS).max(1);
let eval_rows: Vec<usize> = (0..n_rows).step_by(stride).collect();
let g = eval_rows.len();
let coords_mat = self.assignment.coords[k].as_matrix();
let mut band_coords = Array2::<f64>::zeros((g, d));
let mut band_mean = Array2::<f64>::zeros((g, p));
let band_sd = Array2::<f64>::from_elem((g, p), f64::NAN);
let mut decoded = vec![0.0_f64; p];
for (gi, &row) in eval_rows.iter().enumerate() {
for axis in 0..d {
band_coords[[gi, axis]] = coords_mat[[row, axis]];
}
atom.fill_decoded_row(row, &mut decoded);
for c in 0..p {
band_mean[[gi, c]] = decoded[c];
}
}
atoms.push(SaeAtomShapeUncertainty {
decoder_covariance: None,
band_coords,
band_mean,
band_sd,
});
}
SaeShapeUncertainty { dispersion, atoms }
}
}
/// Helper for padded FFI callers. Arrays use `(K, N, M_max)` and
/// `(K, N, M_max, D_max)` storage, with `basis_sizes` and `latent_dims`
/// selecting each atom's active prefix.
///
/// `evaluators`, when non-empty, must have length `K`. Each entry attaches an
/// optional [`SaeBasisSecondJet`] to the matching atom so the Rust Newton
/// loop can refresh `Phi`/`dPhi/dt` between iterations without rebuilding the
/// term from Python. The evaluator is installed through
/// [`SaeManifoldAtom::with_basis_second_jet`], so its closed-form Hessian slot
/// is populated too — this is what lets the #1117 rank-revealing reduction
/// (`reduce_atoms_to_data_supported_rank`) reparametrize a rank-deficient
/// fixed-width decoder (e.g. the periodic circle's 5-column basis whose data
/// Gram comes out rank 3/5 on a near-degenerate checkpoint) onto its
/// data-supported subspace instead of stalling on the flat REML valley. An
/// empty slice leaves every atom in snapshot-only mode.
#[must_use = "build error must be handled"]
pub fn term_from_padded_blocks_with_mode(
n_obs: usize,
p_out: usize,
basis_kinds: &[SaeAtomBasisKind],
basis_values: ArrayView3<'_, f64>,
basis_jacobian: ArrayView4<'_, f64>,
basis_sizes: &[usize],
latent_dims: &[usize],
decoder_coefficients: ArrayView3<'_, f64>,
smooth_penalties: ArrayView3<'_, f64>,
logits: ArrayView2<'_, f64>,
coords: &[Array2<f64>],
mode: AssignmentMode,
evaluators: &[Option<Arc<dyn SaeBasisSecondJet>>],
) -> Result<SaeManifoldTerm, String> {
let k_atoms = basis_sizes.len();
if latent_dims.len() != k_atoms || basis_kinds.len() != k_atoms || coords.len() != k_atoms {
return Err("term_from_padded_blocks: K-length metadata mismatch".into());
}
if !evaluators.is_empty() && evaluators.len() != k_atoms {
return Err(format!(
"term_from_padded_blocks: evaluators length {} must equal K={k_atoms} or be empty",
evaluators.len()
));
}
if logits.dim() != (n_obs, k_atoms) {
return Err(format!(
"term_from_padded_blocks: logits must be ({n_obs}, {k_atoms}); got {:?}",
logits.dim()
));
}
let mut atoms = Vec::with_capacity(k_atoms);
for k in 0..k_atoms {
let m = basis_sizes[k];
let d = latent_dims[k];
let phi = basis_values.slice(s![k, 0..n_obs, 0..m]).to_owned();
let jet = basis_jacobian.slice(s![k, 0..n_obs, 0..m, 0..d]).to_owned();
let b = decoder_coefficients.slice(s![k, 0..m, 0..p_out]).to_owned();
let s = smooth_penalties.slice(s![k, 0..m, 0..m]).to_owned();
let atom = SaeManifoldAtom::new(
format!("atom_{k}"),
basis_kinds[k].clone(),
d,
phi,
jet,
b,
s,
)?;
let atom = match evaluators.get(k).and_then(|slot| slot.clone()) {
// Install through the second-jet slot so the analytic Hessian is
// available: the #1117 rank-revealing reduction needs it to compose
// the reduced jets when it reparametrizes a rank-deficient atom onto
// its data-supported subspace. All production SAE evaluators
// (periodic/sphere/torus/cylinder/Duchon/Euclidean-patch) implement
// `SaeBasisSecondJet`, so this is the standard install path.
Some(evaluator) => atom.with_basis_second_jet(evaluator),
None => atom,
};
atoms.push(atom);
}
let manifolds = basis_kinds
.iter()
.zip(latent_dims.iter().copied())
.map(|(kind, d)| kind.latent_manifold(d))
.collect();
let assignment = SaeAssignment::from_blocks_with_mode_and_manifolds(
logits.to_owned(),
coords.to_vec(),
manifolds,
mode,
)?;
SaeManifoldTerm::new(atoms, assignment)
}
/// Build the per-row Jacobian `J` and Hessian `H` of the decoded output
/// `Z_n = Phi_n B` with respect to the latent coordinates `t_n` of a single
/// SAE atom and install them on the supplied [`IsometryPenalty`].
///
/// Layout follows the convention used by [`IsometryPenalty::grad_target`] and
/// friends:
///
/// * `J ∈ ℝ^{n_obs × (p · d)}`, flattened as `J[n, i*d + a]` —
/// `J[n, i, a] = ∂Z_{n,i} / ∂t_{n,a} = Σ_m dPhi[n, m, a] · B[m, i]`.
/// * `H ∈ ℝ^{n_obs × (p · d · d)}`, flattened as `H[n, (i*d + a)*d + c]` —
/// `H[n, i, a, c] = ∂J[n, i, a] / ∂t_{n, c} = Σ_m d²Phi[n, m, a, c] · B[m, i]`.
/// * `K`, an `Array3` of shape `(n_obs, p, d·d·d)` with last axis packed
/// `((a·d + c)·d + e)` — `K[n, i, a, c, e] = ∂³Z_{n,i} / ∂t_a ∂t_c ∂t_e =
/// Σ_m d³Phi[n, m, a, c, e] · B[m, i]`. Installed via the new third-jet slot
/// whenever the base evaluator's `third_jet_dyn` yields a jet AND the penalty
/// carries no `duchon_radial_source`. This is the residual-curvature source
/// for the exact isometry `hvp`.
///
/// Returns `Ok(true)` when both caches were installed (i.e. the atom was
/// built via [`SaeManifoldAtom::with_basis_second_jet`], so its
/// `basis_second_jet` slot holds a [`SaeBasisSecondJet`] implementation
/// that supplies the analytic Hessian). Returns `Ok(false)` when only the
/// base [`SaeBasisEvaluator`] is installed (no second jet available) — in
/// that case only the first-jet `jacobian_cache` is installed and the
/// penalty's `has_jacobian_second_source` check still has a chance to
/// succeed via a pre-supplied `duchon_radial_source`. Returns `Err` on
/// shape mismatches (which would indicate a buggy evaluator) or when the
/// second-jet implementation itself fails (e.g. wrong latent dimension).
///
/// This entry point takes `&IsometryPenalty` rather than `&mut` because the
/// caches are interior-mutable (see [`IsometryPenalty::refresh_caches`]).
pub fn refresh_isometry_caches_from_atom(
penalty: &IsometryPenalty,
atom: &SaeManifoldAtom,
coords: ArrayView2<'_, f64>,
) -> Result<bool, String> {
let evaluator = atom.basis_evaluator.as_ref().ok_or_else(|| {
format!(
"refresh_isometry_caches_from_atom: atom {} has no basis evaluator",
atom.name
)
})?;
let (_phi, jet) = evaluator.evaluate(coords)?;
let n_obs = coords.nrows();
let d = atom.latent_dim;
let m = atom.basis_size();
let p = atom.decoder_coefficients.ncols();
if penalty.p_out != p {
return Err(format!(
"refresh_isometry_caches_from_atom: penalty.p_out={} but atom.decoder.cols={p}",
penalty.p_out
));
}
if jet.dim() != (n_obs, m, d) {
return Err(format!(
"refresh_isometry_caches_from_atom: evaluator first jet has shape {:?}, expected ({n_obs}, {m}, {d})",
jet.dim()
));
}
// J[n, i*d + a] = Σ_m dPhi[n, m, a] · B[m, i].
let b = &atom.decoder_coefficients;
let mut jac = Array2::<f64>::zeros((n_obs, p * d));
for n in 0..n_obs {
for i in 0..p {
for a in 0..d {
let mut acc = 0.0;
for mm in 0..m {
acc += jet[[n, mm, a]] * b[[mm, i]];
}
jac[[n, i * d + a]] = acc;
}
}
}
// The second jet is sourced from the optional `basis_second_jet`
// slot. The trait split (`SaeBasisEvaluator` vs `SaeBasisSecondJet`)
// encodes "no closed-form Hessian" as trait absence: when the atom
// was built with `with_basis_evaluator` (base trait only) the slot
// is `None` and the `H` cache is not installed. When the atom was
// built with `with_basis_second_jet` the slot holds the same Arc
// upcast to the supertrait, and `second_jet` returns the analytic
// Hessian here.
let jac2_opt = if let Some(second_eval) = atom.basis_second_jet.as_ref() {
let hess = second_eval.second_jet(coords)?;
if hess.dim() != (n_obs, m, d, d) {
return Err(format!(
"refresh_isometry_caches_from_atom: evaluator second jet has shape {:?}, expected ({n_obs}, {m}, {d}, {d})",
hess.dim()
));
}
let mut jac2 = Array2::<f64>::zeros((n_obs, p * d * d));
for n in 0..n_obs {
for i in 0..p {
for a in 0..d {
for c in 0..d {
let mut acc = 0.0;
for mm in 0..m {
acc += hess[[n, mm, a, c]] * b[[mm, i]];
}
jac2[[n, (i * d + a) * d + c]] = acc;
}
}
}
}
Some(Arc::new(jac2))
} else {
None
};
// Third jet K[n, i, ((a·d + c)·d + e)] = Σ_m d³Phi[n, m, a, c, e] · B[m, i]
// feeds the residual-curvature term of the exact isometry Hessian
// B_{ab,cd} = K_{a,cd}^T W J_b + H_{a,c}^T W H_{b,d}
// + H_{a,d}^T W H_{b,c} + J_a^T W K_{b,cd}.
// Sourced from the base evaluator's object-safe `third_jet_dyn` forwarder
// (closed-form analytic override for every basis with an analytic Hessian:
// sphere/circle/torus/affine/euclidean/duchon; `None` otherwise — no
// finite-difference fallback). Installed only when the penalty
// has no `duchon_radial_source` — a Duchon penalty already carries its own
// analytic third source and `jacobian_third` would shadow it with this
// cache. Always written (Some or None) so a stale K from a prior outer step
// never survives a refresh.
let jac3_opt = if penalty.duchon_radial_source.is_none() {
match evaluator.third_jet_dyn(coords) {
Some(third) => {
let t3 = third?;
if t3.dim() != (n_obs, m, d, d, d) {
return Err(format!(
"refresh_isometry_caches_from_atom: evaluator third jet has shape {:?}, expected ({n_obs}, {m}, {d}, {d}, {d})",
t3.dim()
));
}
let mut jac3 = Array3::<f64>::zeros((n_obs, p, d * d * d));
for n in 0..n_obs {
for i in 0..p {
for a in 0..d {
for c in 0..d {
for e in 0..d {
let mut acc = 0.0;
for mm in 0..m {
acc += t3[[n, mm, a, c, e]] * b[[mm, i]];
}
jac3[[n, i, ((a * d) + c) * d + e]] = acc;
}
}
}
}
}
Some(Arc::new(jac3))
}
None => None,
}
} else {
None
};
let installed = jac2_opt.is_some();
penalty.refresh_caches(Some(Arc::new(jac)), jac2_opt);
penalty.set_third_decoder_derivative(jac3_opt);
Ok(installed)
}
/// Walk an [`AnalyticPenaltyRegistry`] and refresh every Isometry penalty
/// against the SAE atom it owns. The alignment rule is positional within each
/// `(latent_dim, p_out)` signature: the penalty's `target.latent_dim` must
/// equal the atom's `latent_dim` AND the penalty's `p_out` must equal the
/// atom's decoder column count `p`. Multi-atom configurations install one
/// isometry penalty per atom, so the *k*-th isometry penalty matching a given
/// signature is paired with the *k*-th atom matching that same signature. This
/// reduces to the unambiguous single-atom/single-penalty case wired by
/// `solver/workflow.rs`, and never collapses multiple penalties onto the first
/// matching atom (which would leave every later atom's coords un-refreshed).
///
/// Returns the number of penalties that got both caches populated (i.e. the
/// number of atoms whose `basis_second_jet` slot holds a
/// [`SaeBasisSecondJet`] implementation supplying the analytic Hessian).
pub fn refresh_isometry_caches_from_term(
registry: &AnalyticPenaltyRegistry,
term: &SaeManifoldTerm,
coords_per_atom: &[Array2<f64>],
) -> Result<usize, String> {
if coords_per_atom.len() != term.atoms.len() {
return Err(format!(
"refresh_isometry_caches_from_term: coords_per_atom length {} != number of atoms {}",
coords_per_atom.len(),
term.atoms.len()
));
}
let mut refreshed_with_second = 0usize;
// Per-signature cursor: how many atoms matching a given (latent_dim, p_out)
// have already been consumed by earlier isometry penalties. Pairing the
// k-th penalty of a signature with the k-th atom of that signature gives a
// stable one-to-one mapping for multi-atom configs.
let mut consumed_per_signature: std::collections::HashMap<(usize, usize), usize> =
std::collections::HashMap::new();
for entry in registry.penalties.iter() {
let AnalyticPenaltyKind::Isometry(p) = entry else {
continue;
};
let Some(p_latent_dim) = p.target.latent_dim else {
continue;
};
let signature = (p_latent_dim, p.p_out);
let already_consumed = consumed_per_signature.entry(signature).or_insert(0);
// Advance to the (already_consumed)-th atom matching this signature.
let mut seen = 0usize;
let mut paired: Option<usize> = None;
for (atom_idx, atom) in term.atoms.iter().enumerate() {
let matches = atom.latent_dim == p_latent_dim
&& atom.decoder_coefficients.ncols() == p.p_out
&& atom.basis_evaluator.is_some();
if !matches {
continue;
}
if seen == *already_consumed {
paired = Some(atom_idx);
break;
}
seen += 1;
}
let Some(atom_idx) = paired else {
continue;
};
*already_consumed += 1;
let atom = &term.atoms[atom_idx];
let coords = coords_per_atom[atom_idx].view();
if refresh_isometry_caches_from_atom(p, atom, coords)? {
refreshed_with_second += 1;
}
}
Ok(refreshed_with_second)
}
#[cfg(test)]
mod amortized_encoder_tests {
use crate::terms::sae::manifold::tests::small_two_atom_periodic_term;
/// #1026 ladder item 2/3 — the amortized encoder is reachable end-to-end
/// from a fitted term and is certificate-honest: it encodes the dictionary's
/// own fit-time target, returns one result per atom with the right shape, and
/// every row is either certified or counted in
/// `encode_uncertified_count` (never silently miscounted), with the exact
/// fallback strictly reducing the uncertified count it inherits.
#[test]
fn amortized_encode_fitted_is_reachable_and_certificate_honest() {
let (term, target, rho) = small_two_atom_periodic_term();
let n = term.n_obs();
let k = term.k_atoms();
let results = term
.amortized_encode_fitted(target.view(), &rho)
.expect("amortized encode of the fit-time target runs end-to-end");
assert_eq!(
results.len(),
k,
"one encode result per atom in dictionary order"
);
for (atom_idx, result) in results.iter().enumerate() {
assert_eq!(
result.coords.nrows(),
n,
"atom {atom_idx} encode must produce one coordinate per row"
);
assert_eq!(
result.coords.ncols(),
term.atoms[atom_idx].latent_dim,
"atom {atom_idx} encode coords must match its latent dim"
);
// The uncertified count is the honest tally of rows the certificate
// could not gate — it must equal the false entries of the mask.
let uncertified = result.certified.iter().filter(|c| !**c).count();
assert_eq!(
result.encode_uncertified_count, uncertified,
"atom {atom_idx} uncertified count must match the certificate mask"
);
assert_eq!(
result.certified.len(),
n,
"atom {atom_idx} certificate mask must cover every row"
);
}
}
/// The fitted amplitudes the encoder derives are exactly the assignment
/// masses the reconstruction is assembled from — feeding them back is the
/// self-consistency the distilled map is supervised against.
#[test]
fn fitted_assignment_amplitudes_match_the_assignment_masses() {
let (term, _target, rho) = small_two_atom_periodic_term();
let n = term.n_obs();
let k = term.k_atoms();
let amplitudes = term
.fitted_assignment_amplitudes(&rho)
.expect("fitted amplitudes derive from the assignment");
assert_eq!(amplitudes.dim(), (n, k));
for row in 0..n {
let a = term
.assignment
.try_assignments_row_for_rho(row, &rho)
.expect("assignment row resolves");
for atom_idx in 0..k {
assert_eq!(
amplitudes[[row, atom_idx]],
a[atom_idx],
"amplitude[{row},{atom_idx}] must equal the assignment mass"
);
}
}
}
}