rullama 0.3.0

Browser-resident Gemma 4 inference: pure Rust → WebAssembly + WebGPU. Loads Ollama's on-disk GGUF blobs and runs the forward pass on the local GPU via hand-written WGSL.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
// Per-token / per-layer dispatchers take many dims (d_model, n_heads,
// head_dim, ffn, n_kv, eps, ...) — they mirror the Go reference and
// bundling them into a struct adds boilerplate without clarity.
#![allow(clippy::too_many_arguments)]

//! Chained GPU forward pass: one [`wgpu::CommandEncoder`] per token, one submit,
//! one final logits readback. Targets ≥ 10 tok/s on M-series Mac.
//!
//! Architecture:
//! * All scratch tensors live in persistent `wgpu::Buffer`s allocated at construction
//!   time (sized to the model's max-shape worst case across all layers).
//! * Per-layer K/V caches are full-history GPU buffers; we append at offset =
//!   `kv_lens[i] * n_kv_heads * head_dim * 4` via `copy_buffer_to_buffer` inside the
//!   token's encoder. KV-shared layers alias the donor's `Arc<wgpu::Buffer>`.
//! * The CPU-resident token embedding row is dequantized once per token and
//!   uploaded — too small (single row of Q6_K) to be worth a GPU kernel.
//! * Logits are read back at the end of each token. Sampling stays on CPU.
//!
//! Behaviour mirrors `forward_gpu::forward_token_gpu` op-for-op; that function
//! remains the parity oracle and is now invoked only by `examples/forward_parity`.

use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};

use crate::backend::dispatch::{
    attention_backward_dkv_chained, attention_backward_dq_chained, attention_chained,
    attention_probs_chained, cross_entropy_backward_chained, geglu_backward_chained, geglu_chained,
    lora_matmul_col_chained, lora_matmul_row_chained, lora_outer_add_chained, make_dummy_storage,
    matmul_q4_k_backward_input_chained, matmul_q4_k_chained, matmul_q6_k_backward_input_chained,
    matmul_q6_k_chained, residual_add_chained, rmsnorm_backward_chained, rmsnorm_chained,
    rmsnorm_per_row_backward_chained, rmsnorm_per_row_chained, rope_neox_backward_chained,
    rope_neox_chained, scale_chained, softcap_chained,
};

/// Activation capture buffers for one transformer layer. Used by the
/// training backward pass to read forward intermediates without
/// recomputing them. Sized for a **single query position** (M0); M1
/// will extend along a seq axis.
///
/// Each buffer must be a STORAGE | COPY_DST | COPY_SRC `wgpu::Buffer`
/// large enough to hold the named tensor at the layer's per-position
/// shape (see `crates/rullama-finetune/src/scratch.rs`).
pub struct LayerCaptureBuffers<'a> {
    /// `self.hidden` snapshot at the start of the layer ([d_model]).
    pub hidden_in: &'a wgpu::Buffer,
    /// Output of attn rmsnorm ([d_model]).
    pub norm_x_attn: &'a wgpu::Buffer,
    /// q matmul output before q_norm rmsnorm ([n_heads · head_dim]).
    pub q_pre_norm: &'a wgpu::Buffer,
    /// q after q_norm rmsnorm AND RoPE ([n_heads · head_dim]).
    pub q_post_rope: &'a wgpu::Buffer,
    /// k matmul output before k_norm rmsnorm ([n_kv · head_dim]).
    pub k_pre_norm: &'a wgpu::Buffer,
    /// v matmul output before v_norm rmsnorm ([n_kv · head_dim]).
    pub v_pre_norm: &'a wgpu::Buffer,
    /// Attention output, input to o_proj ([n_heads · head_dim]).
    pub attn_out: &'a wgpu::Buffer,
    /// o_proj matmul output, input to post_attn_norm rmsnorm ([d_model]).
    pub attn_proj: &'a wgpu::Buffer,
    /// `self.hidden` after the attn residual add ([d_model]).
    pub pre_ffn_rms: &'a wgpu::Buffer,
    /// Output of ffn rmsnorm ([d_model]).
    pub norm_x_ffn: &'a wgpu::Buffer,
    /// Gate matmul output ([ffn_inter]).
    pub ffn_gate: &'a wgpu::Buffer,
    /// Up matmul output ([ffn_inter]).
    pub ffn_up: &'a wgpu::Buffer,
    /// GEGLU output, input to ffn_down ([ffn_inter]).
    pub ffn_act: &'a wgpu::Buffer,
    /// ffn_down matmul output, input to post_ffw_norm rmsnorm ([d_model]).
    pub ffn_out: &'a wgpu::Buffer,
    /// PLE: `inp_gate_w · hidden` (input to PLE GEGLU's gate branch).
    /// Only written when `cfg.has_ple()`. `[ple_dim]`.
    pub ple_state: &'a wgpu::Buffer,
    /// PLE: output of GEGLU (input to `proj_w` matmul). `[ple_dim]`.
    pub ple_act: &'a wgpu::Buffer,
    /// PLE: output of `proj_w` matmul (input to PLE rmsnorm). `[d_model]`.
    pub ple_proj: &'a wgpu::Buffer,
}

/// One LoRA wrapper's GPU state — A, B, and a small `z` scratch that
/// the forward correction writes into and the backward reads from.
///
/// Forward: `y[out_dim] = W·x + scale · B · (A·x)`. The `z` buffer
/// holds `A·x` (size `[rank]`) after the forward correction so the
/// backward can build `dB = scale · dy ⊗ z`.
pub struct LoraSlot<'a> {
    pub a: &'a wgpu::Buffer, // [rank, in_dim]
    pub b: &'a wgpu::Buffer, // [out_dim, rank]
    pub z: &'a wgpu::Buffer, // [rank] scratch
    pub rank: u32,
    pub scale: f32, // alpha / rank
}

/// Per-layer progress callback fired between encoder submits during
/// a forward + backward layer walk. Signature:
/// `(phase, current, total)` where `phase` is one of `"forward"` /
/// `"backward"` and `current` is 1-based logical layer index. Used
/// by training to drive a VisionProgress-style status strip (see
/// `examples/web/src/components/TrainingProgress.tsx`) — without the
/// per-layer beacons the user stares at a "step 0 / N" counter while
/// a 30 s pipeline-compile + first step grinds in silence.
pub type LayerProgressCb<'a> = dyn Fn(&str, u32, u32) + 'a;

/// Per-layer LoRA slots for the four attention projections + three
/// FFN projections. Pass `None` for any projection that isn't
/// LoRA-wrapped.
pub struct LayerLoraSlots<'a> {
    pub q: Option<LoraSlot<'a>>,
    pub k: Option<LoraSlot<'a>>,
    pub v: Option<LoraSlot<'a>>,
    pub o: Option<LoraSlot<'a>>,
    pub ffn_gate: Option<LoraSlot<'a>>,
    pub ffn_up: Option<LoraSlot<'a>>,
    pub ffn_down: Option<LoraSlot<'a>>,
}
use crate::backend::{Pipelines, WeightCache, WgpuCtx};
use crate::error::{Result, RullamaError};
use crate::gguf::GgmlDtype;
use crate::model::config::{Gemma4Config, LayerKind};
use crate::reference::forward::build_donor_map_pub;
use crate::reference::weights::Weights;

use bytemuck::{Pod, Zeroable};
use futures_channel::oneshot;

/// Maximum supported KV history length. Determines per-layer KV buffer size:
/// `MAX_CONTEXT * n_kv_heads(i) * head_dim(i) * 4 bytes` per layer per (K,V).
/// 4096 chosen so a 35-layer Gemma 4 e2b config fits comfortably under 1 GiB.
pub const MAX_CONTEXT: u32 = 4096;

pub struct Forward {
    cfg: Gemma4Config,
    ctx: WgpuCtx,
    pipes: Arc<Pipelines>,
    wcache: Arc<WeightCache>,
    weights: Weights,

    // Running residual stream (d_model f32). Layer body writes into this in-place.
    hidden: wgpu::Buffer,

    // Per-layer scratch (max-shape sized).
    norm_x: wgpu::Buffer, // d_model
    norm_y: wgpu::Buffer, // d_model
    q: wgpu::Buffer,      // n_heads * head_dim_max
    q_norm: wgpu::Buffer, // n_heads * head_dim_max (post-norm Q)
    k: wgpu::Buffer,      // n_kv_heads_max * head_dim_max
    k_norm: wgpu::Buffer,
    v: wgpu::Buffer,
    v_norm: wgpu::Buffer,
    attn_out_buf: wgpu::Buffer, // n_heads * head_dim_max
    attn_proj: wgpu::Buffer,    // d_model
    ffn_gate: wgpu::Buffer,     // ffn_inter_max
    ffn_up: wgpu::Buffer,
    ffn_act: wgpu::Buffer,
    ffn_out: wgpu::Buffer, // d_model

    // PLE prep (computed once per token, then sliced per-layer).
    per_layer_residual: wgpu::Buffer, // n_layers * ple_dim
    per_layer_proj: wgpu::Buffer,
    per_layer: wgpu::Buffer, // final per-layer inputs

    // PLE per-layer scratch.
    ple_state: wgpu::Buffer, // ple_dim
    ple_act: wgpu::Buffer,   // ple_dim
    ple_proj: wgpu::Buffer,  // d_model

    // Output projection per-tile scratch (sized to max tile rows). Each output tile
    // matmul writes into this; we then copy_buffer_to_buffer into `logits` at the
    // correct vocab-offset (storage-buffer offset alignment is 256, but
    // copy_buffer_to_buffer alignment is just 4).
    logits_tile: wgpu::Buffer,

    // Output.
    logits: wgpu::Buffer,
    logits_read: wgpu::Buffer,

    // KV cache: one Buffer per layer for K and per layer for V, possibly aliased
    // (KV-shared layers point to the donor's Arc).
    kv_k: Vec<Arc<wgpu::Buffer>>,
    kv_v: Vec<Arc<wgpu::Buffer>>,
    kv_lens: Vec<u32>,
    donor_map: Vec<Option<u32>>,

    // Per-layer output scalar (typically only on global layers; one f32 each).
    // Loaded once at construction so the encoder doesn't have to read from CPU.
    layer_scalars: Vec<Option<f32>>,

    // Bound dummy zero buffer for "no weight" / "no factors" slots.
    dummy: wgpu::Buffer,

    /// Cap the KV cache can grow to (configured at construction). Step
    /// methods bounds-check against this instead of the compile-time
    /// `MAX_CONTEXT`, so a mobile build with a smaller cache can still
    /// surface a clean "context length exceeded" error.
    max_context: u32,

    /// Cooperative cancel flag for in-flight forward + backward layer
    /// walks. The training cancel button flips this; the per-layer
    /// loops in `run_forward_from_hidden` and `backward_step` check it
    /// after each `encode_layer`. Bounded latency: one layer (~300 ms-
    /// 1 s on browser) instead of one full step (10-30 s). Mirrors
    /// the `Model::encode_cancel` pattern used for multimodal.
    cancel_flag: Arc<AtomicBool>,

    // Cached scale factor for the final logits softcap dispatch.
    pos: u32,
}

impl Forward {
    /// Default constructor — preallocates KV cache for `MAX_CONTEXT` tokens.
    pub async fn new(
        cfg: Gemma4Config,
        ctx: WgpuCtx,
        pipes: Arc<Pipelines>,
        weights: Weights,
        wcache: Arc<WeightCache>,
    ) -> Result<Self> {
        Self::new_with_max_context(cfg, ctx, pipes, weights, wcache, MAX_CONTEXT).await
    }

