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
use std::cell::Cell;
use std::collections::HashMap;
#[cfg(feature = "disk-cache")]
use std::path::Path;
use std::path::PathBuf;
use crate::CeraError;
use crate::model::{BlockType, ModelConfig};
use crate::turboquant::{
CompressedKeyCache, CompressedValueCache, EncodeScratch, QueryRotationScratch, RotationState,
TurboQuantConfig,
};
/// Reserve capacity for `len` values of `T`, returning [`CeraError::OutOfMemory`]
/// instead of aborting the process when the allocation can't be satisfied. Used
/// for the config-driven KV-cache buffers — the uncompressed per-layer f32
/// caches (`capacity * kv_dim`) and the per-head compressed buffers under
/// TurboQuant — the dominant allocations that OOM when a model's context is too
/// large for the device. Returns an empty `Vec` with the capacity reserved
/// (filled during inference), matching `Vec::with_capacity`.
///
/// `TryReserveError::CapacityOverflow` (a request larger than the allocator can
/// ever satisfy) is intentionally folded into `OutOfMemory` alongside a true
/// allocation failure: both mean "this KV won't fit," the caller's recovery is
/// identical (skip the model), and `requested_bytes` reports the attempted size.
/// Callers that derive `len` from a multiplication guard that multiply first
/// via [`checked_elems`] (both the f32 KV path and the compressed buffers) so a
/// `usize` wrap can't silently under-reserve; `len` here is a valid element
/// count, and `try_reserve_exact` itself covers the `len * size_of::<T>()`
/// byte-size overflow.
pub(crate) fn try_alloc<T>(len: usize) -> Result<Vec<T>, CeraError> {
let mut v: Vec<T> = Vec::new();
v.try_reserve_exact(len)
.map_err(|_| CeraError::OutOfMemory {
requested_bytes: (len as u64).saturating_mul(std::mem::size_of::<T>() as u64),
})?;
Ok(v)
}
/// `count * per` (an element count) with overflow guarded — a `usize` wrap would
/// silently under-reserve the buffer and reintroduce an infallible realloc, so
/// map overflow to `OutOfMemory` (the intended size is absurd) rather than let
/// it slip past [`try_alloc`]. Used where a buffer length is `capacity * per`.
///
/// `T` is the element type the resulting length feeds into `try_alloc::<T>`, so
/// the `OutOfMemory` diagnostic reports the intended **byte** size
/// (`count * per * size_of::<T>()`, saturating), consistent with `try_alloc`'s
/// own `requested_bytes` — not a bare element count.
pub(crate) fn checked_elems<T>(count: usize, per: usize) -> Result<usize, CeraError> {
count.checked_mul(per).ok_or(CeraError::OutOfMemory {
requested_bytes: (count as u64)
.saturating_mul(per as u64)
.saturating_mul(std::mem::size_of::<T>() as u64),
})
}
/// A `fill`-initialized buffer of length `len`, reserved fallibly. Reserves via
/// [`try_alloc`] (→ [`CeraError::OutOfMemory`] instead of aborting) and then
/// `resize`s to fill within that reservation, so no further allocation occurs.
/// Used for the fixed-size scratch/conv buffers so every allocation in the
/// constructor is recoverable, not just the context-scaled KV caches.
pub(crate) fn zeroed<T: Clone>(len: usize, fill: T) -> Result<Vec<T>, CeraError> {
let mut v = try_alloc::<T>(len)?;
v.resize(len, fill);
Ok(v)
}
/// [`zeroed`] specialized to zero-filled `f32` scratch buffers.
fn zeroed_f32(len: usize) -> Result<Vec<f32>, CeraError> {
zeroed(len, 0.0)
}
/// KV cache compression mode. Passed to `InferenceState::from_config_with_compression`
/// (or via `GenerateConfig::kv_compression`) — that single call sets up everything
/// TurboQuant needs: the per-layer rotation states, the compressed key/value
/// caches, and the scratch buffers. **No separate `enable_turboquant` call on
/// the model is required.**
///
/// TurboQuant is honored by the CPU backend (`Lfm2Model`) and by both GPU
/// backends (`GpuLfm2Model` and `MetalLfm2Model`). The GPU paths additionally
/// need [`crate::model::Model::configure_kv_compression`] — which `Session`
/// calls — to build their GPU-resident compressed caches, and they only
/// implement the both-sides mode: a single-sided (debug) request, or a
/// `head_dim` their kernels can't handle, warns and falls back to the backend's
/// uncompressed KV (f32 on wgpu, f16 on Metal).
#[derive(Clone, Debug, Default)]
pub enum KvCompression {
/// No compression — the backend's uncompressed KV: f32 on CPU and wgpu,
/// f16 on native Metal, whose cache has always been half precision.
#[default]
None,
/// f16 KV cache — keys and values stored as IEEE-754 half precision
/// (2 bytes/elem instead of 4), halving the KV bytes streamed per decode
/// token. Near-lossless (f16 has 10 mantissa bits; attention is robust to
/// it — this is what llama.cpp uses by default). CPU LFM2 and dense
/// transformer paths; accumulation stays f32 for softmax stability.
F16,
/// TurboQuant compression. Keys and values can be toggled independently
/// for debugging (e.g. to isolate how much drift each side contributes).
/// The common production configuration sets both `keys` and `values` to
/// `true`.
///
/// - Keys: 2-bit PolarQuant + 1-bit QJL residual (3 bits/elem + f16 norms).
/// - Values: 2-bit PolarQuant only (2 bits/elem + f16 norms).
///
/// `seed` drives the per-layer randomized Hadamard rotations — the same
/// seed reproduces the same rotations deterministically.
TurboQuant { seed: u64, keys: bool, values: bool },
}
impl KvCompression {
/// Shortcut for the common "compress everything" configuration.
pub fn turboquant(seed: u64) -> Self {
Self::TurboQuant {
seed,
keys: true,
values: true,
}
}
/// Returns `(compress_keys, compress_values)` for the current mode.
/// f16 is not TurboQuant "compression" in this sense — it reports `false`
/// so the TurboQuant setup paths stay off; the f16 slots are driven by
/// [`Self::is_f16`] instead.
pub fn flags(&self) -> (bool, bool) {
match self {
Self::None | Self::F16 => (false, false),
Self::TurboQuant { keys, values, .. } => (*keys, *values),
}
}
/// Whether the KV cache is stored in f16 (half precision).
pub fn is_f16(&self) -> bool {
matches!(self, Self::F16)
}
/// The mode a state built from `config` will *actually* use.
///
/// `from_config_capped` silently downgrades TurboQuant to plain f32 when
/// `head_dim` isn't a power of two (the Walsh-Hadamard transform needs it), so
/// anything deriving identity from the mode — the prefix-cache tag especially —
/// has to ask what was resolved, not what was requested. Keeping that rule here
/// beside the allocator that applies it stops the two from drifting.
pub fn resolved_for(&self, config: &ModelConfig) -> Self {
let (keys, values) = self.flags();
if (keys || values) && !config.head_dim.is_power_of_two() {
Self::None
} else {
self.clone()
}
}
/// Prefix-cache namespace tag for this mode — `""` for plain f32, else a
/// `"…:"`-terminated discriminator to prepend to a backend's cache id.
///
/// The KV prefix cache's disk tier is keyed by model path (plus a backend
/// prefix), so without this every KV mode over the same GGUF and
/// `--cache-dir` shares one namespace. A snapshot from one mode then
/// permanently shadows another's for the same prefix: the restore-time
/// compatibility gate turns the longest match into a miss and does *not* fall
/// back to a shorter compatible entry, so the other mode stays cold on every
/// subsequent run, not just once. (Measured on wgpu: 134.7 → 10.3 tok/s
/// prefill, sticky.)
///
/// Two things beyond the mode name are load-bearing:
/// - **The seed**, because it drives the per-layer randomized Hadamard
/// rotations. Restore validates only shape (`head_dim` / `n_kv_heads` /
/// `seq_len`), which a different-seed blob passes — so a shared namespace
/// would decode a prefix in the wrong basis and silently corrupt attention
/// rather than miss.
/// - **Which sides are compressed**, since keys-only and values-only produce
/// genuinely different cache contents from both-sides and from f32.
///
/// A TurboQuant config compressing neither side is *not* tagged: it degrades
/// to an f32 cache in `from_config_capped`, so its contents really are f32's
/// and separating them would only waste entries.
///
/// Callers must tag with the mode their state actually ended up in, not the one
/// requested — a backend that falls back to its uncompressed KV (unsupported
/// `head_dim`, or a single-sided request on GPU) must use the uncompressed
/// tag so it shares the namespace it is now writing into. Metal's
/// uncompressed cache is f16 but still takes `None`'s tag; see the note in
/// `MetalLfm2Model::configure_kv_compression`.
pub fn cache_tag(&self) -> String {
match self {
Self::None => String::new(),
Self::F16 => "f16:".to_string(),
Self::TurboQuant { seed, keys, values } => match (keys, values) {
(false, false) => String::new(),
(true, true) => format!("tq3kv-s{seed}:"),
(true, false) => format!("tq3k-s{seed}:"),
(false, true) => format!("tq3v-s{seed}:"),
},
}
}
}
/// Per-layer inference state.
/// Capacity for the Conv rollback ring buffer (in number of tokens).
pub const CONV_HISTORY_CAPACITY: usize = 64;
/// Zero-allocation flat ring buffer storing recent convolution state snapshots
/// for speculative decoding rollback.
#[derive(Clone, Debug)]
pub struct ConvHistory {
snapshots: Vec<f32>,
positions: [usize; CONV_HISTORY_CAPACITY],
head: usize,
count: usize,
buf_len: usize,
}
impl ConvHistory {
/// Create a new pre-allocated history ring buffer for a convolution layer with `buf_len` elements.
pub fn new(buf_len: usize) -> Self {
Self {
snapshots: vec![0.0f32; CONV_HISTORY_CAPACITY * buf_len],
positions: [0; CONV_HISTORY_CAPACITY],
head: 0,
count: 0,
buf_len,
}
}
/// Record a snapshot of `buffer` at sequence position `pos`.
/// Zero heap allocation: copies into the pre-allocated flat storage.
pub fn push(&mut self, pos: usize, buffer: &[f32]) {
if self.buf_len == 0 || buffer.len() != self.buf_len {
return;
}
let offset = self.head * self.buf_len;
self.snapshots[offset..offset + self.buf_len].copy_from_slice(buffer);
self.positions[self.head] = pos;
self.head = (self.head + 1) % CONV_HISTORY_CAPACITY;
if self.count < CONV_HISTORY_CAPACITY {
self.count += 1;
}
}
/// Roll back the convolution `buffer` to the state at `target_pos`.
/// Returns `true` if the state was successfully found and restored.
pub fn rollback_to(&mut self, target_pos: usize, buffer: &mut [f32]) -> bool {
if buffer.len() != self.buf_len {
return false;
}
if target_pos == 0 {
buffer.fill(0.0);
self.clear();
return true;
}
for i in 0..self.count {
let idx = (self.head + CONV_HISTORY_CAPACITY - 1 - i) % CONV_HISTORY_CAPACITY;
if self.positions[idx] == target_pos {
let offset = idx * self.buf_len;
buffer.copy_from_slice(&self.snapshots[offset..offset + self.buf_len]);
self.head = (idx + 1) % CONV_HISTORY_CAPACITY;
self.count = self.count.saturating_sub(i);
return true;
}
}
false
}
/// Check whether a snapshot at `target_pos` is available in this ring buffer.
pub fn has_pos(&self, target_pos: usize) -> bool {
if target_pos == 0 {
return true;
}
for i in 0..self.count {
let idx = (self.head + CONV_HISTORY_CAPACITY - 1 - i) % CONV_HISTORY_CAPACITY;
if self.positions[idx] == target_pos {
return true;
}
}
false
}
/// Reset all snapshot tracking without deallocating.
pub fn clear(&mut self) {
self.head = 0;
self.count = 0;
}
}
#[allow(clippy::large_enum_variant)]
pub enum LayerState {
/// KV cache for attention layers.
Attention {
key_cache: Vec<f32>,
value_cache: Vec<f32>,
/// f16 key cache (IEEE-754 half bits). Populated when `KvCompression::F16`
/// is active; `key_cache` stays empty in that mode. Time-major
/// `[seq_len × kv_dim]`, same layout as `key_cache`.
key_cache_f16: Vec<u16>,
/// f16 value cache (IEEE-754 half bits). Populated when `KvCompression::F16`
/// is active; `value_cache` stays empty in that mode.
value_cache_f16: Vec<u16>,
/// Compressed key cache (populated when TurboQuant is active; key_cache stays empty).
compressed_keys: Option<CompressedKeyCache>,
/// Compressed value cache (populated when TurboQuant is active; value_cache stays empty).
compressed_values: Option<CompressedValueCache>,
},
/// Rolling buffer for convolution layers.
/// Stores previous `d_conv` pre-conv activations (bx values), time-major.
Conv {
buffer: Vec<f32>,
/// Pre-allocated ring buffer history snapshots for speculative decoding rollback.
history: ConvHistory,
},
}
/// Pre-allocated scratch buffers reused across layers and tokens.
pub struct ScratchBuffers {
/// Scratch for the normed hidden state (hidden_size).
pub normed: Vec<f32>,
/// Scratch for FFN input (hidden_size).
pub ffn_input: Vec<f32>,
/// Scratch for shortconv in_proj output (3 * hidden_size).
pub conv_proj: Vec<f32>,
/// Scratch for shortconv bx / conv output (hidden_size).
pub conv_scratch: Vec<f32>,
/// Scratch for Q projection (hidden_size = n_heads * head_dim).
pub q: Vec<f32>,
/// Scratch for K projection (max kv_dim).
pub k: Vec<f32>,
/// Scratch for V projection (max kv_dim).
pub v: Vec<f32>,
/// Scratch for attention output (hidden_size).
pub attn_out: Vec<f32>,
/// Scratch for FFN gate (intermediate_size).
pub gate: Vec<f32>,
/// Scratch for FFN up (intermediate_size).
pub up: Vec<f32>,
/// Scratch for block/FFN output (hidden_size).
pub out: Vec<f32>,
/// Scratch for attention scores (grows with seq_len). Reused across heads
/// when the decode head loop runs serially; when it runs on the pool,
/// `transformer::decode_attention` re-lays it out as one
/// `seq_len + head_dim` row per head so the heads don't share a buffer.
pub scores: Vec<f32>,
/// Q8_0 quantization scratch: scales for the input vector (max_k / 32 entries).
pub q8_scales: Vec<f32>,
/// Q8_0 quantization scratch: quants for the input vector (max_k entries).
pub q8_quants: Vec<i8>,
/// Q8_0 quantization scratch for MoE down-projections.
pub q8_scales_down: Vec<f32>,
/// Q8_0 quantization scratch for MoE down-projections.
pub q8_quants_down: Vec<i8>,
/// Dequantized weight scratch for BLAS prefill. Grown lazily on first use
/// to the largest weight matrix the BLAS path encounters; reused across
/// every subsequent GEMM call within and between forward passes. Stays
/// empty when the `blas` feature is off — the NEON fallback never touches it.
pub dequant_weight_scratch: Vec<f32>,
/// Scratch for the LoRA down-projection intermediate (`A·x`). Reused across
/// apply calls so the LoRA hot path allocates nothing.
pub lora_tmp: Vec<f32>,
/// Router logits, then gate probabilities, for one MoE layer (`n_expert`).
/// Stays empty on dense models, which never touch it.
pub moe_probs: Vec<f32>,
/// One expert's feed-forward output (`hidden_size`), before it is scaled by
/// its routing weight and accumulated. Needed because the down-projection
/// GEMV overwrites its destination rather than accumulating, so the running
/// sum cannot live in `out`. Empty on dense models.
pub moe_expert_out: Vec<f32>,
/// The `n_expert_used` selected expert indices and their normalized
/// combining weights, for one MoE layer. Held in scratch so routing
/// allocates nothing per layer per token. Empty on dense models.
pub moe_selected: Vec<(usize, f32)>,
/// Scratch for LM Head output logits (vocab_size). Reused across tokens to eliminate per-token heap allocation.
pub logits: Vec<f32>,
/// Scratch for token embedding lookup / hidden state input (hidden_size).
pub hidden_in: Vec<f32>,
}
/// Inference state across all layers.
pub struct InferenceState {
pub layers: Vec<LayerState>,
pub seq_len: usize,
pub scratch: ScratchBuffers,
/// Active LoRA adapter for this pass, if any. The Session copies its
/// attached adapter here before each forward; the CPU projection helpers
/// read `lora.get(layer, target)` and add `scale·B·(A·x)` after each base
/// GEMV. `None` ⇒ base model only.
pub lora: Option<std::sync::Arc<crate::lora::LoraAdapterWeights>>,
/// TurboQuant encode scratch (None when disabled). Owned by InferenceState
/// rather than Model so the model remains Sync for concurrent inference.
pub tq_encode_scratch: Option<EncodeScratch>,
/// TurboQuant query rotation scratch (None when disabled).
pub tq_query_scratch: Option<QueryRotationScratch>,
/// Per-layer TurboQuant rotation state (None for conv layers or when
/// compression is disabled). Constructed from the `seed` in `KvCompression`
/// at `from_config_with_compression` time.
pub tq_rotations: Vec<Option<RotationState>>,
/// Shared TurboQuant config (Lloyd-Max centroids, derived from head_dim).
pub tq_config: Option<TurboQuantConfig>,
/// Whether the attention KV caches are stored in f16 (`KvCompression::F16`).
/// Set once at construction; the CPU forward paths read it to choose the
/// f16 append + f16-widening attention kernels over the f32 path. Reliable
/// even before the first token (when both cache vecs are empty).
pub kv_f16: bool,
}
impl InferenceState {
/// Create a new empty inference state.
pub fn new(num_layers: usize) -> Self {
Self {
layers: (0..num_layers)
.map(|_| LayerState::Attention {
key_cache: Vec::new(),
value_cache: Vec::new(),
key_cache_f16: Vec::new(),
value_cache_f16: Vec::new(),
compressed_keys: None,
compressed_values: None,
})
.collect(),
seq_len: 0,
scratch: ScratchBuffers {
normed: Vec::new(),
ffn_input: Vec::new(),
conv_proj: Vec::new(),
conv_scratch: Vec::new(),
q: Vec::new(),
k: Vec::new(),
v: Vec::new(),
attn_out: Vec::new(),
gate: Vec::new(),
up: Vec::new(),
out: Vec::new(),
scores: Vec::new(),
q8_scales: Vec::new(),
q8_quants: Vec::new(),
q8_scales_down: Vec::new(),
q8_quants_down: Vec::new(),
dequant_weight_scratch: Vec::new(),
lora_tmp: Vec::new(),
moe_probs: Vec::new(),
moe_expert_out: Vec::new(),
moe_selected: Vec::new(),
logits: Vec::new(),
hidden_in: Vec::new(),
},
tq_encode_scratch: None,
tq_query_scratch: None,
tq_rotations: Vec::new(),
tq_config: None,
lora: None,
kv_f16: false,
}
}
/// Create inference state matching a model config.
/// Attention layers get empty KV caches; conv layers get zero-filled rolling buffers.
/// Scratch buffers are pre-allocated to avoid per-token allocations.
pub fn from_config(config: &ModelConfig) -> Result<Self, CeraError> {
Self::from_config_with_compression(config, &KvCompression::None)
}
/// Build a throwaway prefill state whose KV capacity is capped to
/// `n_tokens` cells (never the full `max_seq_len`), for one-shot passes
/// like hidden-state extraction. Uncompressed. Capacity is clamped to
/// `[1, max_seq_len]`, so a per-chunk classifier call reserves ~O(T·kv_dim)
/// rather than the hundreds of MB a full-context cache would.
pub fn for_prefill(config: &ModelConfig, n_tokens: usize) -> Result<Self, CeraError> {
let capacity = n_tokens.clamp(1, config.max_seq_len);
Self::from_config_capped(config, &KvCompression::None, capacity)
}
/// Reset an existing state for reuse as a throwaway prefill scratch: zero
/// `seq_len`, truncate the (uncompressed) KV caches to empty, and zero the
/// conv rolling buffers in place (kept at full length) — all while KEEPING
/// allocated capacity, so a reused scratch does no allocation. Intended for
/// the [`Self::for_prefill`] path (no compression); working scratch buffers
/// are resized on demand by the forward pass.
pub fn clear_for_reuse(&mut self) {
self.seq_len = 0;
for layer in &mut self.layers {
match layer {
LayerState::Attention {
key_cache,
value_cache,
key_cache_f16,
value_cache_f16,
..
} => {
key_cache.clear();
value_cache.clear();
key_cache_f16.clear();
value_cache_f16.clear();
}
LayerState::Conv { buffer, history } => {
buffer.fill(0.0);
history.clear();
}
}
}
}
/// Create inference state with optional KV cache compression.
///
/// When `compression` is `KvCompression::TurboQuant`, this single call
/// sets up everything TurboQuant needs: the per-layer rotation states,
/// the compressed caches (keys and/or values), the encode scratch, and
/// the query rotation scratch. The model itself doesn't need to be
/// "enabled" separately — it reads all TurboQuant state from here.
pub fn from_config_with_compression(
config: &ModelConfig,
compression: &KvCompression,
) -> Result<Self, CeraError> {
Self::from_config_capped(config, compression, config.max_seq_len)
}
/// Like [`Self::from_config_with_compression`] but caps the pre-allocated
/// KV-cache capacity to `capacity` cells instead of `config.max_seq_len`.
/// Backs [`Self::for_prefill`]. `capacity` should be pre-clamped to
/// `[1, max_seq_len]`; appending beyond it just triggers normal Vec growth.
pub(crate) fn from_config_capped(
config: &ModelConfig,
compression: &KvCompression,
capacity: usize,
) -> Result<Self, CeraError> {
let kernel_size = config.conv_kernel_size.unwrap_or(3);
assert!(
kernel_size >= 2,
"conv_kernel_size must be at least 2, got {kernel_size}"
);
let d_conv = kernel_size - 1;
// Carried explicitly (≠hidden_size / n_heads for head_dim-decoupled
// models like Qwen3). `q_dim` (Q projection width = attention output
// width) can exceed hidden_size.
let head_dim = config.head_dim;
// Guard the config-derived scratch/conv buffer-length multiplies: a
// `usize` wrap from a malformed GGUF would size a too-small buffer and
// panic on a later index, so map overflow to a recoverable OutOfMemory
// (same policy as the KV path). All of these size f32 buffers.
let q_dim = checked_elems::<f32>(config.n_heads, head_dim)?;
let max_kv_dim = checked_elems::<f32>(
config.kv_heads_per_layer.iter().copied().max().unwrap_or(0),
head_dim,
)?;
// Compressed (TurboQuant) caches start at the same per-layer cap as the
// f32 path, so the compressed-side Vecs also avoid mid-decode reallocs.
let initial_capacity = capacity;
let (compress_keys, compress_values) = compression.flags();
// f16 KV: the f32 slots stay empty and the `*_f16` slots hold the cache
// (half the bytes). Independent of the TurboQuant `compress_*` axis.
let use_f16 = compression.is_f16();
// TurboQuant requires power-of-2 head_dim for the Walsh-Hadamard Transform.
// If the requirement isn't met, silently fall back to uncompressed f32.
let tq_enabled = (compress_keys || compress_values) && head_dim.is_power_of_two();
let (compress_keys, compress_values) = if tq_enabled {
(compress_keys, compress_values)
} else {
(false, false)
};
let (tq_rotations, tq_config) = if tq_enabled {
let seed = match compression {
KvCompression::TurboQuant { seed, .. } => *seed,
KvCompression::None | KvCompression::F16 => 0,
};
// Reserve the outer Vec fallibly too (layer-count-scaled), so no
// allocation on this path can abort — each RotationState is already
// built via the fallible try_from_seed.
let mut rotations = try_alloc::<Option<RotationState>>(config.block_types.len())?;
for (layer_idx, bt) in config.block_types.iter().enumerate() {
rotations.push(match bt {
BlockType::Attention => Some(RotationState::try_from_seed(
seed ^ layer_idx as u64,
head_dim,
)?),
BlockType::GatedConv => None,
});
}
(rotations, Some(TurboQuantConfig::for_head_dim(head_dim)))
} else {
(Vec::new(), None)
};
// Reserve the outer Vec<LayerState> fallibly (layer-count-scaled), then
// push each layer, so every allocation on this path is recoverable.
let mut layers = try_alloc::<LayerState>(config.block_types.len())?;
for layer in config.block_types.iter().enumerate().map(
|(layer_idx, bt)| -> Result<LayerState, CeraError> {
match bt {
BlockType::Attention => {
let n_kv_heads = config.kv_heads_per_layer[layer_idx];
// Guard both multiplies (a usize wrap would silently
// under-reserve). A config bug (e.g. wildly large
// max_seq_len, n_kv_heads, or head_dim from a malformed
// GGUF) surfaces as a recoverable OutOfMemory — same as
// a genuinely too-large KV — rather than aborting the
// process. kv_dim counts f32 slots per token, so its
// overflow guard uses the same f32-sized helper.
let kv_dim = checked_elems::<f32>(n_kv_heads, head_dim)?;
let kv_capacity = checked_elems::<f32>(capacity, kv_dim)?;
let compressed_keys = if compress_keys && n_kv_heads > 0 {
Some(CompressedKeyCache::try_new(
n_kv_heads,
head_dim,
initial_capacity,
)?)
} else {
None
};
let compressed_values = if compress_values && n_kv_heads > 0 {
Some(CompressedValueCache::try_new(
n_kv_heads,
head_dim,
initial_capacity,
)?)
} else {
None
};
// Pre-allocate the f32 KV cache to exactly
// `capacity * kv_dim` floats (capacity = the session's
// requested context, ≤ model max_seq_len) so writes never
// trigger Vec doubling/reallocation. Like every other
// allocation in this constructor it's fallible
// (`try_alloc`) — an over-large context returns
// `OutOfMemory` instead of aborting the process — but this
// is the dominant, context-scaled one. When TurboQuant
// compression is active for that side, the f32 vec stays
// empty and the compressed cache stores it.
// In f16 mode the f32 vecs stay empty and the `*_f16`
// vecs are pre-allocated to `capacity * kv_dim` u16 slots
// (half the bytes of the f32 path), same anti-realloc
// reservation. Otherwise the f16 vecs stay empty.
let key_cache = if (compress_keys && n_kv_heads > 0) || use_f16 {
Vec::new()
} else {
try_alloc::<f32>(kv_capacity)?
};
let value_cache = if (compress_values && n_kv_heads > 0) || use_f16 {
Vec::new()
} else {
try_alloc::<f32>(kv_capacity)?
};
let key_cache_f16 = if use_f16 && n_kv_heads > 0 {
try_alloc::<u16>(kv_capacity)?
} else {
Vec::new()
};
let value_cache_f16 = if use_f16 && n_kv_heads > 0 {
try_alloc::<u16>(kv_capacity)?
} else {
Vec::new()
};
Ok(LayerState::Attention {
key_cache,
value_cache,
key_cache_f16,
value_cache_f16,
compressed_keys,
compressed_values,
})
}
BlockType::GatedConv => {
let buf_len = checked_elems::<f32>(d_conv, config.hidden_size)?;
Ok(LayerState::Conv {
buffer: zeroed_f32(buf_len)?,
history: ConvHistory::new(buf_len),
})
}
}
},
) {
layers.push(layer?);
}
Ok(Self {
layers,
seq_len: 0,
scratch: ScratchBuffers {
normed: zeroed_f32(config.hidden_size)?,
ffn_input: zeroed_f32(config.hidden_size)?,
conv_proj: zeroed_f32(checked_elems::<f32>(3, config.hidden_size)?)?,
conv_scratch: zeroed_f32(config.hidden_size)?,
q: zeroed_f32(q_dim)?,
k: zeroed_f32(max_kv_dim)?,
v: zeroed_f32(max_kv_dim)?,
attn_out: zeroed_f32(q_dim)?,
gate: zeroed_f32(config.intermediate_size)?,
up: zeroed_f32(config.intermediate_size)?,
out: zeroed_f32(config.hidden_size)?,
scores: Vec::new(), // grows with seq_len during inference
q8_scales: Vec::new(), // resized per GEMV input dimension (max of hidden/intermediate)
q8_quants: Vec::new(), // resized per GEMV input dimension
q8_scales_down: Vec::new(),
q8_quants_down: Vec::new(),
// Grown lazily to max(3*hs*hs, is*hs) on the first BLAS GEMM
// call. Stays empty if the `blas` feature is off.
dequant_weight_scratch: Vec::new(),
lora_tmp: Vec::new(),
moe_probs: config
.moe
.as_ref()
.map(|m| zeroed_f32(m.n_expert))
.transpose()?
.unwrap_or_default(),
moe_expert_out: config
.moe
.as_ref()
.map(|_| zeroed_f32(config.hidden_size))
.transpose()?
.unwrap_or_default(),
moe_selected: config
.moe
.as_ref()
.map(|m| Vec::with_capacity(m.n_expert_used))
.unwrap_or_default(),
logits: zeroed_f32(config.vocab_size)?,
hidden_in: zeroed_f32(config.hidden_size)?,
},
// Scratch is needed whenever either side is compressed. The
// EncodeScratch `rot` buffer is shared between key and value
// encode; QueryRotationScratch is shared between key score
// computation and value weighted-sum reconstruction.
tq_encode_scratch: if tq_enabled {
Some(EncodeScratch::try_new(head_dim)?)
} else {
None
},
tq_query_scratch: if tq_enabled {
Some(QueryRotationScratch::try_new(config.n_heads, head_dim)?)
} else {
None
},
tq_rotations,
tq_config,
lora: None,
kv_f16: use_f16,
})
}
/// Append K and V vectors to an attention layer's cache (uncompressed path).
pub fn append_kv(&mut self, layer: usize, k: &[f32], v: &[f32]) {
if let LayerState::Attention {
key_cache,
value_cache,
..
} = &mut self.layers[layer]
{
key_cache.extend_from_slice(k);
value_cache.extend_from_slice(v);
}
}
/// Append K and V to an attention layer's f16 cache, converting each f32 to
/// IEEE-754 half on the way in. Used when `KvCompression::F16` is active.
pub fn append_kv_f16(&mut self, layer: usize, k: &[f32], v: &[f32]) {
if let LayerState::Attention {
key_cache_f16,
value_cache_f16,
..
} = &mut self.layers[layer]
{
key_cache_f16.extend(k.iter().map(|&x| crate::quant::f32_to_f16(x)));
value_cache_f16.extend(v.iter().map(|&x| crate::quant::f32_to_f16(x)));
}
}
/// Borrow the f16 key and value caches for an attention layer (IEEE-754 half
/// bits, time-major `[seq_len × kv_dim]`). Panics on a non-attention layer.
pub fn kv_cache_f16(&self, layer: usize) -> (&[u16], &[u16]) {
if let LayerState::Attention {
key_cache_f16,
value_cache_f16,
..
} = &self.layers[layer]
{
(key_cache_f16, value_cache_f16)
} else {
panic!("kv_cache_f16 called on non-attention layer {layer}");
}
}
/// Borrow the key and value caches for an attention layer.
/// The returned slices are laid out as [seq_len, kv_dim] (time-major).
pub fn kv_cache(&self, layer: usize) -> (&[f32], &[f32]) {
if let LayerState::Attention {
key_cache,
value_cache,
..
} = &self.layers[layer]
{
(key_cache, value_cache)
} else {
panic!("kv_cache called on non-attention layer {layer}");
}
}
/// Borrow the compressed key cache for an attention layer, if present.
pub fn compressed_keys(&self, layer: usize) -> Option<&CompressedKeyCache> {
if let LayerState::Attention {
compressed_keys, ..
} = &self.layers[layer]
{
compressed_keys.as_ref()
} else {
None
}
}
/// Mutably borrow the compressed key cache for an attention layer, if present.
pub fn compressed_keys_mut(&mut self, layer: usize) -> Option<&mut CompressedKeyCache> {
if let LayerState::Attention {
compressed_keys, ..
} = &mut self.layers[layer]
{
compressed_keys.as_mut()
} else {
None
}
}
/// Is any attention layer's KV currently backed by a compressed
/// (TurboQuant) cache? Used by `Session::append_tokens` to decide
/// whether `n_keep` shift is supported for this state — v1 gates
/// shift on uncompressed caches only.
/// `true` iff *every* attention layer has BOTH
/// `compressed_keys` and `compressed_values` populated. Used by
/// the prefix-cache lookup gate to distinguish a fully-
/// TurboQuant state (matchable against
/// `LayerSnapshot::AttentionCompressed`) from a mixed-mode one
/// (no snapshot variant fits — `snapshot()` returns `None`).
pub fn is_fully_compressed(&self) -> bool {
self.layers.iter().all(|l| match l {
LayerState::Attention {
compressed_keys,
compressed_values,
..
} => compressed_keys.is_some() && compressed_values.is_some(),
// Conv layers are never compressed; they don't impact
// the "fully compressed" determination.
LayerState::Conv { .. } => true,
})
}
pub fn is_compressed(&self) -> bool {
self.layers.iter().any(|l| {
matches!(
l,
LayerState::Attention {
compressed_keys: Some(_),
..
} | LayerState::Attention {
compressed_values: Some(_),
..
}
)
})
}
/// Capture the current inference state as a `StateSnapshot` suitable
/// for the KV prefix cache. CPU-flavored: f32 KV caches are byte-cast
/// via `bytemuck`; conv buffers are byte-cast wholesale. Compressed
/// (TurboQuant) attention layers are encoded into the
/// `LayerSnapshot::AttentionCompressed { keys, values }` byte slots
/// via `turboquant::encode_compressed_*`.
///
/// Returns `None` when an attention layer's compression is
/// **mixed** — i.e. exactly one of `compressed_keys` /
/// `compressed_values` is `Some`. The on-disk
/// `AttentionCompressed { keys, values }` shape models both
/// sides as encoded blobs; a single-side-compressed layer
/// would lose the uncompressed side's f32 data on snapshot.
/// `KvCompression::TurboQuant { keys: bool, values: bool }`
/// has both bools as debug knobs; the production config sets
/// both to `true`. Treating mixed as not-snapshotted is
/// conservative + correct — caller falls back to a cold prefill
/// for that turn.
pub fn snapshot(&self) -> Option<StateSnapshot> {
let mut layers = Vec::with_capacity(self.layers.len());
// `kv_f16` is the authoritative mode flag (set once at construction),
// reliable even for a zero-token layer whose `*_f16` slots are still
// empty — so every attention layer of an f16 state snapshots as
// `AttentionF16`, matching the `is_f16()` restore gate.
let kv_f16 = self.kv_f16;
for l in &self.layers {
match l {
LayerState::Attention {
key_cache,
value_cache,
key_cache_f16,
value_cache_f16,
compressed_keys,
compressed_values,
} => {
let snap = if kv_f16 {
// f16 KV: serialize the u16 half-bits **little-endian**
// to match the documented on-disk format and `restore`'s
// `u16::from_le_bytes` decode (a native-endian
// `bytemuck::cast_slice` would byte-swap on a big-endian
// host). f16 and TurboQuant are mutually exclusive
// (`from_config_with_compression` picks one), so the
// compressed slots are always `None` here.
LayerSnapshot::AttentionF16 {
k_data: key_cache_f16.iter().flat_map(|h| h.to_le_bytes()).collect(),
v_data: value_cache_f16
.iter()
.flat_map(|h| h.to_le_bytes())
.collect(),
}
} else {
match (compressed_keys, compressed_values) {
(None, None) => LayerSnapshot::Attention {
k_data: bytemuck::cast_slice(key_cache).to_vec(),
v_data: bytemuck::cast_slice(value_cache).to_vec(),
},
(Some(k), Some(v)) => LayerSnapshot::AttentionCompressed {
keys: crate::turboquant::encode_compressed_keys(k),
values: crate::turboquant::encode_compressed_values(v),
},
// Mixed-mode: refuse the whole snapshot.
(Some(_), None) | (None, Some(_)) => return None,
}
};
layers.push(snap);
}
LayerState::Conv { buffer, .. } => layers.push(LayerSnapshot::Conv {
buffer: bytemuck::cast_slice(buffer).to_vec(),
}),
}
}
Some(StateSnapshot::new(layers, self.seq_len))
}
/// Restore a previously captured `StateSnapshot` into this state's
/// f32 caches (or compressed caches for TurboQuant layers). Inverse
/// of [`Self::snapshot`]. Asserts that the snapshot's layer count
/// matches; f32 byte-length must be a multiple of 4 (one f32 per
/// 4 bytes); compressed blobs are validated via the magic bytes
/// in their headers.
pub fn restore(&mut self, snapshot: &StateSnapshot) {
assert_eq!(
snapshot.layers.len(),
self.layers.len(),
"snapshot layer count {} doesn't match state layer count {}",
snapshot.layers.len(),
self.layers.len()
);
// `bytemuck::cast_slice::<u8, f32>` requires 4-byte alignment of
// the source, which `Vec<u8>` doesn't guarantee. Decode element-
// wise so we don't depend on the snapshot's allocator alignment.
fn decode_f32_into(dst: &mut Vec<f32>, src: &[u8]) {
assert!(
src.len().is_multiple_of(4),
"snapshot byte length {} not a multiple of 4",
src.len()
);
dst.clear();
dst.reserve(src.len() / 4);
for chunk in src.as_chunks::<4>().0 {
dst.push(f32::from_le_bytes(*chunk));
}
}
// f16 sibling of `decode_f32_into`: the source is raw IEEE-754 half
// bits (2 bytes/elem), decoded element-wise so we don't depend on the
// snapshot allocator's alignment for a `u16` cast.
fn decode_u16_into(dst: &mut Vec<u16>, src: &[u8]) {
assert!(
src.len().is_multiple_of(2),
"f16 snapshot byte length {} not a multiple of 2",
src.len()
);
dst.clear();
dst.reserve(src.len() / 2);
for chunk in src.as_chunks::<2>().0 {
dst.push(u16::from_le_bytes(*chunk));
}
}
// Captured before the mutable layer borrow; the `AttentionF16` arm
// asserts on it (symmetric with the compressed arm's slot assert) so a
// mode-mismatched snapshot panics loudly instead of silently writing
// f16 bytes into an f32-configured state. The lfm2.rs compatibility gate
// is the primary guard; this is the backstop.
let kv_f16 = self.kv_f16;
for (layer, snap) in self.layers.iter_mut().zip(snapshot.layers.iter()) {
match (layer, snap) {
(
LayerState::Attention {
key_cache,
value_cache,
key_cache_f16,
value_cache_f16,
..
},
LayerSnapshot::Attention { k_data, v_data },
) => {
// Symmetric backstop to the AttentionF16 arm: an f32
// snapshot must not restore into an f16 state (it would
// populate the f32 slots while the model reads the empty
// f16 slots → silent garbage). The lfm2 gate is the primary
// guard; panic loudly if it's ever bypassed.
assert!(
!kv_f16,
"f32 Attention snapshot restored into an f16 state — \
caller must gate on the snapshot/live compression mode"
);
decode_f32_into(key_cache, k_data);
decode_f32_into(value_cache, v_data);
// Keep the two representations from ever coexisting (f16
// slots stay empty on the f32 path).
key_cache_f16.clear();
value_cache_f16.clear();
}
(
LayerState::Attention {
key_cache,
value_cache,
key_cache_f16,
value_cache_f16,
..
},
LayerSnapshot::AttentionF16 { k_data, v_data },
) => {
assert!(
kv_f16,
"AttentionF16 snapshot restored into a non-f16 state — \
caller must gate on `LayerSnapshot::is_f16()` matching \
the live `kv_f16` mode"
);
decode_u16_into(key_cache_f16, k_data);
decode_u16_into(value_cache_f16, v_data);
// f32 caches are unused under f16; clear them so stale data
// from a prior uncompressed restore can't leak.
key_cache.clear();
value_cache.clear();
}
(
LayerState::Attention {
key_cache,
value_cache,
compressed_keys,
compressed_values,
..
},
LayerSnapshot::AttentionCompressed { keys, values },
) => {
// The live state's compressed slots must already
// be allocated by `from_config_with_compression`;
// we don't allocate them here because the
// associated rotation/scratch state lives
// elsewhere on `InferenceState` and would be
// missing. A `None` slot means the live state
// isn't TurboQuant-configured; the caller
// should have detected the incompatibility
// before calling restore (see `LayerSnapshot::is_compressed`
if let (Some(ck), Some(cv)) = (
crate::turboquant::decode_compressed_keys(keys),
crate::turboquant::decode_compressed_values(values),
) {
*compressed_keys = Some(ck);
*compressed_values = Some(cv);
key_cache.clear();
value_cache.clear();
} else {
tracing::error!(
"invalid TQK1/TQV1 compressed blob in snapshot; skipping layer restore"
);
}
}
(
LayerState::Conv { buffer, history },
LayerSnapshot::Conv { buffer: snap_buf },
) => {
decode_f32_into(buffer, snap_buf);
history.clear();
history.push(snapshot.seq_len, buffer);
}
_ => panic!("snapshot layer kind doesn't match state layer kind"),
}
}
self.seq_len = snapshot.seq_len;
}
/// Truncate the KV cache to its first `len` positions, dropping the tail
/// `[len .. seq_len)` from every attention layer, and set `seq_len = len`.
///
/// This is the speculative-decoding rollback: after verifying K drafted
/// tokens, the rejected ones' K/V cells (appended at the tail) are dropped so
/// the next forward continues from the accepted boundary. Unlike
/// [`Self::shift_kv_with_rope`], **no RoPE fixup is needed** — surviving cells
/// keep their original absolute positions, so this is a plain `Vec::truncate`.
/// For convolution layers, rolling buffer state is rewound using recorded history snapshots.
///
/// Preconditions (enforced in all builds):
/// - `len <= seq_len`
/// - `!self.is_compressed()` — TurboQuant caches have no tail-truncate.
pub fn truncate_to(&mut self, len: usize) {
assert!(
len <= self.seq_len,
"truncate_to({len}) exceeds seq_len {}",
self.seq_len
);
assert!(
!self.is_compressed(),
"truncate_to called on a TurboQuant-compressed state; not supported"
);
if len == self.seq_len {
return;
}
// Validate whether all convolution layers can safely roll back to `len`.
// If any convolution layer lacks `len` in its history ring buffer, force `safe_len = 0`
// to safely reset the state and avoid corrupted convolution history.
let mut safe_len = len;
if safe_len > 0 {
for layer in self.layers.iter() {
if let LayerState::Conv { history, .. } = layer
&& !history.has_pos(safe_len)
{
tracing::warn!(
target: "cera::kv_cache",
target_len = safe_len,
"truncate_to target pos not in ConvHistory ring buffer; forcing full clear to prevent convolution state corruption"
);
safe_len = 0;
break;
}
}
}
let seq_len = self.seq_len;
// Truncate one time-major `[seq_len × kv_dim]` cache to `safe_len` positions.
// `kv_dim = cache.len() / seq_len` (exact — the cache is a whole multiple
// of `seq_len`); an empty cache (the inactive f32/f16 slot) is a no-op.
fn trunc<T>(cache: &mut Vec<T>, seq_len: usize, len: usize) {
if cache.is_empty() {
return;
}
// `kv_dim` is recovered by division, so a cache that is not a whole
// multiple of `seq_len` would yield a wrong stride and truncate to a
// boundary mid-vector — leaving a cache that still looks well-formed
// and decodes to plausible garbage. Assert the invariant instead of
// propagating it silently.
assert_eq!(
cache.len() % seq_len,
0,
"KV cache length {} is not a multiple of seq_len {seq_len}; \
cannot recover kv_dim to truncate on a vector boundary",
cache.len()
);
let kv_dim = cache.len() / seq_len;
cache.truncate(len * kv_dim);
}
for layer in &mut self.layers {
match layer {
LayerState::Attention {
key_cache,
value_cache,
key_cache_f16,
value_cache_f16,
..
} => {
trunc(key_cache, seq_len, safe_len);
trunc(value_cache, seq_len, safe_len);
trunc(key_cache_f16, seq_len, safe_len);
trunc(value_cache_f16, seq_len, safe_len);
}
LayerState::Conv { buffer, history } => {
if !history.rollback_to(safe_len, buffer) {
buffer.fill(0.0);
history.clear();
}
}
}
}
self.seq_len = safe_len;
}
/// Drop KV cells `[n_keep .. n_keep + shift)` from every attention
/// layer, slide the tail down, and re-apply RoPE so each shifted
/// cell's stored K encodes its new absolute position rather than
/// its old one. Implements the core of `n_keep` context shift
/// (Phase 1.5).
///
/// Attention cache layout is time-major `[seq_len × kv_dim]`
/// where `kv_dim = n_kv_heads × head_dim`, so the drop maps to
/// `Vec::drain` of a contiguous range — one memmove per layer.
/// After the drain, cells originally at old position `p_old`
/// (with `p_old >= n_keep + shift`) now sit at new position
/// `p_new = p_old - shift`, but their stored K was rotated via
/// RoPE for `p_old`. We fix this by applying `R(-shift)` (a
/// constant delta rotation across the whole shifted region) to
/// each cell's K via [`crate::backend::cpu::apply_rope_delta_to_head`].
/// Rotations compose additively in each dim-pair plane, so the
/// result is identical to freshly rotating the raw K for `p_new`.
/// V is NOT rotated by RoPE — only the two drain calls touch V.
///
/// f16 KV states (`kv_f16`) shift the `*_f16` half-precision slots the same
/// way: the drain is over the same element range, and each K head is
/// **widened to f32, delta-rotated with the same helper, then narrowed back
/// to f16** (`head_dim ≤ 128`). f16 rounding makes this near-exact, not
/// bit-exact, vs a fresh f16 encode at `p_new`.
///
/// `seq_len` is decremented by `shift`. Compressed (TurboQuant)
/// layers are **not** shifted — callers must check
/// [`Self::is_compressed`] first; this method panics otherwise.
///
/// Conv layers (LFM2's `GatedConv`) are intentionally left
/// untouched: their buffer holds only the last `d_conv`
/// activations, which are post-shift-valid as soon as the next
/// forward pass runs. The quality transient decays to zero within
/// `d_conv` forward passes (typically 3 tokens for LFM2). See
/// `devlog/000034-feat-n-keep-shift.md` for the analysis.
///
/// Preconditions (enforced in all builds — violating them silently
/// corrupts state, so we use `assert!` rather than `debug_assert!`):
/// - `shift > 0`
/// - `n_keep + shift <= seq_len`
/// - `!self.is_compressed()`
/// - `n_kv_heads_per_layer.len() == self.layers.len()`
#[allow(clippy::too_many_arguments)]
pub fn shift_kv_with_rope(
&mut self,
n_keep: usize,
shift: usize,
rope_theta: f32,
head_dim: usize,
n_kv_heads_per_layer: &[usize],
rope_type: crate::backend::cpu::RopeType,
// Llama-3 RoPE frequency-scaling factors (`rope_freqs.weight`); must match
// the forward pass so the delta-rotation composes correctly. Only used on
// the NORM path; `None` ⇒ plain RoPE.
freq_factors: Option<&[f32]>,
) {
// Hard preconditions — keep them in release builds. Silently
// decrementing `seq_len` without actually shifting the
// compressed caches would hand the next forward pass a KV that
// disagrees with `seq_len`, producing garbage output, and the
// bounds asserts catch config errors that would otherwise
// panic deep inside `Vec::drain`.
assert!(shift > 0, "shift must be > 0");
assert!(
n_keep + shift <= self.seq_len,
"shift range out of bounds: n_keep={n_keep} + shift={shift} > seq_len={}",
self.seq_len
);
assert!(
!self.is_compressed(),
"shift_kv_with_rope called on a TurboQuant-compressed state; \
shifting compressed caches is not supported"
);
assert_eq!(
n_kv_heads_per_layer.len(),
self.layers.len(),
"n_kv_heads_per_layer length {} doesn't match layer count {}",
n_kv_heads_per_layer.len(),
self.layers.len(),
);
// `kv_f16` is a whole-state flag captured before the mutable layer
// iteration; in f16 mode the `*_f16` slots hold the cache and the f32
// slots are empty (and vice versa). The delta RoPE re-encoding is
// identical for both — only the storage width (and the widen/narrow
// around the rotation) differs.
let kv_f16 = self.kv_f16;
// The f16 path widens each K head into a fixed stack buffer before
// rotating; `head_dim` is bounded by attention hardware (≤ 128 for
// every supported model). Assert eagerly (f16 only) so a malformed
// config can't index past the buffer; the f32 path never allocates it.
if kv_f16 {
assert!(
head_dim <= 128,
"head_dim {head_dim} exceeds the f16 shift widen buffer (128)"
);
}
let new_seq_len = self.seq_len - shift;
let seq_len = self.seq_len;
let delta = -(shift as i32);
for (layer_idx, layer) in self.layers.iter_mut().enumerate() {
if let LayerState::Attention {
key_cache,
value_cache,
key_cache_f16,
value_cache_f16,
..
} = layer
{
if kv_f16 {
// ── f16 path ──────────────────────────────────────────
// Layer has no KV yet — nothing to shift.
if key_cache_f16.is_empty() && value_cache_f16.is_empty() {
continue;
}
// Same invariants as the f32 path, on the u16 caches:
// equal lengths, a clean multiple of `seq_len` (in u16
// units — the element count is identical to f32).
assert_eq!(
key_cache_f16.len(),
value_cache_f16.len(),
"f16 KV cache length mismatch: key={} value={}",
key_cache_f16.len(),
value_cache_f16.len()
);
assert!(seq_len > 0, "attention layer has KV but seq_len is 0");
assert_eq!(
key_cache_f16.len() % seq_len,
0,
"f16 KV cache length {} not a multiple of seq_len {}",
key_cache_f16.len(),
seq_len
);
let n_kv_heads = n_kv_heads_per_layer[layer_idx];
let kv_dim = key_cache_f16.len() / seq_len;
assert_eq!(
n_kv_heads * head_dim,
kv_dim,
"layer {layer_idx}: n_kv_heads*head_dim ({}) != cached kv_dim ({})",
n_kv_heads * head_dim,
kv_dim
);
let drop_start = n_keep * kv_dim;
let drop_end = (n_keep + shift) * kv_dim;
key_cache_f16.drain(drop_start..drop_end);
value_cache_f16.drain(drop_start..drop_end);
// Re-rotate K cells now at [n_keep, new_seq_len) by
// R(-shift): widen the f16 head to f32, apply the SAME
// delta-RoPE helper as the f32 path, narrow back to f16.
// V is not RoPE'd.
let mut head_buf = [0.0f32; 128];
for t in n_keep..new_seq_len {
let row_base = t * kv_dim;
for h in 0..n_kv_heads {
let head_start = row_base + h * head_dim;
let head = &mut key_cache_f16[head_start..head_start + head_dim];
let buf = &mut head_buf[..head_dim];
for (dst, &bits) in buf.iter_mut().zip(head.iter()) {
*dst = crate::quant::f16_to_f32(bits);
}
match rope_type {
crate::backend::cpu::RopeType::Neox => {
crate::backend::cpu::apply_rope_delta_to_head(
buf, delta, head_dim, rope_theta,
)
}
crate::backend::cpu::RopeType::Norm => {
crate::backend::cpu::apply_rope_norm_delta_to_head(
buf,
delta,
head_dim,
rope_theta,
freq_factors,
)
}
}
for (dst, &x) in head.iter_mut().zip(buf.iter()) {
*dst = crate::quant::f32_to_f16(x);
}
}
}
continue;
}
// ── f32 path ──────────────────────────────────────────────
// Layer has no KV yet — nothing to shift. Reaches here
// for models whose first `n_layers - 1` layers were
// populated but the last one wasn't; guard defensively.
if key_cache.is_empty() && value_cache.is_empty() {
continue;
}
// Invariants we rely on: both caches the same length,
// that length a clean multiple of `seq_len`. Asserting
// here catches cache-corruption bugs with a clear
// message instead of an opaque `Vec::drain` panic.
assert_eq!(
key_cache.len(),
value_cache.len(),
"KV cache length mismatch: key={} value={}",
key_cache.len(),
value_cache.len()
);
assert!(seq_len > 0, "attention layer has KV but seq_len is 0");
assert_eq!(
key_cache.len() % seq_len,
0,
"KV cache length {} not a multiple of seq_len {}",
key_cache.len(),
seq_len
);
let n_kv_heads = n_kv_heads_per_layer[layer_idx];
let kv_dim = key_cache.len() / seq_len;
// Sanity: declared kv_dim matches what's actually stored.
// An off-by-one here (wrong head count passed in) would
// silently corrupt the per-head RoPE application below,
// so assert eagerly.
assert_eq!(
n_kv_heads * head_dim,
kv_dim,
"layer {layer_idx}: n_kv_heads*head_dim ({}) != cached kv_dim ({})",
n_kv_heads * head_dim,
kv_dim
);
let drop_start = n_keep * kv_dim;
let drop_end = (n_keep + shift) * kv_dim;
// `Vec::drain` on a contiguous range is a memmove of
// the tail — no reallocation, one pass.
key_cache.drain(drop_start..drop_end);
value_cache.drain(drop_start..drop_end);
// Re-rotate K cells now at positions [n_keep, new_seq_len).
// Their stored K was rotated for (new_pos + shift); apply
// R(-shift) to re-encode as new_pos. V is not RoPE'd.
for t in n_keep..new_seq_len {
let row_base = t * kv_dim;
for h in 0..n_kv_heads {
let head_start = row_base + h * head_dim;
let head_end = head_start + head_dim;
let head = &mut key_cache[head_start..head_end];
match rope_type {
crate::backend::cpu::RopeType::Neox => {
crate::backend::cpu::apply_rope_delta_to_head(
head, delta, head_dim, rope_theta,
)
}
crate::backend::cpu::RopeType::Norm => {
crate::backend::cpu::apply_rope_norm_delta_to_head(
head,
delta,
head_dim,
rope_theta,
freq_factors,
)
}
}
}
}
}
}
self.seq_len = new_seq_len;
}
}
#[cfg(test)]
mod cache_tag_tests {
use super::KvCompression;
/// Every mode whose cache *contents* differ must get a distinct tag, and modes
/// whose contents are identical must share one. Without this, snapshots from
/// different modes collide in the prefix cache's disk namespace and one
/// permanently shadows the other for the same prefix.
#[test]
fn distinct_modes_get_distinct_tags() {
let f32_tag = KvCompression::None.cache_tag();
let f16_tag = KvCompression::F16.cache_tag();
let both = KvCompression::turboquant(42).cache_tag();
let keys_only = KvCompression::TurboQuant {
seed: 42,
keys: true,
values: false,
}
.cache_tag();
let values_only = KvCompression::TurboQuant {
seed: 42,
keys: false,
values: true,
}
.cache_tag();
// A different seed means different rotations, and restore validates only
// shape — so sharing a namespace would decode a prefix in the wrong basis
// rather than miss.
let other_seed = KvCompression::turboquant(7).cache_tag();
let tags = [
&f32_tag,
&f16_tag,
&both,
&keys_only,
&values_only,
&other_seed,
];
for (i, a) in tags.iter().enumerate() {
for b in tags.iter().skip(i + 1) {
assert_ne!(a, b, "two modes share a cache tag: {a:?} vs {b:?}");
}
}
assert_eq!(f32_tag, "", "plain f32 must be the untagged default");
}
/// Compressing neither side degrades to an f32 cache in
/// `from_config_capped`, so its contents really are f32's — separating the two
/// namespaces would only waste entries.
#[test]
fn turboquant_with_no_sides_shares_the_f32_namespace() {
assert_eq!(
KvCompression::TurboQuant {
seed: 42,
keys: false,
values: false,
}
.cache_tag(),
KvCompression::None.cache_tag()
);
}
}
// ── KV Prefix Cache ─────────────────────────────────────────────────────
/// Classification of semantic boundaries in agentic and multimodal workflows.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
#[repr(u8)]
pub enum SemanticBoundaryKind {
Unspecified = 0,
Turn = 1,
ToolCall = 2,
ToolOutput = 3,
Thinking = 4,
ImageTokens = 5,
SystemPrompt = 6,
}
impl From<u8> for SemanticBoundaryKind {
fn from(val: u8) -> Self {
match val {
1 => Self::Turn,
2 => Self::ToolCall,
3 => Self::ToolOutput,
4 => Self::Thinking,
5 => Self::ImageTokens,
6 => Self::SystemPrompt,
_ => Self::Unspecified,
}
}
}
/// Snapshot of model KV + conv state after prefilling a token sequence.
/// Backend-agnostic: stores raw bytes that the backend knows how to restore.
#[derive(Clone, Debug)]
pub struct StateSnapshot {
pub layers: Vec<LayerSnapshot>,
pub seq_len: usize,
pub anchor_depth: u32,
pub boundary_kind: u8,
pub semantic_hash: u64,
pub shift_offset: u32,
}
impl StateSnapshot {
pub fn new(layers: Vec<LayerSnapshot>, seq_len: usize) -> Self {
Self {
layers,
seq_len,
anchor_depth: 0,
boundary_kind: 0,
semantic_hash: 0,
shift_offset: 0,
}
}
pub fn with_anchor(
mut self,
depth: u32,
kind: SemanticBoundaryKind,
semantic_hash: u64,
) -> Self {
self.anchor_depth = depth;
self.boundary_kind = kind as u8;
self.semantic_hash = semantic_hash;
self
}
pub fn with_shift_offset(mut self, shift: u32) -> Self {
self.shift_offset = shift;
self
}
}
#[derive(Clone, Debug)]
pub enum LayerSnapshot {
/// Raw f32 KV bytes (CPU / wgpu) or raw f16 (Metal). Backend
/// chooses the element width; the byte length implicitly carries
/// it. The `model_fingerprint` plus the `"cpu:"` / `"wgpu:"` /
/// `"metal:"` model_id prefix prevent cross-backend loads.
Attention {
k_data: Vec<u8>,
v_data: Vec<u8>,
},
/// TurboQuant-compressed attention layer with **both** sides
/// compressed. `keys` is the encoded `CompressedKeyCache` (magic
/// "TQK1"); `values` is the encoded `CompressedValueCache` (magic
/// "TQV1"). Mixed-mode (only one side compressed) is not modeled
/// here — `InferenceState::snapshot` returns `None` for such
/// states because the alternate side's f32 data has no slot in
/// this variant. v2 could add per-side variants if a real
/// mixed-mode workload turns up; today the production config
/// (`KvCompression::TurboQuant { keys: true, values: true }`)
/// is uniform.
AttentionCompressed {
keys: Vec<u8>,
values: Vec<u8>,
},
/// f16 (IEEE-754 half) KV bytes. Captured from an attention layer whose
/// `key_cache_f16`/`value_cache_f16` slots hold the cache
/// (`KvCompression::F16`). `k_data`/`v_data` are the raw little-endian u16
/// half-bits — half the width of the f32 `Attention` variant. Distinct from
/// `Attention` so the restore-time cross-mode gate can reject an f16 snapshot
/// against an f32 state (and vice versa) before a byte-width mismatch
/// corrupts the cache.
AttentionF16 {
k_data: Vec<u8>,
v_data: Vec<u8>,
},
Conv {
buffer: Vec<u8>,
},
}
impl LayerSnapshot {
/// `true` iff this snapshot was captured from a TurboQuant-
/// compressed attention layer. Callers about to invoke
/// [`InferenceState::restore`] should check that this matches
/// the target's per-layer compression mode — `restore` panics
/// on a compression-mode mismatch (e.g. compressed snapshot
/// into an uncompressed live state).
pub fn is_compressed(&self) -> bool {
matches!(self, LayerSnapshot::AttentionCompressed { .. })
}
/// `true` iff this snapshot was captured from an f16 (`KvCompression::F16`)
/// attention layer. Mirrors [`Self::is_compressed`]; callers gate `restore`
/// on this matching the target state's `kv_f16` flag so an f16 snapshot is
/// never restored into an f32 state (or vice versa) — the byte widths
/// differ, so a cross-mode restore would corrupt the cache.
pub fn is_f16(&self) -> bool {
matches!(self, LayerSnapshot::AttentionF16 { .. })
}
}
impl StateSnapshot {
pub fn byte_size(&self) -> usize {
self.layers
.iter()
.map(|l| match l {
LayerSnapshot::Attention { k_data, v_data } => k_data.len() + v_data.len(),
LayerSnapshot::AttentionCompressed { keys, values } => keys.len() + values.len(),
LayerSnapshot::AttentionF16 { k_data, v_data } => k_data.len() + v_data.len(),
LayerSnapshot::Conv { buffer } => buffer.len(),
})
.sum()
}
/// `true` iff any attention layer in this snapshot is
/// compressed. Used by `Lfm2Model::forward_prefill` to skip
/// snapshots whose compression mode doesn't match the live
/// state — treat as cache miss instead of panicking on
/// `restore`.
pub fn is_compressed(&self) -> bool {
self.layers.iter().any(LayerSnapshot::is_compressed)
}
/// `true` iff any attention layer in this snapshot is f16
/// (`AttentionF16`). Used by `Lfm2Model::forward_prefill`'s
/// cross-mode gate to reject an f16 snapshot against an f32
/// (or compressed) live state — their byte widths differ, so a
/// cross-mode restore would corrupt the cache.
pub fn is_f16(&self) -> bool {
self.layers.iter().any(LayerSnapshot::is_f16)
}
}
/// Configuration for the KV prefix cache.
///
/// `Clone` so a backend can rebuild its cache under a new namespace without the
/// caller re-supplying the config — the wgpu backend does this when a session
/// configures KV compression, to keep compressed and f32 disk entries apart.
#[derive(Clone)]
pub struct KvCacheConfig {
/// Directory for cold-tier (disk) cache files. None = disk caching disabled.
pub cache_dir: Option<PathBuf>,
/// Max warm-tier (memory) entries.
pub max_warm_entries: usize,
/// Max warm-tier total bytes.
pub max_warm_bytes: u64,
/// Max cold-tier (disk) total size in bytes.
pub max_cold_bytes: u64,
/// Max warm-tier semantic anchor snapshots retained.
pub max_warm_anchors: usize,
}
impl Default for KvCacheConfig {
fn default() -> Self {
Self {
cache_dir: None,
max_warm_entries: 32,
max_warm_bytes: 256 * 1024 * 1024,
max_cold_bytes: 10 * 1024 * 1024 * 1024,
max_warm_anchors: 16,
}
}
}
struct CacheEntry {
tokens: Vec<u32>,
snapshot: StateSnapshot,
last_used: Cell<u64>,
}
/// Two-tier KV prefix cache: warm (memory) + cold (disk via FlatBuffers).
#[cfg_attr(not(feature = "disk-cache"), allow(dead_code))]
pub struct KvPrefixCache {
warm: HashMap<u64, CacheEntry>,
pub config: KvCacheConfig,
model_fingerprint: u64,
warm_bytes: u64,
tick: Cell<u64>,
}
impl KvPrefixCache {
pub fn new(config: KvCacheConfig, model_config: &ModelConfig, model_id: &str) -> Self {
Self {
warm: HashMap::new(),
model_fingerprint: model_fingerprint(model_config, model_id),
config,
warm_bytes: 0,
tick: Cell::new(0),
}
}
fn next_tick(&self) -> u64 {
let t = self.tick.get().wrapping_add(1);
self.tick.set(t);
t
}
/// Find the longest cached **strict** prefix of `tokens` — an entry covering
/// `[0, len)` with `len < tokens.len()`. Checks both warm and cold tiers and
/// returns whichever has the longer match.
///
/// Strictness is not a detail, it is the contract every consumer already
/// enforces: a full-length hit would leave `use_len == tokens.len()`, and the
/// restored state already reflects "after all tokens", so re-running the last
/// token would advance the conv rolling buffer one position past where it
/// belongs (conv layers don't gate on `seq_len`). `Lfm2Model::forward_prefill`
/// and both GPU backends therefore skip full hits.
///
/// Returning them anyway made the cache **effectively single-use per token
/// sequence**: run a prompt once and `insert` stores a full-length entry for
/// it; ask the same question again and that entry is the longest match, so the
/// caller rejects it and falls through to a cold prefill *without* falling back
/// to the shorter, perfectly usable prefix sitting right there. Measured on
/// LFM2-VL-450M: a genuine prefix hit prefills at ~17k tok/s (f32) / ~20k
/// (tq3), versus ~840 / ~367 cold — so the shadowed lookup was giving up a
/// 20-55x speedup on every repeat query.
///
/// Filtering here rather than in each backend keeps warm and cold consistent
/// and fixes all three backends at once.
pub fn find_longest_prefix(&mut self, tokens: &[u32]) -> Option<(StateSnapshot, usize)> {
let tick = self.next_tick();
let warm_hit = self
.warm
.values()
.filter(|e| e.tokens.len() < tokens.len() && tokens.starts_with(&e.tokens))
.max_by_key(|e| e.tokens.len())
.map(|e| {
e.last_used.set(tick);
(e.snapshot.clone(), e.tokens.len())
});
// Check cold tier too — it may have a longer prefix than the warm hit.
// `disk-cache` off → cold tier compiles out; only warm hits matter.
#[cfg(feature = "disk-cache")]
let cold_hit = self
.config
.cache_dir
.clone()
.and_then(|dir| self.find_cold_prefix(&dir, tokens))
.map(|snapshot| {
let len = snapshot.seq_len;
(snapshot, len)
});
#[cfg(not(feature = "disk-cache"))]
let cold_hit: Option<(StateSnapshot, usize)> = None;
let (best, is_cold) = match (warm_hit, cold_hit) {
(Some(w), Some(c)) if c.1 > w.1 => (Some(c), true),
(Some(w), _) => (Some(w), false),
(None, Some(c)) => (Some(c), true),
(None, None) => (None, false),
};
// If the best hit came from the cold tier, promote it to warm without redundant O(N) scan.
if is_cold && let Some((snapshot, len)) = &best {
self.promote_cold_to_warm(snapshot, &tokens[..*len]);
}
best
}
/// Find the deepest cached semantic anchor that is a strict prefix of `tokens`.
/// Semantic anchors are snapshots saved at turn, tool, or thinking boundaries.
/// If no anchor is tagged, falls back to [`Self::find_longest_prefix`].
pub fn find_deepest_semantic_anchor(
&mut self,
tokens: &[u32],
) -> Option<(StateSnapshot, usize)> {
let tick = self.next_tick();
let warm_anchor = self
.warm
.values()
.filter(|e| {
e.tokens.len() < tokens.len()
&& tokens.starts_with(&e.tokens)
&& (e.snapshot.anchor_depth > 0 || e.snapshot.boundary_kind > 0)
})
.max_by_key(|e| e.tokens.len())
.map(|e| {
e.last_used.set(tick);
(e.snapshot.clone(), e.tokens.len())
});
#[cfg(feature = "disk-cache")]
let cold_anchor = self
.config
.cache_dir
.clone()
.and_then(|dir| self.find_cold_anchor(&dir, tokens))
.map(|snapshot| {
let len = snapshot.seq_len;
(snapshot, len)
});
#[cfg(not(feature = "disk-cache"))]
let cold_anchor: Option<(StateSnapshot, usize)> = None;
let (best, is_cold) = match (warm_anchor, cold_anchor) {
(Some(w), Some(c)) if c.1 > w.1 => (Some(c), true),
(Some(w), _) => (Some(w), false),
(None, Some(c)) => (Some(c), true),
(None, None) => (None, false),
};
if is_cold && let Some((snapshot, len)) = &best {
self.promote_cold_to_warm(snapshot, &tokens[..*len]);
}
best.or_else(|| self.find_longest_prefix(tokens))
}
/// Cache a semantic anchor prefix's state with boundary metadata.
pub fn insert_anchor(
&mut self,
tokens: &[u32],
snapshot: StateSnapshot,
anchor_depth: u32,
boundary_kind: SemanticBoundaryKind,
semantic_hash: u64,
) {
let snapshot = snapshot.with_anchor(anchor_depth, boundary_kind, semantic_hash);
self.insert(tokens, snapshot);
}
/// Get total warm cache memory in bytes.
pub fn warm_bytes(&self) -> u64 {
self.warm_bytes
}
/// Number of warm entries.
pub fn warm_count(&self) -> usize {
self.warm.len()
}
/// Number of tagged warm semantic anchors.
pub fn warm_anchor_count(&self) -> usize {
self.warm
.values()
.filter(|e| e.snapshot.anchor_depth > 0 || e.snapshot.boundary_kind > 0)
.count()
}
/// Cache a prefix's state. Stores in warm tier; optionally persists to cold.
pub fn insert(&mut self, tokens: &[u32], snapshot: StateSnapshot) {
// Skip if cache is disabled (max_warm_entries == 0 and no disk).
if self.config.max_warm_entries == 0 && self.config.cache_dir.is_none() {
return;
}
let hash = hash_tokens(tokens);
let snap_bytes = snapshot.byte_size() as u64;
let is_anchor = snapshot.anchor_depth > 0 || snapshot.boundary_kind > 0;
// Remove old entry first if present so warm_bytes and capacity checks are 100% exact
if let Some(old) = self.warm.remove(&hash) {
self.warm_bytes = self
.warm_bytes
.saturating_sub(old.snapshot.byte_size() as u64);
}
// Evict from warm if needed.
self.evict_warm_if_needed(snap_bytes, is_anchor);
// Save to cold tier (if `disk-cache` feature on; otherwise no-op).
#[cfg(feature = "disk-cache")]
if let Some(dir) = &self.config.cache_dir {
self.save_cold(dir, tokens, &snapshot);
}
let tick = self.next_tick();
self.warm.insert(
hash,
CacheEntry {
tokens: tokens.to_vec(),
snapshot,
last_used: Cell::new(tick),
},
);
self.warm_bytes += snap_bytes;
}
/// Clear all warm entries from the in-memory tier (e.g. during OS memory pressure).
pub fn clear_warm(&mut self) {
self.warm.clear();
self.warm_bytes = 0;
}
/// Clear all cache entries for this model (both warm memory tier and cold disk tier).
pub fn clear(&mut self) {
self.clear_warm();
#[cfg(feature = "disk-cache")]
if let Some(dir) = &self.config.cache_dir {
let fp_prefix = format!("{:016x}_", self.model_fingerprint);
if let Ok(entries) = std::fs::read_dir(dir) {
for entry in entries.flatten() {
let name = entry.file_name();
let name_str = name.to_string_lossy();
if name_str.starts_with(&fp_prefix)
&& (name_str.ends_with(".kvcache") || name_str.contains(".kvcache.tmp."))
{
let _ = std::fs::remove_file(entry.path());
}
}
}
}
}
fn promote_cold_to_warm(&mut self, snapshot: &StateSnapshot, tokens: &[u32]) {
if self.config.max_warm_entries == 0 || self.config.max_warm_bytes == 0 {
return;
}
let is_anchor = snapshot.anchor_depth > 0 || snapshot.boundary_kind > 0;
let hash = hash_tokens(tokens);
let snap_bytes = snapshot.byte_size() as u64;
if let Some(old) = self.warm.remove(&hash) {
self.warm_bytes = self
.warm_bytes
.saturating_sub(old.snapshot.byte_size() as u64);
}
self.evict_warm_if_needed(snap_bytes, is_anchor);
let tick = self.next_tick();
self.warm.insert(
hash,
CacheEntry {
tokens: tokens.to_vec(),
snapshot: snapshot.clone(),
last_used: Cell::new(tick),
},
);
self.warm_bytes += snap_bytes;
}
fn evict_warm_if_needed(&mut self, new_bytes: u64, is_anchor: bool) {
// 1. If anchor limit is reached, specifically target and evict the oldest anchor first
if is_anchor
&& self.config.max_warm_anchors > 0
&& self.warm_anchor_count() >= self.config.max_warm_anchors
{
let oldest_anchor_key = self
.warm
.iter()
.filter(|(_, e)| e.snapshot.anchor_depth > 0 || e.snapshot.boundary_kind > 0)
.min_by_key(|(_, e)| e.last_used.get())
.map(|(k, _)| *k);
if let Some(key) = oldest_anchor_key
&& let Some(removed) = self.warm.remove(&key)
{
self.warm_bytes -= removed.snapshot.byte_size() as u64;
}
}
// 2. Fall back to standard LRU eviction for byte-size and total entry constraints
while (self.warm.len() >= self.config.max_warm_entries
|| self.warm_bytes + new_bytes > self.config.max_warm_bytes)
&& !self.warm.is_empty()
{
let oldest = self
.warm
.iter()
.min_by_key(|(_, e)| e.last_used.get())
.map(|(k, _)| *k);
if let Some(key) = oldest
&& let Some(removed) = self.warm.remove(&key)
{
self.warm_bytes -= removed.snapshot.byte_size() as u64;
}
}
}
// ── Cold tier (FlatBuffers) ─────────────────────────────────────
//
// All cold-tier helpers live behind `disk-cache` (default-on).
// Builds without `disk-cache` compile only the warm (memory) tier;
// `cache_dir` on `KvCacheConfig` is retained so consumers don't
// need to conditionally construct the config, but it's ignored.
#[cfg(feature = "disk-cache")]
fn cold_filename(&self, token_hash: u64) -> String {
format!(
"{:016x}_{:016x}.kvcache",
self.model_fingerprint, token_hash
)
}
#[cfg(feature = "disk-cache")]
fn save_cold(&self, dir: &Path, tokens: &[u32], snapshot: &StateSnapshot) {
if std::fs::create_dir_all(dir).is_err() {
return;
}
let mut builder =
flatbuffers::FlatBufferBuilder::with_capacity(snapshot.byte_size() + 1024);
// Build layers. type_tag taxonomy:
// 0 = Attention (raw KV bytes at the BACKEND-NATIVE width — f32 on
// CPU/wgpu, f16 on Metal; the byte length implicitly carries it).
// 1 = Conv (raw f32 rolling buffer bytes).
// 2 = AttentionCompressed (TurboQuant; k_data=encoded keys
// blob "TQK1...", v_data=encoded values blob "TQV1...").
// 3 = AttentionF16 (CPU `KvCompression::F16`; raw IEEE-754 half bits,
// k_data/v_data are little-endian u16 half-widths — no schema
// change, the generic LayerData {type_tag, k_data, v_data} carries
// it. Distinct from Metal's f16-in-tag-0 because a CPU f16 session
// must not restore a tag-0 f32 snapshot; the lfm2 gate enforces it).
let mut layer_offsets = Vec::with_capacity(snapshot.layers.len());
for layer in &snapshot.layers {
let (tag, k_off, v_off) = match layer {
LayerSnapshot::Attention { k_data, v_data } => {
let k = builder.create_vector(k_data);
let v = builder.create_vector(v_data);
(0u8, Some(k), Some(v))
}
LayerSnapshot::Conv { buffer } => {
let k = builder.create_vector(buffer);
(1u8, Some(k), None)
}
LayerSnapshot::AttentionCompressed { keys, values } => {
let k = builder.create_vector(keys);
let v = builder.create_vector(values);
(2u8, Some(k), Some(v))
}
LayerSnapshot::AttentionF16 { k_data, v_data } => {
let k = builder.create_vector(k_data);
let v = builder.create_vector(v_data);
(3u8, Some(k), Some(v))
}
};
let ld = crate::generated::cera::cache::LayerData::create(
&mut builder,
&crate::generated::cera::cache::LayerDataArgs {
type_tag: tag,
k_data: k_off,
v_data: v_off,
},
);
layer_offsets.push(ld);
}
let layers_vec = builder.create_vector(&layer_offsets);
let tokens_vec = builder.create_vector(tokens);
let entry = crate::generated::cera::cache::KvCacheEntry::create(
&mut builder,
&crate::generated::cera::cache::KvCacheEntryArgs {
model_fingerprint: self.model_fingerprint,
seq_len: snapshot.seq_len as u32,
tokens: Some(tokens_vec),
layers: Some(layers_vec),
format_version: 2,
anchor_depth: snapshot.anchor_depth,
boundary_kind: snapshot.boundary_kind,
semantic_hash: snapshot.semantic_hash,
shift_offset: snapshot.shift_offset,
},
);
builder.finish(entry, None);
let data = builder.finished_data();
let hash = hash_tokens(tokens);
let target_path = dir.join(self.cold_filename(hash));
let tmp_path = dir.join(format!(
"{:016x}_{:016x}.kvcache.tmp.{}",
self.model_fingerprint,
hash,
std::process::id()
));
if std::fs::write(&tmp_path, data).is_ok() {
// Cache inserts must be atomic. If rename fails (e.g. cross-device link),
// discard the cache entry rather than falling back to a non-atomic copy
// which could cause concurrent readers to ingest partial flatbuffers files.
let _ = std::fs::rename(&tmp_path, &target_path);
let _ = std::fs::remove_file(&tmp_path);
}
self.evict_cold_if_needed(dir);
}
#[cfg(feature = "disk-cache")]
fn find_cold_prefix(&self, dir: &Path, tokens: &[u32]) -> Option<StateSnapshot> {
// Check specific filenames by pre-computing hashes for all prefixes,
// longest first. This avoids reading the entire directory.
//
// The range stops at `tokens.len() - 1`: only STRICT prefixes are useful
// (see `find_longest_prefix`). Including the full length would match the
// entry this very query wrote last time, which every caller then rejects —
// and because the scan `break`s on its first match, it would never fall
// back to the shorter usable one. A 1-token input has no strict prefix, so
// the loop is correctly empty.
let mut best: Option<StateSnapshot> = None;
for prefix_len in (1..tokens.len()).rev() {
let prefix = &tokens[..prefix_len];
let hash = hash_tokens(prefix);
let path = dir.join(self.cold_filename(hash));
if path.exists()
&& let Some(snapshot) = self.load_cold_file(&path, tokens)
{
best = Some(snapshot);
break; // longest prefix first, so first match is best
}
}
best
}
#[cfg(feature = "disk-cache")]
fn find_cold_anchor(&self, dir: &Path, tokens: &[u32]) -> Option<StateSnapshot> {
let mut best: Option<StateSnapshot> = None;
for prefix_len in (1..tokens.len()).rev() {
let prefix = &tokens[..prefix_len];
let hash = hash_tokens(prefix);
let path = dir.join(self.cold_filename(hash));
if path.exists()
&& let Some(snapshot) = self.load_cold_file(&path, tokens)
&& (snapshot.anchor_depth > 0 || snapshot.boundary_kind > 0)
{
best = Some(snapshot);
break;
}
}
best
}
#[cfg(feature = "disk-cache")]
fn load_cold_file(&self, path: &Path, expected_prefix: &[u32]) -> Option<StateSnapshot> {
let data = std::fs::read(path).ok()?;
let entry = flatbuffers::root::<crate::generated::cera::cache::KvCacheEntry>(&data).ok()?;
// Validate format version: must be v2 (2). Legacy/invalid format triggers graceful cache miss.
if entry.format_version() != 2 {
return None;
}
// Validate model fingerprint.
if entry.model_fingerprint() != self.model_fingerprint {
return None;
}
let cached_tokens = entry.tokens()?;
let seq_len = entry.seq_len() as usize;
// Validate seq_len matches token count.
if seq_len != cached_tokens.len() {
return None;
}
// Check that cached tokens are a prefix of expected tokens.
if cached_tokens.len() > expected_prefix.len() {
return None;
}
for (i, ct) in cached_tokens.iter().enumerate() {
if ct != expected_prefix[i] {
return None;
}
}
// Reconstruct snapshot.
let layers_fb = entry.layers()?;
let mut layers = Vec::with_capacity(layers_fb.len());
for l in layers_fb {
match l.type_tag() {
0 => {
let k_bytes = l.k_data()?.bytes().to_vec();
let v_bytes = l.v_data()?.bytes().to_vec();
if !k_bytes.len().is_multiple_of(4) || !v_bytes.len().is_multiple_of(4) {
return None;
}
layers.push(LayerSnapshot::Attention {
k_data: k_bytes,
v_data: v_bytes,
});
}
1 => {
let buf_bytes = l.k_data()?.bytes().to_vec();
if !buf_bytes.len().is_multiple_of(4) {
return None;
}
layers.push(LayerSnapshot::Conv { buffer: buf_bytes });
}
2 => {
let keys = l.k_data()?.bytes().to_vec();
let values = l.v_data()?.bytes().to_vec();
// Validate the encoded blob shape at load time so a
// corrupted disk entry surfaces as a cache miss
// (return None) rather than a panic later in
// `InferenceState::restore`'s `decode_*().expect(...)`.
if crate::turboquant::decode_compressed_keys(&keys).is_none()
|| crate::turboquant::decode_compressed_values(&values).is_none()
{
return None;
}
layers.push(LayerSnapshot::AttentionCompressed { keys, values });
}
3 => {
let k_data = l.k_data()?.bytes().to_vec();
let v_data = l.v_data()?.bytes().to_vec();
// Validate the f16 blob shape at load time (mirrors the
// tag-2 TurboQuant validation): u16 half-bits are 2 bytes
// each and K/V are the same length, so a corrupted entry
// surfaces as a cache miss (None) here instead of panicking
// later in `restore`'s `decode_u16_into` (`len % 2 == 0`).
if !k_data.len().is_multiple_of(2)
|| !v_data.len().is_multiple_of(2)
|| k_data.len() != v_data.len()
{
return None;
}
layers.push(LayerSnapshot::AttentionF16 { k_data, v_data });
}
_ => return None,
}
}
Some(StateSnapshot {
layers,
seq_len,
anchor_depth: entry.anchor_depth(),
boundary_kind: entry.boundary_kind(),
semantic_hash: entry.semantic_hash(),
shift_offset: entry.shift_offset(),
})
}
#[cfg(feature = "disk-cache")]
fn evict_cold_if_needed(&self, dir: &Path) {
let Ok(entries) = std::fs::read_dir(dir) else {
return;
};
let fp_prefix = format!("{:016x}_", self.model_fingerprint);
let mut files: Vec<(PathBuf, u64, std::time::SystemTime)> = Vec::new();
for entry in entries.filter_map(|e| e.ok()) {
let name = entry.file_name();
let name_str = name.to_string_lossy();
if name_str.contains(".kvcache.tmp.") {
let _ = std::fs::remove_file(entry.path());
continue;
}
if name_str.starts_with(&fp_prefix)
&& name_str.ends_with(".kvcache")
&& let Ok(meta) = entry.metadata()
&& let Ok(modified) = meta.modified()
{
files.push((entry.path(), meta.len(), modified));
}
}
let total: u64 = files.iter().map(|(_, sz, _)| sz).sum();
if total <= self.config.max_cold_bytes {
return;
}
// Sort by mtime ascending (oldest first).
files.sort_by_key(|(_, _, t)| *t);
let mut remaining = total;
for (path, sz, _) in &files {
if remaining <= self.config.max_cold_bytes {
break;
}
let _ = std::fs::remove_file(path);
remaining -= sz;
}
}
}
/// Stable 64-bit FNV-1a hash. Unlike `DefaultHasher`, the output is guaranteed
/// to be identical across Rust versions and platforms — required for the
/// on-disk cold cache where filenames embed the token hash.
fn fnv1a_u64(bytes: &[u8]) -> u64 {
const OFFSET: u64 = 0xcbf29ce484222325;
const PRIME: u64 = 0x100000001b3;
let mut h = OFFSET;
for &b in bytes {
h ^= b as u64;
h = h.wrapping_mul(PRIME);
}
h
}
fn hash_tokens(tokens: &[u32]) -> u64 {
let bytes: &[u8] = bytemuck::cast_slice(tokens);
fnv1a_u64(bytes)
}
/// Compute a fingerprint for a model configuration.
/// Two models with different fingerprints have incompatible KV cache layouts.
/// Callers should pass a `model_id` that uniquely identifies the specific
/// model weights (e.g. a hash of the GGUF file or the model name from metadata),
/// so different models with the same architecture don't share cache entries.
pub fn model_fingerprint(config: &ModelConfig, model_id: &str) -> u64 {
// Build a stable byte representation and hash it via FNV-1a. Using
// DefaultHasher would make the fingerprint non-stable across Rust versions,
// invalidating on-disk cache files at every toolchain bump.
let mut buf = Vec::with_capacity(128);
buf.extend_from_slice(model_id.as_bytes());
buf.push(0);
buf.extend_from_slice(config.architecture.as_bytes());
buf.push(0);
buf.extend_from_slice(&(config.n_layers as u64).to_le_bytes());
buf.extend_from_slice(&(config.hidden_size as u64).to_le_bytes());
buf.extend_from_slice(&(config.n_heads as u64).to_le_bytes());
for bt in &config.block_types {
buf.push(match bt {
crate::model::BlockType::Attention => 0,
crate::model::BlockType::GatedConv => 1,
});
}
for k in &config.kv_heads_per_layer {
buf.extend_from_slice(&(*k as u64).to_le_bytes());
}
fnv1a_u64(&buf)
}
#[cfg(test)]
mod tests {
use super::*;
fn tiny_config(n_layers: usize, hidden_size: usize) -> ModelConfig {
ModelConfig {
architecture: "lfm2".into(),
n_layers,
hidden_size,
intermediate_size: hidden_size * 2,
n_heads: 4,
n_kv_heads: 2,
head_dim: hidden_size / 4,
vocab_size: 256,
max_seq_len: 64,
rope_theta: 1_000_000.0,
rms_norm_eps: 1e-5,
block_types: (0..n_layers)
.map(|i| {
if i % 2 == 0 {
BlockType::Attention
} else {
BlockType::GatedConv
}
})
.collect(),
conv_kernel_size: Some(3),
kv_heads_per_layer: (0..n_layers)
.map(|i| if i % 2 == 0 { 2 } else { 0 })
.collect(),
scalars: crate::model::ScalarMultipliers::default(),
moe: None,
is_causal: true,
class_labels: Vec::new(),
}
}
/// The COLD tier's strict-prefix range, pinned directly.
///
/// Reverting `find_cold_prefix`'s `1..tokens.len()` back to `1..=tokens.len()`
/// left the whole suite green before this test existed — the warm-tier test
/// below doesn't touch the disk path, and `f16_snapshot_disk_round_trips` hits
/// under either range. This asserts the boundary itself.
#[cfg(feature = "disk-cache")]
#[test]
fn cold_tier_ignores_a_full_length_entry() {
let cfg = tiny_config(2, 16);
let dir = std::env::temp_dir().join(format!("cera_cold_strict_{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
let cache = KvPrefixCache::new(KvCacheConfig::default(), &cfg, "cpu:test");
let snapshot = |seq_len: usize| StateSnapshot::new(Vec::new(), seq_len);
let tokens = [7u32, 8, 9];
// Saved at exactly the query length → unusable, must not be returned.
cache.save_cold(&dir, &tokens, &snapshot(tokens.len()));
assert!(
cache.find_cold_prefix(&dir, &tokens).is_none(),
"cold tier returned a full-length entry; every caller rejects it, and \
the scan breaks on its first match so a shorter one would be skipped"
);
// A strict prefix of the same query must still be found.
cache.save_cold(&dir, &tokens[..2], &snapshot(2));
assert_eq!(
cache
.find_cold_prefix(&dir, &tokens)
.expect("strict prefix must be found")
.seq_len,
2
);
let _ = std::fs::remove_dir_all(&dir);
}
/// A cached entry covering *all* of `tokens` must not shadow a shorter, usable
/// one.
///
/// Every consumer rejects a full-length hit (the restored state already
/// reflects "after all tokens", so re-running the last token would over-advance
/// the conv rolling buffer). But `insert` stores a full-length entry after each
/// prefill, so returning them made the cache effectively single-use per token
/// sequence: the second time the same prompt arrived, the full-length entry was
/// the longest match, the caller rejected it, and the lookup never fell back to
/// the strict prefix sitting right there. That cost a 20-55x prefill speedup on
/// every repeat query.
#[test]
fn full_length_entry_does_not_shadow_a_shorter_prefix() {
let cfg = tiny_config(2, 8);
let mut cache = KvPrefixCache::new(
KvCacheConfig {
cache_dir: None,
..KvCacheConfig::default()
},
&cfg,
"test",
);
let snapshot = |seq_len: usize| StateSnapshot::new(Vec::new(), seq_len);
let tokens = [1u32, 2, 3, 4];
// Only a full-length entry exists → no usable hit.
cache.insert(&tokens, snapshot(tokens.len()));
assert!(
cache.find_longest_prefix(&tokens).is_none(),
"a full-length entry was returned; the caller can only reject it"
);
// Add a strict prefix. It must now be found even though the (longer)
// full-length entry is still present — this is the regression.
cache.insert(&tokens[..2], snapshot(2));
let (_, len) = cache
.find_longest_prefix(&tokens)
.expect("strict prefix must be found past the full-length entry");
assert_eq!(len, 2);
// A longer strict prefix still wins over a shorter one.
cache.insert(&tokens[..3], snapshot(3));
assert_eq!(cache.find_longest_prefix(&tokens).expect("hit").1, 3);
}
/// `for_prefill` must cap the pre-allocated KV cache to the prompt length,
/// NOT `max_seq_len` — the whole point of the hidden-states scratch path.
#[test]
fn for_prefill_caps_kv_capacity() {
let cfg = tiny_config(4, 16); // max_seq_len=64; attn kv_dim = 2*4 = 8
let n = 5;
let kv_dim = cfg.kv_heads_per_layer[0] * cfg.head_dim;
let state = InferenceState::for_prefill(&cfg, n).unwrap();
if let LayerState::Attention {
key_cache,
value_cache,
..
} = &state.layers[0]
{
assert!(key_cache.capacity() >= n * kv_dim);
assert!(value_cache.capacity() >= n * kv_dim);
// Far below a full-context reservation (5*8=40 « 64*8=512).
assert!(key_cache.capacity() < cfg.max_seq_len * kv_dim);
} else {
panic!("layer 0 should be attention");
}
// Clamp: a prompt longer than max_seq_len caps at max_seq_len.
let big = InferenceState::for_prefill(&cfg, cfg.max_seq_len + 100).unwrap();
if let LayerState::Attention { key_cache, .. } = &big.layers[0] {
assert!(key_cache.capacity() <= cfg.max_seq_len * kv_dim + kv_dim);
}
}
/// Model-free coverage for `truncate_to` (used by speculative decoding to
/// roll back rejected drafts): it must reset `seq_len` and cut every
/// attention layer's cache to exactly `len * kv_dim`, byte-for-byte equal to
/// the prefix present at that length. The `#[ignore]` model test proves the
/// *logits* round-trip; this pins the pure `Vec` math in CI (which the model
/// tests can't, per the repo's ignore-gated-oracle pattern).
#[test]
fn truncate_to_cuts_caches_to_prefix() {
// Dense (all-attention) config — `truncate_to` panics on Conv layers.
let mut cfg = tiny_config(2, 16);
cfg.architecture = "llama".into();
cfg.block_types = vec![BlockType::Attention; 2];
cfg.kv_heads_per_layer = vec![2; 2];
let kv_dim = cfg.kv_heads_per_layer[0] * cfg.head_dim; // 2 * 4 = 8
let mut state = InferenceState::from_config(&cfg).unwrap();
// Append R rows of distinct KV to every attention layer (value = a
// function of (token, dim) so any mis-slice is caught).
let r = 6usize;
for layer in &mut state.layers {
if let LayerState::Attention {
key_cache,
value_cache,
..
} = layer
{
for t in 0..r {
for d in 0..kv_dim {
key_cache.push((t * 100 + d) as f32);
value_cache.push(-((t * 100 + d) as f32));
}
}
}
}
state.seq_len = r;
// Record the length-L prefix that must survive the truncation.
let l = 4usize;
let mut expect_k = Vec::new();
let mut expect_v = Vec::new();
for layer in &state.layers {
if let LayerState::Attention {
key_cache,
value_cache,
..
} = layer
{
expect_k.push(key_cache[..l * kv_dim].to_vec());
expect_v.push(value_cache[..l * kv_dim].to_vec());
}
}
state.truncate_to(l);
assert_eq!(
state.seq_len, l,
"seq_len must drop to the truncation length"
);
let mut li = 0;
for layer in &state.layers {
if let LayerState::Attention {
key_cache,
value_cache,
..
} = layer
{
assert_eq!(key_cache.len(), l * kv_dim, "key cache cut to len*kv_dim");
assert_eq!(
value_cache.len(),
l * kv_dim,
"value cache cut to len*kv_dim"
);
assert_eq!(
key_cache, &expect_k[li],
"surviving keys must be the prefix"
);
assert_eq!(
value_cache, &expect_v[li],
"surviving values must be the prefix"
);
li += 1;
}
}
assert_eq!(li, 2, "both attention layers must have been checked");
// Truncating to the current length is a no-op.
state.truncate_to(l);
assert_eq!(state.seq_len, l);
}
/// f16 companion to `truncate_to_cuts_caches_to_prefix`: an `F16` KV state
/// (uncompressed, so spec-decode `truncate_to` is legal) must cut the
/// `key_cache_f16`/`value_cache_f16` half-precision slots to the length-L
/// prefix. Covers the `trunc` calls on the f16 caches that the f32 test
/// leaves on their `is_empty()` early-return.
#[test]
fn truncate_to_cuts_f16_caches_to_prefix() {
let mut cfg = tiny_config(2, 16);
cfg.architecture = "llama".into();
cfg.block_types = vec![BlockType::Attention; 2];
cfg.kv_heads_per_layer = vec![2; 2];
let kv_dim = cfg.kv_heads_per_layer[0] * cfg.head_dim; // 8
let mut state =
InferenceState::from_config_with_compression(&cfg, &KvCompression::F16).unwrap();
assert!(!state.is_compressed(), "F16 KV is not `is_compressed`");
let r = 6usize;
for layer in &mut state.layers {
if let LayerState::Attention {
key_cache_f16,
value_cache_f16,
..
} = layer
{
for t in 0..r {
for d in 0..kv_dim {
key_cache_f16.push((t * 100 + d) as u16);
value_cache_f16.push((t * 10 + d) as u16);
}
}
}
}
state.seq_len = r;
let l = 4usize;
state.truncate_to(l);
assert_eq!(state.seq_len, l);
let mut li = 0;
for layer in &state.layers {
if let LayerState::Attention {
key_cache,
key_cache_f16,
value_cache_f16,
..
} = layer
{
assert!(key_cache.is_empty(), "f32 slot stays empty under f16");
assert_eq!(
key_cache_f16.len(),
l * kv_dim,
"f16 keys cut to len*kv_dim"
);
assert_eq!(
value_cache_f16.len(),
l * kv_dim,
"f16 values cut to len*kv_dim"
);
// Prefix values are (t*100+d) / (t*10+d); confirm the first and
// last surviving rows are intact (no shifted slice).
assert_eq!(key_cache_f16[0], 0);
assert_eq!(
key_cache_f16[(l - 1) * kv_dim + (kv_dim - 1)],
(300 + 7) as u16
);
li += 1;
}
}
assert_eq!(li, 2, "both attention layers must have been checked");
}
/// `KvCompression::F16` allocates the f16 slots (f32 slots empty), append
/// converts to half and round-trips within f16 precision, and the snapshot
/// path now emits `AttentionF16` for every attention layer.
#[test]
fn f16_kv_stores_half_and_snapshots() {
let cfg = tiny_config(4, 16); // attn layers 0,2; kv_dim = 2*4 = 8
let kv_dim = cfg.kv_heads_per_layer[0] * cfg.head_dim;
let mut state =
InferenceState::from_config_with_compression(&cfg, &KvCompression::F16).unwrap();
assert!(state.kv_f16, "F16 compression must set kv_f16");
if let LayerState::Attention {
key_cache,
key_cache_f16,
..
} = &state.layers[0]
{
assert!(key_cache.is_empty(), "f32 slot stays empty under f16");
assert!(
key_cache_f16.capacity() >= cfg.max_seq_len * kv_dim,
"f16 slot pre-allocated to full context"
);
} else {
panic!("layer 0 should be attention");
}
let k: Vec<f32> = (0..kv_dim).map(|i| i as f32 * 0.1 - 0.3).collect();
let v: Vec<f32> = (0..kv_dim).map(|i| i as f32 * -0.05 + 0.2).collect();
state.append_kv_f16(0, &k, &v);
let (k16, v16) = state.kv_cache_f16(0);
assert_eq!(k16.len(), kv_dim);
assert_eq!(v16.len(), kv_dim);
for (i, &b) in k16.iter().enumerate() {
let got = crate::quant::f16_to_f32(b);
assert!(
(got - k[i]).abs() < 1e-2,
"f16 roundtrip drift at {i}: {got} vs {}",
k[i]
);
}
state.seq_len = 1;
let snap = state.snapshot().expect("f16 KV must snapshot");
// Every attention layer of an f16 state snapshots as AttentionF16,
// even the one we didn't append to (empty u16 bytes) — the mode flag,
// not the slot contents, drives the variant.
for (i, l) in snap.layers.iter().enumerate() {
match &state.layers[i] {
LayerState::Attention { .. } => assert!(
l.is_f16(),
"attention layer {i} must snapshot as AttentionF16"
),
LayerState::Conv { .. } => {
assert!(matches!(l, LayerSnapshot::Conv { .. }))
}
}
}
}
/// An f16 `InferenceState` snapshots to `AttentionF16` and restores back
/// into a fresh f16 state byte-exactly (the u16 half-bits and seq_len must
/// round-trip losslessly through the prefix-cache path).
#[test]
fn f16_snapshot_restore_round_trips() {
let cfg = tiny_config(4, 16); // attn layers 0,2; kv_dim = 2*4 = 8
let kv_dim = cfg.kv_heads_per_layer[0] * cfg.head_dim;
let mut state =
InferenceState::from_config_with_compression(&cfg, &KvCompression::F16).unwrap();
// Populate both attention layers with two tokens of deterministic KV.
for layer in [0usize, 2] {
for t in 0..2 {
let k: Vec<f32> = (0..kv_dim)
.map(|i| (layer as f32) + 0.1 * t as f32 + 0.01 * i as f32)
.collect();
let v: Vec<f32> = k.iter().map(|x| -x).collect();
state.append_kv_f16(layer, &k, &v);
}
}
state.seq_len = 2;
let snap = state.snapshot().expect("f16 state must snapshot");
for layer in [0usize, 2] {
assert!(
snap.layers[layer].is_f16(),
"attention layer {layer} must snapshot as AttentionF16"
);
}
// Restore into a fresh f16 state and assert byte-exact round-trip.
let mut fresh =
InferenceState::from_config_with_compression(&cfg, &KvCompression::F16).unwrap();
fresh.restore(&snap);
assert_eq!(fresh.seq_len, state.seq_len);
for layer in [0usize, 2] {
let (ko, vo) = state.kv_cache_f16(layer);
let (kr, vr) = fresh.kv_cache_f16(layer);
assert_eq!(
kr, ko,
"f16 key cache must round-trip exactly (layer {layer})"
);
assert_eq!(
vr, vo,
"f16 value cache must round-trip exactly (layer {layer})"
);
}
}
/// Corruption backstop: restoring an `AttentionF16` snapshot into an
/// f32-configured state must panic (not silently write u16 bytes into the
/// f32 slots). The lfm2 compatibility gate is the primary guard; this
/// asserts the `restore()` backstop behind it.
#[test]
#[should_panic(expected = "AttentionF16 snapshot restored into a non-f16 state")]
fn restore_f16_snapshot_into_f32_state_panics() {
let cfg = tiny_config(2, 16); // layer 0 attention, layer 1 conv
let kv_dim = cfg.kv_heads_per_layer[0] * cfg.head_dim;
let mut f16_state =
InferenceState::from_config_with_compression(&cfg, &KvCompression::F16).unwrap();
f16_state.append_kv_f16(0, &vec![0.5f32; kv_dim], &vec![-0.5f32; kv_dim]);
f16_state.seq_len = 1;
let snap = f16_state.snapshot().expect("f16 snapshots");
let mut f32_state =
InferenceState::from_config_with_compression(&cfg, &KvCompression::None).unwrap();
f32_state.restore(&snap); // must panic on the AttentionF16 arm's kv_f16 assert
}
/// Reverse backstop: restoring an f32 `Attention` snapshot into an
/// f16-configured state must panic (else the f16 session gets f32 slots
/// populated while it reads the empty f16 slots — silent garbage).
#[test]
#[should_panic(expected = "f32 Attention snapshot restored into an f16 state")]
fn restore_f32_snapshot_into_f16_state_panics() {
let cfg = tiny_config(2, 16);
let kv_dim = cfg.kv_heads_per_layer[0] * cfg.head_dim;
let mut f32_state =
InferenceState::from_config_with_compression(&cfg, &KvCompression::None).unwrap();
f32_state.append_kv(0, &vec![0.5f32; kv_dim], &vec![-0.5f32; kv_dim]);
f32_state.seq_len = 1;
let snap = f32_state.snapshot().expect("f32 snapshots");
let mut f16_state =
InferenceState::from_config_with_compression(&cfg, &KvCompression::F16).unwrap();
f16_state.restore(&snap); // must panic on the Attention arm's !kv_f16 assert
}
/// f16 snapshot survives the disk (flatbuffer `type_tag = 3`) round-trip:
/// `save_cold` → `find_cold_prefix` returns the same `AttentionF16` bytes.
#[cfg(feature = "disk-cache")]
#[test]
fn f16_snapshot_disk_round_trips() {
let cfg = tiny_config(2, 16);
let kv_dim = cfg.kv_heads_per_layer[0] * cfg.head_dim;
let mut state =
InferenceState::from_config_with_compression(&cfg, &KvCompression::F16).unwrap();
let k: Vec<f32> = (0..kv_dim).map(|i| 0.1 * i as f32 - 0.3).collect();
let v: Vec<f32> = k.iter().map(|x| -x).collect();
state.append_kv_f16(0, &k, &v);
state.seq_len = 1;
let snap = state.snapshot().expect("f16 snapshots");
let (k0, v0) = match &snap.layers[0] {
LayerSnapshot::AttentionF16 { k_data, v_data } => (k_data.clone(), v_data.clone()),
_ => panic!("source layer 0 should be AttentionF16"),
};
let dir = std::env::temp_dir().join(format!("cera_f16_disk_{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
let cache = KvPrefixCache::new(KvCacheConfig::default(), &cfg, "cpu:test");
// The cache requires seq_len == token count; we appended one token.
let tokens = [42u32];
cache.save_cold(&dir, &tokens, &snap);
// Look up a LONGER sequence: `find_cold_prefix` only returns strict
// prefixes (a full-length hit is unusable — see `find_longest_prefix`), so
// the saved 1-token entry has to be read back as the prefix of something
// longer. This test is about the `type_tag = 3` serialization, not the
// matching policy.
let query = [42u32, 43];
let restored = cache
.find_cold_prefix(&dir, &query)
.expect("disk round-trip must find the f16 entry");
assert_eq!(restored.seq_len, snap.seq_len);
match &restored.layers[0] {
LayerSnapshot::AttentionF16 { k_data, v_data } => {
assert_eq!(*k_data, k0, "f16 key bytes must survive disk round-trip");
assert_eq!(*v_data, v0, "f16 value bytes must survive disk round-trip");
}
_ => panic!("restored layer 0 should be AttentionF16 (type_tag 3)"),
}
let _ = std::fs::remove_dir_all(&dir);
}
/// After an f16 `n_keep` shift, a shifted K head decodes (within f16
/// tolerance) to the value a fresh RoPE at the new position would produce
/// from the same raw K — the widen→rotate→narrow path composes the delta
/// rotation exactly like the f32 shift does. V is drained but unrotated.
#[test]
fn f16_shift_reencodes_like_fresh() {
let head_dim = 16usize;
let n_kv_heads = 2usize;
let kv_dim = n_kv_heads * head_dim;
let seq_len = 12usize;
let n_keep = 3usize;
let shift = 4usize;
let freq_base = 10_000.0f32;
// A 2-layer config: layer 0 attention, layer 1 conv. head_dim=16 →
// hidden_size = n_heads(4) * head_dim = 64.
let mut cfg = tiny_config(2, 64);
cfg.head_dim = head_dim;
cfg.n_kv_heads = n_kv_heads;
cfg.kv_heads_per_layer = vec![n_kv_heads, 0];
cfg.max_seq_len = 64;
let mut state =
InferenceState::from_config_with_compression(&cfg, &KvCompression::F16).unwrap();
// Raw (pre-RoPE) K per (head, dim); the same raw is used at every
// position so the oracle is a pure function of the new position.
let raw = |h: usize, d: usize| 0.1 * (h as f32) + 0.01 * (d as f32);
// Fill each position's K with the raw values rotated for that position
// (mirroring the forward pass), and V with the unrotated raw.
for t in 0..seq_len {
let mut k = vec![0.0f32; kv_dim];
let mut v = vec![0.0f32; kv_dim];
for h in 0..n_kv_heads {
let mut head: Vec<f32> = (0..head_dim).map(|d| raw(h, d)).collect();
crate::backend::cpu::apply_rope_delta_to_head(
&mut head, t as i32, head_dim, freq_base,
);
for d in 0..head_dim {
k[h * head_dim + d] = head[d];
v[h * head_dim + d] = raw(h, d);
}
}
state.append_kv_f16(0, &k, &v);
}
state.seq_len = seq_len;
state.shift_kv_with_rope(
n_keep,
shift,
freq_base,
head_dim,
&cfg.kv_heads_per_layer,
crate::backend::cpu::RopeType::Neox,
None,
);
let new_seq_len = seq_len - shift;
assert_eq!(state.seq_len, new_seq_len);
let (k16, v16) = state.kv_cache_f16(0);
assert_eq!(k16.len(), new_seq_len * kv_dim);
// Tail cells: the cell now at t_new was at t_old = t_new + shift; its
// stored K must decode to a fresh RoPE for t_new. V stays unrotated.
for t_new in n_keep..new_seq_len {
for h in 0..n_kv_heads {
let mut oracle: Vec<f32> = (0..head_dim).map(|d| raw(h, d)).collect();
crate::backend::cpu::apply_rope_delta_to_head(
&mut oracle,
t_new as i32,
head_dim,
freq_base,
);
for (d, &oracle_d) in oracle.iter().enumerate() {
let idx = t_new * kv_dim + h * head_dim + d;
let got_k = crate::quant::f16_to_f32(k16[idx]);
assert!(
(got_k - oracle_d).abs() < 1e-2,
"K reencode drift at t_new={t_new} h={h} d={d}: {got_k} vs {oracle_d}"
);
let got_v = crate::quant::f16_to_f32(v16[idx]);
assert!(
(got_v - raw(h, d)).abs() < 1e-2,
"V must stay unrotated at t_new={t_new} h={h} d={d}: {got_v} vs {}",
raw(h, d)
);
}
}
}
}
/// `clear_for_reuse` zeroes seq_len and empties KV/conv buffers while KEEPING
/// capacity, so a reused hidden-states scratch does no allocation.
#[test]
fn clear_for_reuse_resets_but_keeps_capacity() {
let cfg = tiny_config(4, 16);
let mut state = InferenceState::for_prefill(&cfg, 8).unwrap();
state.seq_len = 3;
let (cap_k, cap_v) = if let LayerState::Attention {
key_cache,
value_cache,
..
} = &mut state.layers[0]
{
for i in 0..16 {
key_cache.push(i as f32);
value_cache.push(i as f32);
}
(key_cache.capacity(), value_cache.capacity())
} else {
panic!("layer 0 should be attention");
};
if let LayerState::Conv { buffer, .. } = &mut state.layers[1] {
buffer.iter_mut().for_each(|x| *x = 1.0);
}
state.clear_for_reuse();
assert_eq!(state.seq_len, 0);
if let LayerState::Attention {
key_cache,
value_cache,
..
} = &state.layers[0]
{
assert!(key_cache.is_empty() && value_cache.is_empty());
assert_eq!(key_cache.capacity(), cap_k, "capacity must be retained");
assert_eq!(value_cache.capacity(), cap_v);
}
if let LayerState::Conv { buffer, .. } = &state.layers[1] {
assert!(
buffer.iter().all(|&x| x == 0.0),
"conv buffer must be zeroed"
);
}
}
/// Snapshot then restore on a populated `InferenceState` must
/// reproduce the exact same byte-level contents — the prefix
/// cache's correctness depends on this round-trip being lossless.
#[test]
fn snapshot_restore_round_trip_attention_and_conv() {
let cfg = tiny_config(4, 16);
let mut state = InferenceState::from_config(&cfg).unwrap();
// Populate attention layer 0's KV with deterministic values
// that fit kv_dim = n_kv_heads * head_dim = 2 * 4 = 8.
if let LayerState::Attention {
key_cache,
value_cache,
..
} = &mut state.layers[0]
{
for i in 0..16 {
key_cache.push(i as f32 * 0.5);
value_cache.push(-(i as f32) * 0.25);
}
}
if let LayerState::Conv { buffer, .. } = &mut state.layers[1] {
for v in buffer.iter_mut() {
*v = 0.7;
}
}
state.seq_len = 2;
let snap = state.snapshot().expect("uncompressed state must snapshot");
// Drop the existing KV by recreating, then restore.
let mut fresh = InferenceState::from_config(&cfg).unwrap();
fresh.restore(&snap);
match (&fresh.layers[0], &state.layers[0]) {
(
LayerState::Attention {
key_cache: kr,
value_cache: vr,
..
},
LayerState::Attention {
key_cache: ko,
value_cache: vo,
..
},
) => {
assert_eq!(kr, ko, "key_cache must round-trip exactly");
assert_eq!(vr, vo, "value_cache must round-trip exactly");
}
_ => panic!("expected attention layer 0"),
}
match (&fresh.layers[1], &state.layers[1]) {
(LayerState::Conv { buffer: br, .. }, LayerState::Conv { buffer: bo, .. }) => {
assert_eq!(br, bo, "conv buffer must round-trip exactly")
}
_ => panic!("expected conv layer 1"),
}
assert_eq!(fresh.seq_len, state.seq_len);
}
/// Empty (no-prefill-yet) state must still round-trip — Vec<u8>
/// length zero on both sides.
#[test]
fn snapshot_restore_round_trip_empty_state() {
let cfg = tiny_config(2, 8);
let state = InferenceState::from_config(&cfg).unwrap();
let snap = state.snapshot().expect("empty state still snapshots");
let mut fresh = InferenceState::from_config(&cfg).unwrap();
fresh.restore(&snap);
assert_eq!(fresh.seq_len, 0);
}
/// Compressed state now snapshots into the
/// `LayerSnapshot::AttentionCompressed { keys, values }` variant
/// (was `None` before this PR). The encoded blobs round-trip
/// through `restore` byte-equal to the source cache.
#[test]
fn snapshot_compressed_state_emits_attention_compressed() {
let cfg = tiny_config(2, 8);
let mut state = InferenceState::from_config(&cfg).unwrap();
// Layer 0 is an attention layer per `tiny_config`. Populate
// both compressed_keys and compressed_values with a synthetic
// 1-token compressed cache.
if let LayerState::Attention {
compressed_keys,
compressed_values,
..
} = &mut state.layers[0]
{
let mut keys = CompressedKeyCache::new(2, 8, 4);
let mut values = CompressedValueCache::new(2, 8, 4);
for h in 0..2 {
keys.append(h, &[0xAB, 0xCD], &[0x55], 0x1234, 0x5678);
values.append(h, &[0xEF, 0x01], 0x9ABC);
}
*compressed_keys = Some(keys);
*compressed_values = Some(values);
}
state.seq_len = 1;
assert!(state.is_compressed());
let snap = state.snapshot().expect("compressed state must snapshot");
match &snap.layers[0] {
LayerSnapshot::AttentionCompressed { keys, values } => {
assert!(keys.starts_with(b"TQK1"));
assert!(values.starts_with(b"TQV1"));
}
_ => panic!("layer 0 should be AttentionCompressed"),
}
// Restore into a fresh state and assert the polar/jl bytes
// and norms match the original.
let mut fresh = InferenceState::from_config(&cfg).unwrap();
// `from_config` (uncompressed) doesn't allocate
// `compressed_keys` slots; manually wire empty caches so
// `restore`'s `Some(_)` write target exists.
if let LayerState::Attention {
compressed_keys,
compressed_values,
..
} = &mut fresh.layers[0]
{
*compressed_keys = Some(CompressedKeyCache::new(2, 8, 4));
*compressed_values = Some(CompressedValueCache::new(2, 8, 4));
}
fresh.restore(&snap);
match (&state.layers[0], &fresh.layers[0]) {
(
LayerState::Attention {
compressed_keys: Some(orig_k),
compressed_values: Some(orig_v),
..
},
LayerState::Attention {
compressed_keys: Some(restored_k),
compressed_values: Some(restored_v),
..
},
) => {
assert_eq!(restored_k.polar_data, orig_k.polar_data);
assert_eq!(restored_k.jl_data, orig_k.jl_data);
assert_eq!(restored_k.norms, orig_k.norms);
assert_eq!(restored_k.residual_norms, orig_k.residual_norms);
assert_eq!(restored_v.polar_data, orig_v.polar_data);
assert_eq!(restored_v.norms, orig_v.norms);
// f32 caches are recomputed at decode — must match.
assert_eq!(restored_k.norms_f32, orig_k.norms_f32);
assert_eq!(restored_v.norms_f32, orig_v.norms_f32);
}
_ => panic!("expected both states to have populated compressed caches"),
}
assert_eq!(fresh.seq_len, state.seq_len);
}
#[test]
fn conv_history_ring_buffer_and_rollback() {
let buf_len = 16;
let mut history = ConvHistory::new(buf_len);
let mut buf = vec![0.0f32; buf_len];
// Push 10 snapshots with recognizable values
for pos in 1..=10 {
buf.fill(pos as f32);
history.push(pos, &buf);
}
// Roll back to pos = 5
let mut restored = vec![0.0f32; buf_len];
assert!(history.rollback_to(5, &mut restored));
assert_eq!(restored, vec![5.0f32; buf_len]);
// Roll back to pos = 0 (clears buffer)
assert!(history.rollback_to(0, &mut restored));
assert_eq!(restored, vec![0.0f32; buf_len]);
// Roll back to non-existent position fails cleanly
assert!(!history.rollback_to(99, &mut restored));
}
#[test]
fn conv_history_ring_buffer_wraparound() {
let buf_len = 8;
let mut history = ConvHistory::new(buf_len);
let mut buf = vec![0.0f32; buf_len];
// Push 100 snapshots (exceeding CONV_HISTORY_CAPACITY = 64)
for pos in 1..=100 {
buf.fill(pos as f32);
history.push(pos, &buf);
}
let mut restored = vec![0.0f32; buf_len];
// Positions within the latest 64 steps (37..=100) must be retrievable
assert!(history.rollback_to(100, &mut restored));
assert_eq!(restored, vec![100.0f32; buf_len]);
assert!(history.rollback_to(50, &mut restored));
assert_eq!(restored, vec![50.0f32; buf_len]);
// Older positions that fell out of the ring buffer (> 64 steps old) return false
assert!(!history.rollback_to(10, &mut restored));
}
#[test]
fn semantic_anchor_lookup_and_boundary_tags() {
let cfg = tiny_config(2, 8);
let mut cache = KvPrefixCache::new(
KvCacheConfig {
cache_dir: None,
..KvCacheConfig::default()
},
&cfg,
"test_anchor",
);
let snapshot = |seq_len: usize| StateSnapshot::new(Vec::new(), seq_len);
let tokens = [10u32, 20, 30, 40, 50, 60, 70];
// Insert non-anchor prefix at len 5
cache.insert(&tokens[..5], snapshot(5));
// Insert semantic anchor at len 3 (e.g. Turn boundary)
cache.insert_anchor(
&tokens[..3],
snapshot(3),
1,
SemanticBoundaryKind::Turn,
0x12345678,
);
// find_deepest_semantic_anchor finds the anchor at len 3
let (snap, len) = cache
.find_deepest_semantic_anchor(&tokens)
.expect("must find anchor");
assert_eq!(len, 3);
assert_eq!(snap.anchor_depth, 1);
assert_eq!(snap.boundary_kind, SemanticBoundaryKind::Turn as u8);
assert_eq!(snap.semantic_hash, 0x12345678);
// Standard find_longest_prefix still prefers the longest prefix (len 5)
let (_, longest_len) = cache
.find_longest_prefix(&tokens)
.expect("must find longest prefix");
assert_eq!(longest_len, 5);
}
#[cfg(feature = "disk-cache")]
#[test]
fn flatbuffers_v2_disk_roundtrip_with_anchors_and_atomic_tmp() {
let cfg = tiny_config(2, 16);
let dir = std::env::temp_dir().join(format!("cera_cold_v2_{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
let cache = KvPrefixCache::new(KvCacheConfig::default(), &cfg, "cpu:test_v2");
let snap = StateSnapshot::new(
vec![
LayerSnapshot::Attention {
k_data: vec![1, 2, 3, 4],
v_data: vec![5, 6, 7, 8],
},
LayerSnapshot::Conv {
buffer: vec![9, 10, 11, 12],
},
],
4,
)
.with_anchor(2, SemanticBoundaryKind::ToolCall, 0xdeadbeef)
.with_shift_offset(0);
let tokens = [100u32, 101, 102, 103];
cache.save_cold(&dir, &tokens, &snap);
// Verify the file was written atomically and no orphan .tmp remains
let entries: Vec<_> = std::fs::read_dir(&dir)
.unwrap()
.filter_map(|e| e.ok())
.collect();
assert_eq!(entries.len(), 1);
assert!(
entries[0]
.file_name()
.to_string_lossy()
.ends_with(".kvcache")
);
// Load from disk and verify v2 schema fields
let loaded = cache
.find_cold_prefix(&dir, &[100, 101, 102, 103, 104])
.expect("must load v2 snapshot from disk");
assert_eq!(loaded.seq_len, 4);
assert_eq!(loaded.anchor_depth, 2);
assert_eq!(loaded.boundary_kind, SemanticBoundaryKind::ToolCall as u8);
assert_eq!(loaded.semantic_hash, 0xdeadbeef);
assert_eq!(loaded.shift_offset, 0);
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn test_conv_history_restore_and_truncate_safety() {
let mut conv = ConvHistory::new(4);
let buf = [1.0f32, 2.0, 3.0, 4.0];
conv.push(10, &buf);
assert!(conv.has_pos(10));
assert!(!conv.has_pos(5));
assert!(conv.has_pos(0));
let mut out = [0.0f32; 4];
assert!(conv.rollback_to(10, &mut out));
assert_eq!(out, buf);
// When rolled back beyond capacity (>64 tokens), has_pos returns false
conv.clear();
for pos in 1..=70 {
conv.push(pos, &[pos as f32; 4]);
}
assert!(!conv.has_pos(5)); // pos 5 was evicted from capacity 64
assert!(conv.has_pos(70));
}
}