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
// GPU-accelerated LFM2 forward pass using wgpu compute shaders.
//
// All weights are dequantized to f32 at load time and uploaded to GPU buffers.
// The full forward pass runs in a single CommandEncoder per token — only the
// logits vector is read back to CPU.
use std::sync::Mutex;
use std::sync::atomic::{AtomicUsize, Ordering};
use anyhow::Result;
use crate::backend::cpu::RopeType;
use crate::backend::wgpu::{GpuContext, GpuTensor, shaders};
use crate::gguf::GgufFile;
use crate::kv_cache::{InferenceState, KvPrefixCache, LayerSnapshot, StateSnapshot};
use crate::model::gpu_weight_source::GpuWeightSource;
use crate::model::transformer::WeightRef;
use crate::model::{BlockType, Model, ModelConfig, ScalarMultipliers};
use crate::tensor::DType;
/// Maximum N for a single batched-prefill dispatch. Mirrors the Metal
/// backend's `MAX_PREFILL_TOKENS = 512`. Prompts longer than this are
/// chunked at the host side; each chunk shares the same prefill batch
/// scratch, so the worst-case scratch footprint is bounded.
const MAX_PREFILL_TOKENS: usize = 512;
// Tile geometry for the register-tiled matmul pipeline. The shader
// receives these via preprocessor #defines below; keeping a single
// source of truth here means dispatch geometry can never drift out of
// sync with the kernel.
const MUL_MAT_TILE_WG_M: u32 = 8;
const MUL_MAT_TILE_WG_N: u32 = 32;
const MUL_MAT_TILE_M: u32 = 4;
const MUL_MAT_TILE_N: u32 = 1;
const MUL_MAT_TILE_K: u32 = 32;
/// Build a `mul_mat_reg_tile` pipeline for the requested variant.
/// `use_vec` enables vec4 loads/stores (requires the matrix dimensions and
/// effective row strides used by each dispatch to be multiples of 4).
fn build_mul_mat_pipeline(ctx: &GpuContext, label: &str, use_vec: bool) -> wgpu::ComputePipeline {
let wg_m = format!("{MUL_MAT_TILE_WG_M}u");
let wg_n = format!("{MUL_MAT_TILE_WG_N}u");
let tile_m = format!("{MUL_MAT_TILE_M}u");
let tile_n = format!("{MUL_MAT_TILE_N}u");
let tile_k = format!("{MUL_MAT_TILE_K}u");
let variant = if use_vec { "VEC" } else { "SCALAR" };
ctx.create_pipeline_with_defines(
shaders::MUL_MAT_REG_TILE,
"main",
label,
&[
(variant, ""),
("SRC0_INNER_TYPE", "u32"),
("SRC1_INNER_TYPE", "f32"),
("INIT_SRC0_SHMEM_Q4_0", ""),
("INIT_SRC1_SHMEM_FLOAT", ""),
("WORKGROUP_SIZE_M", &wg_m),
("WORKGROUP_SIZE_N", &wg_n),
("TILE_M", &tile_m),
("TILE_N", &tile_n),
("TILE_K", &tile_k),
],
)
}
fn gcd_u64(mut a: u64, mut b: u64) -> u64 {
while b != 0 {
let r = a % b;
a = b;
b = r;
}
a
}
fn lcm_u64(a: u64, b: u64) -> u64 {
(a / gcd_u64(a, b)) * b
}
fn f32_gemv_tile_rows(m: u32, k: u32, max_binding: u64, offset_alignment: u64) -> u32 {
const ROWS_PER_WG: u64 = 8;
let row_bytes = u64::from(k) * 4;
let full_bytes = u64::from(m) * row_bytes;
if full_bytes <= max_binding {
return m;
}
let max_rows = (max_binding / row_bytes) as u32;
assert!(
max_rows > 0,
"GPU max storage binding size {} is too small for one f32 GEMV row of {} bytes",
max_binding,
row_bytes
);
let offset_alignment = offset_alignment.max(4);
let row_alignment = (offset_alignment / gcd_u64(row_bytes, offset_alignment)).max(1) as u32;
let tile_alignment = lcm_u64(u64::from(row_alignment), ROWS_PER_WG) as u32;
let tile_rows = if max_rows >= tile_alignment {
max_rows - (max_rows % tile_alignment)
} else if max_rows >= row_alignment {
max_rows - (max_rows % row_alignment)
} else {
max_rows
};
assert!(
tile_rows > 0 && (u64::from(tile_rows) * row_bytes) % offset_alignment == 0,
"GPU storage binding alignment {} cannot be satisfied for f32 GEMV rows of {} bytes",
offset_alignment,
row_bytes
);
tile_rows
}
/// A weight matrix on GPU — tracks buffer + dtype + pre-allocated params for dispatch.
struct GpuWeight {
tensor: GpuTensor,
/// Pre-allocated params buffer with [m, k, row_base, 0] — eliminates per-dispatch allocation.
params_buf: wgpu::Buffer,
/// Pre-created bind group for this weight's primary GEMV dispatch.
/// Created after all scratch buffers are allocated, to avoid per-token
/// create_bind_group overhead (~16 µs each, 300×/token = 4.8 ms).
cached_bg: Option<wgpu::BindGroup>,
}
/// GPU buffer handles for one layer's weights.
/// Q4_0/Q8_0 weights are uploaded quantized; f32 norms uploaded as-is.
struct GpuLayerWeights {
attn_norm: wgpu::Buffer,
ffn_norm: wgpu::Buffer,
ffn_gate: GpuWeight,
ffn_up: GpuWeight,
ffn_down: GpuWeight,
// Conv-specific
conv_in_proj: Option<GpuWeight>,
conv_out_proj: Option<GpuWeight>,
conv_weight: Option<wgpu::Buffer>,
// Attention-specific
attn_q: Option<GpuWeight>,
attn_k: Option<GpuWeight>,
attn_v: Option<GpuWeight>,
attn_output: Option<GpuWeight>,
attn_q_norm: Option<wgpu::Buffer>,
attn_k_norm: Option<wgpu::Buffer>,
// Qwen2 Q/K/V projection biases (f32), added after each projection GEMV.
// `None` for archs without QKV bias.
attn_q_bias: Option<wgpu::Buffer>,
attn_k_bias: Option<wgpu::Buffer>,
attn_v_bias: Option<wgpu::Buffer>,
}
/// Compute pipelines for all shader entry points.
#[allow(dead_code)]
struct GpuPipelines {
gemv_f32: wgpu::ComputePipeline,
gemv_q4_0: wgpu::ComputePipeline,
gemv_q4_0_fast: wgpu::ComputePipeline,
gemv_q6_k: wgpu::ComputePipeline,
gemv_q8_0: wgpu::ComputePipeline,
add_inplace: wgpu::ComputePipeline,
/// Residual add with a scalar on the addend (`a += s*b`). Used for the
/// attention/FFN residual adds so Granite's residual multiplier folds in;
/// `s = 1.0` for every other arch.
scaled_add_inplace: wgpu::ComputePipeline,
/// In-place scale by a constant (`a *= s`). Granite logit/residual scalars.
scale_f32: wgpu::ComputePipeline,
mul_inplace: wgpu::ComputePipeline,
silu_mul_inplace: wgpu::ComputePipeline,
rmsnorm: wgpu::ComputePipeline,
per_head_rmsnorm: wgpu::ComputePipeline,
softmax: wgpu::ComputePipeline,
rope: wgpu::ComputePipeline,
attention: wgpu::ComputePipeline,
conv1d_fused: wgpu::ComputePipeline,
argmax_f32: wgpu::ComputePipeline,
// ── Batched-prefill pipelines ─────────────────────────────────────
rmsnorm_batch: wgpu::ComputePipeline,
add_rmsnorm_batch: wgpu::ComputePipeline,
qk_norm_rope_batch: wgpu::ComputePipeline,
conv1d_fused_batch: wgpu::ComputePipeline,
mul_mat_reg_tile_q4_0_vec: wgpu::ComputePipeline,
mul_mat_reg_tile_q4_0_scalar: wgpu::ComputePipeline,
gemm_q8_0: wgpu::ComputePipeline,
attention_prefill: wgpu::ComputePipeline,
}
/// GPU-resident inference state (KV cache + conv rolling buffers).
#[allow(dead_code)]
struct GpuState {
/// Per attention layer: (key_cache, value_cache) buffers, pre-allocated.
kv_caches: Vec<Option<(wgpu::Buffer, wgpu::Buffer)>>,
/// Per conv layer: rolling buffer.
conv_buffers: Vec<Option<wgpu::Buffer>>,
seq_len: AtomicUsize,
max_seq_len: usize,
/// Pre-dequantized embedding rows (CPU-side cache for fast lookup).
embedding_f32: Vec<f32>,
}
/// GPU-accelerated LFM2 model.
///
/// NOTE: This model is stateful — KV caches and conv rolling buffers live on
/// the GPU and persist across forward() calls. This is inherent to GPU backends
/// (GPU-resident state can't live in the CPU-side InferenceState). Consequence:
/// one GpuLfm2Model instance = one session for throughput. The internal
/// `infer_lock` makes the backend self-defending: two `Session`s sharing this
/// `Arc<dyn Model>` and running `forward()` / `forward_prefill()` concurrently
/// will serialize cleanly on the lock instead of racing on per-instance scratch
/// buffers + GPU KV caches. For genuine throughput across concurrent Sessions,
/// create multiple model instances.
pub struct GpuLfm2Model {
ctx: GpuContext,
config: ModelConfig,
pipelines: GpuPipelines,
// GPU weight buffers
embedding: wgpu::Buffer,
#[allow(dead_code)]
embedding_params: wgpu::Buffer,
/// Separate output projection (`output.weight`), dequantized to f32, when
/// the model has untied embeddings. `None` ⇒ the logit projection reuses
/// `embedding` (tied embeddings — LFM2, Qwen, Llama-3.2, Granite).
output_weight: Option<wgpu::Buffer>,
output_norm: wgpu::Buffer,
layers: Vec<GpuLayerWeights>,
/// RoPE pair layout for this model (`Neox` LFM2/Qwen, `Norm` Llama family).
rope_type: RopeType,
/// Granite 3.x scalar multipliers (identity for every other arch). The
/// embedding multiplier is pre-folded into `gpu_state.embedding_f32`; the
/// residual/attention/logit multipliers are applied during the forward pass.
scalars: ScalarMultipliers,
/// Whether the batched-prefill GPU path is enabled (LFM2 only today; the
/// dense transformers prefill via the per-token decode loop).
batched_prefill: bool,
/// Llama-3 RoPE frequency factors (`rope_freqs.weight`), or a 1-element
/// dummy when the model uses plain RoPE. Always bound (binding 3) on the
/// decode rope dispatch; `has_freq_factors` in `rope_params` gates its use.
rope_freqs_buf: wgpu::Buffer,
has_freq_factors: bool,
// GPU scratch buffers (reused across layers)
hidden_buf: wgpu::Buffer, // [hidden_size]
normed_buf: wgpu::Buffer, // [hidden_size]
ffn_input_buf: wgpu::Buffer, // [hidden_size]
gate_buf: wgpu::Buffer, // [intermediate_size]
up_buf: wgpu::Buffer, // [intermediate_size]
out_buf: wgpu::Buffer, // [hidden_size]
q_buf: wgpu::Buffer, // [hidden_size]
k_buf: wgpu::Buffer, // [max_kv_dim]
v_buf: wgpu::Buffer, // [max_kv_dim]
attn_out_buf: wgpu::Buffer, // [hidden_size]
logits_buf: wgpu::Buffer, // [vocab_size]
scores_buf: wgpu::Buffer, // [n_heads × max_seq_len]
/// 4 bytes — receives argmax(logits) as a single u32. Cached so
/// `forward_greedy` doesn't allocate per call. The `download_u32`
/// readback over this 4-byte buffer is the wasm-async-friendly
/// replacement for downloading `vocab_size * 4` bytes of logits.
argmax_out_buf: wgpu::Buffer,
/// Pre-uploaded `vec2<u32>{ vocab_size, 0 }` for the argmax shader.
/// Held to keep the buffer alive for the cached `argmax_bg`'s
/// reference; not directly read after construction.
#[allow(dead_code)]
argmax_params: wgpu::Buffer,
/// Cached bind group for the argmax kernel — bindings never change
/// (logits_buf, argmax_out_buf, argmax_params), so build it once.
argmax_bg: wgpu::BindGroup,
// Pre-allocated shader params (avoids upload_storage per dispatch).
rmsnorm_hs_params: wgpu::Buffer, // [hs, eps_bits, 0, 0]
elementwise_hs_params: wgpu::Buffer, // [hs, 0]
elementwise_is_params: wgpu::Buffer, // [intermediate_size, 0]
/// `[n_heads*head_dim, 0]` — Q bias add length (= hs when head_dim=hs/n_heads).
elementwise_qdim_params: wgpu::Buffer,
/// `[n_kv_heads*head_dim, 0]` — K/V bias add length.
elementwise_kvdim_params: wgpu::Buffer,
/// `[hs, residual_scale_bits]` — addend scalar for the attention/FFN
/// residual `scaled_add_inplace` (Granite residual multiplier; 1.0 else).
residual_add_params: wgpu::Buffer,
/// `[vocab_size, (1/logit_scale)_bits]` — Granite logit-scale divide, applied
/// via `scale_f32` after the LM head. `None` when logit_scale == 1.0.
logit_scale_params: Option<wgpu::Buffer>,
conv1d_params: wgpu::Buffer, // [hs, kernel_size, d_conv, 0]
per_head_norm_params: wgpu::Buffer, // [head_dim, eps_bits, 0, 0]
// [pos, n_heads, n_kv_heads, head_dim, theta_bits, rope_type, has_freq_factors]
// — 7 u32, updated per token; must stay in sync with rope.wgsl's params array.
rope_params: wgpu::Buffer,
attn_params: wgpu::Buffer, // [n_heads, n_kv_heads, head_dim, kv_dim, seq_len, scale, 0, 0] — updated per token
gemv_f32_tile_params: Vec<wgpu::Buffer>, // [rows, k, row_base, 0] per output-projection tile
// Conv scratch
conv_proj_buf: wgpu::Buffer, // [3 × hidden_size]
conv_gate_buf: wgpu::Buffer, // [hidden_size] — fused conv writes here, out_proj reads
// ── Batched-prefill scratch (sized to MAX_PREFILL_TOKENS rows) ────────
// Mirrors MetalLfm2Model's prefill_*_buf set. Used only by the batched
// prefill path; the per-token forward path keeps using the scalar
// scratch buffers above.
/// `[MAX_PREFILL_TOKENS × hidden_size]` — running residual-stream
/// activation across layers. Last token's slice is the final input
/// to the output norm/projection.
prefill_batch_buf: wgpu::Buffer,
/// `[MAX_PREFILL_TOKENS × hidden_size]` — post-rmsnorm activations,
/// also reused as the attention output sink and as the conv1d output.
prefill_normed_buf: wgpu::Buffer,
/// `[MAX_PREFILL_TOKENS × 3 × hidden_size]` — sized to fit the
/// largest batched projection. For attention layers it's split into
/// Q (offset 0, stride hs); the K/V projections land in the gate/up
/// scratches because `mul_mat_reg_tile` writes contiguous token rows. For conv
/// layers the full `3 × hs` slab is the in-projection target.
prefill_proj_buf: wgpu::Buffer,
/// `[MAX_PREFILL_TOKENS × intermediate_size]` — FFN gate output;
/// also reused as scratch for K projections and per-(layer,FFN)
/// add-residual targets.
prefill_gate_buf: wgpu::Buffer,
/// `[MAX_PREFILL_TOKENS × intermediate_size]` — FFN up output;
/// also reused as scratch for V projections.
prefill_up_buf: wgpu::Buffer,
/// `[MAX_PREFILL_TOKENS × n_heads × max_seq_len]` — per-(query,
/// head) scratch slab consumed by `attention_prefill.wgsl`.
/// Allocated once; sized to the worst case per the model config.
prefill_scores_buf: wgpu::Buffer,
// GPU state
gpu_state: GpuState,
/// Serializes Model trait calls on this instance. Without it, two
/// `Session`s sharing this `Arc<dyn Model>` and running `forward()` /
/// `forward_prefill()` concurrently would race on the per-instance
/// scratch buffers (`hidden_buf`, `q_buf`, `k_buf`, etc.) and on the
/// GPU KV caches in `gpu_state`. Mirrors the equivalent guard on
/// `MetalLfm2Model`. Lock cost is ~50 ns uncontended (negligible vs
/// wgpu dispatch); the wgpu queue already serializes GPU work — this
/// just synchronizes the CPU-side bookkeeping that stages each
/// command encoder and reads back logits.
infer_lock: Mutex<()>,
/// Caller-supplied identifier (typically the GGUF file path) used to
/// namespace prefix-cache disk files. Prefixed with `"wgpu:"` before
/// being fed to `model_fingerprint` so wgpu's f32 disk-cache files
/// don't collide with Metal's f16 nor CPU's f32 ones at the same
/// model path. CPU's f32 layout matches wgpu's, but the CPU model's
/// own internal state shape (InferenceState-backed) differs from
/// the GPU-resident state, so cross-loading isn't safe even when
/// the byte format would line up — the prefix tag enforces backend
/// separation cleanly.
model_id: String,
/// Two-tier prefix cache (warm in-memory + cold on-disk via
/// FlatBuffers). Replaced wholesale by `Model::configure_cache`.
/// Defaults to `KvCacheConfig::default()` (warm-only) at
/// construction time so warm hits work without explicit config.
prefix_cache: Mutex<KvPrefixCache>,
}
impl GpuLfm2Model {
/// Construct without a model identifier. Equivalent to
/// `from_gguf_with_id(gguf, context_size, "")`. Warm prefix cache
/// works after `Model::configure_cache`; disk cache (when
/// configured) would namespace-collide between path-less loads of
/// different models.
pub fn from_gguf(gguf: GgufFile, context_size: usize) -> Result<Self> {
Self::from_gguf_with_id(gguf, context_size, String::new())
}
/// Construct with an explicit model identifier (typically the GGUF
/// path) used to namespace prefix-cache disk files. The id is
/// prefixed with `"wgpu:"` before being fed to `model_fingerprint`
/// so different backends (cpu / metal / wgpu) sharing a
/// `--cache-dir` don't collide on file names — see CPU's `"cpu:"`
/// in PR #119 for the same pattern.
pub fn from_gguf_with_id(
gguf: GgufFile,
context_size: usize,
model_id: String,
) -> Result<Self> {
let cpu_model = super::lfm2::Lfm2Model::from_gguf(gguf, context_size)?;
Self::from_weight_source(&cpu_model, context_size, model_id)
}
/// Construct a GPU model for a dense transformer (Qwen2/Qwen3/LLaMA/
/// Mistral/Granite) — the `LlamaModel` family. Mirrors `from_gguf_with_id`
/// but feeds the shared loader a `LlamaModel` weight source instead of
/// `Lfm2Model`. The GPU forward path is arch-generic; per-arch behavior
/// (NEOX/NORM rope, QK-norm, QKV bias, untied output, Granite scalars) is
/// driven by the `GpuWeightSource` accessors + `config`.
pub fn from_llama_with_id(
gguf: GgufFile,
context_size: usize,
model_id: String,
) -> Result<Self> {
let cpu_model =
super::llama::LlamaModel::from_gguf_with_id(gguf, context_size, model_id.clone())?;
Self::from_weight_source(&cpu_model, context_size, model_id)
}
/// Generalized GPU loader over any [`GpuWeightSource`]. Uploads weights,
/// builds pipelines + scratch, and wires the arch-specific knobs. The
/// concrete CPU model (`Lfm2Model` / `LlamaModel`) is only borrowed here
/// for its weights/metadata; it is dropped on return.
fn from_weight_source(
src: &dyn GpuWeightSource,
context_size: usize,
model_id: String,
) -> Result<Self> {
let ctx = GpuContext::new()?;
// The CPU loader already caps max_seq_len to context_size internally,
// so the second .min() below is redundant but kept for clarity.
let mut config = src.config().clone();
let max_seq_len = context_size.min(config.max_seq_len);
config.max_seq_len = max_seq_len;
let hs = config.hidden_size;
let is = config.intermediate_size;
// head_dim is decoupled from hidden/n_heads (Qwen3 sets it explicitly),
// so size Q/K/V/attn-out buffers by config.head_dim, not hs/n_heads.
let head_dim = config.head_dim;
let q_dim = config.n_heads * head_dim;
let max_kv_dim = config.kv_heads_per_layer.iter().copied().max().unwrap_or(0) * head_dim;
let rope_type = src.rope_type();
let scalars = config.scalars;
let batched_prefill = src.supports_batched_prefill();
tracing::info!(
"GPU model: {} layers, hs={hs}, is={is}, vocab={}",
config.n_layers,
config.vocab_size
);
// Create pipelines
let pipelines = GpuPipelines {
gemv_f32: ctx.create_pipeline(shaders::GEMV_F32, "gemv_f32", "gemv_f32"),
gemv_q4_0: ctx.create_pipeline(shaders::GEMV_Q4_0, "gemv_q4_0", "gemv_q4_0"),
gemv_q4_0_fast: ctx.create_pipeline(
shaders::GEMV_Q4_0_FAST,
"gemv_q4_0_fast",
"gemv_q4_0_fast",
),
gemv_q6_k: ctx.create_pipeline(shaders::GEMV_Q6_K, "gemv_q6_k", "gemv_q6_k"),
gemv_q8_0: ctx.create_pipeline(shaders::GEMV_Q8_0, "gemv_q8_0", "gemv_q8_0"),
add_inplace: ctx.create_pipeline(shaders::ELEMENTWISE, "add_inplace", "add"),
scaled_add_inplace: ctx.create_pipeline(
shaders::ELEMENTWISE,
"scaled_add_inplace",
"scaled_add",
),
scale_f32: ctx.create_pipeline(shaders::SCALE_F32, "scale_f32", "scale_f32"),
mul_inplace: ctx.create_pipeline(shaders::ELEMENTWISE, "mul_inplace", "mul"),
silu_mul_inplace: ctx.create_pipeline(
shaders::ELEMENTWISE,
"silu_mul_inplace",
"silu_mul",
),
rmsnorm: ctx.create_pipeline(shaders::RMSNORM, "rmsnorm", "rmsnorm"),
per_head_rmsnorm: ctx.create_pipeline(
shaders::PER_HEAD_RMSNORM,
"per_head_rmsnorm",
"per_head_rmsnorm",
),
softmax: ctx.create_pipeline(shaders::SOFTMAX, "softmax", "softmax"),
rope: ctx.create_pipeline(shaders::ROPE, "rope", "rope"),
attention: ctx.create_pipeline(shaders::ATTENTION, "attention", "attention"),
conv1d_fused: ctx.create_pipeline(
shaders::CONV1D_FUSED,
"conv1d_fused",
"conv1d_fused",
),
argmax_f32: ctx.create_pipeline(shaders::ARGMAX_F32, "argmax_f32", "argmax_f32"),
rmsnorm_batch: ctx.create_pipeline(
shaders::RMSNORM_BATCH,
"rmsnorm_batch",
"rmsnorm_batch",
),
add_rmsnorm_batch: ctx.create_pipeline(
shaders::RMSNORM_BATCH,
"add_rmsnorm_batch",
"add_rmsnorm_batch",
),
qk_norm_rope_batch: ctx.create_pipeline(
shaders::QK_NORM_ROPE_BATCH,
"qk_norm_rope_batch",
"qk_norm_rope_batch",
),
conv1d_fused_batch: ctx.create_pipeline(
shaders::CONV1D_FUSED_BATCH,
"conv1d_fused_batch",
"conv1d_fused_batch",
),
mul_mat_reg_tile_q4_0_vec: build_mul_mat_pipeline(&ctx, "mul_mat_q4_0_vec", true),
mul_mat_reg_tile_q4_0_scalar: build_mul_mat_pipeline(
&ctx,
"mul_mat_q4_0_scalar",
false,
),
gemm_q8_0: ctx.create_pipeline(shaders::GEMM_Q8_0, "gemm_q8_0", "gemm_q8_0"),
attention_prefill: ctx.create_pipeline(
shaders::ATTENTION_PREFILL,
"attention_prefill",
"attention_prefill",
),
};
// Upload weights: Q4_0/Q8_0 stay quantized, others dequantized to f32.
let emb_tensor = src.gguf().get_tensor("token_embd.weight")?;
// The GPU `embedding` buffer feeds the (tied) logit projection and must
// stay UNSCALED. The CPU-side `embedding_f32` cache feeds the input
// embedding lookup; Granite's embedding multiplier is pre-folded into it
// (no-op for every other arch). Keeping the two copies separate means a
// tied-embedding Granite gets the scale on input only, exactly like the
// CPU LlamaModel (`scale_inplace` after `dequantize_row`).
let embedding_raw = emb_tensor.to_f32_vec();
let embedding = ctx.upload_f32(&embedding_raw, "token_embd.weight");
let mut embedding_f32 = embedding_raw;
if scalars.embedding != 1.0 {
for v in embedding_f32.iter_mut() {
*v *= scalars.embedding;
}
}
let embedding_params = ctx.upload_storage(
bytemuck::cast_slice(&[
config.vocab_size as u32,
config.hidden_size as u32,
0u32,
0u32,
]),
"emb_params",
);
let output_norm = ctx.upload_f32(src.output_norm_weight(), "output_norm");
let upload_weight = |wref: &WeightRef, name: &str| -> GpuWeight {
let (buf, dtype) = if matches!(wref.dtype, DType::Q4_0 | DType::Q8_0) {
let data = src.weight_bytes(wref);
(ctx.upload_storage(data, name), wref.dtype)
} else {
// TODO: Upload as F16 to save bandwidth (requires F16-aware matmul shaders
// in Phase B.1). For now we dequantize all non-Q4_0 to F32.
let f32_data = src.dequantize_weight(wref);
(ctx.upload_f32(&f32_data, name), DType::F32)
};
let params_buf = ctx.upload_storage(
bytemuck::cast_slice(&[wref.m as u32, wref.k as u32, 0u32, 0u32]),
&format!("{name}.params"),
);
GpuWeight {
tensor: GpuTensor {
buffer: buf,
dtype,
shape: vec![wref.m, wref.k],
},
params_buf,
cached_bg: None,
}
};
// Optional per-head QK-norm (Qwen3) and QKV bias (Qwen2) upload helpers.
let upload_opt_f32 = |data: Option<&[f32]>, name: &str| -> Option<wgpu::Buffer> {
data.map(|d| ctx.upload_f32(d, name))
};
let mut layers = Vec::with_capacity(config.n_layers);
for i in 0..config.n_layers {
let attn_norm = ctx.upload_f32(src.attn_norm_weight(i), &format!("l{i}.anorm"));
let ffn_norm = ctx.upload_f32(src.ffn_norm_weight(i), &format!("l{i}.fnorm"));
let ffn_gate = upload_weight(src.ffn_gate_ref(i), &format!("l{i}.ffn_gate"));
let ffn_up = upload_weight(src.ffn_up_ref(i), &format!("l{i}.ffn_up"));
let ffn_down = upload_weight(src.ffn_down_ref(i), &format!("l{i}.ffn_down"));
let is_conv = config.block_types[i] == BlockType::GatedConv;
let (conv_in_proj, conv_out_proj, conv_weight) = if is_conv {
let ip = src.conv_in_proj_ref(i).expect("conv layer missing in_proj");
let op = src
.conv_out_proj_ref(i)
.expect("conv layer missing out_proj");
(
Some(upload_weight(ip, &format!("l{i}.conv_ip"))),
Some(upload_weight(op, &format!("l{i}.conv_op"))),
Some(ctx.upload_f32(
src.conv_weight(i).expect("conv layer missing conv weight"),
&format!("l{i}.conv_w"),
)),
)
} else {
(None, None, None)
};
// Attention weights. Plain transformers have every attention layer;
// LFM2 has them only on attention blocks. QK-norm (Qwen3) and QKV
// bias (Qwen2) are uploaded only when the source carries them.
let (attn_q, attn_k, attn_v, attn_output, attn_q_norm, attn_k_norm) = if !is_conv {
(
Some(upload_weight(
src.attn_q_ref(i).expect("attn layer missing q"),
&format!("l{i}.attn_q"),
)),
Some(upload_weight(
src.attn_k_ref(i).expect("attn layer missing k"),
&format!("l{i}.attn_k"),
)),
Some(upload_weight(
src.attn_v_ref(i).expect("attn layer missing v"),
&format!("l{i}.attn_v"),
)),
Some(upload_weight(
src.attn_output_ref(i).expect("attn layer missing output"),
&format!("l{i}.attn_o"),
)),
upload_opt_f32(src.attn_q_norm_weight(i), &format!("l{i}.qn")),
upload_opt_f32(src.attn_k_norm_weight(i), &format!("l{i}.kn")),
)
} else {
(None, None, None, None, None, None)
};
let attn_q_bias = upload_opt_f32(src.attn_q_bias(i), &format!("l{i}.qb"));
let attn_k_bias = upload_opt_f32(src.attn_k_bias(i), &format!("l{i}.kb"));
let attn_v_bias = upload_opt_f32(src.attn_v_bias(i), &format!("l{i}.vb"));
layers.push(GpuLayerWeights {
attn_norm,
ffn_norm,
ffn_gate,
ffn_up,
ffn_down,
conv_in_proj,
conv_out_proj,
conv_weight,
attn_q,
attn_k,
attn_v,
attn_output,
attn_q_norm,
attn_k_norm,
attn_q_bias,
attn_k_bias,
attn_v_bias,
});
}
// Untied output projection (`output.weight`), dequantized to f32 like
// the embedding table. `None` ⇒ tied embeddings (reuse `embedding`).
let output_weight = src
.output_ref()
.map(|wref| ctx.upload_f32(&src.dequantize_weight(wref), "output.weight"));
// Create scratch buffers
let f = |size: usize, name: &str| ctx.create_storage_rw((size * 4) as u64, name);
let hidden_buf = f(hs, "hidden");
let normed_buf = f(hs, "normed");
let ffn_input_buf = f(hs, "ffn_input");
let gate_buf = f(is, "gate");
let up_buf = f(is, "up");
let out_buf = f(hs, "out");
// Q and the attention output are sized by n_heads*head_dim (= q_dim),
// which exceeds hs when head_dim is decoupled (Qwen3). The out_proj maps
// q_dim → hs. K/V are sized by max_kv_heads*head_dim.
let q_buf = f(q_dim, "q");
let k_buf = f(max_kv_dim, "k");
let v_buf = f(max_kv_dim, "v");
let attn_out_buf = f(q_dim, "attn_out");
let logits_buf = f(config.vocab_size, "logits");
let scores_buf = f(config.n_heads * max_seq_len, "scores");
let conv_proj_buf = f(3 * hs, "conv_proj");
let conv_gate_buf = f(hs, "conv_gate");
// Batched-prefill scratch. Sized for the worst case of
// `MAX_PREFILL_TOKENS` rows; chunking on the host side keeps
// larger prompts within this footprint.
let max_pref = max_seq_len.min(MAX_PREFILL_TOKENS);
let prefill_batch_buf = f(hs * max_pref, "prefill_batch");
let prefill_normed_buf = f(hs * max_pref, "prefill_normed");
let prefill_proj_buf = f(3 * hs * max_pref, "prefill_proj");
let prefill_gate_buf = f(is * max_pref, "prefill_gate");
let prefill_up_buf = f(is * max_pref, "prefill_up");
// attention_prefill scratch: per-(query, head, time) f32 slab.
let prefill_scores_buf = f(max_pref * config.n_heads * max_seq_len, "prefill_scores");
// Initialize GPU KV caches + conv buffers
let kernel_size = config.conv_kernel_size.unwrap_or(3);
let d_conv = kernel_size - 1;
let mut kv_caches = Vec::with_capacity(config.n_layers);
let mut conv_buffers = Vec::with_capacity(config.n_layers);
for i in 0..config.n_layers {
if config.block_types[i] == BlockType::Attention {
let kv_dim = config.kv_heads_per_layer[i] * head_dim;
let k_cache = f(max_seq_len * kv_dim, &format!("l{i}.k_cache"));
let v_cache = f(max_seq_len * kv_dim, &format!("l{i}.v_cache"));
kv_caches.push(Some((k_cache, v_cache)));
conv_buffers.push(None);
} else {
kv_caches.push(None);
let cb = f(d_conv * hs, &format!("l{i}.conv_buf"));
conv_buffers.push(Some(cb));
}
}
let gpu_state = GpuState {
kv_caches,
conv_buffers,
seq_len: AtomicUsize::new(0),
max_seq_len,
embedding_f32,
};
// Pre-allocate shader params buffers (avoids upload_storage per dispatch).
let rmsnorm_hs_params = ctx.upload_storage(
bytemuck::cast_slice(&[hs as u32, config.rms_norm_eps.to_bits(), 0u32, 0u32]),
"rmsnorm_hs_params",
);
let elementwise_hs_params =
ctx.upload_storage(bytemuck::cast_slice(&[hs as u32, 0u32]), "ew_hs_params");
let elementwise_is_params =
ctx.upload_storage(bytemuck::cast_slice(&[is as u32, 0u32]), "ew_is_params");
// QKV-bias add lengths (Qwen2). q_dim == hs unless head_dim is decoupled.
let kv_dim_bias = config.n_kv_heads * head_dim;
let elementwise_qdim_params = ctx.upload_storage(
bytemuck::cast_slice(&[q_dim as u32, 0u32]),
"ew_qdim_params",
);
let elementwise_kvdim_params = ctx.upload_storage(
bytemuck::cast_slice(&[kv_dim_bias as u32, 0u32]),
"ew_kvdim_params",
);
// Residual add scalar (Granite residual multiplier; 1.0 elsewhere).
let residual_add_params = ctx.upload_storage(
bytemuck::cast_slice(&[hs as u32, scalars.residual.to_bits()]),
"residual_add_params",
);
// Granite logit divide: scale by 1/logit_scale. None when identity.
let logit_scale_params = (scalars.logit != 1.0).then(|| {
ctx.upload_storage(
bytemuck::cast_slice(&[config.vocab_size as u32, (1.0 / scalars.logit).to_bits()]),
"logit_scale_params",
)
});
let kernel_size = config.conv_kernel_size.unwrap_or(3) as u32;
let d_conv = kernel_size - 1;
let head_dim_u32 = head_dim as u32;
let conv1d_params = ctx.upload_storage(
bytemuck::cast_slice(&[hs as u32, kernel_size, d_conv, 0u32]),
"conv1d_params",
);
let per_head_norm_params = ctx.upload_storage(
bytemuck::cast_slice(&[head_dim_u32, config.rms_norm_eps.to_bits(), 0u32, 0u32]),
"ph_norm_params",
);
// rope_params is updated per token via queue.write_buffer — needs COPY_DST.
// 7 u32: pos, n_heads, n_kv_heads, head_dim, freq_base_bits, rope_type,
// has_freq_factors.
let rope_params = ctx.create_storage_rw(7 * 4, "rope_params");
// Llama-3 RoPE frequency factors (binding 3 of the rope dispatch).
// Always bound; a 1-element dummy when the model uses plain RoPE.
let has_freq_factors = src.rope_freqs().is_some();
let rope_freqs_buf = match src.rope_freqs() {
Some(rf) => ctx.upload_f32(rf, "rope_freqs"),
None => ctx.upload_f32(&[1.0f32], "rope_freqs_dummy"),
};
let attn_params = ctx.create_storage_rw(8 * 4, "attn_params");
let gemv_f32_tile_rows = f32_gemv_tile_rows(
config.vocab_size as u32,
hs as u32,
ctx.max_storage_buffer_binding_size,
ctx.min_storage_buffer_offset_alignment,
);
let gemv_f32_tile_count = (config.vocab_size as u32).div_ceil(gemv_f32_tile_rows);
let mut gemv_f32_tile_params = Vec::with_capacity(gemv_f32_tile_count as usize);
for i in 0..gemv_f32_tile_count {
gemv_f32_tile_params
.push(ctx.create_storage_rw(4 * 4, &format!("gemv_f32_tile_params.{i}")));
}
// Argmax I/O buffers. `argmax_params` is uploaded once with
// vocab_size; `argmax_out_buf` is a 4-byte sink. Bind group is
// built after `pipelines` exists below.
let argmax_out_buf = ctx.create_storage_rw(4, "argmax_out");
let argmax_params = ctx.upload_storage(
bytemuck::cast_slice(&[config.vocab_size as u32, 0u32]),
"argmax_params",
);
let argmax_bg = ctx.device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("argmax_bg"),
layout: &pipelines.argmax_f32.get_bind_group_layout(0),
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: logits_buf.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: argmax_out_buf.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 2,
resource: argmax_params.as_entire_binding(),
},
],
});
// Build the prefix cache before constructing `Self` so we can
// borrow `&config` here without conflicting with the upcoming
// move of `config` into the struct literal.
let prefix_cache = Mutex::new(KvPrefixCache::new(
crate::kv_cache::KvCacheConfig::default(),
&config,
&format!("wgpu:{model_id}"),
));
let mut model = Self {
ctx,
config,
pipelines,
embedding,
embedding_params,
output_weight,
output_norm,
layers,
rope_type,
scalars,
batched_prefill,
rope_freqs_buf,
has_freq_factors,
hidden_buf,
normed_buf,
ffn_input_buf,
gate_buf,
up_buf,
out_buf,
q_buf,
k_buf,
v_buf,
attn_out_buf,
logits_buf,
scores_buf,
argmax_out_buf,
argmax_params,
argmax_bg,
rmsnorm_hs_params,
elementwise_hs_params,
elementwise_is_params,
elementwise_qdim_params,
elementwise_kvdim_params,
residual_add_params,
logit_scale_params,
conv1d_params,
per_head_norm_params,
rope_params,
attn_params,
gemv_f32_tile_params,
conv_proj_buf,
conv_gate_buf,
prefill_batch_buf,
prefill_normed_buf,
prefill_proj_buf,
prefill_gate_buf,
prefill_up_buf,
prefill_scores_buf,
gpu_state,
infer_lock: Mutex::new(()),
prefix_cache,
model_id,
};
model.cache_bind_groups();
Ok(model)
}
/// Create a GEMV bind group for a given (weight, input, output) triple.
fn make_gemv_bg(
&self,
w: &GpuWeight,
input: &wgpu::Buffer,
output: &wgpu::Buffer,
) -> wgpu::BindGroup {
let (pipeline, _, _) = self.gemv_pipeline_rows_label(w);
self.ctx
.device
.create_bind_group(&wgpu::BindGroupDescriptor {
label: None,
layout: &pipeline.get_bind_group_layout(0),
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: w.tensor.buffer.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: input.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 2,
resource: output.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 3,
resource: w.params_buf.as_entire_binding(),
},
],
})
}
fn gemv_pipeline_rows_label(
&self,
w: &GpuWeight,
) -> (&wgpu::ComputePipeline, u32, &'static str) {
// rows-per-workgroup MUST match each shader's `NR`/`ROWS_PER_WG`
// constant: gemv_q4_0_fast=4, gemv_q8_0=8, gemv_f32=8. A mismatch
// over-dispatches and the shaders bounds-check only writes, not
// weight reads, so a too-small value reads past the weight buffer.
match w.tensor.dtype {
DType::Q4_0 => (&self.pipelines.gemv_q4_0_fast, 4, "gemv_q4"),
DType::Q8_0 => (&self.pipelines.gemv_q8_0, 8, "gemv_q8"),
_ => (&self.pipelines.gemv_f32, 8, "gemv_f32"),
}
}
fn gemv_workgroups(&self, w: &GpuWeight) -> (u32, u32, u32) {
let (_, rows_per_wg, _) = self.gemv_pipeline_rows_label(w);
let rows = (w.tensor.shape[0] as u32).div_ceil(rows_per_wg);
(rows.min(65535), rows.div_ceil(65535), 1)
}
fn dispatch_gemv_into(
&self,
pass: &mut wgpu::ComputePass<'_>,
w: &GpuWeight,
bind_group: &wgpu::BindGroup,
) {
let (pipeline, _, _) = self.gemv_pipeline_rows_label(w);
self.dispatch_into(pass, pipeline, bind_group, self.gemv_workgroups(w));
}
/// Pre-create bind groups for all per-layer GEMV dispatches.
/// Eliminates ~150 create_bind_group calls per token (~2.4 ms CPU).
fn cache_bind_groups(&mut self) {
let cfg = &self.config;
for i in 0..cfg.n_layers {
// FFN
let gate_bg = self.make_gemv_bg(
&self.layers[i].ffn_gate,
&self.ffn_input_buf,
&self.gate_buf,
);
self.layers[i].ffn_gate.cached_bg = Some(gate_bg);
let up_bg =
self.make_gemv_bg(&self.layers[i].ffn_up, &self.ffn_input_buf, &self.up_buf);
self.layers[i].ffn_up.cached_bg = Some(up_bg);
let down_bg =
self.make_gemv_bg(&self.layers[i].ffn_down, &self.gate_buf, &self.out_buf);
self.layers[i].ffn_down.cached_bg = Some(down_bg);
if cfg.block_types[i] == BlockType::GatedConv {
if let Some(ref w) = self.layers[i].conv_in_proj {
let bg = self.make_gemv_bg(w, &self.normed_buf, &self.conv_proj_buf);
self.layers[i].conv_in_proj.as_mut().unwrap().cached_bg = Some(bg);
}
if let Some(ref w) = self.layers[i].conv_out_proj {
let bg = self.make_gemv_bg(w, &self.conv_gate_buf, &self.out_buf);
self.layers[i].conv_out_proj.as_mut().unwrap().cached_bg = Some(bg);
}
} else {
if let Some(ref w) = self.layers[i].attn_q {
let bg = self.make_gemv_bg(w, &self.normed_buf, &self.q_buf);
self.layers[i].attn_q.as_mut().unwrap().cached_bg = Some(bg);
}
if let Some(ref w) = self.layers[i].attn_k {
let bg = self.make_gemv_bg(w, &self.normed_buf, &self.k_buf);
self.layers[i].attn_k.as_mut().unwrap().cached_bg = Some(bg);
}
if let Some(ref w) = self.layers[i].attn_v {
let bg = self.make_gemv_bg(w, &self.normed_buf, &self.v_buf);
self.layers[i].attn_v.as_mut().unwrap().cached_bg = Some(bg);
}
if let Some(ref w) = self.layers[i].attn_output {
let bg = self.make_gemv_bg(w, &self.attn_out_buf, &self.out_buf);
self.layers[i].attn_output.as_mut().unwrap().cached_bg = Some(bg);
}
}
}
}
// ── GPU dispatch helpers ────────────────────────────────────────────
/// Encode a compute pass into the given encoder (batched, no submit).
fn encode(
&self,
enc: &mut wgpu::CommandEncoder,
pipeline: &wgpu::ComputePipeline,
bind_group: &wgpu::BindGroup,
workgroups: (u32, u32, u32),
label: &str,
) {
let ts = self.ctx.begin_profile_span(label);
{
let mut pass = enc.begin_compute_pass(&wgpu::ComputePassDescriptor {
label: Some(label),
timestamp_writes: ts,
});
pass.set_pipeline(pipeline);
pass.set_bind_group(0, bind_group, &[]);
pass.dispatch_workgroups(workgroups.0, workgroups.1, workgroups.2);
}
}
/// Dispatch into an existing compute pass (no pass creation overhead).
fn dispatch_into(
&self,
pass: &mut wgpu::ComputePass<'_>,
pipeline: &wgpu::ComputePipeline,
bind_group: &wgpu::BindGroup,
workgroups: (u32, u32, u32),
) {
pass.set_pipeline(pipeline);
pass.set_bind_group(0, bind_group, &[]);
pass.dispatch_workgroups(workgroups.0, workgroups.1, workgroups.2);
}
/// Submit encoder and wait for GPU to finish.
fn submit_and_wait(&self, enc: wgpu::CommandEncoder) {
self.ctx.queue.submit(Some(enc.finish()));
self.ctx.device.poll(wgpu::Maintain::Wait);
}
fn new_encoder(&self) -> wgpu::CommandEncoder {
self.ctx.device.create_command_encoder(&Default::default())
}
// ── Encode helpers (add passes to an existing encoder) ────────────
/// Encode GEMV dispatch — uses cached bind group if available, else creates one.
#[allow(dead_code)]
fn encode_gemv_weight(
&self,
enc: &mut wgpu::CommandEncoder,
w: &GpuWeight,
input: &wgpu::Buffer,
output: &wgpu::Buffer,
) {
let (pipeline, _, label) = self.gemv_pipeline_rows_label(w);
// Use cached BG if available (pre-created at init for known
// weight/input/output triples — saves ~16µs per dispatch).
let fresh_bg;
let bg = if let Some(ref cached) = w.cached_bg {
cached
} else {
fresh_bg = self.make_gemv_bg(w, input, output);
&fresh_bg
};
self.encode(enc, pipeline, bg, self.gemv_workgroups(w), label);
}
/// Encode f32 GEMV (for tied embeddings output projection which stays f32).
fn encode_gemv_f32(
&self,
enc: &mut wgpu::CommandEncoder,
weight: &wgpu::Buffer,
input: &wgpu::Buffer,
output: &wgpu::Buffer,
m: u32,
k: u32,
) {
let weight_bytes = u64::from(m) * u64::from(k) * 4;
let max_binding = self.ctx.max_storage_buffer_binding_size;
if weight_bytes > max_binding {
self.encode_gemv_f32_tiled(enc, weight, input, output, m, k);
return;
}
// Use pre-allocated params (m=vocab_size, k=hs are constant).
let params_buf = &self.embedding_params;
let bg = self
.ctx
.device
.create_bind_group(&wgpu::BindGroupDescriptor {
label: None,
layout: &self.pipelines.gemv_f32.get_bind_group_layout(0),
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: weight.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: input.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 2,
resource: output.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 3,
resource: params_buf.as_entire_binding(),
},
],
});
let groups = m.div_ceil(8);
self.encode(
enc,
&self.pipelines.gemv_f32,
&bg,
(groups.min(65535), groups.div_ceil(65535), 1),
"gemv_f32",
);
}
/// Encode f32 GEMV in row tiles for adapters with small
/// max_storage_buffer_binding_size limits. The tied embedding/output
/// projection can exceed those limits even though each row slice is legal.
fn encode_gemv_f32_tiled(
&self,
enc: &mut wgpu::CommandEncoder,
weight: &wgpu::Buffer,
input: &wgpu::Buffer,
output: &wgpu::Buffer,
m: u32,
k: u32,
) {
let row_bytes = u64::from(k) * 4;
let max_binding = self.ctx.max_storage_buffer_binding_size;
let tile_rows = f32_gemv_tile_rows(
m,
k,
max_binding,
self.ctx.min_storage_buffer_offset_alignment,
);
let layout = self.pipelines.gemv_f32.get_bind_group_layout(0);
let mut row_start = 0u32;
let mut tile_idx = 0usize;
while row_start < m {
let rows = (m - row_start).min(tile_rows);
let weight_offset = u64::from(row_start) * row_bytes;
let params_buf = self
.gemv_f32_tile_params
.get(tile_idx)
.expect("preallocated f32 GEMV tile params");
self.ctx.queue.write_buffer(
params_buf,
0,
bytemuck::cast_slice(&[rows, k, row_start, 0u32]),
);
let bg = self
.ctx
.device
.create_bind_group(&wgpu::BindGroupDescriptor {
label: None,
layout: &layout,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding {
buffer: weight,
offset: weight_offset,
size: wgpu::BufferSize::new(u64::from(rows) * row_bytes),
}),
},
wgpu::BindGroupEntry {
binding: 1,
resource: input.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 2,
resource: output.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 3,
resource: params_buf.as_entire_binding(),
},
],
});
let groups = rows.div_ceil(8);
self.encode(
enc,
&self.pipelines.gemv_f32,
&bg,
(groups.min(65535), groups.div_ceil(65535), 1),
"gemv_f32_tiled",
);
row_start += rows;
tile_idx += 1;
}
}
fn encode_rmsnorm(
&self,
enc: &mut wgpu::CommandEncoder,
x: &wgpu::Buffer,
weight: &wgpu::Buffer,
_n: u32,
_eps: f32,
) {
// Use pre-allocated params buffer (n and eps are always hs and config.rms_norm_eps).
let params_buf = &self.rmsnorm_hs_params;
let bg = self
.ctx
.device
.create_bind_group(&wgpu::BindGroupDescriptor {
label: None,
layout: &self.pipelines.rmsnorm.get_bind_group_layout(0),
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: x.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: weight.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 2,
resource: params_buf.as_entire_binding(),
},
],
});
self.encode(enc, &self.pipelines.rmsnorm, &bg, (1, 1, 1), "rmsnorm");
}
#[allow(clippy::too_many_arguments)]
fn encode_attention(
&self,
enc: &mut wgpu::CommandEncoder,
q: &wgpu::Buffer,
k_cache: &wgpu::Buffer,
v_cache: &wgpu::Buffer,
out: &wgpu::Buffer,
n_heads: u32,
n_kv_heads: u32,
head_dim: u32,
kv_dim: u32,
seq_len: u32,
scale: f32,
) {
let params: [u32; 8] = [
n_heads,
n_kv_heads,
head_dim,
kv_dim,
seq_len,
scale.to_bits(),
0,
0,
];
self.ctx
.queue
.write_buffer(&self.attn_params, 0, bytemuck::cast_slice(¶ms));
let params_buf = &self.attn_params;
let bg = self
.ctx
.device
.create_bind_group(&wgpu::BindGroupDescriptor {
label: None,
layout: &self.pipelines.attention.get_bind_group_layout(0),
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: q.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: k_cache.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 2,
resource: v_cache.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 3,
resource: out.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 4,
resource: self.scores_buf.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 5,
resource: params_buf.as_entire_binding(),
},
],
});
self.encode(
enc,
&self.pipelines.attention,
&bg,
(n_heads, 1, 1),
"attention",
);
}
// encode_per_head_rmsnorm, encode_rope, encode_elementwise, encode_conv1d
// removed — logic inlined into batched forward pass.
fn encode_copy(
&self,
enc: &mut wgpu::CommandEncoder,
src: &wgpu::Buffer,
src_off: u64,
dst: &wgpu::Buffer,
dst_off: u64,
n_floats: u64,
) {
enc.copy_buffer_to_buffer(src, src_off, dst, dst_off, n_floats * 4);
}
}
impl GpuLfm2Model {
/// Lock-free body of [`Model::forward`]. Callers must already hold
/// `infer_lock` — enter via the trait's `forward()` for a single
/// token, or `forward_prefill` for the hot prefill loop. The
/// `std::sync::Mutex` guarding the Model trait surface is not
/// reentrant, so calling `Model::forward` from inside this body
/// would deadlock.
fn forward_inner(&self, tokens: &[u32], pos: usize, state: &mut InferenceState) -> Vec<f32> {
self.forward_inner_compute(tokens, pos, state);
self.ctx
.download_f32(&self.logits_buf, self.config.vocab_size)
}
/// Computes one forward pass and leaves the resulting logits in
/// `self.logits_buf` on the GPU **without** reading them back. Caller
/// chooses how to consume the logits — full readback for sampling
/// (`forward_inner`) or a single-`u32` argmax readback for greedy
/// decoding (`forward_greedy_inner`). This split lets the wasm-async
/// path avoid the vocab-sized blocking download every step.
fn forward_inner_compute(&self, tokens: &[u32], pos: usize, state: &mut InferenceState) {
assert_eq!(tokens.len(), 1, "GPU forward expects single token");
let token_id = tokens[0] as usize;
let cfg = &self.config;
let hs = cfg.hidden_size;
let hs32 = hs as u32;
self.ctx.reset_profiler();
// Bounds check: KV cache capacity
assert!(
self.gpu_state.seq_len.load(Ordering::Relaxed) < self.gpu_state.max_seq_len,
"GPU seq_len {} exceeds max_seq_len {}",
self.gpu_state.seq_len.load(Ordering::Relaxed),
self.gpu_state.max_seq_len,
);
// 1. Embedding lookup from CPU cache (4KB upload per token)
let emb_offset = token_id * hs;
self.ctx.queue.write_buffer(
&self.hidden_buf,
0,
bytemuck::cast_slice(&self.gpu_state.embedding_f32[emb_offset..emb_offset + hs]),
);
// 2. Per-layer loop — one encoder per layer (block + FFN merged).
// Each layer submits independently to maintain CPU-GPU pipeline overlap.
for i in 0..cfg.n_layers {
let lw = &self.layers[i];
let mut enc = self.new_encoder();
if cfg.block_types[i] == BlockType::GatedConv {
let kernel_size = cfg.conv_kernel_size.unwrap_or(3) as u32;
let _d_conv = kernel_size - 1;
let conv_buf = self.gpu_state.conv_buffers[i].as_ref().unwrap();
// Pre-create BGs for conv block (using pre-allocated params).
let norm_bg = self
.ctx
.device
.create_bind_group(&wgpu::BindGroupDescriptor {
label: None,
layout: &self.pipelines.rmsnorm.get_bind_group_layout(0),
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: self.normed_buf.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: lw.attn_norm.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 2,
resource: self.rmsnorm_hs_params.as_entire_binding(),
},
],
});
let in_w = lw.conv_in_proj.as_ref().unwrap();
let in_bg_tmp;
let in_bg = match in_w.cached_bg.as_ref() {
Some(b) => b,
None => {
in_bg_tmp = self.make_gemv_bg(in_w, &self.normed_buf, &self.conv_proj_buf);
&in_bg_tmp
}
};
// Pass 1: rmsnorm + in_proj (after hidden→normed copy).
self.encode_copy(
&mut enc,
&self.hidden_buf,
0,
&self.normed_buf,
0,
hs as u64,
);
{
let mut pass = enc.begin_compute_pass(&wgpu::ComputePassDescriptor {
label: Some("conv_pre"),
timestamp_writes: self.ctx.begin_profile_span("conv_pre"),
});
self.dispatch_into(&mut pass, &self.pipelines.rmsnorm, &norm_bg, (1, 1, 1));
self.dispatch_gemv_into(&mut pass, in_w, in_bg);
}
// Pre-create BGs for passes 2 and 3. The fused conv shader reads
// x/c/b directly from `conv_proj_buf` at offsets 0/hs/2*hs and
// writes output to `conv_gate_buf` (where the post-conv out_proj
// gemv reads from) — replaces the prior mul1 + conv1d + mul2
// sequence and the three encoder copies that fed it.
let conv_p = &self.conv1d_params;
let conv_fused_bg = self
.ctx
.device
.create_bind_group(&wgpu::BindGroupDescriptor {
label: None,
layout: &self.pipelines.conv1d_fused.get_bind_group_layout(0),
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: self.conv_proj_buf.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: conv_buf.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 2,
resource: lw.conv_weight.as_ref().unwrap().as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 3,
resource: self.conv_gate_buf.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 4,
resource: conv_p.as_entire_binding(),
},
],
});
let out_w = lw.conv_out_proj.as_ref().unwrap();
let out_bg_tmp;
let out_bg = match out_w.cached_bg.as_ref() {
Some(b) => b,
None => {
out_bg_tmp = self.make_gemv_bg(out_w, &self.conv_gate_buf, &self.out_buf);
&out_bg_tmp
}
};
let add_p = &self.elementwise_hs_params;
let add_bg = self
.ctx
.device
.create_bind_group(&wgpu::BindGroupDescriptor {
label: None,
layout: &self.pipelines.add_inplace.get_bind_group_layout(0),
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: self.hidden_buf.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: self.out_buf.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 2,
resource: add_p.as_entire_binding(),
},
],
});
// Pass 2: fused conv block (bx = x*b → conv → c*conv_out).
// One dispatch replaces the prior mul1 + conv1d + mul2 trio
// plus three encoder copies that extracted x/c/b from the
// proj buffer into separate per-channel buffers.
{
let mut pass = enc.begin_compute_pass(&wgpu::ComputePassDescriptor {
label: Some("conv_mid"),
timestamp_writes: self.ctx.begin_profile_span("conv_mid"),
});
self.dispatch_into(
&mut pass,
&self.pipelines.conv1d_fused,
&conv_fused_bg,
(hs32.div_ceil(256), 1, 1),
);
}
// Pass 3: out_proj + add.
{
let mut pass = enc.begin_compute_pass(&wgpu::ComputePassDescriptor {
label: Some("conv_post"),
timestamp_writes: self.ctx.begin_profile_span("conv_post"),
});
self.dispatch_gemv_into(&mut pass, out_w, out_bg);
self.dispatch_into(
&mut pass,
&self.pipelines.add_inplace,
&add_bg,
(hs32.div_ceil(256), 1, 1),
);
}
} else {
// Attention block — batched into 2 compute passes (separated by KV cache copies).
let head_dim = cfg.head_dim as u32;
let n_kv_heads = cfg.kv_heads_per_layer[i] as u32;
let kv_dim = n_kv_heads * head_dim;
let n_heads = cfg.n_heads as u32;
let q_dim = n_heads * head_dim;
// Pre-create all BGs before opening passes.
let norm_bg = self
.ctx
.device
.create_bind_group(&wgpu::BindGroupDescriptor {
label: None,
layout: &self.pipelines.rmsnorm.get_bind_group_layout(0),
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: self.normed_buf.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: lw.attn_norm.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 2,
resource: self.rmsnorm_hs_params.as_entire_binding(),
},
],
});
let q_w = lw.attn_q.as_ref().unwrap();
let q_bg_tmp;
let q_bg = match q_w.cached_bg.as_ref() {
Some(b) => b,
None => {
q_bg_tmp = self.make_gemv_bg(q_w, &self.normed_buf, &self.q_buf);
&q_bg_tmp
}
};
let k_w = lw.attn_k.as_ref().unwrap();
let k_bg_tmp;
let k_bg = match k_w.cached_bg.as_ref() {
Some(b) => b,
None => {
k_bg_tmp = self.make_gemv_bg(k_w, &self.normed_buf, &self.k_buf);
&k_bg_tmp
}
};
let v_w = lw.attn_v.as_ref().unwrap();
let v_bg_tmp;
let v_bg = match v_w.cached_bg.as_ref() {
Some(b) => b,
None => {
v_bg_tmp = self.make_gemv_bg(v_w, &self.normed_buf, &self.v_buf);
&v_bg_tmp
}
};
// QK-norm (Qwen3) — only when the layer carries per-head norm
// weights. Built as `Option` so non-Qwen3 archs skip the dispatch.
let per_head_norm_bg = |buf: &wgpu::Buffer, norm: &wgpu::Buffer| {
self.ctx
.device
.create_bind_group(&wgpu::BindGroupDescriptor {
label: None,
layout: &self.pipelines.per_head_rmsnorm.get_bind_group_layout(0),
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: buf.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: norm.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 2,
resource: self.per_head_norm_params.as_entire_binding(),
},
],
})
};
let qn_bg = lw
.attn_q_norm
.as_ref()
.map(|w| per_head_norm_bg(&self.q_buf, w));
let kn_bg = lw
.attn_k_norm
.as_ref()
.map(|w| per_head_norm_bg(&self.k_buf, w));
// QKV bias (Qwen2) — added right after each projection GEMV,
// before QK-norm/RoPE. `Option` so bias-less archs skip it.
let bias_bg = |buf: &wgpu::Buffer, bias: &wgpu::Buffer, params: &wgpu::Buffer| {
self.ctx
.device
.create_bind_group(&wgpu::BindGroupDescriptor {
label: None,
layout: &self.pipelines.add_inplace.get_bind_group_layout(0),
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: buf.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: bias.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 2,
resource: params.as_entire_binding(),
},
],
})
};
let qb_bg = lw
.attn_q_bias
.as_ref()
.map(|b| bias_bg(&self.q_buf, b, &self.elementwise_qdim_params));
let kb_bg = lw
.attn_k_bias
.as_ref()
.map(|b| bias_bg(&self.k_buf, b, &self.elementwise_kvdim_params));
let vb_bg = lw
.attn_v_bias
.as_ref()
.map(|b| bias_bg(&self.v_buf, b, &self.elementwise_kvdim_params));
let rope_data: [u32; 7] = [
pos as u32,
n_heads,
n_kv_heads,
head_dim,
cfg.rope_theta.to_bits(),
self.rope_type as u32,
self.has_freq_factors as u32,
];
self.ctx
.queue
.write_buffer(&self.rope_params, 0, bytemuck::cast_slice(&rope_data));
let rope_bg = self
.ctx
.device
.create_bind_group(&wgpu::BindGroupDescriptor {
label: None,
layout: &self.pipelines.rope.get_bind_group_layout(0),
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: self.q_buf.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: self.k_buf.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 2,
resource: self.rope_params.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 3,
resource: self.rope_freqs_buf.as_entire_binding(),
},
],
});
let max_pairs = std::cmp::max(n_heads, n_kv_heads) * (head_dim / 2);
// Copy hidden → normed, then pass 1: norm + QKV + per-head norm + rope.
self.encode_copy(
&mut enc,
&self.hidden_buf,
0,
&self.normed_buf,
0,
hs as u64,
);
{
let mut pass = enc.begin_compute_pass(&wgpu::ComputePassDescriptor {
label: Some("attn_pre"),
timestamp_writes: self.ctx.begin_profile_span("attn_pre"),
});
self.dispatch_into(&mut pass, &self.pipelines.rmsnorm, &norm_bg, (1, 1, 1));
self.dispatch_gemv_into(&mut pass, q_w, q_bg);
self.dispatch_gemv_into(&mut pass, k_w, k_bg);
self.dispatch_gemv_into(&mut pass, v_w, v_bg);
// QKV bias (Qwen2): add right after the projections.
if let Some(bg) = qb_bg.as_ref() {
self.dispatch_into(
&mut pass,
&self.pipelines.add_inplace,
bg,
(q_dim.div_ceil(256), 1, 1),
);
}
if let Some(bg) = kb_bg.as_ref() {
self.dispatch_into(
&mut pass,
&self.pipelines.add_inplace,
bg,
(kv_dim.div_ceil(256), 1, 1),
);
}
if let Some(bg) = vb_bg.as_ref() {
self.dispatch_into(
&mut pass,
&self.pipelines.add_inplace,
bg,
(kv_dim.div_ceil(256), 1, 1),
);
}
// QK-norm (Qwen3): per-head RMSNorm before RoPE.
if let Some(bg) = qn_bg.as_ref() {
self.dispatch_into(
&mut pass,
&self.pipelines.per_head_rmsnorm,
bg,
(n_heads, 1, 1),
);
}
if let Some(bg) = kn_bg.as_ref() {
self.dispatch_into(
&mut pass,
&self.pipelines.per_head_rmsnorm,
bg,
(n_kv_heads, 1, 1),
);
}
self.dispatch_into(
&mut pass,
&self.pipelines.rope,
&rope_bg,
(max_pairs.div_ceil(256), 1, 1),
);
}
// KV cache copies (encoder-level), then pass 2: attention + out_proj + add.
let (k_cache, v_cache) = self.gpu_state.kv_caches[i].as_ref().unwrap();
let seq_len = self.gpu_state.seq_len.load(Ordering::Relaxed);
let kv_offset = (seq_len * kv_dim as usize * 4) as u64;
self.encode_copy(&mut enc, &self.k_buf, 0, k_cache, kv_offset, kv_dim as u64);
self.encode_copy(&mut enc, &self.v_buf, 0, v_cache, kv_offset, kv_dim as u64);
let attn_seq_len = (seq_len + 1) as u32;
// Granite overrides the softmax scale with its attention
// multiplier; every other arch uses 1/sqrt(head_dim).
let scale = self
.scalars
.attn
.unwrap_or_else(|| 1.0 / (head_dim as f32).sqrt());
// Attention BG (changes per token due to seq_len).
self.encode_attention(
&mut enc,
&self.q_buf,
k_cache,
v_cache,
&self.attn_out_buf,
n_heads,
n_kv_heads,
head_dim,
kv_dim,
attn_seq_len,
scale,
);
// out_proj + add — batch into one pass.
let out_w = lw.attn_output.as_ref().unwrap();
let out_bg_tmp;
let out_bg = match out_w.cached_bg.as_ref() {
Some(b) => b,
None => {
out_bg_tmp = self.make_gemv_bg(out_w, &self.attn_out_buf, &self.out_buf);
&out_bg_tmp
}
};
// Residual add: `scaled_add_inplace` folds Granite's residual
// multiplier into the addend (1.0 for every other arch).
let add_bg = self
.ctx
.device
.create_bind_group(&wgpu::BindGroupDescriptor {
label: None,
layout: &self.pipelines.scaled_add_inplace.get_bind_group_layout(0),
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: self.hidden_buf.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: self.out_buf.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 2,
resource: self.residual_add_params.as_entire_binding(),
},
],
});
{
let mut pass = enc.begin_compute_pass(&wgpu::ComputePassDescriptor {
label: Some("attn_post"),
timestamp_writes: self.ctx.begin_profile_span("attn_post"),
});
self.dispatch_gemv_into(&mut pass, out_w, out_bg);
self.dispatch_into(
&mut pass,
&self.pipelines.scaled_add_inplace,
&add_bg,
(hs32.div_ceil(256), 1, 1),
);
}
}
// FFN — same encoder as block above.
self.encode_copy(
&mut enc,
&self.hidden_buf,
0,
&self.ffn_input_buf,
0,
hs as u64,
);
// FFN: batch 6 dispatches into ONE compute pass.
// Pre-create bind groups before opening the pass.
let norm_params = &self.rmsnorm_hs_params;
let norm_bg = self
.ctx
.device
.create_bind_group(&wgpu::BindGroupDescriptor {
label: None,
layout: &self.pipelines.rmsnorm.get_bind_group_layout(0),
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: self.ffn_input_buf.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: lw.ffn_norm.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 2,
resource: norm_params.as_entire_binding(),
},
],
});
let gate_bg_tmp;
let gate_bg = match lw.ffn_gate.cached_bg.as_ref() {
Some(bg) => bg,
None => {
gate_bg_tmp =
self.make_gemv_bg(&lw.ffn_gate, &self.ffn_input_buf, &self.gate_buf);
&gate_bg_tmp
}
};
let up_bg_tmp;
let up_bg = match lw.ffn_up.cached_bg.as_ref() {
Some(bg) => bg,
None => {
up_bg_tmp = self.make_gemv_bg(&lw.ffn_up, &self.ffn_input_buf, &self.up_buf);
&up_bg_tmp
}
};
let silu_params = &self.elementwise_is_params;
let silu_bg = self
.ctx
.device
.create_bind_group(&wgpu::BindGroupDescriptor {
label: None,
layout: &self.pipelines.silu_mul_inplace.get_bind_group_layout(0),
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: self.gate_buf.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: self.up_buf.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 2,
resource: silu_params.as_entire_binding(),
},
],
});
let down_bg_tmp;
let down_bg = match lw.ffn_down.cached_bg.as_ref() {
Some(bg) => bg,
None => {
down_bg_tmp = self.make_gemv_bg(&lw.ffn_down, &self.gate_buf, &self.out_buf);
&down_bg_tmp
}
};
// Residual add: `scaled_add_inplace` folds Granite's residual
// multiplier into the addend (1.0 for every other arch).
let add_bg = self
.ctx
.device
.create_bind_group(&wgpu::BindGroupDescriptor {
label: None,
layout: &self.pipelines.scaled_add_inplace.get_bind_group_layout(0),
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: self.hidden_buf.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: self.out_buf.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 2,
resource: self.residual_add_params.as_entire_binding(),
},
],
});
{
let mut pass = enc.begin_compute_pass(&wgpu::ComputePassDescriptor {
label: Some("ffn"),
timestamp_writes: self.ctx.begin_profile_span("ffn"),
});
// rmsnorm
self.dispatch_into(&mut pass, &self.pipelines.rmsnorm, &norm_bg, (1, 1, 1));
// gate + up GEMVs
self.dispatch_gemv_into(&mut pass, &lw.ffn_gate, gate_bg);
self.dispatch_gemv_into(&mut pass, &lw.ffn_up, up_bg);
// silu_mul
self.dispatch_into(
&mut pass,
&self.pipelines.silu_mul_inplace,
&silu_bg,
((lw.ffn_gate.tensor.shape[0] as u32).div_ceil(256), 1, 1),
);
// down GEMV
self.dispatch_gemv_into(&mut pass, &lw.ffn_down, down_bg);
// residual add
self.dispatch_into(
&mut pass,
&self.pipelines.scaled_add_inplace,
&add_bg,
(hs32.div_ceil(256), 1, 1),
);
}
self.ctx.queue.submit(Some(enc.finish()));
}
// 3. Output norm + projection. Untied models project through
// `output.weight`; tied models reuse the embedding table.
let mut enc = self.new_encoder();
self.encode_rmsnorm(
&mut enc,
&self.hidden_buf,
&self.output_norm,
hs32,
cfg.rms_norm_eps,
);
let out_proj = self.output_weight.as_ref().unwrap_or(&self.embedding);
self.encode_gemv_f32(
&mut enc,
out_proj,
&self.hidden_buf,
&self.logits_buf,
cfg.vocab_size as u32,
hs32,
);
// Granite divides the logits by `logits_scaling` (identity elsewhere).
if let Some(params) = self.logit_scale_params.as_ref() {
let scale_bg = self
.ctx
.device
.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("logit_scale_bg"),
layout: &self.pipelines.scale_f32.get_bind_group_layout(0),
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: self.logits_buf.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: params.as_entire_binding(),
},
],
});
let mut pass = enc.begin_compute_pass(&wgpu::ComputePassDescriptor {
label: Some("logit_scale"),
timestamp_writes: None,
});
self.dispatch_into(
&mut pass,
&self.pipelines.scale_f32,
&scale_bg,
((cfg.vocab_size as u32).div_ceil(256), 1, 1),
);
drop(pass);
}
self.submit_and_wait(enc);
// 4. Update seq_len + profile bookkeeping. Logits are now in
// `logits_buf` on the GPU; the caller decides how to consume
// them (full readback vs. argmax-then-u32-readback).
self.gpu_state.seq_len.fetch_add(1, Ordering::Relaxed);
state.seq_len += 1;
self.ctx.finish_profiler();
}
/// Greedy single-token forward: runs the same kernels as
/// [`forward_inner`] but replaces the vocab-sized logits download
/// with a 4-byte argmax readback. Cuts per-token PCIe/USB-C
/// readback from `vocab_size * 4` bytes to `4` bytes — the
/// wasm-async-friendly path, since a 4-byte map_async still
/// blocks the JS event loop briefly but doesn't transfer megabytes.
fn forward_greedy_inner(&self, tokens: &[u32], pos: usize, state: &mut InferenceState) -> u32 {
self.forward_inner_compute(tokens, pos, state);
// Encode + submit the argmax pass on its own. Could be folded
// into the output-projection encoder for one fewer submission,
// but that's a `forward_inner_compute` refactor we're keeping
// out of this PR.
self.ctx.reset_profiler();
let mut enc = self.new_encoder();
{
let mut pass = enc.begin_compute_pass(&wgpu::ComputePassDescriptor {
label: Some("argmax"),
timestamp_writes: self.ctx.begin_profile_span("argmax"),
});
pass.set_pipeline(&self.pipelines.argmax_f32);
pass.set_bind_group(0, &self.argmax_bg, &[]);
pass.dispatch_workgroups(1, 1, 1);
}
self.submit_and_wait(enc);
self.ctx.finish_profiler();
let out = self.ctx.download_u32(&self.argmax_out_buf, 1);
out[0]
}
}
// === Batched prefill — encode helpers + main method ========================
//
// Mirror `MetalLfm2Model::prefill_layers_and_logits` (metal_lfm2.rs:2906).
// Uses the five batched shaders landed in PRs #154 + #156:
// rmsnorm_batch / add_rmsnorm_batch (PR #154)
// qk_norm_rope_batch (PR #154)
// conv1d_fused_batch (PR #154)
// mul_mat_reg_tile (PR #162)
// attention_prefill (PR #156)
//
// Scope:
// * `forward_prefill_batched_locked` accepts any `start_pos`, so the
// dispatcher chunks long prompts through it in
// `min(max_seq_len, MAX_PREFILL_TOKENS)` chunks (each chunk advances
// `start_pos`; conv rolling state and KV cache writes carry across).
// * `1 <= n <= MAX_PREFILL_TOKENS` per call (asserted).
// * `start_pos + n <= max_seq_len` (asserted).
// * All matmul weights must be Q4_0 (the LFM2 Q4_0 GGUF default).
// Non-Q4_0 paths fall through to the per-token loop at the dispatcher.
//
// The non-Q4_0 fallback (an f32 `gemm_f32` shader, or per-token gemv with
// offset bindings) can land in a follow-up PR without disturbing this
// contract.
//
// Per-dispatch overhead note: each `encode_*` helper builds a fresh
// `wgpu::BindGroup` and uploads a small params buffer per call. The CPU
// cost is ~1 % of total prefill time at the workloads measured in PR #157;
// promoting the params buffers to model-resident state and caching the
// bind groups for fixed prefill scratch buffers is a clean follow-up
// optimization. Kept simple here so the refactor is reviewable.
//
// `prefill_scores_buf` size note: this scratch is sized to
// `MAX_PREFILL_TOKENS × n_heads × max_seq_len × 4` bytes (256 MB on
// LFM2-VL-450M / 512 MB on LFM2.5-VL-1.6B at the default 8192 context).
// On native macOS this is fine (M1+ unified memory). For wasm / WebGPU
// tier-1 (256 MB max storage buffer) this becomes load-bearing — the
// proper fix is a two-pass online softmax in `attention_prefill.wgsl`
// that doesn't materialize the full scores matrix; queued as a follow-up
// shader PR.
impl GpuLfm2Model {
/// Returns true iff every matmul weight on every layer has a batched
/// prefill kernel. Q4_0 uses the register-tiled path; Q8_0 uses the
/// conservative batched GEMV-shaped path.
/// precondition for `forward_prefill_batched_locked` to take the
/// batched path. Cheap O(n_layers) walk; not memoized because it's
/// called once per `forward_prefill` invocation.
fn all_matmul_weights_batched_supported(&self) -> bool {
for lw in &self.layers {
let weights = [
Some(&lw.ffn_gate),
Some(&lw.ffn_up),
Some(&lw.ffn_down),
lw.attn_q.as_ref(),
lw.attn_k.as_ref(),
lw.attn_v.as_ref(),
lw.attn_output.as_ref(),
lw.conv_in_proj.as_ref(),
lw.conv_out_proj.as_ref(),
];
for w in weights.into_iter().flatten() {
if !matches!(w.tensor.dtype, DType::Q4_0 | DType::Q8_0) {
return false;
}
}
}
true
}
/// Encode `rmsnorm_batch`: dst[t, i] = src[t, i] * inv_rms(src[t]) * w[i]
/// for t in 0..n. Workgroup per token. Uses the binding layout shared
/// with `add_rmsnorm_batch`; naga drops binding 4 from the
/// auto-inferred layout for this entry point.
fn encode_rmsnorm_batch(
&self,
enc: &mut wgpu::CommandEncoder,
src: &wgpu::Buffer,
dst: &wgpu::Buffer,
weight: &wgpu::Buffer,
n: u32,
hs: u32,
) {
let params: [u32; 4] = [hs, self.config.rms_norm_eps.to_bits(), hs, hs];
let p_buf = self
.ctx
.upload_storage(bytemuck::cast_slice(¶ms), "rmsnorm_batch_params");
let bg = self
.ctx
.device
.create_bind_group(&wgpu::BindGroupDescriptor {
label: None,
layout: &self.pipelines.rmsnorm_batch.get_bind_group_layout(0),
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: src.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: dst.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 2,
resource: weight.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 3,
resource: p_buf.as_entire_binding(),
},
],
});
self.encode(
enc,
&self.pipelines.rmsnorm_batch,
&bg,
(n, 1, 1),
"rmsnorm_batch",
);
}
/// Encode `add_rmsnorm_batch`: src[t,i] += residual[t,i]; dst[t,i] =
/// src[t,i] * inv_rms(src[t]) * w[i]. One pass; src is read-write.
#[allow(clippy::too_many_arguments)]
fn encode_add_rmsnorm_batch(
&self,
enc: &mut wgpu::CommandEncoder,
src: &wgpu::Buffer,
dst: &wgpu::Buffer,
weight: &wgpu::Buffer,
residual: &wgpu::Buffer,
n: u32,
hs: u32,
) {
let params: [u32; 4] = [hs, self.config.rms_norm_eps.to_bits(), hs, hs];
let p_buf = self
.ctx
.upload_storage(bytemuck::cast_slice(¶ms), "add_rmsnorm_batch_params");
let bg = self
.ctx
.device
.create_bind_group(&wgpu::BindGroupDescriptor {
label: None,
layout: &self.pipelines.add_rmsnorm_batch.get_bind_group_layout(0),
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: src.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: dst.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 2,
resource: weight.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 3,
resource: p_buf.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 4,
resource: residual.as_entire_binding(),
},
],
});
self.encode(
enc,
&self.pipelines.add_rmsnorm_batch,
&bg,
(n, 1, 1),
"add_rmsnorm_batch",
);
}
/// Encode batched 2D matmul: y = weight * x.
/// Batched prefill supports quantized Q4_0 and Q8_0 weights. F32 weights
/// are not a production path in this model. `x_stride` and `y_stride` are
/// measured in f32 elements between consecutive token vectors.
#[allow(clippy::too_many_arguments)] // tile geometry + strides; splitting hurts clarity
fn encode_mul_mat_reg_tile(
&self,
enc: &mut wgpu::CommandEncoder,
w: &GpuWeight,
x: &wgpu::Buffer,
y: &wgpu::Buffer,
n: u32,
k: u32,
x_stride: u32,
y_stride: u32,
) {
debug_assert!(
matches!(w.tensor.dtype, DType::Q4_0 | DType::Q8_0),
"encode_mul_mat_reg_tile only supports Q4_0/Q8_0 weights"
);
let m = w.tensor.shape[0] as u32;
let (pipeline, wg_m, wg_n, label) = match w.tensor.dtype {
DType::Q4_0 => {
let use_vec = m % 4 == 0 && k % 4 == 0 && x_stride % 4 == 0 && y_stride % 4 == 0;
let pipeline = if use_vec {
&self.pipelines.mul_mat_reg_tile_q4_0_vec
} else {
&self.pipelines.mul_mat_reg_tile_q4_0_scalar
};
let wg_m = m.div_ceil(MUL_MAT_TILE_WG_M * MUL_MAT_TILE_M);
let wg_n = n.div_ceil(MUL_MAT_TILE_WG_N * MUL_MAT_TILE_N);
(pipeline, wg_m, wg_n, "mul_mat_tile")
}
DType::Q8_0 => {
let wg_m = m.div_ceil(8);
// gemm_q8_0 indexes the row tile with `wid.x` directly (no
// get_wid flattening — `wid.y` carries the token axis), so
// unlike the GEMV path it cannot fold an overflowing row
// dimension into Y. LFM2 weight rows stay far below this
// (largest is vocab/embd ≈ 64–128k → /8 ≈ 8–16k), so this
// is a guard against a future oversized weight, not a live
// limit.
debug_assert!(
wg_m <= 65535,
"gemm_q8_0 row tiles {wg_m} exceed the 65535 dispatch \
limit (m={m}); Q8_0 batched prefill needs a flattened \
dispatch for weights this large"
);
(&self.pipelines.gemm_q8_0, wg_m, n, "gemm_q8")
}
// Unreachable in practice: the batched path is only entered when
// `all_matmul_weights_batched_supported()` already confirmed every
// weight is Q4_0/Q8_0. The debug_assert above documents the same
// precondition; this arm is the release-mode backstop.
_ => unreachable!("batched prefill only supports Q4_0/Q8_0"),
};
let params: [u32; 6] = [m, k, n, x_stride, y_stride, 0];
let p_buf = self
.ctx
.upload_storage(bytemuck::cast_slice(¶ms), "mul_mat_tile_params");
let bg = self
.ctx
.device
.create_bind_group(&wgpu::BindGroupDescriptor {
label: None,
layout: &pipeline.get_bind_group_layout(0),
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: w.tensor.buffer.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: x.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 2,
resource: y.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 3,
resource: p_buf.as_entire_binding(),
},
],
});
self.encode(enc, pipeline, &bg, (wg_m, wg_n, 1), label);
}
/// Encode `qk_norm_rope_batch`: in-place rmsnorm + RoPE on Q (n × n_heads
/// × head_dim) and K (n × n_kv_heads × head_dim) at positions
/// `start_pos + token_idx`.
#[allow(clippy::too_many_arguments)]
fn encode_qk_norm_rope_batch(
&self,
enc: &mut wgpu::CommandEncoder,
q_batch: &wgpu::Buffer,
k_batch: &wgpu::Buffer,
q_norm_w: &wgpu::Buffer,
k_norm_w: &wgpu::Buffer,
start_pos: u32,
n: u32,
n_heads: u32,
n_kv_heads: u32,
head_dim: u32,
q_stride: u32,
k_stride: u32,
) {
let params: [u32; 10] = [
start_pos,
n,
n_heads,
n_kv_heads,
head_dim,
self.config.rms_norm_eps.to_bits(),
self.config.rope_theta.to_bits(),
0, // rope_type 0 = split-halves (matches existing rope.wgsl)
q_stride,
k_stride,
];
let p_buf = self
.ctx
.upload_storage(bytemuck::cast_slice(¶ms), "qk_norm_rope_batch_params");
let bg = self
.ctx
.device
.create_bind_group(&wgpu::BindGroupDescriptor {
label: None,
layout: &self.pipelines.qk_norm_rope_batch.get_bind_group_layout(0),
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: q_batch.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: k_batch.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 2,
resource: q_norm_w.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 3,
resource: k_norm_w.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 4,
resource: p_buf.as_entire_binding(),
},
],
});
let tg_count = n * (n_heads + n_kv_heads);
self.encode(
enc,
&self.pipelines.qk_norm_rope_batch,
&bg,
(tg_count, 1, 1),
"qk_norm_rope_batch",
);
}
/// Encode `conv1d_fused_batch`. One thread per channel walks all n
/// tokens sequentially; rolling-buffer state is in `rbuffer` and is
/// updated in place.
#[allow(clippy::too_many_arguments)]
fn encode_conv1d_fused_batch(
&self,
enc: &mut wgpu::CommandEncoder,
proj: &wgpu::Buffer,
rbuffer: &wgpu::Buffer,
weight: &wgpu::Buffer,
output: &wgpu::Buffer,
n: u32,
hs: u32,
) {
let kernel_size = self.config.conv_kernel_size.unwrap_or(3) as u32;
let d_conv = kernel_size - 1;
let params: [u32; 6] = [hs, kernel_size, d_conv, n, 3 * hs, hs];
let p_buf = self
.ctx
.upload_storage(bytemuck::cast_slice(¶ms), "conv1d_fused_batch_params");
let bg = self
.ctx
.device
.create_bind_group(&wgpu::BindGroupDescriptor {
label: None,
layout: &self.pipelines.conv1d_fused_batch.get_bind_group_layout(0),
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: proj.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: rbuffer.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 2,
resource: weight.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 3,
resource: output.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 4,
resource: p_buf.as_entire_binding(),
},
],
});
let groups = hs.div_ceil(256);
self.encode(
enc,
&self.pipelines.conv1d_fused_batch,
&bg,
(groups, 1, 1),
"conv1d_fused_batch",
);
}
/// Encode `attention_prefill`. Reads Q from `q_batch`, K/V from the
/// model's KV caches, writes per-(token, head) output to `out_batch`.
/// `scores_buf` is a per-(query, head, time) scratch slab.
#[allow(clippy::too_many_arguments)]
fn encode_attention_prefill(
&self,
enc: &mut wgpu::CommandEncoder,
q_batch: &wgpu::Buffer,
k_cache: &wgpu::Buffer,
v_cache: &wgpu::Buffer,
out_batch: &wgpu::Buffer,
n: u32,
n_heads: u32,
n_kv_heads: u32,
head_dim: u32,
kv_dim: u32,
max_seq: u32,
start_pos: u32,
q_stride: u32,
out_stride: u32,
) {
let scale = 1.0f32 / (head_dim as f32).sqrt();
let params: [u32; 12] = [
n_heads,
n_kv_heads,
head_dim,
kv_dim,
max_seq,
scale.to_bits(),
start_pos,
n,
q_stride,
out_stride,
0,
0,
];
let p_buf = self
.ctx
.upload_storage(bytemuck::cast_slice(¶ms), "attention_prefill_params");
let bg = self
.ctx
.device
.create_bind_group(&wgpu::BindGroupDescriptor {
label: None,
layout: &self.pipelines.attention_prefill.get_bind_group_layout(0),
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: q_batch.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: k_cache.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 2,
resource: v_cache.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 3,
resource: out_batch.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 4,
resource: self.prefill_scores_buf.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 5,
resource: p_buf.as_entire_binding(),
},
],
});
self.encode(
enc,
&self.pipelines.attention_prefill,
&bg,
(n_heads, n, 1),
"attention_prefill",
);
}
/// Batched prefill — single-pass over `n` tokens for all layers, then
/// final output norm + LM head on the last token only.
///
/// Preconditions (caller-enforced):
/// * `start_pos == 0`. (Continuation prefills go through the
/// per-token loop.)
/// * `1 <= tokens.len() <= MAX_PREFILL_TOKENS`.
/// * All matmul weights on every layer are Q4_0
/// (`all_matmul_weights_q4_0() == true`).
/// * Caller already holds `infer_lock`.
///
/// Mirrors `MetalLfm2Model::prefill_layers_and_logits`
/// (metal_lfm2.rs:2906); the Metal version is the canonical
/// reference for the dispatch order + buffer assignment.
fn forward_prefill_batched_locked(
&self,
tokens: &[u32],
start_pos: usize,
state: &mut InferenceState,
) -> Vec<f32> {
debug_assert!(!tokens.is_empty());
let n = tokens.len();
// Bounds checks — make a misuse fail deterministically rather
// than show up later as a wgpu validation error during a buffer
// copy or as silent out-of-bounds attention reads.
assert!(
start_pos + n <= self.gpu_state.max_seq_len,
"prefill start_pos {start_pos} + n {n} exceeds max_seq_len {}",
self.gpu_state.max_seq_len,
);
debug_assert!(
n <= self.gpu_state.max_seq_len.min(MAX_PREFILL_TOKENS),
"n {n} exceeds chunk capacity (max_seq_len = {}, MAX_PREFILL_TOKENS = {MAX_PREFILL_TOKENS})",
self.gpu_state.max_seq_len,
);
// `start_pos > 0` is supported for chunked prefills — the
// dispatcher walks through chunks of up to
// `min(max_seq_len, MAX_PREFILL_TOKENS)` and increments
// `start_pos` per chunk.
let cfg = &self.config;
let hs = cfg.hidden_size;
let is = cfg.intermediate_size;
// Reset profiler spans + seq_len mirror so this chunk owns its
// own profile output and starts clean. Conv buffer zeroing is
// the dispatcher's responsibility (happens once per fresh
// prefill, regardless of which path runs and how many chunks).
self.ctx.reset_profiler();
self.gpu_state.seq_len.store(start_pos, Ordering::Relaxed);
// ─── Stage embeddings into prefill_batch_buf ──────────────────────
// CPU-side gather + one queue.write_buffer (the `embedding_f32`
// table is pre-dequantized at load time and lives on the host).
let mut staged: Vec<f32> = Vec::with_capacity(n * hs);
for &t in tokens {
let off = (t as usize) * hs;
staged.extend_from_slice(&self.gpu_state.embedding_f32[off..off + hs]);
}
self.ctx
.queue
.write_buffer(&self.prefill_batch_buf, 0, bytemuck::cast_slice(&staged));
let mut enc = self.new_encoder();
let n_u = n as u32;
let hs_u = hs as u32;
let is_u = is as u32;
for layer in 0..cfg.n_layers {
let lw = &self.layers[layer];
// ─── Phase 1: rmsnorm (or fused add_rmsnorm with prev FFN
// residual) → prefill_normed_buf ─────────────────
if layer > 0 {
// Fuse: batch_buf += prev_layer_ffn_down (`prefill_up_buf`),
// then rmsnorm into `prefill_normed_buf`.
//
// Metal aliases dst === residual on `prefill_normed_buf`;
// wgpu 24's binding-aliasing validator rejects that
// pattern (binding 1 read_write + binding 4 read on the
// same buffer in one dispatch). Route FFN down to
// `prefill_up_buf` so dst and residual stay distinct.
self.encode_add_rmsnorm_batch(
&mut enc,
&self.prefill_batch_buf,
&self.prefill_normed_buf,
&lw.attn_norm,
&self.prefill_up_buf,
n_u,
hs_u,
);
} else {
self.encode_rmsnorm_batch(
&mut enc,
&self.prefill_batch_buf,
&self.prefill_normed_buf,
&lw.attn_norm,
n_u,
hs_u,
);
}
if cfg.block_types[layer] == BlockType::GatedConv {
let conv_buf = self.gpu_state.conv_buffers[layer].as_ref().unwrap();
let w_in = lw.conv_in_proj.as_ref().unwrap();
let w_out = lw.conv_out_proj.as_ref().unwrap();
let conv_weight = lw.conv_weight.as_ref().unwrap();
// Phase 2: in_proj batched GEMM (3*hs columns per token).
self.encode_mul_mat_reg_tile(
&mut enc,
w_in,
&self.prefill_normed_buf,
&self.prefill_proj_buf,
n_u,
hs_u,
hs_u,
3 * hs_u,
);
// Phase 3: fused conv1d (1 dispatch over all N tokens;
// rolling buffer state walks sequentially per channel).
self.encode_conv1d_fused_batch(
&mut enc,
&self.prefill_proj_buf,
conv_buf,
conv_weight,
&self.prefill_normed_buf,
n_u,
hs_u,
);
// Phase 4: out_proj GEMM → prefill_gate_buf (residual
// scratch; FFN's add_rmsnorm_batch will fuse the add).
self.encode_mul_mat_reg_tile(
&mut enc,
w_out,
&self.prefill_normed_buf,
&self.prefill_gate_buf,
n_u,
hs_u,
hs_u,
hs_u,
);
} else {
// Attention layer.
let head_dim = (hs / cfg.n_heads) as u32;
let n_kv_heads = cfg.kv_heads_per_layer[layer] as u32;
let kv_dim = n_kv_heads * head_dim;
let n_heads = cfg.n_heads as u32;
let (k_cache, v_cache) = self.gpu_state.kv_caches[layer].as_ref().unwrap();
let w_q = lw.attn_q.as_ref().unwrap();
let w_k = lw.attn_k.as_ref().unwrap();
let w_v = lw.attn_v.as_ref().unwrap();
let w_o = lw.attn_output.as_ref().unwrap();
// Phase A: Q/K/V batched GEMMs.
// Q → prefill_proj_buf, stride hs
// K → prefill_gate_buf, stride kv_dim
// V → prefill_up_buf, stride kv_dim
self.encode_mul_mat_reg_tile(
&mut enc,
w_q,
&self.prefill_normed_buf,
&self.prefill_proj_buf,
n_u,
hs_u,
hs_u,
hs_u,
);
self.encode_mul_mat_reg_tile(
&mut enc,
w_k,
&self.prefill_normed_buf,
&self.prefill_gate_buf,
n_u,
hs_u,
hs_u,
kv_dim,
);
self.encode_mul_mat_reg_tile(
&mut enc,
w_v,
&self.prefill_normed_buf,
&self.prefill_up_buf,
n_u,
hs_u,
hs_u,
kv_dim,
);
// Phase B: batched per-head Q/K rmsnorm + RoPE.
self.encode_qk_norm_rope_batch(
&mut enc,
&self.prefill_proj_buf,
&self.prefill_gate_buf,
lw.attn_q_norm.as_ref().unwrap(),
lw.attn_k_norm.as_ref().unwrap(),
start_pos as u32,
n_u,
n_heads,
n_kv_heads,
head_dim,
hs_u,
kv_dim,
);
// Phase C: bulk-write K/V into the cache. The KV cache is
// `max_seq_len × kv_dim` f32; write `n × kv_dim` floats
// starting at `start_pos × kv_dim × 4` bytes. wgpu's
// copy_buffer_to_buffer is a no-shader memcpy.
let kv_off_bytes = (start_pos * kv_dim as usize * 4) as u64;
let kv_chunk_bytes = (n * kv_dim as usize * 4) as u64;
enc.copy_buffer_to_buffer(
&self.prefill_gate_buf,
0,
k_cache,
kv_off_bytes,
kv_chunk_bytes,
);
enc.copy_buffer_to_buffer(
&self.prefill_up_buf,
0,
v_cache,
kv_off_bytes,
kv_chunk_bytes,
);
// Phase D: batched causal attention.
let max_seq_for_kv = (start_pos + n) as u32;
self.encode_attention_prefill(
&mut enc,
&self.prefill_proj_buf,
k_cache,
v_cache,
&self.prefill_normed_buf,
n_u,
n_heads,
n_kv_heads,
head_dim,
kv_dim,
max_seq_for_kv,
start_pos as u32,
hs_u,
hs_u,
);
// Phase E: output projection → prefill_gate_buf (residual
// scratch; FFN's add_rmsnorm_batch fuses the add).
self.encode_mul_mat_reg_tile(
&mut enc,
w_o,
&self.prefill_normed_buf,
&self.prefill_gate_buf,
n_u,
hs_u,
hs_u,
hs_u,
);
}
// ─── Phase 7: FFN ──────────────────────────────────────────────
// Fused add(prefill_gate_buf residual) + ffn_norm.
self.encode_add_rmsnorm_batch(
&mut enc,
&self.prefill_batch_buf,
&self.prefill_normed_buf,
&lw.ffn_norm,
&self.prefill_gate_buf,
n_u,
hs_u,
);
// gate + up GEMMs.
self.encode_mul_mat_reg_tile(
&mut enc,
&lw.ffn_gate,
&self.prefill_normed_buf,
&self.prefill_gate_buf,
n_u,
hs_u,
hs_u,
is_u,
);
self.encode_mul_mat_reg_tile(
&mut enc,
&lw.ffn_up,
&self.prefill_normed_buf,
&self.prefill_up_buf,
n_u,
hs_u,
hs_u,
is_u,
);
// silu_mul over the full N × is buffer.
{
let total = n_u * is_u;
let params: [u32; 2] = [total, 0];
let p_buf = self
.ctx
.upload_storage(bytemuck::cast_slice(¶ms), "silu_mul_batch_params");
let bg = self
.ctx
.device
.create_bind_group(&wgpu::BindGroupDescriptor {
label: None,
layout: &self.pipelines.silu_mul_inplace.get_bind_group_layout(0),
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: self.prefill_gate_buf.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: self.prefill_up_buf.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 2,
resource: p_buf.as_entire_binding(),
},
],
});
self.encode(
&mut enc,
&self.pipelines.silu_mul_inplace,
&bg,
(total.div_ceil(256), 1, 1),
"silu_mul_batch",
);
}
// FFN down → prefill_up_buf (next layer's residual scratch).
// The next layer's add_rmsnorm_batch reads from this buffer
// as `residual`; using `prefill_up_buf` (rather than
// `prefill_normed_buf` which Metal uses) keeps the dst and
// residual bindings on distinct buffers — see the Phase 1
// comment above for the wgpu validation reason. The buffer
// is is×N, plenty of room for hs×N writes.
self.encode_mul_mat_reg_tile(
&mut enc,
&lw.ffn_down,
&self.prefill_gate_buf,
&self.prefill_up_buf,
n_u,
is_u,
is_u,
hs_u,
);
}
// ─── Final residual add: batch_buf += prefill_up_buf ──────────────
// Last layer's FFN down residual lives in `prefill_up_buf`;
// add it back into the running residual stream.
{
let total = n_u * hs_u;
let params: [u32; 2] = [total, 0];
let p_buf = self
.ctx
.upload_storage(bytemuck::cast_slice(¶ms), "final_add_params");
let bg = self
.ctx
.device
.create_bind_group(&wgpu::BindGroupDescriptor {
label: None,
layout: &self.pipelines.add_inplace.get_bind_group_layout(0),
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: self.prefill_batch_buf.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: self.prefill_up_buf.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 2,
resource: p_buf.as_entire_binding(),
},
],
});
self.encode(
&mut enc,
&self.pipelines.add_inplace,
&bg,
(total.div_ceil(256), 1, 1),
"final_add",
);
}
// ─── Final output: norm + LM head, last token only ────────────────
// Copy batch_buf[(n-1)*hs..n*hs] → hidden_buf (single-token
// scratch), then rmsnorm + output projection through the existing
// single-token helpers.
let last_off_bytes = ((n - 1) * hs * 4) as u64;
enc.copy_buffer_to_buffer(
&self.prefill_batch_buf,
last_off_bytes,
&self.hidden_buf,
0,
(hs * 4) as u64,
);
self.encode_rmsnorm(
&mut enc,
&self.hidden_buf,
&self.output_norm,
hs_u,
cfg.rms_norm_eps,
);
// Output projection uses the tied embedding (f32). Reuses the
// existing per-token gemv_f32 helper.
self.encode_gemv_f32(
&mut enc,
&self.embedding,
&self.hidden_buf,
&self.logits_buf,
cfg.vocab_size as u32,
hs_u,
);
self.submit_and_wait(enc);
// Update seq_len mirrors after the GPU work completes.
self.gpu_state
.seq_len
.store(start_pos + n, Ordering::Relaxed);
state.seq_len = start_pos + n;
self.ctx.finish_profiler();
self.ctx.download_f32(&self.logits_buf, cfg.vocab_size)
}
}
impl GpuLfm2Model {
/// Lock-free body of `Model::snapshot_state`. Callers that already
/// hold `infer_lock` (e.g. `forward_prefill`'s prefix-cache write
/// step) call this directly to avoid a recursive `Mutex::lock()`
/// deadlock — `std::sync::Mutex` is not reentrant.
///
/// Snapshot layout (mirrors Metal's pattern but with f32 KV instead
/// of f16): per attention layer, download the live `seq_len * kv_dim`
/// floats from K and V; per conv layer, download the full
/// `d_conv * hidden_size` rolling buffer. f32 → bytes via
/// `bytemuck::cast_slice` on the contiguous `Vec<f32>` from
/// `download_f32` (source-aligned, safe).
fn snapshot_state_locked(&self) -> StateSnapshot {
let seq_len = self.gpu_state.seq_len.load(Ordering::Relaxed);
let cfg = &self.config;
// Use config.head_dim, NOT hidden_size/n_heads: Qwen3 decouples head_dim
// (attention.key_length), so the KV cache is sized by config.head_dim. The
// stale formula under-counts the snapshot/restore floats and corrupts the
// KV cache on a prefix-cache hit. Matches the from_weight_source alloc.
let head_dim = cfg.head_dim;
let kernel_size = cfg.conv_kernel_size.unwrap_or(3);
let d_conv = kernel_size - 1;
// `download_f32` now slices the staging buffer to exactly
// `count * 4` bytes, so the returned `Vec<f32>` length
// equals `count` directly — no truncation needed. The
// closure is kept as the single calling site so a future
// regression in `download_f32` re-introduces a single edit
// point, not N call sites.
let download_exact =
|buf: &wgpu::Buffer, count: usize| -> Vec<f32> { self.ctx.download_f32(buf, count) };
let mut layers = Vec::with_capacity(cfg.n_layers);
for i in 0..cfg.n_layers {
if cfg.block_types[i] == BlockType::Attention {
let kv_dim = cfg.kv_heads_per_layer[i] * head_dim;
let count = seq_len * kv_dim;
let (k_buf, v_buf) = self.gpu_state.kv_caches[i]
.as_ref()
.expect("attention layer must have KV buffers");
let k_floats = download_exact(k_buf, count);
let v_floats = download_exact(v_buf, count);
layers.push(LayerSnapshot::Attention {
k_data: bytemuck::cast_slice(&k_floats).to_vec(),
v_data: bytemuck::cast_slice(&v_floats).to_vec(),
});
} else {
let count = d_conv * cfg.hidden_size;
let conv_buf = self.gpu_state.conv_buffers[i]
.as_ref()
.expect("conv layer must have rolling buffer");
let floats = download_exact(conv_buf, count);
layers.push(LayerSnapshot::Conv {
buffer: bytemuck::cast_slice(&floats).to_vec(),
});
}
}
StateSnapshot { layers, seq_len }
}
/// Lock-free body of `Model::restore_state`. See
/// [`Self::snapshot_state_locked`] for the locking contract.
/// Writes raw bytes via `queue.write_buffer` at offset 0 — wgpu's
/// `COPY_BUFFER_ALIGNMENT` is 4, which f32 byte counts always
/// satisfy. The remainder of the pre-allocated cache (past
/// `seq_len * kv_dim`) is left as-is; the kernels only read up
/// to the seq_len reported by the atomic, so stale tail data
/// can't influence subsequent forwards.
fn restore_state_locked(&self, snapshot: &StateSnapshot) {
let cfg = &self.config;
for (i, layer_snap) in snapshot.layers.iter().enumerate() {
match layer_snap {
LayerSnapshot::Attention { k_data, v_data } => {
assert_eq!(
cfg.block_types[i],
BlockType::Attention,
"snapshot layer {i} attention vs state config"
);
let (k_buf, v_buf) = self.gpu_state.kv_caches[i]
.as_ref()
.expect("attention layer must have KV buffers");
self.ctx.queue.write_buffer(k_buf, 0, k_data);
self.ctx.queue.write_buffer(v_buf, 0, v_data);
}
LayerSnapshot::Conv { buffer } => {
assert_eq!(
cfg.block_types[i],
BlockType::GatedConv,
"snapshot layer {i} conv vs state config"
);
let conv_buf = self.gpu_state.conv_buffers[i]
.as_ref()
.expect("conv layer must have rolling buffer");
self.ctx.queue.write_buffer(conv_buf, 0, buffer);
}
LayerSnapshot::AttentionCompressed { .. } => {
// Unreachable in normal operation: wgpu doesn't
// configure TurboQuant compression. `model_id`
// is `"wgpu:..."` vs CPU's `"cpu:..."`, separating
// their on-disk namespaces. Panic on the hard
// error path so an accidental cross-namespace
// load surfaces fast instead of corrupting state.
panic!(
"GpuLfm2Model::restore_state_locked received \
a TurboQuant-compressed snapshot at layer {i}; \
wgpu does not support TurboQuant. This indicates \
a cross-backend cache-namespace leak."
);
}
}
}
self.gpu_state
.seq_len
.store(snapshot.seq_len, Ordering::Relaxed);
}
/// Zero every conv layer's GPU rolling buffer. Called on a fresh
/// prefill (`start_pos == 0`) cache MISS so stale conv state
/// from a prior generation can't leak into the new run. Cache
/// HITs go through `restore_state_locked` which overwrites the
/// buffers from the snapshot, so this only fires on the cold
/// path. Mirrors `MetalLfm2Model::zero_conv_buffers_locked`.
///
/// Conv layers always read the entire rolling buffer regardless
/// of `seq_len`, so the seq_len atomic reset alone isn't enough
/// to fence stale state. Without this an FFI / long-lived
/// process that reuses the same `GpuLfm2Model` across multiple
/// `Session`s would drift on conv state.
///
/// Uses wgpu's native `clear_buffer` so the zero fill happens
/// GPU-side — no CPU-allocated zero buffer, no CPU→GPU upload.
/// One encoder, one submit, regardless of layer count.
fn zero_conv_buffers_locked(&self) {
let cfg = &self.config;
let mut enc = self
.ctx
.device
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("zero_conv_buffers"),
});
for i in 0..cfg.n_layers {
if cfg.block_types[i] == BlockType::GatedConv
&& let Some(conv_buf) = self.gpu_state.conv_buffers[i].as_ref()
{
// `None` size = clear entire buffer.
enc.clear_buffer(conv_buf, 0, None);
}
}
self.ctx.queue.submit(Some(enc.finish()));
}
}
impl Model for GpuLfm2Model {
fn forward(&self, tokens: &[u32], pos: usize, state: &mut InferenceState) -> Vec<f32> {
let _guard = self.infer_lock.lock().expect("infer_lock poisoned");
self.forward_inner(tokens, pos, state)
}
fn forward_greedy(&self, tokens: &[u32], pos: usize, state: &mut InferenceState) -> u32 {
let _guard = self.infer_lock.lock().expect("infer_lock poisoned");
self.forward_greedy_inner(tokens, pos, state)
}
// forward_embedding and forward_from_embedding use default impls
// (unimplemented). Audio generation requires Metal backend for now.
// wgpu support would need refactoring forward() to split the layer
// dispatch from the logit projection, plus a hidden_buf download path.
fn forward_prefill(
&self,
tokens: &[u32],
start_pos: usize,
state: &mut InferenceState,
) -> Vec<f32> {
let _guard = self.infer_lock.lock().expect("infer_lock poisoned");
// Reset internal seq_len so repeated generate() calls (bench) work.
self.gpu_state.seq_len.store(start_pos, Ordering::Relaxed);
// Cache lookup: only on a fresh prefill (`start_pos == 0`).
// Continuation prefills (chunked / mid-sequence) carry KV from
// the prior chunk; restoring would clobber it. Same gate Metal
// and CPU use.
if start_pos == 0 {
let hit = self
.prefix_cache
.lock()
.expect("prefix_cache mutex poisoned")
.find_longest_prefix(tokens);
if let Some((snapshot, prefix_len)) = hit {
// Strict-prefix hits only. A `prefix_len == tokens.len()`
// hit would force `use_len = tokens.len() - 1`, but the
// restored state already reflects "after all tokens" —
// re-running the last token would advance the conv
// rolling buffer one position past where it should be
// and overwrite already-correct attention KV cells.
// The conv layer state isn't seq_len-gated, so the
// off-by-one would corrupt logits.
if prefix_len < tokens.len() && prefix_len > 0 {
let use_len = prefix_len;
self.restore_state_locked(&snapshot);
// `restore_state_locked` set `gpu_state.seq_len`
// to `snapshot.seq_len == prefix_len`, which
// matches `use_len` in this strict-prefix path.
// (Kept explicit so future use_len-vs-prefix_len
// splits don't drift.)
self.gpu_state.seq_len.store(use_len, Ordering::Relaxed);
state.seq_len = use_len;
// Skip the per-token vocab-sized download_f32 for
// every prefill step except the last — only the
// final logits are returned to the caller.
// `prefix_len < tokens.len()` is enforced above, so
// `remaining` is always >= 1 here.
let remaining = &tokens[use_len..];
let last = remaining.len() - 1;
let mut logits = Vec::new();
for (j, &token) in remaining.iter().enumerate() {
if j == last {
logits = self.forward_inner(&[token], use_len + j, state);
} else {
self.forward_inner_compute(&[token], use_len + j, state);
}
}
self.prefix_cache
.lock()
.expect("prefix_cache mutex poisoned")
.insert(tokens, self.snapshot_state_locked());
return logits;
}
}
// Cache miss on a fresh prefill: zero the GPU conv
// rolling buffers so stale state from a prior
// generation can't leak in. Cache hits skip this
// (`restore_state_locked` rewrites the buffers from
// the snapshot). Mirrors the equivalent fix on Metal.
self.zero_conv_buffers_locked();
// Try the batched prefill path. Preconditions:
// * fresh prefill (start_pos == 0, already checked above)
// * non-empty
// * all matmul weights have a batched quantized kernel
// (Q4_0/Q8_0 today; other dtypes fall through to the
// per-token loop)
// * the model wires the batched-prefill path (`batched_prefill`).
// LFM2 does; the dense transformers prefill via the per-token
// decode loop (their batched shaders are a follow-up), so they
// fall through to the sequential loop below.
//
// Long prompts are chunked through the batched path in
// MAX_PREFILL_TOKENS-sized chunks so the scratch buffers stay
// bounded. Each chunk advances `start_pos`; conv rolling
// state and KV cache writes carry across chunks naturally.
if !tokens.is_empty()
&& self.batched_prefill
&& self.all_matmul_weights_batched_supported()
{
// Chunk size respects both the static MAX_PREFILL_TOKENS
// budget AND the model's actual `max_seq_len` — otherwise
// a caller with `--context-size < 512` would dispatch
// batched chunks larger than the KV cache and OOB on the
// copy_buffer_to_buffer write.
let chunk_size = self.gpu_state.max_seq_len.min(MAX_PREFILL_TOKENS);
let mut logits = Vec::new();
let mut pos = 0usize;
while pos < tokens.len() {
let end = (pos + chunk_size).min(tokens.len());
// `start_pos + pos` rather than `pos`: defensive against
// a future caller passing non-zero start_pos through
// this branch (today the outer `if start_pos == 0`
// gate makes them equal).
logits = self.forward_prefill_batched_locked(
&tokens[pos..end],
start_pos + pos,
state,
);
pos = end;
}
self.prefix_cache
.lock()
.expect("prefix_cache mutex poisoned")
.insert(tokens, self.snapshot_state_locked());
return logits;
}
}
// Cache miss (or continuation prefill): full prefill loop.
// Sequential single-token forward via the lock-free body — calling
// `self.forward()` here would re-acquire the (non-reentrant)
// `infer_lock` we already hold and deadlock.
//
// For every step except the last, drive the GPU via
// `forward_inner_compute` so the per-token vocab-sized
// `download_f32` is skipped — only the final iteration's
// logits make it back to the caller. At p=4096 this drops
// 4095 vocab-sized blocking readbacks (vocab × 4 bytes ×
// 4095 = ~1 GB at vocab=65536). Empty `tokens` makes
// `last` underflow — guarded by `if !tokens.is_empty()`.
let mut logits = Vec::new();
if !tokens.is_empty() {
let last = tokens.len() - 1;
for (i, &token) in tokens.iter().enumerate() {
if i == last {
logits = self.forward_inner(&[token], start_pos + i, state);
} else {
self.forward_inner_compute(&[token], start_pos + i, state);
}
}
}
if start_pos == 0 {
self.prefix_cache
.lock()
.expect("prefix_cache mutex poisoned")
.insert(tokens, self.snapshot_state_locked());
}
logits
}
fn configure_cache(&self, config: crate::kv_cache::KvCacheConfig) {
*self
.prefix_cache
.lock()
.expect("prefix_cache mutex poisoned") =
KvPrefixCache::new(config, &self.config, &format!("wgpu:{}", self.model_id));
}
/// Public Model trait surface for `_locked` snapshot/restore so
/// external state-management callers (FFI / parity harness)
/// can drive the prefix cache directly without going through
/// `forward_prefill`. Mirrors `MetalLfm2Model`'s overrides.
fn snapshot_state(&self) -> StateSnapshot {
let _guard = self.infer_lock.lock().expect("infer_lock poisoned");
self.snapshot_state_locked()
}
fn restore_state(&self, snapshot: &StateSnapshot) {
let _guard = self.infer_lock.lock().expect("infer_lock poisoned");
self.restore_state_locked(snapshot);
}
fn config(&self) -> &ModelConfig {
&self.config
}
}