    /// Variant of [`new`] that lets the caller cap the KV-cache pre-allocation
    /// at fewer than `MAX_CONTEXT` tokens. The KV cache is the dominant GPU
    /// memory cost at load time: per non-donor layer it's
    /// `max_context * n_kv_heads * head_dim * 4 bytes` × 2 (K and V). On
    /// gemma4:e2b a `max_context=4096` cache lands at several hundred MB
    /// before any tensor is uploaded; on iPhone-class shared RAM (8 GB total)
    /// that's enough to push the WebContent process over Jetsam during the
    /// first inference step. Mobile callers pass a smaller value (e.g. 512)
    /// and get a working model that just can't grow past that turn length.
    pub async fn new_with_max_context(
        cfg: Gemma4Config,
        ctx: WgpuCtx,
        pipes: Arc<Pipelines>,
        weights: Weights,
        wcache: Arc<WeightCache>,
        max_context: u32,
    ) -> Result<Self> {
        if max_context == 0 || max_context > MAX_CONTEXT {
            return Err(crate::error::RullamaError::Inference(format!(
                "max_context={max_context} out of range (1..={MAX_CONTEXT})"
            )));
        }
        let device = &ctx.device;

        let alloc_storage = |label: &str, n: usize| -> wgpu::Buffer {
            device.create_buffer(&wgpu::BufferDescriptor {
                label: Some(label),
                size: (n * 4).max(4) as u64,
                usage: wgpu::BufferUsages::STORAGE
                    | wgpu::BufferUsages::COPY_DST
                    | wgpu::BufferUsages::COPY_SRC,
                mapped_at_creation: false,
            })
        };

        let d_model = cfg.d_model as usize;
        let n_heads = cfg.n_heads as usize;
        let head_dim_max = cfg.head_dim_global.max(cfg.head_dim_swa) as usize;
        let n_kv_heads_max = cfg.n_kv_heads_global.max(cfg.n_kv_heads_swa) as usize;
        let ffn_inter_max = (0..cfg.n_layers).map(|i| cfg.ffn(i)).max().unwrap_or(0) as usize;
        let ple_dim = cfg.ple_dim as usize;
        let n_layers = cfg.n_layers as usize;
        let vocab = cfg.vocab_size as usize;

        let hidden = alloc_storage("fwd.hidden", d_model);
        let norm_x = alloc_storage("fwd.norm_x", d_model);
        let norm_y = alloc_storage("fwd.norm_y", d_model);
        let q = alloc_storage("fwd.q", n_heads * head_dim_max);
        let q_norm = alloc_storage("fwd.q_norm", n_heads * head_dim_max);
        let k = alloc_storage("fwd.k", n_kv_heads_max * head_dim_max);
        let k_norm = alloc_storage("fwd.k_norm", n_kv_heads_max * head_dim_max);
        let v = alloc_storage("fwd.v", n_kv_heads_max * head_dim_max);
        let v_norm = alloc_storage("fwd.v_norm", n_kv_heads_max * head_dim_max);
        let attn_out_buf = alloc_storage("fwd.attn_out", n_heads * head_dim_max);
        let attn_proj = alloc_storage("fwd.attn_proj", d_model);
        let ffn_gate = alloc_storage("fwd.ffn_gate", ffn_inter_max);
        let ffn_up = alloc_storage("fwd.ffn_up", ffn_inter_max);
        let ffn_act = alloc_storage("fwd.ffn_act", ffn_inter_max);
        let ffn_out = alloc_storage("fwd.ffn_out", d_model);

        let per_layer_residual = alloc_storage("fwd.per_layer_residual", n_layers * ple_dim.max(1));
        let per_layer_proj = alloc_storage("fwd.per_layer_proj", n_layers * ple_dim.max(1));
        let per_layer = alloc_storage("fwd.per_layer", n_layers * ple_dim.max(1));

        let ple_state = alloc_storage("fwd.ple_state", ple_dim.max(1));
        let ple_act = alloc_storage("fwd.ple_act", ple_dim.max(1));
        let ple_proj = alloc_storage("fwd.ple_proj", d_model);

        // Output projection tile scratch: large enough to hold the worst-case tile
        // (MAX_TILE_BYTES / row_bytes rows × 4 bytes per row of f32 logits). 80 MiB
        // tile / 1 byte-per-row-of-Q6_K... actually the tile size is in *weight*
        // bytes, not output bytes. The output is n_rows f32, where n_rows is at
        // most ceil(MAX_TILE_BYTES / row_bytes_of_token_embd). For Gemma 4 e2b
        // that's roughly 80 MiB / 1228 bytes/row ≈ 68 K rows × 4 = 272 KB. We
        // overprovision to vocab_size to keep things simple.
        let logits_tile = alloc_storage("fwd.logits_tile", vocab);

        let logits = device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("fwd.logits"),
            size: (vocab * 4) as u64,
            usage: wgpu::BufferUsages::STORAGE
                | wgpu::BufferUsages::COPY_SRC
                | wgpu::BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });
        let logits_read = device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("fwd.logits_read"),
            size: (vocab * 4) as u64,
            usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ,
            mapped_at_creation: false,
        });

        // KV cache: alloc owned buffers for non-donor layers, then alias the rest.
        let donor_map = build_donor_map_pub(&cfg);
        let mut kv_k_opt: Vec<Option<Arc<wgpu::Buffer>>> = vec![None; n_layers];
        let mut kv_v_opt: Vec<Option<Arc<wgpu::Buffer>>> = vec![None; n_layers];
        for i in 0..n_layers {
            if donor_map[i].is_none() {
                let n_kv = cfg.n_kv_heads(i as u32) as usize;
                let hd = cfg.head_dim(i as u32) as usize;
                let bytes = (max_context as usize * n_kv * hd * 4) as u64;
                kv_k_opt[i] = Some(Arc::new(device.create_buffer(&wgpu::BufferDescriptor {
                    label: Some(&format!("fwd.kv_k.{i}")),
                    size: bytes,
                    usage: wgpu::BufferUsages::STORAGE
                        | wgpu::BufferUsages::COPY_DST
                        | wgpu::BufferUsages::COPY_SRC,
                    mapped_at_creation: false,
                })));
                kv_v_opt[i] = Some(Arc::new(device.create_buffer(&wgpu::BufferDescriptor {
                    label: Some(&format!("fwd.kv_v.{i}")),
                    size: bytes,
                    usage: wgpu::BufferUsages::STORAGE
                        | wgpu::BufferUsages::COPY_DST
                        | wgpu::BufferUsages::COPY_SRC,
                    mapped_at_creation: false,
                })));
            }
        }
        for i in 0..n_layers {
            if let Some(d) = donor_map[i] {
                kv_k_opt[i] = kv_k_opt[d as usize].clone();
                kv_v_opt[i] = kv_v_opt[d as usize].clone();
            }
        }
        let kv_k: Vec<Arc<wgpu::Buffer>> = kv_k_opt.into_iter().map(|x| x.unwrap()).collect();
        let kv_v: Vec<Arc<wgpu::Buffer>> = kv_v_opt.into_iter().map(|x| x.unwrap()).collect();
        let kv_lens = vec![0u32; n_layers];

        let dummy = make_dummy_storage(device, "fwd.dummy");

        // Load per-layer output scalars once. The CPU oracle does
        // `weights.load_opt(layer_output_scale.weight)?.first()` per layer per
        // token; we cache the f32 here so the encoder can hand it to scale_chained
        // without an extra GPU↔CPU bounce.
        let mut layer_scalars: Vec<Option<f32>> = Vec::with_capacity(n_layers);
        for i in 0..cfg.n_layers {
            let name = format!("blk.{i}.layer_output_scale.weight");
            let v = weights.load_opt_async(&name).await?;
            layer_scalars.push(v.and_then(|vec| vec.first().copied()));
        }

        Ok(Self {
            cfg,
            ctx,
            pipes,
            wcache,
            weights,
            hidden,
            norm_x,
            norm_y,
            q,
            q_norm,
            k,
            k_norm,
            v,
            v_norm,
            attn_out_buf,
            attn_proj,
            ffn_gate,
            ffn_up,
            ffn_act,
            ffn_out,
            per_layer_residual,
            per_layer_proj,
            per_layer,
            ple_state,
            ple_act,
            ple_proj,
            logits_tile,
            logits,
            logits_read,
            kv_k,
            kv_v,
            kv_lens,
            donor_map,
            layer_scalars,
            dummy,
            max_context,
            cancel_flag: Arc::new(AtomicBool::new(false)),
            pos: 0,
        })
    }

    /// Flip the cooperative cancel flag. Any in-flight forward or
    /// backward layer walk bails with `RullamaError::Cancelled` at
    /// the next layer boundary. Safe to call when no work is
    /// in-flight — the flag is cleared at the top of each `step` /
    /// `step_with_lora*` / `backward_step` call.
    pub fn cancel(&self) {
        self.cancel_flag.store(true, Ordering::Release);
    }

    /// Clear the cancel flag. Called at the top of each layer-walking
    /// entry point so a stale flag from a previous cancel doesn't
    /// poison the next step.
    fn reset_cancel(&self) {
        self.cancel_flag.store(false, Ordering::Release);
    }

    /// Check the cancel flag — returns `Err(Cancelled)` if it's set.
    /// Called between per-layer encoder submits.
    fn check_cancelled(&self) -> Result<()> {
        if self.cancel_flag.load(Ordering::Acquire) {
            Err(RullamaError::Cancelled)
        } else {
            Ok(())
        }
    }

    /// Shared cancel-flag handle so `TrainingSession::cancel` can
    /// reach the flag without taking a `&mut` borrow on the model.
    pub fn cancel_flag(&self) -> Arc<AtomicBool> {
        self.cancel_flag.clone()
    }

    pub fn cfg(&self) -> &Gemma4Config {
        &self.cfg
    }
    pub fn pos(&self) -> u32 {
        self.pos
    }
    /// Borrow the shared GPU weight cache. Exposed so `Model` can evict
    /// multimodal tower weights between turns.
    pub fn wcache(&self) -> &Arc<WeightCache> {
        &self.wcache
    }
    /// Borrow the GPU context (`WgpuCtx` is internally `Arc`-backed and
    /// cheap to clone). Used by `rullama-finetune` to allocate LoRA and
    /// scratch buffers on the same device + queue as the model.
    pub fn ctx(&self) -> &WgpuCtx {
        &self.ctx
    }
    /// Borrow the pipeline cache. The training crate doesn't need this
    /// directly (the backward path goes through `Forward::backward_step`),
    /// but exposing it keeps the surface symmetric for future test code.
    pub fn pipes(&self) -> &std::sync::Arc<Pipelines> {
        &self.pipes
    }
    /// Read-only handle on the model's logits buffer (post-forward).
    /// `TrainingSession::step` uses this to feed
    /// `cross_entropy_backward` without exposing the rest of Forward's
    /// scratch.
    pub fn logits_buffer(&self) -> &wgpu::Buffer {
        &self.logits
    }

    /// Access the running `hidden` residual buffer. Exposed for the
    /// training crate's single-forward PerPosition orchestrator,
    /// which captures `self.hidden` (= pre-final-norm) per position.
    pub fn hidden_buffer(&self) -> &wgpu::Buffer {
        &self.hidden
    }

    /// Run final rmsnorm + the tiled output projection (no
    /// softcap) over the current `self.hidden`, leaving the result
    /// in `self.logits`. Used by the single-forward PerPosition
    /// backward to compute logits at any captured pre-final-norm
    /// position without re-running the layer stack.
    pub async fn run_final_norm_and_output_proj_only(&mut self) -> Result<()> {
        let d_model = self.cfg.d_model as usize;
        let eps = self.cfg.rms_norm_eps;
        let wc = self.wcache.clone();
        let final_norm = wc.buffer_async("output_norm.weight").await?;
        let token_embd_dtype = wc.dtype("token_embd.weight")?;

        let mut enc = self
            .ctx
            .device
            .create_command_encoder(&wgpu::CommandEncoderDescriptor {
                label: Some("fwd.out_proj_only"),
            });
        rmsnorm_chained(
            &self.ctx,
            &self.pipes,
            &mut enc,
            &self.hidden,
            Some(&final_norm),
            &self.dummy,
            &self.norm_x,
            d_model,
            eps,
        );
        self.ctx.queue.submit(Some(enc.finish()));

        // Tiled output projection — same MAX_TILE_BYTES discipline as
        // the in-line one in `run_forward_from_hidden`.
        const MAX_TILE_BYTES: usize = 8 * 1024 * 1024;
        let tiles = wc
            .buffer_tiles_async("token_embd.weight", MAX_TILE_BYTES)
            .await?;
        for tile in &tiles {
            let mut enc = self
                .ctx
                .device
                .create_command_encoder(&wgpu::CommandEncoderDescriptor {
                    label: Some("fwd.out_proj_only.tile"),
                });
            run_matmul_into_buf(
                &self.ctx,
                &self.pipes,
                &mut enc,
                token_embd_dtype,
                &tile.buffer,
                &self.norm_x,
                &self.logits_tile,
                tile.n_rows,
                d_model,
                "fwd.out_proj_only_tile",
            )?;
            enc.copy_buffer_to_buffer(
                &self.logits_tile,
                0,
                &self.logits,
                (tile.row_start as u64) * 4,
                (tile.n_rows as u64) * 4,
            );
            self.ctx.queue.submit(Some(enc.finish()));
        }
        Ok(())
    }

    /// Overwrite `self.hidden` from a slice of `src` at byte offset
    /// `src_offset`. Used by the single-forward PerPosition
    /// orchestrator to point the final-norm + output proj at a
    /// previously captured per-position pre-final-norm slice.
    pub fn set_hidden_from(&self, src: &wgpu::Buffer, src_offset: u64) {
        let d_model = self.cfg.d_model as usize;
        let bytes = (d_model as u64) * 4;
        let mut enc = self
            .ctx
            .device
            .create_command_encoder(&wgpu::CommandEncoderDescriptor {
                label: Some("fwd.set_hidden_from"),
            });
        enc.copy_buffer_to_buffer(src, src_offset, &self.hidden, 0, bytes);
        self.ctx.queue.submit(Some(enc.finish()));
    }

    pub fn reset(&mut self) {
        self.pos = 0;
        for l in self.kv_lens.iter_mut() {
            *l = 0;
        }
    }

    /// Hash of the per-layer KV geometry. Used to refuse a `load_kv` from a
    /// snapshot taken under a different model architecture (e.g. user
    /// switched gemma4 variants between sessions).
    fn kv_layout_hash(&self) -> u32 {
        let mut h: u32 = 0x811C9DC5; // FNV-1a offset basis
        for i in 0..self.cfg.n_layers {
            let nkv = self.cfg.n_kv_heads(i);
            let hd = self.cfg.head_dim(i);
            for byte in nkv.to_le_bytes().iter().chain(hd.to_le_bytes().iter()) {
                h ^= *byte as u32;
                h = h.wrapping_mul(0x01000193);
            }
        }
        h
    }

    /// Snapshot the KV cache + position counter into a versioned byte blob
    /// for suspend/resume. Format (little-endian):
    ///
    /// ```text
    ///   [0..4]    magic = "RLKV"
    ///   [4]       version = 1
    ///   [5]       n_owned_layers (u8) — non-donor layer count
    ///   [6..8]    reserved
    ///   [8..12]   position (u32) — Forward.pos at snapshot time
    ///   [12..16]  layout_hash (u32)
    ///   per owned layer (12 bytes each):
    ///     layer_idx  (u32)
    ///     kv_len     (u32) — tokens, not bytes
    ///     n_kv_heads (u16)
    ///     head_dim   (u16)
    ///   raw payload, same order as headers:
    ///     K bytes [kv_len * n_kv_heads * head_dim * 4]
    ///     V bytes [same]
    /// ```
    ///
    /// Donor layers carry no separate data — on `load_kv` they pick up the
    /// donor's KV via their shared Arc. `kv_lens` is per-layer; donor /
    /// dependent layers' counters stay at 0 by construction.
    pub async fn dump_kv(&self) -> Result<Vec<u8>> {
        let n_layers = self.cfg.n_layers as usize;

        struct Section {
            layer_idx: u32,
            kv_len: u32,
            n_kv_heads: u16,
            head_dim: u16,
            bytes: u64,
        }
        let mut sections: Vec<Section> = Vec::new();
        let mut total_payload: u64 = 0;
        for i in 0..n_layers {
            if self.donor_map[i].is_some() {
                continue;
            }
            let kv_len = self.kv_lens[i];
            if kv_len == 0 {
                continue;
            }
            let nkv = self.cfg.n_kv_heads(i as u32);
            let hd = self.cfg.head_dim(i as u32);
            let bytes = (kv_len as u64) * (nkv as u64) * (hd as u64) * 4;
            sections.push(Section {
                layer_idx: i as u32,
                kv_len,
                n_kv_heads: nkv as u16,
                head_dim: hd as u16,
                bytes,
            });
            total_payload += bytes * 2; // K + V
        }

        let mut header = Vec::<u8>::with_capacity(16 + 12 * sections.len());
        header.extend_from_slice(b"RLKV");
        header.push(1u8);
        header.push(sections.len() as u8);
        header.extend_from_slice(&[0u8, 0u8]);
        header.extend_from_slice(&self.pos.to_le_bytes());
        header.extend_from_slice(&self.kv_layout_hash().to_le_bytes());
        for s in &sections {
            header.extend_from_slice(&s.layer_idx.to_le_bytes());
            header.extend_from_slice(&s.kv_len.to_le_bytes());
            header.extend_from_slice(&s.n_kv_heads.to_le_bytes());
            header.extend_from_slice(&s.head_dim.to_le_bytes());
        }

        if total_payload == 0 {
            return Ok(header);
        }

        // One staging buffer + one encoder for all K/V copies — minimizes
        // submission overhead on the suspension-warning hot path.
        let staging = self.ctx.device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("fwd.kv_dump.staging"),
            size: total_payload,
            usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ,
            mapped_at_creation: false,
        });
        let mut enc = self
            .ctx
            .device
            .create_command_encoder(&wgpu::CommandEncoderDescriptor {
                label: Some("fwd.kv_dump.enc"),
            });
        let mut offset: u64 = 0;
        for s in &sections {
            let i = s.layer_idx as usize;
            enc.copy_buffer_to_buffer(&self.kv_k[i], 0, &staging, offset, s.bytes);
            offset += s.bytes;
            enc.copy_buffer_to_buffer(&self.kv_v[i], 0, &staging, offset, s.bytes);
            offset += s.bytes;
        }
        self.ctx.queue.submit(Some(enc.finish()));
        let payload = read_back_bytes(&self.ctx.device, &staging).await?;

        let mut out = header;
        out.extend_from_slice(&payload);
        Ok(out)
    }

    /// Inverse of [`dump_kv`]. Validates the header (magic, version,
    /// layout_hash), uploads payload bytes back into the existing
    /// pre-allocated K/V buffers, and restores `pos` + `kv_lens`.
    ///
    /// Returns an error (without mutating self) if the snapshot is from a
    /// different model architecture, the format is unknown, or the byte
    /// count doesn't match the headers.
    pub fn load_kv(&mut self, bytes: &[u8]) -> Result<()> {
        if bytes.len() < 16 {
            return Err(RullamaError::Inference(format!(
                "kv snapshot too short: {} bytes",
                bytes.len()
            )));
        }
        if &bytes[0..4] != b"RLKV" {
            return Err(RullamaError::Inference("kv snapshot: bad magic".into()));
        }
        let version = bytes[4];
        if version != 1 {
            return Err(RullamaError::Inference(format!(
                "kv snapshot: unknown version {version}"
            )));
        }
        let n_owned = bytes[5] as usize;
        let position = u32::from_le_bytes(bytes[8..12].try_into().unwrap());
        let layout_hash = u32::from_le_bytes(bytes[12..16].try_into().unwrap());
        let expected_hash = self.kv_layout_hash();
        if layout_hash != expected_hash {
            return Err(RullamaError::Inference(format!(
                "kv snapshot: layout_hash mismatch (snapshot=0x{layout_hash:08X}, model=0x{expected_hash:08X})"
            )));
        }
        let header_size = 16 + 12 * n_owned;
        if bytes.len() < header_size {
            return Err(RullamaError::Inference(
                "kv snapshot: truncated header".into(),
            ));
        }
        if position > self.max_context {
            return Err(RullamaError::Inference(format!(
                "kv snapshot: position {position} exceeds max_context {}",
                self.max_context
            )));
        }

        struct Section {
            layer_idx: u32,
            kv_len: u32,
            bytes: u64,
        }
        let mut sections: Vec<Section> = Vec::with_capacity(n_owned);
        let mut total_payload: u64 = 0;
        for s in 0..n_owned {
            let off = 16 + 12 * s;
            let layer_idx = u32::from_le_bytes(bytes[off..off + 4].try_into().unwrap());
            let kv_len = u32::from_le_bytes(bytes[off + 4..off + 8].try_into().unwrap());
            let nkv = u16::from_le_bytes(bytes[off + 8..off + 10].try_into().unwrap());
            let hd = u16::from_le_bytes(bytes[off + 10..off + 12].try_into().unwrap());

            if (layer_idx as usize) >= self.kv_lens.len() {
                return Err(RullamaError::Inference(format!(
                    "kv snapshot: layer_idx {layer_idx} out of range"
                )));
            }
            if self.donor_map[layer_idx as usize].is_some() {
                return Err(RullamaError::Inference(format!(
                    "kv snapshot: layer {layer_idx} marked as donor in current model but snapshot has data"
                )));
            }
            let exp_nkv = self.cfg.n_kv_heads(layer_idx) as u16;
            let exp_hd = self.cfg.head_dim(layer_idx) as u16;
            if nkv != exp_nkv || hd != exp_hd {
                return Err(RullamaError::Inference(format!(
                    "kv snapshot: layer {layer_idx} geometry mismatch \
                     (snapshot n_kv={nkv} hd={hd}, model n_kv={exp_nkv} hd={exp_hd})"
                )));
            }
            if kv_len > self.max_context {
                return Err(RullamaError::Inference(format!(
                    "kv snapshot: layer {layer_idx} kv_len {kv_len} exceeds max_context {}",
                    self.max_context
                )));
            }
            let layer_bytes = (kv_len as u64) * (nkv as u64) * (hd as u64) * 4;
            sections.push(Section {
                layer_idx,
                kv_len,
                bytes: layer_bytes,
            });
            total_payload += layer_bytes * 2;
        }
        let payload_off = header_size;
        if (bytes.len() as u64) < (payload_off as u64) + total_payload {
            return Err(RullamaError::Inference(format!(
                "kv snapshot: payload truncated (have {}, need {})",
                bytes.len() - payload_off,
                total_payload,
            )));
        }

        // Validation passed — commit. write_buffer is synchronous from the
        // caller's POV; the queue copies on submit.
        let queue = &self.ctx.queue;
        let mut off: usize = payload_off;
        for s in &sections {
            let i = s.layer_idx as usize;
            let n = s.bytes as usize;
            queue.write_buffer(&self.kv_k[i], 0, &bytes[off..off + n]);
            off += n;
            queue.write_buffer(&self.kv_v[i], 0, &bytes[off..off + n]);
            off += n;
            self.kv_lens[i] = s.kv_len;
        }
        // Clear non-owned layers (donor-dependents stay at 0; non-snapshot
        // owned layers reset to 0 so the model behaves like an empty cache
        // for them).
        for i in 0..self.kv_lens.len() {
            if self.donor_map[i].is_some() {
                continue;
            }
            if !sections.iter().any(|s| s.layer_idx as usize == i) {
                self.kv_lens[i] = 0;
            }
        }
        self.pos = position;
        Ok(())
    }

    /// Run one forward step from a token id. Looks up the token's embedding row,
    /// uploads it to the hidden buffer, then runs the rest of the forward.
    pub async fn step(&mut self, token_id: u32) -> Result<Vec<f32>> {
        self.step_inner(token_id, None, None).await
    }

    /// Run one forward step **with per-layer activation capture** into
    /// the supplied buffers. Used by the training backward pass —
    /// `capture[i]` receives the layer-`i` intermediates needed by the
    /// reverse walker. Pass exactly `cfg.n_layers` entries.
    ///
    /// Capture only emits `copy_buffer_to_buffer` commands inside the
    /// per-token encoder; there is no extra submit. Adds ~12 small
    /// copies per layer (≤ d_model floats each), trivial vs. the
    /// per-layer matmul cost.
    pub async fn step_capture<'a>(
        &mut self,
        token_id: u32,
        capture: &'a [LayerCaptureBuffers<'a>],
        loras: Option<&'a [LayerLoraSlots<'a>]>,
    ) -> Result<Vec<f32>> {
        if capture.len() != self.cfg.n_layers as usize {
            return Err(RullamaError::Inference(format!(
                "step_capture: got {} capture layers, expected {}",
                capture.len(),
                self.cfg.n_layers
            )));
        }
        if let Some(l) = loras
            && l.len() != self.cfg.n_layers as usize
        {
            return Err(RullamaError::Inference(format!(
                "step_capture: got {} lora slots, expected {}",
                l.len(),
                self.cfg.n_layers
            )));
        }
        self.step_inner(token_id, Some(capture), loras).await
    }

    /// Run a forward step with LoRA correction enabled but **without**
    /// capturing activations. Used for the prompt-prefill pass during
    /// training (positions 0..N-2 just fill KV; only the final position
    /// is captured + has its loss measured).
    pub async fn step_with_lora<'a>(
        &mut self,
        token_id: u32,
        loras: &'a [LayerLoraSlots<'a>],
    ) -> Result<Vec<f32>> {
        if loras.len() != self.cfg.n_layers as usize {
            return Err(RullamaError::Inference(format!(
                "step_with_lora: got {} lora slots, expected {}",
                loras.len(),
                self.cfg.n_layers
            )));
        }
        self.step_inner(token_id, None, Some(loras)).await
    }

    /// Same as [`step_with_lora`] but ALSO captures the per-position
    /// seq-shaped activations (`norm_x_attn`, `k_pre_norm`,
    /// `v_pre_norm`) into the supplied capture buffers at offset
    /// `pos·per_position_size`. Used during training prefill so the
    /// per-history K/V LoRA backward can read each position's
    /// activations without re-running forward.
    ///
    /// The 11 non-seq captures (q*, attn_out, attn_proj, hidden_in,
    /// pre_ffn_rms, norm_x_ffn, ffn_*, ple_*) are STILL written by
    /// `encode_layer` at offset 0 — they get overwritten by every
    /// position. Only the seq captures are position-stable.
    pub async fn step_with_lora_seqcap<'a>(
        &mut self,
        token_id: u32,
        loras: &'a [LayerLoraSlots<'a>],
        capture: &'a [LayerCaptureBuffers<'a>],
    ) -> Result<Vec<f32>> {
        self.step_with_lora_seqcap_with_progress(token_id, loras, capture, None)
            .await
    }

    /// Variant of [`step_with_lora_seqcap`] that fires
    /// `progress_cb(layer_index, total_layers, "forward")` between
    /// per-layer encoder submits. Used by training to drive a
    /// detailed status indicator without rewriting the existing
    /// callers that don't care about per-layer ticks.
    pub async fn step_with_lora_seqcap_with_progress<'a>(
        &mut self,
        token_id: u32,
        loras: &'a [LayerLoraSlots<'a>],
        capture: &'a [LayerCaptureBuffers<'a>],
        progress_cb: Option<&LayerProgressCb<'_>>,
    ) -> Result<Vec<f32>> {
        if loras.len() != self.cfg.n_layers as usize {
            return Err(RullamaError::Inference(format!(
                "step_with_lora_seqcap: got {} lora slots, expected {}",
                loras.len(),
                self.cfg.n_layers
            )));
        }
        if capture.len() != self.cfg.n_layers as usize {
            return Err(RullamaError::Inference(format!(
                "step_with_lora_seqcap: got {} captures, expected {}",
                capture.len(),
                self.cfg.n_layers
            )));
        }
        self.step_inner_with_progress(token_id, Some(capture), Some(loras), progress_cb)
            .await
    }

    async fn step_inner<'a>(
        &mut self,
        token_id: u32,
        capture: Option<&'a [LayerCaptureBuffers<'a>]>,
        loras: Option<&'a [LayerLoraSlots<'a>]>,
    ) -> Result<Vec<f32>> {
        self.step_inner_with_progress(token_id, capture, loras, None)
            .await
    }

    async fn step_inner_with_progress<'a>(
        &mut self,
        token_id: u32,
        capture: Option<&'a [LayerCaptureBuffers<'a>]>,
        loras: Option<&'a [LayerLoraSlots<'a>]>,
        progress_cb: Option<&LayerProgressCb<'_>>,
    ) -> Result<Vec<f32>> {
        if (token_id as u64) >= self.cfg.vocab_size as u64 {
            return Err(RullamaError::Inference(format!(
                "token_id {token_id} >= vocab_size {}",
                self.cfg.vocab_size
            )));
        }
        if self.pos >= self.max_context {
            return Err(RullamaError::Inference(format!(
                "context length exceeded max_context={}",
                self.max_context
            )));
        }
        let d_model = self.cfg.d_model as usize;
        let ple_dim = self.cfg.ple_dim as usize;

        // ---- CPU-side per-token preamble: token embed + PLE input dequant + upload ----
        let mut hidden_cpu = self
            .weights
            .load_row_async("token_embd.weight", token_id as usize)
            .await?;
        let scale_factor = (d_model as f32).sqrt();
        for v in hidden_cpu.iter_mut() {
            *v *= scale_factor;
        }
        self.ctx
            .queue
            .write_buffer(&self.hidden, 0, bytemuck::cast_slice(&hidden_cpu));
        drop(hidden_cpu);

        if self.cfg.has_ple() {
            let mut ple_in = self
                .weights
                .load_row_async("per_layer_token_embd.weight", token_id as usize)
                .await?;
            let s = (ple_dim as f32).sqrt();
            for v in ple_in.iter_mut() {
                *v *= s;
            }
            self.ctx
                .queue
                .write_buffer(&self.per_layer_residual, 0, bytemuck::cast_slice(&ple_in));
            drop(ple_in);
        }

        self.run_forward_from_hidden_with_progress(capture, loras, progress_cb)
            .await
    }

    /// Run one forward step from a pre-computed `[d_model]` embedding (vision soft
    /// token, audio soft token, etc.). Skips the `token_embd` lookup; the caller is
    /// responsible for the embedding scale (vision/audio projectors already produce
    /// rmsnorm-normalised outputs).
    ///
    /// PLE prep is run with a zeroed per-layer-residual — there is no
    /// `per_layer_token_embd` lookup possible without a token id; the per-layer
    /// projection from the residual stream still contributes. This matches
    /// Ollama's behaviour: multimodal soft tokens flow through the LM as frozen
    /// inputs and don't get PLE injection.
    pub async fn step_with_embedding(&mut self, embedding: &[f32]) -> Result<Vec<f32>> {
        self.step_with_embedding_inner(embedding, None).await
    }

    /// Variant of [`step_with_embedding`] that applies a LoRA adapter
    /// to every layer's q/k/v/o (+ optional FFN) during the forward.
    /// Used by `Model::step_with_embedding_native` when an inference
    /// adapter is active — without this, image and audio soft-token
    /// steps would silently bypass the loaded adapter while pure-text
    /// steps respect it.
    pub async fn step_with_embedding_with_lora<'a>(
        &mut self,
        embedding: &[f32],
        loras: &'a [LayerLoraSlots<'a>],
    ) -> Result<Vec<f32>> {
        if loras.len() != self.cfg.n_layers as usize {
            return Err(RullamaError::Inference(format!(
                "step_with_embedding_with_lora: got {} lora slots, expected {}",
                loras.len(),
                self.cfg.n_layers
            )));
        }
        self.step_with_embedding_inner(embedding, Some(loras)).await
    }

    async fn step_with_embedding_inner<'a>(
        &mut self,
        embedding: &[f32],
        loras: Option<&'a [LayerLoraSlots<'a>]>,
    ) -> Result<Vec<f32>> {
        let d_model = self.cfg.d_model as usize;
        if embedding.len() != d_model {
            return Err(RullamaError::Inference(format!(
                "step_with_embedding: got {} f32s, expected d_model = {d_model}",
                embedding.len(),
            )));
        }
        if self.pos >= self.max_context {
            return Err(RullamaError::Inference(format!(
                "context length exceeded max_context={}",
                self.max_context
            )));
        }
        // Direct upload — caller's embedding is the new hidden state.
        self.ctx
            .queue
            .write_buffer(&self.hidden, 0, bytemuck::cast_slice(embedding));

        // Zero out per_layer_residual for this step (no token id → no PLE lookup).
        if self.cfg.has_ple() {
            let n_layers = self.cfg.n_layers as usize;
            let zeros = vec![0f32; n_layers * self.cfg.ple_dim as usize];
            self.ctx
                .queue
                .write_buffer(&self.per_layer_residual, 0, bytemuck::cast_slice(&zeros));
        }

        self.run_forward_from_hidden(None, loras).await
    }

    /// Forward pass starting from `self.hidden` already populated. Shared by
    /// `step` (token-id path) and `step_with_embedding` (multimodal soft tokens).
    async fn run_forward_from_hidden<'a>(
        &mut self,
        capture: Option<&'a [LayerCaptureBuffers<'a>]>,
        loras: Option<&'a [LayerLoraSlots<'a>]>,
    ) -> Result<Vec<f32>> {
        self.run_forward_from_hidden_with_progress(capture, loras, None)
            .await
    }

    /// Variant of [`run_forward_from_hidden`] that fires
    /// `progress_cb(layer_index, total_layers, "forward")` between
    /// per-layer encoder submits. Used by training; chat-side
    /// inference passes `None`.
    async fn run_forward_from_hidden_with_progress<'a>(
        &mut self,
        capture: Option<&'a [LayerCaptureBuffers<'a>]>,
        loras: Option<&'a [LayerLoraSlots<'a>]>,
        progress_cb: Option<&LayerProgressCb<'_>>,
    ) -> Result<Vec<f32>> {
        // Clear any stale cancel flag from a previous step so this
        // call starts fresh; the per-layer loop below checks it after
        // each `encode_layer`.
        self.reset_cancel();
        let d_model = self.cfg.d_model as usize;
        let n_layers = self.cfg.n_layers as usize;
        let ple_dim = self.cfg.ple_dim as usize;
        let eps = self.cfg.rms_norm_eps;
        let pos = self.pos;

        // ---- weights we need on GPU before encoder construction ----
        // (WeightCache.buffer_async fetches + uploads on first touch; cached afterwards.)
        let wc = self.wcache.clone();
        let final_norm = wc.buffer_async("output_norm.weight").await?;
        let token_embd_dtype = wc.dtype("token_embd.weight")?;

        // PLE prep weights
        let (ple_proj_w_buf, ple_proj_norm_w_buf, ple_proj_n) = if self.cfg.has_ple() {
            if wc.dtype("per_layer_model_proj.weight")? != GgmlDtype::Q4_K {
                return Err(RullamaError::Inference(
                    "per_layer_model_proj expected Q4_K".into(),
                ));
            }
            let proj_w = wc.buffer_async("per_layer_model_proj.weight").await?;
            let proj_norm = wc.buffer_async("per_layer_proj_norm.weight").await?;
            (Some(proj_w), Some(proj_norm), n_layers * ple_dim)
        } else {
            (None, None, 0)
        };

        // ---- build the per-token CommandEncoder ----
        let mut enc = self
            .ctx
            .device
            .create_command_encoder(&wgpu::CommandEncoderDescriptor {
                label: Some("fwd.token_encoder"),
            });

        // ---- PLE prep (chained, all GPU) ----
        // per_layer_residual *= sqrt(ple_dim)  → already done CPU-side above (one mul each).
        // proj = matmul(per_layer_model_proj, hidden) → per_layer_proj
        // proj *= 1/sqrt(d_model)
        // per_layer = rmsnorm_per_row(per_layer_proj, per_layer_proj_norm.weight)
        // per_layer += per_layer_residual
        // per_layer *= 1/sqrt(2)
        if self.cfg.has_ple() {
            let proj_w = ple_proj_w_buf.as_ref().unwrap();
            let proj_norm_w = ple_proj_norm_w_buf.as_ref().unwrap();

            matmul_q4_k_chained(
                &self.ctx,
                &self.pipes,
                &mut enc,
                proj_w,
                &self.hidden,
                &self.per_layer_proj,
                d_model,
                ple_proj_n,
            );
            scale_chained(
                &self.ctx,
                &self.pipes,
                &mut enc,
                &self.per_layer_proj,
                ple_proj_n,
                1.0 / (d_model as f32).sqrt(),
            );
            rmsnorm_per_row_chained(
                &self.ctx,
                &self.pipes,
                &mut enc,
                &self.per_layer_proj,
                Some(proj_norm_w),
                &self.dummy,
                &self.per_layer,
                n_layers,
                ple_dim,
                eps,
            );
            residual_add_chained(
                &self.ctx,
                &self.pipes,
                &mut enc,
                &self.per_layer,
                &self.per_layer_residual,
                ple_proj_n,
            );
            scale_chained(
                &self.ctx,
                &self.pipes,
                &mut enc,
                &self.per_layer,
                ple_proj_n,
                1.0 / 2.0_f32.sqrt(),
            );
        }

        // ---- transformer layers ----
        // Per-layer submit + restart. Each flush hands its commands off to the
        // GPU and frees the CPU-side encoder; persistent buffer state on the
        // GPU is unaffected. Empirically anything wider than 1 layer per
        // submit (tried 3) re-introduces the iPhone WebContent crash on the
        // first step — the per-layer cadence is the working strip-line.
        for i in 0..n_layers as u32 {
            let cap = capture.map(|c| &c[i as usize]);
            let lora = loras.map(|l| &l[i as usize]);
            self.encode_layer(&mut enc, i, pos, cap, lora).await?;
            self.ctx.queue.submit(Some(enc.finish()));
            // Per-layer cancel check. Encoder submits are the natural
            // boundary because the GPU is idle between layers under
            // this submission strategy. Bounded latency: one layer
            // (~300 ms - 1 s on browser) instead of one full step.
            self.check_cancelled()?;
            // Per-layer progress beacon — fired AFTER the submit so
            // the caller's "layer N done" message correlates with the
            // GPU having actually finished it. `i + 1` is 1-based for
            // "N of n_layers" UX semantics.
            if let Some(cb) = progress_cb {
                cb("forward", i + 1, n_layers as u32);
            }
            enc = self
                .ctx
                .device
                .create_command_encoder(&wgpu::CommandEncoderDescriptor {
                    label: Some("fwd.token_encoder.cont"),
                });
        }

        // ---- final norm (in-place into hidden via norm_y as scratch) ----
        rmsnorm_chained(
            &self.ctx,
            &self.pipes,
            &mut enc,
            &self.hidden,
            Some(&final_norm),
            &self.dummy,
            &self.norm_x,
            d_model,
            eps,
        );

        // Flush before the output projection — it's the second-largest concentration
        // of GPU work in the step (262K-row matmul against the embedding) and we
        // don't want it queued behind a still-encoding layer batch.
        self.ctx.queue.submit(Some(enc.finish()));
        enc = self
            .ctx
            .device
            .create_command_encoder(&wgpu::CommandEncoderDescriptor {
                label: Some("fwd.out_proj_encoder"),
            });

        // ---- output projection (tiled): tile along vocab axis ----
        // Each tile matmul writes its rows into `logits_tile` starting at offset 0
        // (so it always satisfies the storage-binding alignment), then we copy
        // those bytes into `logits` at offset `row_start * 4` (copy_buffer_to_buffer
        // only needs 4-byte alignment). Submit between tiles too, for the same
        // command-buffer-size reason that we submit between layers.
        // token_embd is the largest single tensor in the model (315 MiB
        // compressed Q6_K for gemma4:e2b). Empirically 80 MiB tiles crash
        // the WebContent process on iPhone 16e mid-step even after the
        // wasm-side per-tile range fetch landed — the issue isn't the
        // staging allocation, it's a single 80 MiB wgpu::Buffer creation
        // on top of ~2 GB of resident layer weights. 8 MiB tiles work.
        const MAX_TILE_BYTES: usize = 8 * 1024 * 1024;
        let tiles = wc
            .buffer_tiles_async("token_embd.weight", MAX_TILE_BYTES)
            .await?;
        for tile in &tiles {
            run_matmul_into_buf(
                &self.ctx,
                &self.pipes,
                &mut enc,
                token_embd_dtype,
                &tile.buffer,
                &self.norm_x,
                &self.logits_tile,
                tile.n_rows,
                d_model,
                "fwd.output_tile",
            )?;
            enc.copy_buffer_to_buffer(
                &self.logits_tile,
                0,
                &self.logits,
                (tile.row_start as u64) * 4,
                (tile.n_rows as u64) * 4,
            );
            self.ctx.queue.submit(Some(enc.finish()));
            enc = self
                .ctx
                .device
                .create_command_encoder(&wgpu::CommandEncoderDescriptor {
                    label: Some("fwd.out_proj_encoder.cont"),
                });
        }

        // ---- softcap ----
        // Out-of-place: read from `logits`, write into `logits_tile`. wgpu
        // disallows binding the same buffer as both read-only and read-write
        // within one dispatch, so we can't softcap in-place.
        let final_src: &wgpu::Buffer = if self.cfg.final_logit_softcap > 0.0 {
            softcap_chained(
                &self.ctx,
                &self.pipes,
                &mut enc,
                &self.logits,
                &self.logits_tile,
                self.cfg.vocab_size as usize,
                self.cfg.final_logit_softcap,
            );
            &self.logits_tile
        } else {
            &self.logits
        };

        // ---- copy logits → readback buffer ----
        enc.copy_buffer_to_buffer(
            final_src,
            0,
            &self.logits_read,
            0,
            (self.cfg.vocab_size as u64) * 4,
        );

        // ---- submit + readback ----
        self.ctx.queue.submit(Some(enc.finish()));
        let logits = read_back_f32(&self.ctx.device, &self.logits_read).await?;

        self.pos = self.pos.saturating_add(1);
        Ok(logits)
    }

    async fn encode_layer<'a>(
        &mut self,
        enc: &mut wgpu::CommandEncoder,
        i: u32,
        pos: u32,
        capture: Option<&'a LayerCaptureBuffers<'a>>,
        loras: Option<&'a LayerLoraSlots<'a>>,
    ) -> Result<()> {
        let prefix = format!("blk.{i}.");
        let d_model = self.cfg.d_model as usize;
        let eps = self.cfg.rms_norm_eps;
        let n_heads = self.cfg.n_heads as usize;
        let n_kv_heads = self.cfg.n_kv_heads(i) as usize;
        let head_dim = self.cfg.head_dim(i) as usize;
        let ffn_n = self.cfg.ffn(i) as usize;
        let kind = self.cfg.kind(i);
        let donor = self.donor_map[i as usize];

        // ---- CAPTURE: hidden_in (start-of-layer residual stream) ----
        if let Some(cap) = capture {
            enc.copy_buffer_to_buffer(
                &self.hidden,
                0,
                cap.hidden_in,
                (pos as u64) * (d_model as u64) * 4,
                (d_model * 4) as u64,
            );
        }

        // Pre-fetch all weights this layer needs (each is cached after first call).
        let attn_norm_w = self
            .wcache
            .buffer_async(&format!("{prefix}attn_norm.weight"))
            .await?;
        let post_attn_w = self
            .wcache
            .buffer_async(&format!("{prefix}post_attention_norm.weight"))
            .await?;
        let mlp_norm_w = self
            .wcache
            .buffer_async(&format!("{prefix}ffn_norm.weight"))
            .await?;
        let post_ffw_w = self
            .wcache
            .buffer_async(&format!("{prefix}post_ffw_norm.weight"))
            .await?;

        let q_w = self
            .wcache
            .buffer_async(&format!("{prefix}attn_q.weight"))
            .await?;
        let q_norm_w = self
            .wcache
            .buffer_async(&format!("{prefix}attn_q_norm.weight"))
            .await?;
        let o_w = self
            .wcache
            .buffer_async(&format!("{prefix}attn_output.weight"))
            .await?;

        let (k_w, k_norm_w, v_w, v_w_dtype) = if donor.is_none() {
            let kw = self
                .wcache
                .buffer_async(&format!("{prefix}attn_k.weight"))
                .await?;
            let knw = self
                .wcache
                .buffer_async(&format!("{prefix}attn_k_norm.weight"))
                .await?;
            let v_name = format!("{prefix}attn_v.weight");
            let vw = self.wcache.buffer_async(&v_name).await?;
            let dt = self.wcache.dtype(&v_name)?;
            (Some(kw), Some(knw), Some(vw), Some(dt))
        } else {
            (None, None, None, None)
        };

        let gate_w = self
            .wcache
            .buffer_async(&format!("{prefix}ffn_gate.weight"))
            .await?;
        let up_w = self
            .wcache
            .buffer_async(&format!("{prefix}ffn_up.weight"))
            .await?;
        let down_name = format!("{prefix}ffn_down.weight");
        let down_w = self.wcache.buffer_async(&down_name).await?;
        let down_dtype = self.wcache.dtype(&down_name)?;

        // PLE-injection weights (only when has_ple)
        let (inp_gate_w, proj_w, post_norm_w) = if self.cfg.has_ple() {
            let a = self
                .wcache
                .buffer_async(&format!("{prefix}inp_gate.weight"))
                .await?;
            let b = self
                .wcache
                .buffer_async(&format!("{prefix}proj.weight"))
                .await?;
            let c = self
                .wcache
                .buffer_async(&format!("{prefix}post_norm.weight"))
                .await?;
            (Some(a), Some(b), Some(c))
        } else {
            (None, None, None)
        };

        let factors_w = if matches!(kind, LayerKind::Global) {
            // Same RoPE factors tensor across global layers — would benefit from caching;
            // the cache key is the tensor name so it's already a single GPU buffer.
            self.wcache.buffer_opt_async("rope_freqs.weight").await?
        } else {
            None
        };

        // ===== ATTENTION =====
        // norm_x = rmsnorm(hidden, attn_norm)
        rmsnorm_chained(
            &self.ctx,
            &self.pipes,
            enc,
            &self.hidden,
            Some(&attn_norm_w),
            &self.dummy,
            &self.norm_x,
            d_model,
            eps,
        );

        // ---- CAPTURE: norm_x_attn (input to q/k/v matmul + LoRA) ----
        if let Some(cap) = capture {
            // Per-position seq capture: write at `pos·d_model` offset.
            enc.copy_buffer_to_buffer(
                &self.norm_x,
                0,
                cap.norm_x_attn,
                (pos as u64) * (d_model as u64) * 4,
                (d_model * 4) as u64,
            );
        }

        // Q/K/V projections from norm_x
        matmul_q4_k_chained(
            &self.ctx,
            &self.pipes,
            enc,
            &q_w,
            &self.norm_x,
            &self.q,
            d_model,
            n_heads * head_dim,
        );

        // ---- LoRA forward correction (q): self.q += scale · B · (A · norm_x) ----
        if let Some(slot) = loras.and_then(|l| l.q.as_ref()) {
            // z = A · norm_x  ([rank] = [rank, d_model] @ [d_model])
            lora_matmul_row_chained(
                &self.ctx,
                &self.pipes,
                enc,
                slot.a,
                &self.norm_x,
                slot.z,
                d_model,
                slot.rank as usize,
                1.0,
                false,
            );
            // self.q += scale · B · z  ([n_heads*head_dim] += [n_heads*head_dim, rank] @ [rank])
            lora_matmul_row_chained(
                &self.ctx,
                &self.pipes,
                enc,
                slot.b,
                slot.z,
                &self.q,
                slot.rank as usize,
                n_heads * head_dim,
                slot.scale,
                true,
            );
        }

        // ---- CAPTURE: q_pre_norm (q matmul output, input to q_norm rmsnorm) ----
        if let Some(cap) = capture {
            enc.copy_buffer_to_buffer(
                &self.q,
                0,
                cap.q_pre_norm,
                (pos as u64) * (n_heads as u64) * (head_dim as u64) * 4,
                (n_heads * head_dim * 4) as u64,
            );
        }

        // per-head q_norm (weighted)
        rmsnorm_per_row_chained(
            &self.ctx,
            &self.pipes,
            enc,
            &self.q,
            Some(&q_norm_w),
            &self.dummy,
            &self.q_norm,
            n_heads,
            head_dim,
            eps,
        );
        // RoPE in-place into q_norm
        let (rope_base, rope_dims) = match kind {
            LayerKind::SlidingWindow => {
                (self.cfg.rope_freq_base_swa, self.cfg.rope_dim_swa as usize)
            }
            LayerKind::Global => (self.cfg.rope_freq_base, self.cfg.rope_dim_global as usize),
        };
        rope_neox_chained(
            &self.ctx,
            &self.pipes,
            enc,
            &self.q_norm,
            factors_w.as_ref(),
            &self.dummy,
            head_dim,
            n_heads,
            pos as usize,
            rope_dims,
            rope_base,
        );

        // ---- CAPTURE: q_post_rope (input to attention; reused in dkv pass) ----
        if let Some(cap) = capture {
            enc.copy_buffer_to_buffer(
                &self.q_norm,
                0,
                cap.q_post_rope,
                (pos as u64) * (n_heads as u64) * (head_dim as u64) * 4,
                (n_heads * head_dim * 4) as u64,
            );
        }

        if donor.is_none() {
            let kw = k_w.as_ref().unwrap();
            let knw = k_norm_w.as_ref().unwrap();
            let vw = v_w.as_ref().unwrap();
            let vdt = v_w_dtype.unwrap();

            matmul_q4_k_chained(
                &self.ctx,
                &self.pipes,
                enc,
                kw,
                &self.norm_x,
                &self.k,
                d_model,
                n_kv_heads * head_dim,
            );

            // ---- LoRA forward correction (k) ----
            if let Some(slot) = loras.and_then(|l| l.k.as_ref()) {
                lora_matmul_row_chained(
                    &self.ctx,
                    &self.pipes,
                    enc,
                    slot.a,
                    &self.norm_x,
                    slot.z,
                    d_model,
                    slot.rank as usize,
                    1.0,
                    false,
                );
                lora_matmul_row_chained(
                    &self.ctx,
                    &self.pipes,
                    enc,
                    slot.b,
                    slot.z,
                    &self.k,
                    slot.rank as usize,
                    n_kv_heads * head_dim,
                    slot.scale,
                    true,
                );
            }

            // ---- CAPTURE: k_pre_norm (k matmul output, input to k_norm rmsnorm) ----
            if let Some(cap) = capture {
                // Per-position seq capture: write at `pos·(n_kv·head_dim)` offset.
                enc.copy_buffer_to_buffer(
                    &self.k,
                    0,
                    cap.k_pre_norm,
                    (pos as u64) * (n_kv_heads as u64) * (head_dim as u64) * 4,
                    (n_kv_heads * head_dim * 4) as u64,
                );
            }

            rmsnorm_per_row_chained(
                &self.ctx,
                &self.pipes,
                enc,
                &self.k,
                Some(knw),
                &self.dummy,
                &self.k_norm,
                n_kv_heads,
                head_dim,
                eps,
            );
            rope_neox_chained(
                &self.ctx,
                &self.pipes,
                enc,
                &self.k_norm,
                factors_w.as_ref(),
                &self.dummy,
                head_dim,
                n_kv_heads,
                pos as usize,
                rope_dims,
                rope_base,
            );

            match vdt {
                GgmlDtype::Q6_K => matmul_q6_k_chained(
                    &self.ctx,
                    &self.pipes,
                    enc,
                    vw,
                    &self.norm_x,
                    &self.v,
                    d_model,
                    n_kv_heads * head_dim,
                ),
                GgmlDtype::Q4_K => matmul_q4_k_chained(
                    &self.ctx,
                    &self.pipes,
                    enc,
                    vw,
                    &self.norm_x,
                    &self.v,
                    d_model,
                    n_kv_heads * head_dim,
                ),
                other => {
                    return Err(RullamaError::Inference(format!(
                        "attn_v dtype {other:?} unsupported"
                    )));
                }
            }

            // ---- LoRA forward correction (v) ----
            if let Some(slot) = loras.and_then(|l| l.v.as_ref()) {
                lora_matmul_row_chained(
                    &self.ctx,
                    &self.pipes,
                    enc,
                    slot.a,
                    &self.norm_x,
                    slot.z,
                    d_model,
                    slot.rank as usize,
                    1.0,
                    false,
                );
                lora_matmul_row_chained(
                    &self.ctx,
                    &self.pipes,
                    enc,
                    slot.b,
                    slot.z,
                    &self.v,
                    slot.rank as usize,
                    n_kv_heads * head_dim,
                    slot.scale,
                    true,
                );
            }

            // ---- CAPTURE: v_pre_norm (v matmul output, input to unweighted v_norm rmsnorm) ----
            if let Some(cap) = capture {
                // Per-position seq capture.
                enc.copy_buffer_to_buffer(
                    &self.v,
                    0,
                    cap.v_pre_norm,
                    (pos as u64) * (n_kv_heads as u64) * (head_dim as u64) * 4,
                    (n_kv_heads * head_dim * 4) as u64,
                );
            }

            // V-norm is unweighted
            rmsnorm_per_row_chained(
                &self.ctx,
                &self.pipes,
                enc,
                &self.v,
                None,
                &self.dummy,
                &self.v_norm,
                n_kv_heads,
                head_dim,
                eps,
            );

            // Append rotated K + normed V into this layer's KV cache at offset = kv_lens[i].
            let row_bytes = (n_kv_heads * head_dim * 4) as u64;
            let dst_offset = self.kv_lens[i as usize] as u64 * row_bytes;
            enc.copy_buffer_to_buffer(
                &self.k_norm,
                0,
                &self.kv_k[i as usize],
                dst_offset,
                row_bytes,
            );
            enc.copy_buffer_to_buffer(
                &self.v_norm,
                0,
                &self.kv_v[i as usize],
                dst_offset,
                row_bytes,
            );
            self.kv_lens[i as usize] = self.kv_lens[i as usize].saturating_add(1);
        }

        // attention: kv buffers are kv_k[i], kv_v[i] (alias for donor); history_len from
        // donor's len if shared, else this layer's len (which we just incremented).
        let history_layer = donor.map(|d| d as usize).unwrap_or(i as usize);
        let history_len = self.kv_lens[history_layer] as usize;
        let window = if matches!(kind, LayerKind::SlidingWindow) {
            self.cfg.sliding_window as usize
        } else {
            0
        };

        attention_chained(
            &self.ctx,
            &self.pipes,
            enc,
            &self.q_norm,
            &self.kv_k[i as usize],
            &self.kv_v[i as usize],
            &self.attn_out_buf,
            head_dim,
            n_heads,
            n_kv_heads,
            pos as usize,
            history_len,
            window,
        );

        // ---- CAPTURE: attn_out (input to o_proj) ----
        if let Some(cap) = capture {
            enc.copy_buffer_to_buffer(
                &self.attn_out_buf,
                0,
                cap.attn_out,
                (pos as u64) * (n_heads as u64) * (head_dim as u64) * 4,
                (n_heads * head_dim * 4) as u64,
            );
        }

        // attn_proj = matmul(attn_out_buf, attn_output.weight)
        matmul_q4_k_chained(
            &self.ctx,
            &self.pipes,
            enc,
            &o_w,
            &self.attn_out_buf,
            &self.attn_proj,
            n_heads * head_dim,
            d_model,
        );

        // ---- LoRA forward correction (o): self.attn_proj += scale · B · (A · attn_out_buf) ----
        if let Some(slot) = loras.and_then(|l| l.o.as_ref()) {
            lora_matmul_row_chained(
                &self.ctx,
                &self.pipes,
                enc,
                slot.a,
                &self.attn_out_buf,
                slot.z,
                n_heads * head_dim,
                slot.rank as usize,
                1.0,
                false,
            );
            lora_matmul_row_chained(
                &self.ctx,
                &self.pipes,
                enc,
                slot.b,
                slot.z,
                &self.attn_proj,
                slot.rank as usize,
                d_model,
                slot.scale,
                true,
            );
        }

        // ---- CAPTURE: attn_proj (input to post_attn_norm rmsnorm) ----
        if let Some(cap) = capture {
            enc.copy_buffer_to_buffer(
                &self.attn_proj,
                0,
                cap.attn_proj,
                (pos as u64) * (d_model as u64) * 4,
                (d_model * 4) as u64,
            );
        }

        // norm_y = rmsnorm(attn_proj, post_attn_norm.weight)
        rmsnorm_chained(
            &self.ctx,
            &self.pipes,
            enc,
            &self.attn_proj,
            Some(&post_attn_w),
            &self.dummy,
            &self.norm_y,
            d_model,
            eps,
        );
        // hidden += norm_y
        residual_add_chained(
            &self.ctx,
            &self.pipes,
            enc,
            &self.hidden,
            &self.norm_y,
            d_model,
        );

        // ---- CAPTURE: pre_ffn_rms (hidden after attn residual add) ----
        if let Some(cap) = capture {
            enc.copy_buffer_to_buffer(
                &self.hidden,
                0,
                cap.pre_ffn_rms,
                (pos as u64) * (d_model as u64) * 4,
                (d_model * 4) as u64,
            );
        }

        // ===== MLP =====
        rmsnorm_chained(
            &self.ctx,
            &self.pipes,
            enc,
            &self.hidden,
            Some(&mlp_norm_w),
            &self.dummy,
            &self.norm_x,
            d_model,
            eps,
        );

        // ---- CAPTURE: norm_x_ffn (input to gate/up matmul + LoRA) ----
        if let Some(cap) = capture {
            enc.copy_buffer_to_buffer(
                &self.norm_x,
                0,
                cap.norm_x_ffn,
                (pos as u64) * (d_model as u64) * 4,
                (d_model * 4) as u64,
            );
        }

        matmul_q4_k_chained(
            &self.ctx,
            &self.pipes,
            enc,
            &gate_w,
            &self.norm_x,
            &self.ffn_gate,
            d_model,
            ffn_n,
        );

        // ---- LoRA forward correction (ffn_gate): ffn_gate += scale · B · (A · norm_x) ----
        if let Some(slot) = loras.and_then(|l| l.ffn_gate.as_ref()) {
            lora_matmul_row_chained(
                &self.ctx,
                &self.pipes,
                enc,
                slot.a,
                &self.norm_x,
                slot.z,
                d_model,
                slot.rank as usize,
                1.0,
                false,
            );
            lora_matmul_row_chained(
                &self.ctx,
                &self.pipes,
                enc,
                slot.b,
                slot.z,
                &self.ffn_gate,
                slot.rank as usize,
                ffn_n,
                slot.scale,
                true,
            );
        }

        matmul_q4_k_chained(
            &self.ctx,
            &self.pipes,
            enc,
            &up_w,
            &self.norm_x,
            &self.ffn_up,
            d_model,
            ffn_n,
        );

        // ---- LoRA forward correction (ffn_up): ffn_up += scale · B · (A · norm_x) ----
        if let Some(slot) = loras.and_then(|l| l.ffn_up.as_ref()) {
            lora_matmul_row_chained(
                &self.ctx,
                &self.pipes,
                enc,
                slot.a,
                &self.norm_x,
                slot.z,
                d_model,
                slot.rank as usize,
                1.0,
                false,
            );
            lora_matmul_row_chained(
                &self.ctx,
                &self.pipes,
                enc,
                slot.b,
                slot.z,
                &self.ffn_up,
                slot.rank as usize,
                ffn_n,
                slot.scale,
                true,
            );
        }

        // ---- CAPTURE: ffn_gate, ffn_up (inputs to GEGLU) ----
        if let Some(cap) = capture {
            let ffn_pos_off = (pos as u64) * (ffn_n as u64) * 4;
            enc.copy_buffer_to_buffer(
                &self.ffn_gate,
                0,
                cap.ffn_gate,
                ffn_pos_off,
                (ffn_n * 4) as u64,
            );
            enc.copy_buffer_to_buffer(&self.ffn_up, 0, cap.ffn_up, ffn_pos_off, (ffn_n * 4) as u64);
        }

        geglu_chained(
            &self.ctx,
            &self.pipes,
            enc,
            &self.ffn_gate,
            &self.ffn_up,
            &self.ffn_act,
            ffn_n,
        );

        // ---- CAPTURE: ffn_act (input to ffn_down matmul) ----
        if let Some(cap) = capture {
            enc.copy_buffer_to_buffer(
                &self.ffn_act,
                0,
                cap.ffn_act,
                (pos as u64) * (ffn_n as u64) * 4,
                (ffn_n * 4) as u64,
            );
        }

        match down_dtype {
            GgmlDtype::Q6_K => matmul_q6_k_chained(
                &self.ctx,
                &self.pipes,
                enc,
                &down_w,
                &self.ffn_act,
                &self.ffn_out,
                ffn_n,
                d_model,
            ),
            GgmlDtype::Q4_K => matmul_q4_k_chained(
                &self.ctx,
                &self.pipes,
                enc,
                &down_w,
                &self.ffn_act,
                &self.ffn_out,
                ffn_n,
                d_model,
            ),
            other => {
                return Err(RullamaError::Inference(format!(
                    "ffn_down dtype {other:?} unsupported"
                )));
            }
        }

        // ---- LoRA forward correction (ffn_down): ffn_out += scale · B · (A · ffn_act) ----
        if let Some(slot) = loras.and_then(|l| l.ffn_down.as_ref()) {
            lora_matmul_row_chained(
                &self.ctx,
                &self.pipes,
                enc,
                slot.a,
                &self.ffn_act,
                slot.z,
                ffn_n,
                slot.rank as usize,
                1.0,
                false,
            );
            lora_matmul_row_chained(
                &self.ctx,
                &self.pipes,
                enc,
                slot.b,
                slot.z,
                &self.ffn_out,
                slot.rank as usize,
                d_model,
                slot.scale,
                true,
            );
        }

        // ---- CAPTURE: ffn_out (input to post_ffw_norm rmsnorm) ----
        if let Some(cap) = capture {
            enc.copy_buffer_to_buffer(
                &self.ffn_out,
                0,
                cap.ffn_out,
                (pos as u64) * (d_model as u64) * 4,
                (d_model * 4) as u64,
            );
        }

        rmsnorm_chained(
            &self.ctx,
            &self.pipes,
            enc,
            &self.ffn_out,
            Some(&post_ffw_w),
            &self.dummy,
            &self.norm_y,
            d_model,
            eps,
        );
        residual_add_chained(
            &self.ctx,
            &self.pipes,
            enc,
            &self.hidden,
            &self.norm_y,
            d_model,
        );

        // ===== PLE injection =====
        if self.cfg.has_ple() {
            let inp_gate_w = inp_gate_w.unwrap();
            let proj_w = proj_w.unwrap();
            let post_norm_w = post_norm_w.unwrap();
            let ple_dim = self.cfg.ple_dim as usize;

            // ple_state = matmul(hidden, inp_gate_w) [d_model -> ple_dim]
            matmul_q4_k_chained(
                &self.ctx,
                &self.pipes,
                enc,
                &inp_gate_w,
                &self.hidden,
                &self.ple_state,
                d_model,
                ple_dim,
            );

            // ---- CAPTURE: ple_state (input gate branch to PLE GEGLU) ----
            if let Some(cap) = capture {
                enc.copy_buffer_to_buffer(
                    &self.ple_state,
                    0,
                    cap.ple_state,
                    (pos as u64) * (ple_dim as u64) * 4,
                    (ple_dim * 4) as u64,
                );
            }

            // Need the per-layer slice of `per_layer` as the second geglu input.
            // geglu_chained currently binds entire buffers — we'd need a sliced bind.
            // For simplicity, do a copy_buffer_to_buffer of the layer-i slice into
            // ple_act before geglu, then run geglu(ple_state, ple_act_copy). One more
            // copy per layer; trivial cost compared to the full forward.
            // Note: ple_act_copy is reused for the geglu output too — geglu does
            //   y = gate * gelu(up); the input `up` is read once before the output write.
            // To keep correctness, write the slice into a separate tmp: reuse ple_proj
            // (since it's not used until later in this block).
            let layer_off = (i as u64) * (ple_dim as u64) * 4;
            let layer_bytes = (ple_dim as u64) * 4;
            enc.copy_buffer_to_buffer(&self.per_layer, layer_off, &self.ple_proj, 0, layer_bytes);
            geglu_chained(
                &self.ctx,
                &self.pipes,
                enc,
                &self.ple_state,
                &self.ple_proj,
                &self.ple_act,
                ple_dim,
            );

            // ---- CAPTURE: ple_act (input to proj_w matmul) ----
            if let Some(cap) = capture {
                enc.copy_buffer_to_buffer(
                    &self.ple_act,
                    0,
                    cap.ple_act,
                    (pos as u64) * (ple_dim as u64) * 4,
                    (ple_dim * 4) as u64,
                );
            }

            // projected = matmul(ple_act, proj_w) [ple_dim -> d_model]
            matmul_q4_k_chained(
                &self.ctx,
                &self.pipes,
                enc,
                &proj_w,
                &self.ple_act,
                &self.ple_proj,
                ple_dim,
                d_model,
            );

            // ---- CAPTURE: ple_proj (input to PLE rmsnorm) ----
            if let Some(cap) = capture {
                enc.copy_buffer_to_buffer(
                    &self.ple_proj,
                    0,
                    cap.ple_proj,
                    (pos as u64) * (d_model as u64) * 4,
                    (d_model * 4) as u64,
                );
            }

            // norm_y = rmsnorm(ple_proj, post_norm_w)
            rmsnorm_chained(
                &self.ctx,
                &self.pipes,
                enc,
                &self.ple_proj,
                Some(&post_norm_w),
                &self.dummy,
                &self.norm_y,
                d_model,
                eps,
            );
            // hidden += norm_y
            residual_add_chained(
                &self.ctx,
                &self.pipes,
                enc,
                &self.hidden,
                &self.norm_y,
                d_model,
            );
        }

        // Per-layer output scalar (loaded at construction; applied as scale_chained).
        if let Some(s) = self.layer_scalars[i as usize] {
            scale_chained(&self.ctx, &self.pipes, enc, &self.hidden, d_model, s);
        }

        Ok(())
    }
}

// ---------- helpers ----------

#[repr(C)]
#[derive(Clone, Copy, Pod, Zeroable, Debug)]
struct MatmulParams {
    k: u32,
    n: u32,
    _p0: u32,
    _p1: u32,
}

/// Run a matmul kernel that writes its output rows starting at offset 0 of `dst`.
/// Used for the tiled output projection: caller copies the rows from `dst` into
/// the per-tile slice of the global logits buffer.
fn run_matmul_into_buf(
    ctx: &WgpuCtx,
    pipes: &Pipelines,
    enc: &mut wgpu::CommandEncoder,
    dtype: GgmlDtype,
    w: &wgpu::Buffer,
    x: &wgpu::Buffer,
    dst: &wgpu::Buffer,
    n_rows: usize,
    k: usize,
    label: &str,
) -> Result<()> {
    let device = &ctx.device;
    let queue = &ctx.queue;
    // Naive kernel beats tiled here on Apple GPUs (verified empirically on
    // M-series). Tiled pipelines stay built in case future hardware / kernel
    // tuning reverses this — flip these back if perf_bench shows tiled wins.
    let pipeline = match dtype {
        GgmlDtype::Q4_K => &pipes.q4_k_matmul,
        GgmlDtype::Q6_K => &pipes.q6_k_matmul,
        other => {
            return Err(RullamaError::Inference(format!(
                "output proj dtype {other:?} not supported"
            )));
        }
    };
    let params = MatmulParams {
        k: k as u32,
        n: n_rows as u32,
        _p0: 0,
        _p1: 0,
    };
    let p_buf = device.create_buffer(&wgpu::BufferDescriptor {
        label: Some(&format!("{label}.params")),
        size: std::mem::size_of::<MatmulParams>() as u64,
        usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
        mapped_at_creation: false,
    });
    queue.write_buffer(&p_buf, 0, bytemuck::bytes_of(&params));
    let bg = device.create_bind_group(&wgpu::BindGroupDescriptor {
        label: Some(&format!("{label}.bg")),
        layout: &pipeline.get_bind_group_layout(0),
        entries: &[
            wgpu::BindGroupEntry {
                binding: 0,
                resource: p_buf.as_entire_binding(),
            },
            wgpu::BindGroupEntry {
                binding: 1,
                resource: w.as_entire_binding(),
            },
            wgpu::BindGroupEntry {
                binding: 2,
                resource: x.as_entire_binding(),
            },
            wgpu::BindGroupEntry {
                binding: 3,
                resource: dst.as_entire_binding(),
            },
        ],
    });
    let mut cp = enc.begin_compute_pass(&wgpu::ComputePassDescriptor {
        label: Some(label),
        timestamp_writes: None,
    });
    cp.set_pipeline(pipeline);
    cp.set_bind_group(0, &bg, &[]);
    cp.dispatch_workgroups((n_rows as u32).div_ceil(64), 1, 1);
    Ok(())
}

async fn read_buf_stats(ctx: &WgpuCtx, buf: &wgpu::Buffer, n: usize) -> Result<(f32, usize)> {
    let bytes = (n * 4) as u64;
    let read_buf = ctx.device.create_buffer(&wgpu::BufferDescriptor {
        label: Some("trace.read"),
        size: bytes,
        usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ,
        mapped_at_creation: false,
    });
    let mut enc = ctx
        .device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor {
            label: Some("trace.enc"),
        });
    enc.copy_buffer_to_buffer(buf, 0, &read_buf, 0, bytes);
    ctx.queue.submit(Some(enc.finish()));
    let v = read_back_f32(&ctx.device, &read_buf).await?;
    let mut max_abs = 0.0f32;
    let mut nans = 0usize;
    for &x in &v {
        if x.is_nan() {
            nans += 1;
        } else if x.abs() > max_abs {
            max_abs = x.abs();
        }
    }
    Ok((max_abs, nans))
}

async fn read_back_f32(device: &wgpu::Device, buf: &wgpu::Buffer) -> Result<Vec<f32>> {
    let slice = buf.slice(..);
    let (sender, receiver) = oneshot::channel();
    slice.map_async(wgpu::MapMode::Read, move |r| {
        let _ = sender.send(r);
    });
    device
        .poll(wgpu::PollType::Wait {
            submission_index: None,
            timeout: None,
        })
        .map_err(|e| RullamaError::Inference(format!("{e:?}")))?;
    receiver
        .await
        .map_err(|e| RullamaError::BufferMap(format!("{e}")))?
        .map_err(|e| RullamaError::BufferMap(format!("{e}")))?;
    let data = slice.get_mapped_range();
    let v: Vec<f32> = bytemuck::cast_slice(&data).to_vec();
    drop(data);
    buf.unmap();
    Ok(v)
}

/// Same as [`read_back_f32`] but returns raw bytes — for snapshotting the
/// KV cache where we don't care about the f32 alignment, only the byte
/// stream.
async fn read_back_bytes(device: &wgpu::Device, buf: &wgpu::Buffer) -> Result<Vec<u8>> {
    let slice = buf.slice(..);
    let (sender, receiver) = oneshot::channel();
    slice.map_async(wgpu::MapMode::Read, move |r| {
        let _ = sender.send(r);
    });
    device
        .poll(wgpu::PollType::Wait {
            submission_index: None,
            timeout: None,
        })
        .map_err(|e| RullamaError::Inference(format!("{e:?}")))?;
    receiver
        .await
        .map_err(|e| RullamaError::BufferMap(format!("{e}")))?
        .map_err(|e| RullamaError::BufferMap(format!("{e}")))?;
    let data = slice.get_mapped_range();
    let v: Vec<u8> = data.to_vec();
    drop(data);
    buf.unmap();
    Ok(v)
}

// =========================================================================
//                            BACKWARD PASS
// =========================================================================
//
// Reverse-mode chained backward, parallel in structure to `encode_layer`.
// Mirrors the forward graph node-for-node — no tape, no autodiff. The
// fully-captured `LayerCaptureBuffers` for each layer plus the live KV
// cache provide every activation the reverse pass needs.
//
// Encoder cadence matches forward: one `wgpu::CommandEncoder` per layer
// (preserves the iPhone WebContent per-encoder workaround), plus single
// encoders for the CE+output-proj+final-norm head and the Adam step.

/// Per-LoRA gradient accumulators. Mirrors `LoraSlot` (the forward
/// view) with the addition of `d_a` and `d_b` — the buffers Adam will
/// step over. Backward writes into these via `lora_outer_add_chained`.
pub struct LoraGradPair<'a> {
    pub a: &'a wgpu::Buffer,   // [rank, in_dim] — read for u = Bᵀ·dy
    pub b: &'a wgpu::Buffer,   // [out_dim, rank] — read for u = Bᵀ·dy
    pub z: &'a wgpu::Buffer,   // [rank] — captured A·x from forward, dB needs it
    pub d_a: &'a wgpu::Buffer, // [rank, in_dim] — gradient accumulator
    pub d_b: &'a wgpu::Buffer, // [out_dim, rank] — gradient accumulator
    pub rank: u32,
    pub scale: f32,
}

/// Per-layer LoRA gradient accumulators for the four attention
/// projections + three FFN projections. Each pair drives both the
/// LoRA backward (computing dA, dB into d_a, d_b) AND the LoRA
/// contribution to dx (Aᵀ·Bᵀ·dy added to the running input gradient).
pub struct LayerLoraGrads<'a> {
    pub q: Option<LoraGradPair<'a>>,
    pub k: Option<LoraGradPair<'a>>,
    pub v: Option<LoraGradPair<'a>>,
    pub o: Option<LoraGradPair<'a>>,
    pub ffn_gate: Option<LoraGradPair<'a>>,
    pub ffn_up: Option<LoraGradPair<'a>>,
    pub ffn_down: Option<LoraGradPair<'a>>,
}

/// All scratch buffers the backward orchestration writes into. Sized
/// at construction time and reused across steps. Allocated by
/// `rullama-finetune::TrainingScratch`.
#[allow(clippy::struct_field_names)]
pub struct BackwardScratchView<'a> {
    /// `[vocab]` — softmax(logits) - one_hot(target).
    pub d_logits: &'a wgpu::Buffer,
    /// `[1]` — scalar CE loss (read back to CPU after backward).
    pub loss: &'a wgpu::Buffer,
    /// `[d_model]` — gradient at the final post-norm hidden (= input
    /// to output projection); used as the running d_hidden after the
    /// output proj backward chains in.
    pub d_hidden_final: &'a wgpu::Buffer,
    /// `[d_model]` — running gradient on the residual stream.
    pub d_hidden: &'a wgpu::Buffer,
    /// `[d_model]` — second d_model scratch (post-attn/post-ffn intermediates).
    pub d_hidden_tmp: &'a wgpu::Buffer,
    /// `[d_model]` — third d_model scratch (sum two contributions).
    pub d_hidden_tmp2: &'a wgpu::Buffer,
    /// `[n_heads · history_len]` — recomputed attention probs.
    pub attn_probs: &'a wgpu::Buffer,
    /// `[n_heads · history_len]` — staged d_scores (pass 1 → pass 2).
    pub attn_d_scores: &'a wgpu::Buffer,
    /// `[n_heads · head_dim]` — d_attn_out (input to attn back dq).
    pub d_attn_out: &'a wgpu::Buffer,
    /// `[n_heads · head_dim]` — d_q output of attn back dq (= d_q_post_rope).
    pub d_q: &'a wgpu::Buffer,
    /// `[history_len · n_kv · head_dim]` — d_k_hist (only row[pos] consumed in M0).
    pub d_k_hist: &'a wgpu::Buffer,
    /// `[history_len · n_kv · head_dim]` — d_v_hist.
    pub d_v_hist: &'a wgpu::Buffer,
    /// `[n_heads · head_dim]` — d after rope_back of q.
    pub d_q_pre_rope: &'a wgpu::Buffer,
    /// `[n_kv · head_dim]` — d after rope_back of k.
    pub d_k_pre_rope: &'a wgpu::Buffer,
    /// `[n_heads · head_dim]` — d after q_norm rmsnorm_back.
    pub d_q_pre_norm: &'a wgpu::Buffer,
    /// `[n_kv · head_dim]` — d after k_norm rmsnorm_back.
    pub d_k_pre_norm: &'a wgpu::Buffer,
    /// `[n_kv · head_dim]` — d after v_norm rmsnorm_back.
    pub d_v_pre_norm: &'a wgpu::Buffer,
    /// `[ffn_inter]` — d_ffn_out (matmul_back output, going into geglu_back).
    pub d_ffn_a: &'a wgpu::Buffer,
    /// `[ffn_inter]` — d_ffn_gate (geglu_back output).
    pub d_ffn_b: &'a wgpu::Buffer,
    /// `[ffn_inter]` — d_ffn_up (geglu_back output).
    pub d_ffn_c: &'a wgpu::Buffer,
    /// `[ple_dim]` — d_gate output of PLE geglu_back.
    pub d_ple_state: &'a wgpu::Buffer,
    /// `[ple_dim]` — d input to PLE geglu_back (= proj_w matmul-back output).
    pub d_ple_act: &'a wgpu::Buffer,
    /// `[ple_dim]` — discarded `d_up` output of PLE geglu_back.
    pub d_ple_up_discard: &'a wgpu::Buffer,
    /// `[ple_dim]` — staging copy of `self.per_layer[i*ple_dim..]` for
    /// PLE geglu_back's read-only `up` input.
    pub ple_per_layer_tmp: &'a wgpu::Buffer,
    /// `[d_model]` window into a layer's seq-sized `norm_x_attn`
    /// capture — pre-copied per backward iteration.
    pub norm_x_attn_window: &'a wgpu::Buffer,
    /// `[n_kv · head_dim]` window into a layer's seq-sized
    /// `k_pre_norm` capture.
    pub k_pre_norm_window: &'a wgpu::Buffer,
    /// `[n_kv · head_dim]` window into a layer's seq-sized
    /// `v_pre_norm` capture.
    pub v_pre_norm_window: &'a wgpu::Buffer,
    /// `[d_model]` window into `hidden_in` capture.
    pub hidden_in_window: &'a wgpu::Buffer,
    /// `[n_heads · head_dim]` window into `q_pre_norm` capture.
    pub q_pre_norm_window: &'a wgpu::Buffer,
    /// `[n_heads · head_dim]` window into `q_post_rope` capture.
    pub q_post_rope_window: &'a wgpu::Buffer,
    /// `[n_heads · head_dim]` window into `attn_out` capture.
    pub attn_out_window: &'a wgpu::Buffer,
    /// `[d_model]` window into `attn_proj` capture.
    pub attn_proj_window: &'a wgpu::Buffer,
    /// `[d_model]` window into `pre_ffn_rms` capture.
    pub pre_ffn_rms_window: &'a wgpu::Buffer,
    /// `[d_model]` window into `norm_x_ffn` capture.
    pub norm_x_ffn_window: &'a wgpu::Buffer,
    /// `[ffn_inter]` window into `ffn_gate` capture.
    pub ffn_gate_window: &'a wgpu::Buffer,
    /// `[ffn_inter]` window into `ffn_up` capture.
    pub ffn_up_window: &'a wgpu::Buffer,
    /// `[ffn_inter]` window into `ffn_act` capture.
    pub ffn_act_window: &'a wgpu::Buffer,
    /// `[d_model]` window into `ffn_out` capture.
    pub ffn_out_window: &'a wgpu::Buffer,
    /// `[ple_dim]` window into `ple_state` capture.
    pub ple_state_window: &'a wgpu::Buffer,
    /// `[ple_dim]` window into `ple_act` capture.
    pub ple_act_window: &'a wgpu::Buffer,
    /// `[d_model]` window into `ple_proj` capture.
    pub ple_proj_window: &'a wgpu::Buffer,
}

impl Forward {
    /// Full backward pass — produces gradients into `grads` for every
    /// registered LoRA, writes the scalar CE loss into `scratch.loss`,
    /// and returns the loss value.
    ///
    /// Preconditions:
    /// - `step_capture(...)` has just run on this same `Forward`, with
    ///   `capture` and `loras` matching the slices passed here.
    /// - `self.logits` still holds the final-position logits.
    /// - `self.hidden` still holds the pre-final-norm residual stream.
    /// - `self.norm_x` still holds the post-final-norm hidden (input
    ///   to the output projection).
    /// - KV caches `self.kv_k[i]` / `self.kv_v[i]` still hold the
    ///   prompt's K/V (history length = current `pos`).
    /// - `grads[i].*.d_a` and `d_b` are pre-zeroed by the caller (the
    ///   training step's `zero_all_grads` before forward).
    ///
    /// `target_id ≥ vocab_size` masks the gradient (zero loss / zero
    /// gradient at this position).
    #[allow(clippy::too_many_arguments)]
    pub async fn backward_step<'a>(
        &mut self,
        target_id: u32,
        capture: &'a [LayerCaptureBuffers<'a>],
        loras: &'a [LayerLoraSlots<'a>],
        grads: &'a [LayerLoraGrads<'a>],
        scratch: &'a BackwardScratchView<'a>,
        history_len: u32,
        pos: u32,
        recompute_captures: bool,
    ) -> Result<f32> {
        self.backward_step_with_progress(
            target_id,
            capture,
            loras,
            grads,
            scratch,
            history_len,
            pos,
            recompute_captures,
            None,
        )
        .await
    }

    /// Variant of [`backward_step`] that fires
    /// `progress_cb(layer_index, total_layers, "backward")` between
    /// per-layer encoder submits. The layer index in the callback is
    /// the **logical position** (1..=n_layers walking top-down), so
    /// a 35-layer model fires `(1, 35) ... (35, 35)` mirroring the
    /// forward beacon order — friendlier for the UI to render than
    /// the actual reverse-walk index `(n_layers-1) ... 0`.
    #[allow(clippy::too_many_arguments)]
    pub async fn backward_step_with_progress<'a>(
        &mut self,
        target_id: u32,
        capture: &'a [LayerCaptureBuffers<'a>],
        loras: &'a [LayerLoraSlots<'a>],
        grads: &'a [LayerLoraGrads<'a>],
        scratch: &'a BackwardScratchView<'a>,
        history_len: u32,
        pos: u32,
        recompute_captures: bool,
        progress_cb: Option<&LayerProgressCb<'_>>,
    ) -> Result<f32> {
        // Clear any stale cancel flag from a previous step; the layer
        // walk below checks it after each `backward_layer` submit.
        self.reset_cancel();
        let n_layers = self.cfg.n_layers as usize;
        if capture.len() != n_layers || loras.len() != n_layers || grads.len() != n_layers {
            return Err(RullamaError::Inference(
                "backward_step: capture/loras/grads slice length must equal n_layers".into(),
            ));
        }
        let d_model = self.cfg.d_model as usize;
        let vocab = self.cfg.vocab_size as usize;
        let eps = self.cfg.rms_norm_eps;

        // Fetch top-level frozen weights.
        let wc = self.wcache.clone();
        let final_norm = wc.buffer_async("output_norm.weight").await?;
        let token_embd = wc.buffer_async("token_embd.weight").await?;
        let token_embd_dtype = wc.dtype("token_embd.weight")?;

        // ===== Head: CE → output_proj_back → final norm back =====
        let mut enc = self
            .ctx
            .device
            .create_command_encoder(&wgpu::CommandEncoderDescriptor {
                label: Some("bwd.head"),
            });

        // d_logits + scalar loss
        cross_entropy_backward_chained(
            &self.ctx,
            &self.pipes,
            &mut enc,
            &self.logits,
            scratch.d_logits,
            scratch.loss,
            vocab,
            target_id,
        );

        // d_norm_x_final = embedᵀ @ d_logits → write into scratch.d_hidden_final
        match token_embd_dtype {
            GgmlDtype::Q6_K => matmul_q6_k_backward_input_chained(
                &self.ctx,
                &self.pipes,
                &mut enc,
                &token_embd,
                scratch.d_logits,
                scratch.d_hidden_final,
                d_model,
                vocab,
            ),
            GgmlDtype::Q4_K => matmul_q4_k_backward_input_chained(
                &self.ctx,
                &self.pipes,
                &mut enc,
                &token_embd,
                scratch.d_logits,
                scratch.d_hidden_final,
                d_model,
                vocab,
            ),
            other => {
                return Err(RullamaError::Inference(format!(
                    "backward_step: token_embd dtype {other:?} unsupported"
                )));
            }
        }

        // d_hidden (running, top-of-stack) = rmsnorm_back(self.hidden,
        // output_norm.weight, d_norm_x_final).
        rmsnorm_backward_chained(
            &self.ctx,
            &self.pipes,
            &mut enc,
            &self.hidden,
            &final_norm,
            scratch.d_hidden_final,
            scratch.d_hidden,
            d_model,
            eps,
            true,
        );

        self.ctx.queue.submit(Some(enc.finish()));

        let trace_hidden = std::env::var("RULLAMA_TRACE_DHIDDEN").is_ok();
        // Adaptive max-abs clip on d_hidden between layers. Defaults to
        // 1.0 to keep deep-network gradient flow finite for LoRA
        // fine-tuning of pretrained models, where the backward graph
        // (which the pretrained weights were *not* initialized for) can
        // amplify 100-1500x per layer. Adam normalises to ≈ lr · sign(g)
        // anyway, so absolute magnitude is mostly informational — but
        // preventing overflow is the bare minimum the optimiser needs.
        let clip_max: f32 = std::env::var("RULLAMA_CLIP_DHIDDEN")
            .ok()
            .and_then(|s| s.parse().ok())
            .unwrap_or(1.0);
        if trace_hidden {
            let (max_abs, nans) =
                read_buf_stats(&self.ctx, scratch.d_hidden, self.cfg.d_model as usize).await?;
            eprintln!("[trace] after head section: d_hidden max_abs={max_abs:.3e} nan={nans}");
            let (max_abs_f, nans_f) =
                read_buf_stats(&self.ctx, scratch.d_hidden_final, self.cfg.d_model as usize)
                    .await?;
            eprintln!("[trace] d_hidden_final (head): max_abs={max_abs_f:.3e} nan={nans_f}");
            let (max_abs_l, nans_l) =
                read_buf_stats(&self.ctx, scratch.d_logits, self.cfg.vocab_size as usize).await?;
            eprintln!("[trace] d_logits: max_abs={max_abs_l:.3e} nan={nans_l}");
        }
        // ===== Walk layers top-down =====
        let d_model_bytes = (self.cfg.d_model as u64) * 4;
        for li in (0..n_layers).rev() {
            let i = li as u32;
            let cap = &capture[li];
            let lora = &loras[li];
            let grad = &grads[li];

            // Gradient-checkpointing replay: rewrite the per-layer
            // captures by re-running this layer's forward pass.
            // Uses `cap.hidden_in` (saved at the top of the original
            // forward) as the input. The K/V cache write at slot
            // `pos` is idempotent (same value written again);
            // `kv_lens[i]` is save/restored so the cache-count
            // bookkeeping survives the replay.
            if recompute_captures {
                let mut renc =
                    self.ctx
                        .device
                        .create_command_encoder(&wgpu::CommandEncoderDescriptor {
                            label: Some("bwd.replay"),
                        });
                renc.copy_buffer_to_buffer(
                    cap.hidden_in,
                    (pos as u64) * d_model_bytes,
                    &self.hidden,
                    0,
                    d_model_bytes,
                );
                let saved_len = self.kv_lens[li];
                if self.donor_map[li].is_none() && saved_len > 0 {
                    self.kv_lens[li] = saved_len - 1;
                }
                self.encode_layer(&mut renc, i, pos, Some(cap), Some(lora))
                    .await?;
                // encode_layer's K/V write re-incremented kv_lens[i]; assert.
                debug_assert_eq!(
                    self.kv_lens[li], saved_len,
                    "replay should leave kv_lens unchanged for layer {li}"
                );
                self.ctx.queue.submit(Some(renc.finish()));
            }

            let mut lenc =
                self.ctx
                    .device
                    .create_command_encoder(&wgpu::CommandEncoderDescriptor {
                        label: Some("bwd.layer"),
                    });
            self.backward_layer(&mut lenc, i, history_len, pos, cap, lora, grad, scratch)
                .await?;
            self.ctx.queue.submit(Some(lenc.finish()));
            // Per-layer cancel check — same boundary the forward loop
            // uses. Cancellation latency is bounded by one
            // `backward_layer` (~300 ms - 1 s on browser).
            self.check_cancelled()?;
            // Per-layer progress beacon. Convert reverse-walk index
            // `li` (counting top-down from n_layers-1) into the
            // logical 1-based position so UI shows "backward 1/35,
            // 2/35, …" mirroring the forward order — easier to read
            // than the underlying reverse walk.
            if let Some(cb) = progress_cb {
                let logical = (n_layers as u32) - i;
                cb("backward", logical, n_layers as u32);
            }

            // Adaptive renorm of d_hidden — if max-abs exceeds the
            // configured ceiling, scale d_hidden in-place to bring
            // max-abs back down. Preserves direction (every element
            // scaled by the same factor); Adam doesn't care about
            // absolute scale.
            if clip_max > 0.0 {
                let (max_abs, _) =
                    read_buf_stats(&self.ctx, scratch.d_hidden, self.cfg.d_model as usize).await?;
                if max_abs > clip_max && max_abs.is_finite() {
                    let s = clip_max / max_abs;
                    let mut cenc =
                        self.ctx
                            .device
                            .create_command_encoder(&wgpu::CommandEncoderDescriptor {
                                label: Some("bwd.clip"),
                            });
                    scale_chained(
                        &self.ctx,
                        &self.pipes,
                        &mut cenc,
                        scratch.d_hidden,
                        self.cfg.d_model as usize,
                        s,
                    );
                    self.ctx.queue.submit(Some(cenc.finish()));
                }
            }

            if trace_hidden {
                let (max_abs, nans) =
                    read_buf_stats(&self.ctx, scratch.d_hidden, self.cfg.d_model as usize).await?;
                eprintln!(
                    "[trace] after layer {li} bwd: d_hidden max_abs={max_abs:.3e} nan={nans}"
                );
            }
        }

        // ===== Loss readback =====
        let loss_read = self.ctx.device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("bwd.loss_read"),
            size: 4,
            usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ,
            mapped_at_creation: false,
        });
        let mut renc = self
            .ctx
            .device
            .create_command_encoder(&wgpu::CommandEncoderDescriptor {
                label: Some("bwd.loss_copy"),
            });
        renc.copy_buffer_to_buffer(scratch.loss, 0, &loss_read, 0, 4);
        self.ctx.queue.submit(Some(renc.finish()));
        let loss_vec = read_back_f32(&self.ctx.device, &loss_read).await?;
        Ok(loss_vec[0])
    }

    /// Backward through one transformer layer. Reads `cap` (forward
    /// activations) and the live KV cache; writes LoRA gradients into
    /// `grad`; carries `d_hidden` running into the next-down layer.
    ///
    /// Skips PLE-injection backward (Gemma 4 e2b's PLE has no LoRA;
    /// the gradient leakage through `inp_gate_w` is dropped — an M0
    /// approximation, documented in `MIGRATION-REPORT.md`).
    #[allow(clippy::too_many_arguments)]
    async fn backward_layer<'a>(
        &mut self,
        enc: &mut wgpu::CommandEncoder,
        i: u32,
        history_len: u32,
        pos: u32,
        cap: &LayerCaptureBuffers<'a>,
        lora: &LayerLoraSlots<'a>,
        grad: &LayerLoraGrads<'a>,
        scratch: &BackwardScratchView<'a>,
    ) -> Result<()> {
        let prefix = format!("blk.{i}.");
        let d_model = self.cfg.d_model as usize;
        let eps = self.cfg.rms_norm_eps;
        let n_heads = self.cfg.n_heads as usize;
        let n_kv_heads = self.cfg.n_kv_heads(i) as usize;
        let head_dim = self.cfg.head_dim(i) as usize;
        let ffn_n = self.cfg.ffn(i) as usize;
        let kind = self.cfg.kind(i);

        // Frozen weights this layer needs (cache hits after the forward).
        let wc = self.wcache.clone();
        let attn_norm_w = wc
            .buffer_async(&format!("{prefix}attn_norm.weight"))
            .await?;
        let post_attn_w = wc
            .buffer_async(&format!("{prefix}post_attention_norm.weight"))
            .await?;
        let mlp_norm_w = wc.buffer_async(&format!("{prefix}ffn_norm.weight")).await?;
        let post_ffw_w = wc
            .buffer_async(&format!("{prefix}post_ffw_norm.weight"))
            .await?;
        let q_w = wc.buffer_async(&format!("{prefix}attn_q.weight")).await?;
        let q_norm_w = wc
            .buffer_async(&format!("{prefix}attn_q_norm.weight"))
            .await?;
        let o_w = wc
            .buffer_async(&format!("{prefix}attn_output.weight"))
            .await?;
        let k_w = wc.buffer_async(&format!("{prefix}attn_k.weight")).await?;
        let k_norm_w = wc
            .buffer_async(&format!("{prefix}attn_k_norm.weight"))
            .await?;
        let v_name = format!("{prefix}attn_v.weight");
        let v_w = wc.buffer_async(&v_name).await?;
        let v_w_dtype = wc.dtype(&v_name)?;
        let gate_w = wc.buffer_async(&format!("{prefix}ffn_gate.weight")).await?;
        let up_w = wc.buffer_async(&format!("{prefix}ffn_up.weight")).await?;
        let down_name = format!("{prefix}ffn_down.weight");
        let down_w = wc.buffer_async(&down_name).await?;
        let down_dtype = wc.dtype(&down_name)?;
        let factors_w = if matches!(kind, LayerKind::Global) {
            wc.buffer_opt_async("rope_freqs.weight").await?
        } else {
            None
        };

        // Undo per-layer output scale.
        if let Some(s) = self.layer_scalars[i as usize] {
            scale_chained(&self.ctx, &self.pipes, enc, scratch.d_hidden, d_model, s);
        }

        // Pre-copy the `pos`-slices of the seq-sized captures into
        // single-position windows so the rest of backward_layer can
        // bind them via `as_entire_binding()` without paying offset
        // alignment friction. The per-history K/V LoRA backward and
        // single-forward PerPosition both re-copy *other* positions
        // into the same windows.
        let d_model_bytes = (d_model as u64) * 4;
        let kv_row_bytes = (n_kv_heads as u64) * (head_dim as u64) * 4;
        let n_heads_row_bytes = (n_heads as u64) * (head_dim as u64) * 4;
        let ffn_row_bytes = (ffn_n as u64) * 4;
        let pos_off = pos as u64;
        // Three were already pre-copied (norm_x_attn, k_pre_norm,
        // v_pre_norm) for per-history K/V LoRA backward; the rest
        // (hidden_in, q_pre_norm, q_post_rope, attn_out, attn_proj,
        // pre_ffn_rms, norm_x_ffn, ffn_gate, ffn_up, ffn_act,
        // ffn_out, plus PLE if applicable) are needed for the full
        // backward_layer chain to work uniformly across positions.
        enc.copy_buffer_to_buffer(
            cap.norm_x_attn,
            pos_off * d_model_bytes,
            scratch.norm_x_attn_window,
            0,
            d_model_bytes,
        );
        enc.copy_buffer_to_buffer(
            cap.k_pre_norm,
            pos_off * kv_row_bytes,
            scratch.k_pre_norm_window,
            0,
            kv_row_bytes,
        );
        enc.copy_buffer_to_buffer(
            cap.v_pre_norm,
            pos_off * kv_row_bytes,
            scratch.v_pre_norm_window,
            0,
            kv_row_bytes,
        );
        enc.copy_buffer_to_buffer(
            cap.hidden_in,
            pos_off * d_model_bytes,
            scratch.hidden_in_window,
            0,
            d_model_bytes,
        );
        enc.copy_buffer_to_buffer(
            cap.q_pre_norm,
            pos_off * n_heads_row_bytes,
            scratch.q_pre_norm_window,
            0,
            n_heads_row_bytes,
        );
        enc.copy_buffer_to_buffer(
            cap.q_post_rope,
            pos_off * n_heads_row_bytes,
            scratch.q_post_rope_window,
            0,
            n_heads_row_bytes,
        );
        enc.copy_buffer_to_buffer(
            cap.attn_out,
            pos_off * n_heads_row_bytes,
            scratch.attn_out_window,
            0,
            n_heads_row_bytes,
        );
        enc.copy_buffer_to_buffer(
            cap.attn_proj,
            pos_off * d_model_bytes,
            scratch.attn_proj_window,
            0,
            d_model_bytes,
        );
        enc.copy_buffer_to_buffer(
            cap.pre_ffn_rms,
            pos_off * d_model_bytes,
            scratch.pre_ffn_rms_window,
            0,
            d_model_bytes,
        );
        enc.copy_buffer_to_buffer(
            cap.norm_x_ffn,
            pos_off * d_model_bytes,
            scratch.norm_x_ffn_window,
            0,
            d_model_bytes,
        );
        enc.copy_buffer_to_buffer(
            cap.ffn_gate,
            pos_off * ffn_row_bytes,
            scratch.ffn_gate_window,
            0,
            ffn_row_bytes,
        );
        enc.copy_buffer_to_buffer(
            cap.ffn_up,
            pos_off * ffn_row_bytes,
            scratch.ffn_up_window,
            0,
            ffn_row_bytes,
        );
        enc.copy_buffer_to_buffer(
            cap.ffn_act,
            pos_off * ffn_row_bytes,
            scratch.ffn_act_window,
            0,
            ffn_row_bytes,
        );
        enc.copy_buffer_to_buffer(
            cap.ffn_out,
            pos_off * d_model_bytes,
            scratch.ffn_out_window,
            0,
            d_model_bytes,
        );
        if self.cfg.has_ple() {
            let ple_dim_bytes = (self.cfg.ple_dim as u64) * 4;
            enc.copy_buffer_to_buffer(
                cap.ple_state,
                pos_off * ple_dim_bytes,
                scratch.ple_state_window,
                0,
                ple_dim_bytes,
            );
            enc.copy_buffer_to_buffer(
                cap.ple_act,
                pos_off * ple_dim_bytes,
                scratch.ple_act_window,
                0,
                ple_dim_bytes,
            );
            enc.copy_buffer_to_buffer(
                cap.ple_proj,
                pos_off * d_model_bytes,
                scratch.ple_proj_window,
                0,
                d_model_bytes,
            );
        }

        // ----- PLE injection backward -----
        //
        // Forward order:
        //   ple_state  = matmul(inp_gate_w, hidden, ple_dim)
        //   ple_act    = geglu(ple_state, per_layer[i*ple_dim..])
        //   ple_proj   = matmul(proj_w, ple_act, d_model)
        //   norm_y     = rmsnorm(ple_proj, post_norm_w)
        //   hidden    += norm_y
        //
        // Reverse: residual_add back (d_norm_y = d_hidden, then
        // accumulate d_hidden_from_ple) → rmsnorm back (post_norm_w)
        // → matmul back (proj_w) → geglu back (drop d_up — per_layer
        // is not a trainable parameter) → matmul back (inp_gate_w) →
        // add into running d_hidden.
        if self.cfg.has_ple() {
            let ple_dim = self.cfg.ple_dim as usize;
            let inp_gate_w = wc.buffer_async(&format!("{prefix}inp_gate.weight")).await?;
            let proj_w = wc.buffer_async("per_layer_model_proj.weight").await?;
            let post_norm_w = wc.buffer_async("per_layer_proj_norm.weight").await?;

            // d_norm_y = d_hidden (residual_add backward — both
            // additive branches carry d_hidden_out through unchanged).
            // post_ffw_norm rmsnorm backward of the PLE rmsnorm:
            // d_ple_proj = rmsnorm_back(cap.ple_proj, post_norm_w, d_hidden) → d_hidden_tmp
            rmsnorm_backward_chained(
                &self.ctx,
                &self.pipes,
                enc,
                scratch.ple_proj_window,
                &post_norm_w,
                scratch.d_hidden,
                scratch.d_hidden_tmp,
                d_model,
                eps,
                true,
            );
            // matmul back through proj_w: d_ple_act = proj_wᵀ · d_ple_proj.
            matmul_q4_k_backward_input_chained(
                &self.ctx,
                &self.pipes,
                enc,
                &proj_w,
                scratch.d_hidden_tmp,
                scratch.d_ple_act,
                ple_dim,
                d_model,
            );
            // Copy per_layer[i*ple_dim..] into the staging buf so
            // geglu_back's `up` binding is read-only and distinct
            // from `dy` / `d_gate` / `d_up`.
            let layer_off = (i as u64) * (ple_dim as u64) * 4;
            let layer_bytes = (ple_dim as u64) * 4;
            enc.copy_buffer_to_buffer(
                &self.per_layer,
                layer_off,
                scratch.ple_per_layer_tmp,
                0,
                layer_bytes,
            );
            // geglu back: d_gate → d_ple_state, d_up → d_ple_up_discard.
            geglu_backward_chained(
                &self.ctx,
                &self.pipes,
                enc,
                scratch.ple_state_window,
                scratch.ple_per_layer_tmp,
                scratch.d_ple_act,
                scratch.d_ple_state,
                scratch.d_ple_up_discard,
                ple_dim,
            );
            // matmul back through inp_gate_w: d_hidden_from_ple = inp_gate_wᵀ · d_ple_state
            //   → d_hidden_tmp (safe to overwrite at this point).
            matmul_q4_k_backward_input_chained(
                &self.ctx,
                &self.pipes,
                enc,
                &inp_gate_w,
                scratch.d_ple_state,
                scratch.d_hidden_tmp,
                d_model,
                ple_dim,
            );
            // d_hidden += d_hidden_from_ple (residual_add backward
            // combines PLE branch's input grad with the through-path).
            residual_add_chained(
                &self.ctx,
                &self.pipes,
                enc,
                scratch.d_hidden,
                scratch.d_hidden_tmp,
                d_model,
            );
        }

        // ----- FFN block backward -----
        // residual_add backward (ffn): d_norm_y_ffn = d_hidden (alias).
        // d_hidden continues as d_pre_ffn_residual (= d_h1 path through residual).
        //
        // post_ffw_norm rmsnorm backward → d_ffn_out into d_hidden_tmp.
        rmsnorm_backward_chained(
            &self.ctx,
            &self.pipes,
            enc,
            scratch.ffn_out_window,
            &post_ffw_w,
            scratch.d_hidden,
            scratch.d_hidden_tmp,
            d_model,
            eps,
            true,
        );

        // ffn_down matmul backward: d_ffn_act = down_wᵀ · d_ffn_out → d_ffn_a.
        match down_dtype {
            GgmlDtype::Q6_K => matmul_q6_k_backward_input_chained(
                &self.ctx,
                &self.pipes,
                enc,
                &down_w,
                scratch.d_hidden_tmp,
                scratch.d_ffn_a,
                ffn_n,
                d_model,
            ),
            GgmlDtype::Q4_K => matmul_q4_k_backward_input_chained(
                &self.ctx,
                &self.pipes,
                enc,
                &down_w,
                scratch.d_hidden_tmp,
                scratch.d_ffn_a,
                ffn_n,
                d_model,
            ),
            other => {
                return Err(RullamaError::Inference(format!(
                    "ffn_down dtype {other:?} unsupported in backward"
                )));
            }
        }

        // ffn_down LoRA backward:
        //   dB += s · d_ffn_out ⊗ z;  u = Bᵀ · d_ffn_out;
        //   d_ffn_a += s · Aᵀ · u;    dA += s · u ⊗ cap.ffn_act.
        if let (Some(d_lora), Some(d_grad)) = (lora.ffn_down.as_ref(), grad.ffn_down.as_ref()) {
            let r = d_lora.rank as usize;
            let s = d_lora.scale;
            lora_outer_add_chained(
                &self.ctx,
                &self.pipes,
                enc,
                scratch.d_hidden_tmp,
                d_lora.z,
                d_grad.d_b,
                d_model,
                r,
                s,
                true,
            );
            lora_matmul_col_chained(
                &self.ctx,
                &self.pipes,
                enc,
                d_lora.b,
                scratch.d_hidden_tmp,
                d_lora.z,
                d_model,
                r,
                1.0,
                false,
            );
            lora_matmul_col_chained(
                &self.ctx,
                &self.pipes,
                enc,
                d_lora.a,
                d_lora.z,
                scratch.d_ffn_a,
                r,
                ffn_n,
                s,
                true,
            );
            lora_outer_add_chained(
                &self.ctx,
                &self.pipes,
                enc,
                d_lora.z,
                scratch.ffn_act_window,
                d_grad.d_a,
                r,
                ffn_n,
                s,
                true,
            );
        }

        // geglu backward → d_ffn_gate (d_ffn_b), d_ffn_up (d_ffn_c).
        geglu_backward_chained(
            &self.ctx,
            &self.pipes,
            enc,
            scratch.ffn_gate_window,
            scratch.ffn_up_window,
            scratch.d_ffn_a,
            scratch.d_ffn_b,
            scratch.d_ffn_c,
            ffn_n,
        );

        // gate matmul backward: d_norm_x_ffn_via_gate = gate_wᵀ · d_ffn_gate → d_hidden_tmp.
        matmul_q4_k_backward_input_chained(
            &self.ctx,
            &self.pipes,
            enc,
            &gate_w,
            scratch.d_ffn_b,
            scratch.d_hidden_tmp,
            d_model,
            ffn_n,
        );
        // ffn_gate LoRA backward:
        //   dB += s · d_ffn_gate ⊗ z;  u = Bᵀ · d_ffn_gate;
        //   d_hidden_tmp += s · Aᵀ · u; dA += s · u ⊗ cap.norm_x_ffn.
        if let (Some(g_lora), Some(g_grad)) = (lora.ffn_gate.as_ref(), grad.ffn_gate.as_ref()) {
            let r = g_lora.rank as usize;
            let s = g_lora.scale;
            lora_outer_add_chained(
                &self.ctx,
                &self.pipes,
                enc,
                scratch.d_ffn_b,
                g_lora.z,
                g_grad.d_b,
                ffn_n,
                r,
                s,
                true,
            );
            lora_matmul_col_chained(
                &self.ctx,
                &self.pipes,
                enc,
                g_lora.b,
                scratch.d_ffn_b,
                g_lora.z,
                ffn_n,
                r,
                1.0,
                false,
            );
            lora_matmul_col_chained(
                &self.ctx,
                &self.pipes,
                enc,
                g_lora.a,
                g_lora.z,
                scratch.d_hidden_tmp,
                r,
                d_model,
                s,
                true,
            );
            lora_outer_add_chained(
                &self.ctx,
                &self.pipes,
                enc,
                g_lora.z,
                scratch.norm_x_ffn_window,
                g_grad.d_a,
                r,
                d_model,
                s,
                true,
            );
        }
        // up matmul backward: d_norm_x_ffn_via_up → d_hidden_tmp2.
        matmul_q4_k_backward_input_chained(
            &self.ctx,
            &self.pipes,
            enc,
            &up_w,
            scratch.d_ffn_c,
            scratch.d_hidden_tmp2,
            d_model,
            ffn_n,
        );
        // ffn_up LoRA backward (mirrors gate but accumulates into d_hidden_tmp2).
        if let (Some(u_lora), Some(u_grad)) = (lora.ffn_up.as_ref(), grad.ffn_up.as_ref()) {
            let r = u_lora.rank as usize;
            let s = u_lora.scale;
            lora_outer_add_chained(
                &self.ctx,
                &self.pipes,
                enc,
                scratch.d_ffn_c,
                u_lora.z,
                u_grad.d_b,
                ffn_n,
                r,
                s,
                true,
            );
            lora_matmul_col_chained(
                &self.ctx,
                &self.pipes,
                enc,
                u_lora.b,
                scratch.d_ffn_c,
                u_lora.z,
                ffn_n,
                r,
                1.0,
                false,
            );
            lora_matmul_col_chained(
                &self.ctx,
                &self.pipes,
                enc,
                u_lora.a,
                u_lora.z,
                scratch.d_hidden_tmp2,
                r,
                d_model,
                s,
                true,
            );
            lora_outer_add_chained(
                &self.ctx,
                &self.pipes,
                enc,
                u_lora.z,
                scratch.norm_x_ffn_window,
                u_grad.d_a,
                r,
                d_model,
                s,
                true,
            );
        }
        // d_hidden_tmp += d_hidden_tmp2 (full d_norm_x_ffn).
        residual_add_chained(
            &self.ctx,
            &self.pipes,
            enc,
            scratch.d_hidden_tmp,
            scratch.d_hidden_tmp2,
            d_model,
        );

        // mlp_norm rmsnorm backward → d_pre_ffn_rms into d_hidden_tmp2.
        rmsnorm_backward_chained(
            &self.ctx,
            &self.pipes,
            enc,
            scratch.pre_ffn_rms_window,
            &mlp_norm_w,
            scratch.d_hidden_tmp,
            scratch.d_hidden_tmp2,
            d_model,
            eps,
            true,
        );
        // Accumulate FFN block branch contribution into running d_hidden.
        residual_add_chained(
            &self.ctx,
            &self.pipes,
            enc,
            scratch.d_hidden,
            scratch.d_hidden_tmp2,
            d_model,
        );

        // ----- Attention block backward -----
        // residual_add backward (attn): d_norm_y_attn = d_hidden (alias).
        //
        // post_attn_norm rmsnorm backward → d_attn_proj into d_hidden_tmp.
        rmsnorm_backward_chained(
            &self.ctx,
            &self.pipes,
            enc,
            scratch.attn_proj_window,
            &post_attn_w,
            scratch.d_hidden,
            scratch.d_hidden_tmp,
            d_model,
            eps,
            true,
        );

        // o_proj matmul backward: d_attn_out = o_wᵀ · d_attn_proj → scratch.d_attn_out.
        matmul_q4_k_backward_input_chained(
            &self.ctx,
            &self.pipes,
            enc,
            &o_w,
            scratch.d_hidden_tmp,
            scratch.d_attn_out,
            n_heads * head_dim,
            d_model,
        );

        // o LoRA backward: dB += scale·dy⊗z; u=Bᵀ·dy; d_attn_out += scale·Aᵀ·u; dA += scale·u⊗x.
        if let (Some(o_lora), Some(o_grad)) = (lora.o.as_ref(), grad.o.as_ref()) {
            let r = o_lora.rank as usize;
            let s = o_lora.scale;
            // dB_o += s · d_attn_proj ⊗ z_o  (using captured z from forward).
            lora_outer_add_chained(
                &self.ctx,
                &self.pipes,
                enc,
                scratch.d_hidden_tmp,
                o_lora.z,
                o_grad.d_b,
                d_model,
                r,
                s,
                true,
            );
            // u_o = B_oᵀ · d_attn_proj → o_lora.z (overwrite).
            lora_matmul_col_chained(
                &self.ctx,
                &self.pipes,
                enc,
                o_lora.b,
                scratch.d_hidden_tmp,
                o_lora.z,
                d_model,
                r,
                1.0,
                false,
            );
            // d_attn_out += s · A_oᵀ · u_o.
            lora_matmul_col_chained(
                &self.ctx,
                &self.pipes,
                enc,
                o_lora.a,
                o_lora.z,
                scratch.d_attn_out,
                r,
                n_heads * head_dim,
                s,
                true,
            );
            // dA_o += s · u_o ⊗ attn_out (= cap.attn_out).
            lora_outer_add_chained(
                &self.ctx,
                &self.pipes,
                enc,
                o_lora.z,
                scratch.attn_out_window,
                o_grad.d_a,
                r,
                n_heads * head_dim,
                s,
                true,
            );
        }

        // Recompute attention probs (from q_post_rope + kv cache) into scratch.attn_probs.
        let window = if matches!(kind, LayerKind::SlidingWindow) {
            self.cfg.sliding_window as usize
        } else {
            0
        };
        attention_probs_chained(
            &self.ctx,
            &self.pipes,
            enc,
            scratch.q_post_rope_window,
            &self.kv_k[i as usize],
            scratch.attn_probs,
            head_dim,
            n_heads,
            n_kv_heads,
            pos as usize,
            history_len as usize,
            window,
        );

        // Attn backward pass 1: d_q + d_scores (staged).
        attention_backward_dq_chained(
            &self.ctx,
            &self.pipes,
            enc,
            &self.kv_k[i as usize],
            &self.kv_v[i as usize],
            scratch.attn_probs,
            scratch.d_attn_out,
            scratch.attn_d_scores,
            scratch.d_q,
            head_dim,
            n_heads,
            n_kv_heads,
            history_len as usize,
        );
        // Attn backward pass 2: d_k_hist, d_v_hist.
        attention_backward_dkv_chained(
            &self.ctx,
            &self.pipes,
            enc,
            scratch.q_post_rope_window,
            scratch.attn_probs,
            scratch.d_attn_out,
            scratch.attn_d_scores,
            scratch.d_k_hist,
            scratch.d_v_hist,
            head_dim,
            n_heads,
            n_kv_heads,
            history_len as usize,
        );

        // rope backward of q (in-place into d_q → now d_q_pre_rope's value).
        let (rope_base, rope_dims) = match kind {
            LayerKind::SlidingWindow => {
                (self.cfg.rope_freq_base_swa, self.cfg.rope_dim_swa as usize)
            }
            LayerKind::Global => (self.cfg.rope_freq_base, self.cfg.rope_dim_global as usize),
        };
        rope_neox_backward_chained(
            &self.ctx,
            &self.pipes,
            enc,
            scratch.d_q,
            factors_w.as_ref(),
            &self.dummy,
            head_dim,
            n_heads,
            pos as usize,
            rope_dims,
            rope_base,
        );
        // q_norm rmsnorm backward → d_q_pre_norm.
        rmsnorm_per_row_backward_chained(
            &self.ctx,
            &self.pipes,
            enc,
            scratch.q_pre_norm_window,
            &q_norm_w,
            scratch.d_q,
            scratch.d_q_pre_norm,
            n_heads,
            head_dim,
            eps,
            true,
        );
        // q matmul backward: d_norm_x_attn_via_q → d_hidden_tmp (overwrites d_attn_proj).
        matmul_q4_k_backward_input_chained(
            &self.ctx,
            &self.pipes,
            enc,
            &q_w,
            scratch.d_q_pre_norm,
            scratch.d_hidden_tmp,
            d_model,
            n_heads * head_dim,
        );
        // q LoRA backward.
        if let (Some(q_lora), Some(q_grad)) = (lora.q.as_ref(), grad.q.as_ref()) {
            let r = q_lora.rank as usize;
            let s = q_lora.scale;
            lora_outer_add_chained(
                &self.ctx,
                &self.pipes,
                enc,
                scratch.d_q_pre_norm,
                q_lora.z,
                q_grad.d_b,
                n_heads * head_dim,
                r,
                s,
                true,
            );
            lora_matmul_col_chained(
                &self.ctx,
                &self.pipes,
                enc,
                q_lora.b,
                scratch.d_q_pre_norm,
                q_lora.z,
                n_heads * head_dim,
                r,
                1.0,
                false,
            );
            lora_matmul_col_chained(
                &self.ctx,
                &self.pipes,
                enc,
                q_lora.a,
                q_lora.z,
                scratch.d_hidden_tmp,
                r,
                d_model,
                s,
                true,
            );
            lora_outer_add_chained(
                &self.ctx,
                &self.pipes,
                enc,
                q_lora.z,
                scratch.norm_x_attn_window,
                q_grad.d_a,
                r,
                d_model,
                s,
                true,
            );
        }

        // K/V backward — only on layers that own their own K/V (i.e.
        // `donor.is_none()`). KV-shared layers (`donor.is_some()`) read
        // K/V from the donor's cache during forward, so they have no
        // K/V matmul or norm of their own to differentiate. Running
        // the chain anyway on donor layers would consume stale captures
        // (cap.k_pre_norm / cap.v_pre_norm carry the donor's last
        // values, not this layer's, because forward never wrote them
        // here). For now the shared layers' contribution to the
        // donor's K/V LoRA gradient is dropped — a small M0
        // approximation; the correct fix is to route d_k_hist /
        // d_v_hist into the donor's grad accumulators.
        let donor = self.donor_map[i as usize];
        if donor.is_none() {
            // K backward — pull d_k at the final position from d_k_hist.
            // For M0 we only consume the final-position slice (history positions
            // before `pos` get zero LoRA grad contribution — see plan).
            let row_bytes = (n_kv_heads * head_dim * 4) as u64;
            let dk_final_off = pos as u64 * row_bytes;
            enc.copy_buffer_to_buffer(
                scratch.d_k_hist,
                dk_final_off,
                scratch.d_k_pre_rope,
                0,
                row_bytes,
            );
            rope_neox_backward_chained(
                &self.ctx,
                &self.pipes,
                enc,
                scratch.d_k_pre_rope,
                factors_w.as_ref(),
                &self.dummy,
                head_dim,
                n_kv_heads,
                pos as usize,
                rope_dims,
                rope_base,
            );
            rmsnorm_per_row_backward_chained(
                &self.ctx,
                &self.pipes,
                enc,
                scratch.k_pre_norm_window,
                &k_norm_w,
                scratch.d_k_pre_rope,
                scratch.d_k_pre_norm,
                n_kv_heads,
                head_dim,
                eps,
                true,
            );
            // d_norm_x_attn_via_k → d_hidden_tmp2.
            matmul_q4_k_backward_input_chained(
                &self.ctx,
                &self.pipes,
                enc,
                &k_w,
                scratch.d_k_pre_norm,
                scratch.d_hidden_tmp2,
                d_model,
                n_kv_heads * head_dim,
            );
            residual_add_chained(
                &self.ctx,
                &self.pipes,
                enc,
                scratch.d_hidden_tmp,
                scratch.d_hidden_tmp2,
                d_model,
            );
            if let (Some(k_lora), Some(k_grad)) = (lora.k.as_ref(), grad.k.as_ref()) {
                let r = k_lora.rank as usize;
                let s = k_lora.scale;
                lora_outer_add_chained(
                    &self.ctx,
                    &self.pipes,
                    enc,
                    scratch.d_k_pre_norm,
                    k_lora.z,
                    k_grad.d_b,
                    n_kv_heads * head_dim,
                    r,
                    s,
                    true,
                );
                lora_matmul_col_chained(
                    &self.ctx,
                    &self.pipes,
                    enc,
                    k_lora.b,
                    scratch.d_k_pre_norm,
                    k_lora.z,
                    n_kv_heads * head_dim,
                    r,
                    1.0,
                    false,
                );
                lora_matmul_col_chained(
                    &self.ctx,
                    &self.pipes,
                    enc,
                    k_lora.a,
                    k_lora.z,
                    scratch.d_hidden_tmp,
                    r,
                    d_model,
                    s,
                    true,
                );
                lora_outer_add_chained(
                    &self.ctx,
                    &self.pipes,
                    enc,
                    k_lora.z,
                    scratch.norm_x_attn_window,
                    k_grad.d_a,
                    r,
                    d_model,
                    s,
                    true,
                );
            }

            // V backward — pull d_v at the final position from d_v_hist into
            // d_k_pre_norm (free at this point — k backward is done) so it
            // can serve as the rmsnorm_back `dy` without aliasing the `dx`
            // output buffer.
            enc.copy_buffer_to_buffer(
                scratch.d_v_hist,
                dk_final_off,
                scratch.d_k_pre_norm,
                0,
                row_bytes,
            );
            // V was passed through unweighted rmsnorm_per_row; do the unweighted backward.
            rmsnorm_per_row_backward_chained(
                &self.ctx,
                &self.pipes,
                enc,
                scratch.v_pre_norm_window,
                &self.dummy,
                scratch.d_k_pre_norm,
                scratch.d_v_pre_norm,
                n_kv_heads,
                head_dim,
                eps,
                false,
            );
            // d_norm_x_attn_via_v → d_hidden_tmp2.
            match v_w_dtype {
                GgmlDtype::Q6_K => matmul_q6_k_backward_input_chained(
                    &self.ctx,
                    &self.pipes,
                    enc,
                    &v_w,
                    scratch.d_v_pre_norm,
                    scratch.d_hidden_tmp2,
                    d_model,
                    n_kv_heads * head_dim,
                ),
                GgmlDtype::Q4_K => matmul_q4_k_backward_input_chained(
                    &self.ctx,
                    &self.pipes,
                    enc,
                    &v_w,
                    scratch.d_v_pre_norm,
                    scratch.d_hidden_tmp2,
                    d_model,
                    n_kv_heads * head_dim,
                ),
                other => {
                    return Err(RullamaError::Inference(format!(
                        "attn_v dtype {other:?} unsupported in backward"
                    )));
                }
            }
            residual_add_chained(
                &self.ctx,
                &self.pipes,
                enc,
                scratch.d_hidden_tmp,
                scratch.d_hidden_tmp2,
                d_model,
            );
            if let (Some(v_lora), Some(v_grad)) = (lora.v.as_ref(), grad.v.as_ref()) {
                let r = v_lora.rank as usize;
                let s = v_lora.scale;
                lora_outer_add_chained(
                    &self.ctx,
                    &self.pipes,
                    enc,
                    scratch.d_v_pre_norm,
                    v_lora.z,
                    v_grad.d_b,
                    n_kv_heads * head_dim,
                    r,
                    s,
                    true,
                );
                lora_matmul_col_chained(
                    &self.ctx,
                    &self.pipes,
                    enc,
                    v_lora.b,
                    scratch.d_v_pre_norm,
                    v_lora.z,
                    n_kv_heads * head_dim,
                    r,
                    1.0,
                    false,
                );
                lora_matmul_col_chained(
                    &self.ctx,
                    &self.pipes,
                    enc,
                    v_lora.a,
                    v_lora.z,
                    scratch.d_hidden_tmp,
                    r,
                    d_model,
                    s,
                    true,
                );
                lora_outer_add_chained(
                    &self.ctx,
                    &self.pipes,
                    enc,
                    v_lora.z,
                    scratch.norm_x_attn_window,
                    v_grad.d_a,
                    r,
                    d_model,
                    s,
                    true,
                );
            }

            // ----- Per-history K/V LoRA backward -----
            //
            // For each history position `hp != pos`, accumulate dA/dB
            // contributions into the K and V LoRAs using the
            // per-position seq captures + d_k_hist[hp] / d_v_hist[hp].
            // We do NOT update the running `d_hidden` (which is a
            // single-position scratch carrying the gradient at the
            // FINAL position only); the matmul-back-through-k_w /
            // v_w contributions to d_hidden_at_hp are dropped — that's
            // the per-position-d_hidden story owned by the
            // single-forward PerPosition variant.
            //
            // `z` per LoRA is recomputed inline as A · norm_x_attn[hp]
            // (cheap rank·d_model matmul) so we don't need per-position
            // `z` storage.
            for hp_u in 0..history_len {
                if hp_u == pos {
                    continue;
                }
                let hp = hp_u as usize;
                let p_kv_off = hp_u as u64 * row_bytes;
                let p_dm_off = hp_u as u64 * d_model_bytes;
                // Refresh windows for this history position.
                enc.copy_buffer_to_buffer(
                    cap.norm_x_attn,
                    p_dm_off,
                    scratch.norm_x_attn_window,
                    0,
                    d_model_bytes,
                );
                enc.copy_buffer_to_buffer(
                    cap.k_pre_norm,
                    p_kv_off,
                    scratch.k_pre_norm_window,
                    0,
                    row_bytes,
                );

                // K at history position hp.
                enc.copy_buffer_to_buffer(
                    scratch.d_k_hist,
                    p_kv_off,
                    scratch.d_k_pre_rope,
                    0,
                    row_bytes,
                );
                rope_neox_backward_chained(
                    &self.ctx,
                    &self.pipes,
                    enc,
                    scratch.d_k_pre_rope,
                    factors_w.as_ref(),
                    &self.dummy,
                    head_dim,
                    n_kv_heads,
                    hp,
                    rope_dims,
                    rope_base,
                );
                rmsnorm_per_row_backward_chained(
                    &self.ctx,
                    &self.pipes,
                    enc,
                    scratch.k_pre_norm_window,
                    &k_norm_w,
                    scratch.d_k_pre_rope,
                    scratch.d_k_pre_norm,
                    n_kv_heads,
                    head_dim,
                    eps,
                    true,
                );
                if let (Some(k_lora), Some(k_grad)) = (lora.k.as_ref(), grad.k.as_ref()) {
                    let r = k_lora.rank as usize;
                    let s = k_lora.scale;
                    // z_k[hp] = A_k · norm_x_attn[hp]
                    lora_matmul_row_chained(
                        &self.ctx,
                        &self.pipes,
                        enc,
                        k_lora.a,
                        scratch.norm_x_attn_window,
                        k_lora.z,
                        d_model,
                        r,
                        1.0,
                        false,
                    );
                    lora_outer_add_chained(
                        &self.ctx,
                        &self.pipes,
                        enc,
                        scratch.d_k_pre_norm,
                        k_lora.z,
                        k_grad.d_b,
                        n_kv_heads * head_dim,
                        r,
                        s,
                        true,
                    );
                    lora_matmul_col_chained(
                        &self.ctx,
                        &self.pipes,
                        enc,
                        k_lora.b,
                        scratch.d_k_pre_norm,
                        k_lora.z,
                        n_kv_heads * head_dim,
                        r,
                        1.0,
                        false,
                    );
                    lora_outer_add_chained(
                        &self.ctx,
                        &self.pipes,
                        enc,
                        k_lora.z,
                        scratch.norm_x_attn_window,
                        k_grad.d_a,
                        r,
                        d_model,
                        s,
                        true,
                    );
                }

                // V at history position hp.
                enc.copy_buffer_to_buffer(
                    cap.v_pre_norm,
                    p_kv_off,
                    scratch.v_pre_norm_window,
                    0,
                    row_bytes,
                );
                enc.copy_buffer_to_buffer(
                    scratch.d_v_hist,
                    p_kv_off,
                    scratch.d_k_pre_norm,
                    0,
                    row_bytes,
                );
                rmsnorm_per_row_backward_chained(
                    &self.ctx,
                    &self.pipes,
                    enc,
                    scratch.v_pre_norm_window,
                    &self.dummy,
                    scratch.d_k_pre_norm,
                    scratch.d_v_pre_norm,
                    n_kv_heads,
                    head_dim,
                    eps,
                    false,
                );
                if let (Some(v_lora), Some(v_grad)) = (lora.v.as_ref(), grad.v.as_ref()) {
                    let r = v_lora.rank as usize;
                    let s = v_lora.scale;
                    lora_matmul_row_chained(
                        &self.ctx,
                        &self.pipes,
                        enc,
                        v_lora.a,
                        scratch.norm_x_attn_window,
                        v_lora.z,
                        d_model,
                        r,
                        1.0,
                        false,
                    );
                    lora_outer_add_chained(
                        &self.ctx,
                        &self.pipes,
                        enc,
                        scratch.d_v_pre_norm,
                        v_lora.z,
                        v_grad.d_b,
                        n_kv_heads * head_dim,
                        r,
                        s,
                        true,
                    );
                    lora_matmul_col_chained(
                        &self.ctx,
                        &self.pipes,
                        enc,
                        v_lora.b,
                        scratch.d_v_pre_norm,
                        v_lora.z,
                        n_kv_heads * head_dim,
                        r,
                        1.0,
                        false,
                    );
                    lora_outer_add_chained(
                        &self.ctx,
                        &self.pipes,
                        enc,
                        v_lora.z,
                        scratch.norm_x_attn_window,
                        v_grad.d_a,
                        r,
                        d_model,
                        s,
                        true,
                    );
                }
            }
        }

        // After the per-history loop, the windows hold the LAST
        // history position's values. Restore them to the `pos`-slice
        // so any downstream code that relies on the windows holding
        // the final-position activations (currently only the
        // `attn_norm` backward below, which doesn't read these) sees
        // the right state.
        enc.copy_buffer_to_buffer(
            cap.norm_x_attn,
            (pos as u64) * d_model_bytes,
            scratch.norm_x_attn_window,
            0,
            d_model_bytes,
        );

        // attn_norm rmsnorm backward — flows the attn block contribution
        // into d_hidden_tmp2, then accumulates into running d_hidden.
        rmsnorm_backward_chained(
            &self.ctx,
            &self.pipes,
            enc,
            scratch.hidden_in_window,
            &attn_norm_w,
            scratch.d_hidden_tmp,
            scratch.d_hidden_tmp2,
            d_model,
            eps,
            true,
        );
        residual_add_chained(
            &self.ctx,
            &self.pipes,
            enc,
            scratch.d_hidden,
            scratch.d_hidden_tmp2,
            d_model,
        );

        Ok(())
    }
}