rlx-metal 0.2.14

Metal backend for RLX — Apple Silicon GPU via Metal Performance Shaders + custom MSL kernels
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824
11825
11826
11827
11828
11829
11830
11831
11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
11904
11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
11965
11966
11967
11968
11969
11970
11971
11972
11973
11974
11975
11976
11977
11978
11979
11980
11981
11982
11983
11984
11985
11986
11987
11988
11989
11990
11991
11992
11993
11994
11995
11996
11997
11998
11999
12000
12001
12002
12003
12004
12005
12006
12007
12008
12009
12010
12011
12012
12013
12014
12015
12016
12017
12018
12019
12020
12021
12022
12023
12024
12025
12026
12027
12028
12029
12030
12031
12032
12033
12034
12035
12036
12037
12038
12039
12040
12041
12042
12043
12044
12045
12046
12047
12048
12049
12050
12051
12052
12053
12054
12055
12056
12057
12058
12059
12060
12061
12062
12063
12064
12065
12066
12067
12068
12069
12070
12071
12072
12073
12074
12075
12076
12077
12078
12079
12080
12081
12082
12083
12084
12085
12086
12087
12088
12089
12090
12091
12092
12093
12094
12095
12096
12097
12098
12099
12100
12101
12102
12103
12104
12105
12106
12107
12108
12109
12110
12111
12112
12113
12114
12115
12116
12117
12118
12119
12120
12121
12122
12123
12124
12125
12126
12127
12128
12129
12130
12131
12132
12133
12134
12135
12136
12137
12138
12139
12140
12141
12142
12143
12144
12145
12146
12147
12148
12149
12150
12151
12152
12153
12154
12155
12156
12157
12158
12159
12160
12161
12162
12163
12164
12165
12166
12167
12168
12169
12170
12171
12172
12173
12174
12175
12176
12177
12178
12179
12180
12181
12182
12183
12184
12185
12186
12187
12188
12189
12190
12191
12192
12193
12194
12195
12196
12197
12198
12199
12200
12201
12202
12203
12204
12205
12206
12207
12208
12209
12210
12211
12212
12213
12214
12215
12216
12217
12218
12219
12220
12221
12222
12223
12224
12225
12226
12227
12228
12229
12230
12231
12232
12233
12234
12235
12236
12237
12238
12239
12240
12241
12242
12243
12244
12245
12246
12247
12248
12249
12250
12251
12252
12253
12254
12255
12256
12257
12258
12259
12260
12261
12262
12263
12264
12265
12266
12267
12268
12269
12270
12271
12272
12273
12274
12275
12276
12277
12278
12279
12280
12281
12282
12283
12284
12285
12286
12287
12288
12289
12290
12291
12292
12293
12294
12295
12296
12297
12298
12299
12300
12301
12302
12303
12304
12305
12306
12307
12308
12309
12310
12311
12312
12313
12314
12315
12316
12317
12318
12319
12320
12321
12322
12323
12324
12325
12326
12327
12328
12329
12330
12331
12332
12333
12334
12335
12336
12337
12338
12339
12340
12341
12342
12343
12344
12345
12346
12347
12348
12349
12350
12351
12352
12353
12354
12355
12356
12357
12358
12359
12360
12361
12362
12363
12364
12365
12366
12367
12368
12369
12370
12371
12372
12373
12374
12375
12376
12377
12378
12379
12380
12381
12382
12383
12384
12385
12386
12387
12388
12389
12390
12391
12392
12393
12394
12395
12396
12397
12398
12399
12400
12401
12402
12403
12404
12405
12406
12407
12408
12409
12410
12411
12412
12413
12414
12415
12416
12417
12418
12419
12420
12421
12422
12423
12424
12425
12426
12427
12428
12429
12430
12431
12432
12433
12434
12435
12436
12437
12438
12439
12440
12441
12442
12443
12444
12445
12446
12447
12448
12449
12450
12451
12452
12453
12454
12455
12456
12457
12458
12459
12460
12461
12462
12463
12464
12465
12466
12467
12468
12469
12470
12471
12472
12473
12474
12475
12476
12477
12478
12479
12480
12481
12482
12483
12484
12485
12486
12487
12488
12489
12490
12491
12492
12493
12494
12495
12496
12497
12498
12499
12500
12501
12502
12503
12504
12505
12506
12507
12508
12509
12510
12511
12512
12513
12514
12515
12516
12517
12518
12519
12520
12521
12522
12523
12524
12525
12526
12527
12528
12529
12530
12531
12532
12533
12534
12535
12536
12537
12538
12539
12540
12541
12542
12543
12544
12545
12546
12547
12548
12549
12550
12551
12552
12553
12554
12555
12556
12557
12558
12559
12560
12561
12562
12563
12564
12565
12566
12567
12568
12569
12570
12571
12572
12573
12574
12575
12576
12577
12578
12579
12580
12581
12582
12583
12584
12585
12586
12587
12588
12589
12590
12591
12592
12593
12594
12595
12596
12597
12598
12599
12600
12601
12602
12603
12604
12605
12606
12607
12608
12609
12610
12611
12612
12613
12614
12615
12616
12617
12618
12619
12620
12621
12622
12623
12624
12625
12626
12627
12628
12629
12630
12631
12632
12633
12634
12635
12636
12637
12638
12639
12640
12641
12642
12643
12644
12645
12646
12647
12648
12649
12650
12651
12652
12653
12654
12655
12656
12657
12658
12659
12660
12661
12662
12663
12664
12665
12666
12667
12668
12669
12670
12671
12672
12673
12674
12675
12676
12677
12678
12679
12680
12681
12682
12683
12684
12685
12686
12687
12688
12689
12690
12691
12692
12693
12694
12695
12696
12697
12698
12699
12700
12701
12702
12703
12704
12705
12706
12707
12708
12709
12710
12711
12712
12713
12714
12715
12716
12717
12718
12719
12720
12721
12722
12723
12724
12725
12726
12727
12728
12729
12730
12731
12732
12733
12734
12735
12736
12737
12738
12739
12740
12741
12742
12743
12744
12745
12746
12747
12748
12749
12750
12751
12752
12753
12754
12755
12756
12757
12758
12759
12760
12761
12762
12763
12764
12765
12766
12767
12768
12769
12770
12771
12772
12773
12774
12775
12776
12777
12778
12779
12780
12781
12782
12783
12784
12785
12786
12787
12788
12789
12790
12791
12792
12793
12794
12795
12796
12797
12798
12799
12800
12801
12802
12803
12804
12805
12806
12807
12808
12809
12810
12811
12812
12813
12814
12815
12816
12817
12818
12819
12820
12821
12822
12823
12824
12825
12826
12827
12828
12829
12830
12831
12832
12833
12834
12835
12836
12837
12838
12839
12840
12841
12842
12843
12844
12845
12846
12847
12848
12849
12850
12851
12852
12853
12854
12855
12856
12857
12858
12859
12860
12861
12862
12863
12864
12865
12866
12867
12868
12869
12870
12871
12872
12873
12874
12875
12876
12877
12878
12879
12880
12881
12882
12883
12884
12885
12886
12887
12888
12889
12890
12891
12892
12893
12894
12895
12896
12897
12898
12899
12900
12901
12902
12903
12904
12905
12906
12907
12908
12909
12910
12911
12912
12913
12914
12915
12916
12917
12918
12919
12920
12921
12922
12923
12924
12925
12926
12927
12928
12929
12930
12931
12932
12933
12934
12935
12936
12937
12938
12939
12940
12941
12942
12943
12944
12945
12946
12947
12948
12949
12950
12951
12952
12953
12954
12955
12956
12957
12958
12959
12960
12961
12962
12963
12964
12965
12966
12967
12968
12969
12970
12971
12972
12973
12974
12975
12976
12977
12978
12979
12980
12981
12982
12983
12984
12985
12986
12987
12988
12989
12990
12991
12992
12993
12994
12995
12996
12997
12998
12999
13000
13001
13002
13003
13004
13005
13006
13007
13008
13009
13010
13011
13012
13013
13014
13015
13016
13017
13018
13019
13020
13021
13022
13023
13024
13025
13026
13027
13028
13029
13030
13031
13032
13033
13034
13035
13036
13037
13038
13039
13040
13041
13042
13043
13044
13045
13046
13047
13048
13049
13050
13051
13052
13053
13054
13055
13056
13057
13058
13059
13060
13061
13062
13063
13064
13065
13066
13067
13068
13069
13070
13071
13072
13073
13074
13075
13076
13077
13078
13079
13080
13081
13082
13083
13084
13085
13086
13087
13088
13089
13090
13091
13092
13093
13094
13095
13096
13097
13098
13099
13100
13101
13102
13103
13104
13105
13106
13107
13108
13109
13110
13111
13112
13113
13114
13115
13116
13117
13118
13119
13120
13121
13122
13123
13124
13125
13126
13127
13128
13129
13130
13131
13132
13133
13134
13135
13136
13137
13138
13139
13140
13141
13142
13143
13144
13145
13146
13147
13148
13149
13150
13151
13152
13153
13154
13155
13156
13157
13158
13159
13160
13161
13162
13163
13164
13165
13166
13167
13168
13169
13170
13171
13172
13173
13174
13175
13176
13177
13178
13179
13180
13181
13182
13183
13184
13185
13186
13187
13188
13189
13190
13191
13192
13193
13194
13195
13196
13197
13198
13199
13200
13201
13202
13203
13204
13205
13206
13207
13208
13209
13210
13211
13212
13213
13214
13215
13216
13217
13218
13219
13220
13221
13222
13223
13224
13225
13226
13227
13228
13229
13230
13231
13232
13233
13234
13235
13236
13237
13238
13239
13240
13241
13242
13243
13244
13245
13246
13247
13248
13249
13250
13251
13252
13253
13254
13255
13256
13257
13258
13259
13260
13261
13262
13263
13264
13265
13266
13267
13268
13269
13270
13271
13272
13273
13274
13275
13276
13277
13278
13279
13280
13281
13282
13283
13284
13285
13286
13287
13288
13289
13290
13291
13292
13293
13294
13295
13296
13297
13298
13299
13300
13301
13302
13303
13304
13305
13306
13307
13308
13309
13310
13311
13312
13313
13314
13315
13316
13317
13318
13319
13320
13321
13322
13323
13324
13325
13326
13327
13328
13329
13330
13331
13332
13333
13334
13335
13336
13337
13338
13339
13340
13341
13342
13343
13344
13345
13346
13347
13348
13349
13350
13351
13352
13353
13354
13355
13356
13357
13358
13359
13360
13361
13362
13363
13364
13365
13366
13367
13368
13369
13370
13371
13372
13373
13374
13375
13376
13377
13378
13379
13380
13381
13382
13383
13384
13385
13386
13387
13388
13389
13390
13391
13392
13393
13394
13395
13396
13397
13398
13399
13400
13401
13402
13403
13404
13405
13406
13407
13408
13409
13410
13411
13412
13413
13414
13415
13416
13417
13418
13419
13420
13421
13422
13423
13424
13425
13426
13427
13428
13429
13430
13431
13432
13433
13434
13435
13436
13437
13438
13439
13440
13441
13442
13443
13444
13445
13446
13447
13448
13449
13450
13451
13452
13453
13454
13455
13456
13457
13458
13459
13460
13461
13462
13463
13464
13465
13466
13467
13468
13469
13470
13471
13472
13473
13474
13475
13476
13477
13478
13479
13480
13481
13482
13483
13484
13485
13486
13487
13488
13489
13490
13491
13492
13493
13494
13495
13496
13497
13498
13499
13500
13501
13502
13503
13504
13505
13506
13507
13508
13509
13510
13511
13512
13513
13514
13515
13516
13517
13518
13519
13520
13521
13522
13523
13524
13525
13526
13527
13528
13529
13530
13531
13532
13533
13534
13535
13536
13537
13538
13539
13540
13541
13542
13543
13544
13545
13546
13547
13548
13549
13550
13551
13552
13553
13554
13555
13556
13557
13558
13559
13560
13561
13562
13563
13564
13565
13566
13567
13568
13569
13570
13571
13572
13573
13574
13575
13576
13577
13578
13579
13580
13581
13582
13583
13584
13585
13586
13587
13588
13589
13590
13591
13592
13593
13594
13595
13596
13597
13598
13599
13600
13601
13602
13603
13604
13605
13606
13607
13608
13609
13610
13611
13612
13613
13614
13615
13616
13617
13618
13619
13620
13621
13622
13623
// RLX — versatile ML compiler + runtime.
// Copyright (C) 2026 Eugene Hauptmann, Nataliya Kosmyna.
// SPDX-License-Identifier: MIT OR Apache-2.0

//! Custom MSL compute kernels for element-wise + fused operations.
//!
//! Each kernel is a Metal compute pipeline. Compiled once at startup
//! from inline MSL source, dispatched via command encoder at runtime.
//!
//! Mirrors rlx-cpu/src/kernels.rs but for GPU.

use crate::device::metal_device;
use crate::mtl::{Buffer, ComputePipelineState, Library, MTLResourceOptions};
use std::sync::OnceLock;

/// Inline MSL source for all kernels — compiled once at startup.
pub const RLX_KERNELS_MSL: &str = r#"
#include <metal_stdlib>
using namespace metal;

// Scalar activation math — one `inline float rlx_<name>_scalar(float x)` per
// activation, generated from the shared rlxsl manifest. Every activation kernel
// below (scalar f32/f16 + tuned vec4) calls these, so each activation's math
// (the A&S erf polynomial, softplus trick, …) has a single on-device definition.
// @@RLX_SCALAR_ACT_FNS@@

// Rust-`powf`-matching scalar pow (signed for negative base + integer exponent;
// bare MSL `pow` NaNs on any negative base). Generated from rlxsl so the tuned
// broadcast kernels below get the correct semantics at zero perf cost (inlined).
// @@RLX_POW_SCALAR_FN@@

// Naive sgemm: one thread per output element, one dot product each.
// C[m,n] = A[m,k] @ B[k,n]. Good baseline; tiled version below for speed.
kernel void sgemm(
    device const float* A [[buffer(0)]],
    device const float* B [[buffer(1)]],
    device float* C       [[buffer(2)]],
    constant uint& M      [[buffer(3)]],
    constant uint& K      [[buffer(4)]],
    constant uint& N      [[buffer(5)]],
    uint2 gid [[thread_position_in_grid]]
) {
    uint row = gid.y;
    uint col = gid.x;
    if (row >= M || col >= N) return;
    float sum = 0.0;
    for (uint k = 0; k < K; ++k) {
        sum += A[row * K + k] * B[k * N + col];
    }
    C[row * N + col] = sum;
}

// F16 activations × F32 weights → F32, f32 accumulate. Mirror of `sgemm_f16w`
// for the A operand. Training's mixed-precision backward produces grad matmuls
// `A_f16^T @ grad_f32` (activation cast to f16, upstream grad in f32); routing
// those through the plain f32 `sgemm` would reinterpret the 2-byte f16 A bytes
// as f32 and corrupt the result. This reads A as half and accumulates in f32,
// so K-long reductions (e.g. K=B*T=1024) never overflow f16's ±65504 range.
kernel void sgemm_f16a(
    device const half* A  [[buffer(0)]],
    device const float* B [[buffer(1)]],
    device float* C       [[buffer(2)]],
    constant uint& M      [[buffer(3)]],
    constant uint& K      [[buffer(4)]],
    constant uint& N      [[buffer(5)]],
    uint2 gid [[thread_position_in_grid]]
) {
    uint row = gid.y;
    uint col = gid.x;
    if (row >= M || col >= N) return;
    float sum = 0.0;
    for (uint k = 0; k < K; ++k) {
        sum += float(A[row * K + k]) * B[k * N + col];
    }
    C[row * N + col] = sum;
}

// F32 activations × F16 weights → F32 (Zonos Metal Linear). Avoids casting
// the full weight matrix to F32 on every decode step.
kernel void sgemm_f16w(
    device const float* A [[buffer(0)]],
    device const half* B  [[buffer(1)]],
    device float* C       [[buffer(2)]],
    constant uint& M      [[buffer(3)]],
    constant uint& K      [[buffer(4)]],
    constant uint& N      [[buffer(5)]],
    uint2 gid [[thread_position_in_grid]]
) {
    uint row = gid.y;
    uint col = gid.x;
    if (row >= M || col >= N) return;
    float sum = 0.0;
    for (uint k = 0; k < K; ++k) {
        sum += A[row * K + k] * float(B[k * N + col]);
    }
    C[row * N + col] = sum;
}

// Small-M F16-weight GEMM (CFG decode: M=2). One thread per output column;
// all M rows share each B load — padded simdgroup wastes 6/8 A rows when M=2.
kernel void sgemm_f16w_small_m(
    device const float* A [[buffer(0)]],
    device const half* B  [[buffer(1)]],
    device float* C       [[buffer(2)]],
    constant uint& M      [[buffer(3)]],
    constant uint& K      [[buffer(4)]],
    constant uint& N      [[buffer(5)]],
    uint gid [[thread_position_in_grid]]
) {
    uint col = gid;
    if (col >= N || M == 0u || M > 4u) return;

    // Hot path: M==2 (Zonos CFG) — no per-k row branches.
    if (M == 2u) {
        float acc0 = 0.0f;
        float acc1 = 0.0f;
        uint k = 0;
        for (; k + 3u < K; k += 4u) {
            float4 bv = float4(
                float(B[(k + 0u) * N + col]),
                float(B[(k + 1u) * N + col]),
                float(B[(k + 2u) * N + col]),
                float(B[(k + 3u) * N + col]));
            float4 a0 = *(device const float4*)(A + k);
            float4 a1 = *(device const float4*)(A + K + k);
            acc0 += a0.x * bv.x + a0.y * bv.y + a0.z * bv.z + a0.w * bv.w;
            acc1 += a1.x * bv.x + a1.y * bv.y + a1.z * bv.z + a1.w * bv.w;
        }
        for (; k < K; ++k) {
            float b = float(B[k * N + col]);
            acc0 += A[k] * b;
            acc1 += A[K + k] * b;
        }
        C[col] = acc0;
        C[N + col] = acc1;
        return;
    }

    float acc0 = 0.0f;
    float acc1 = 0.0f;
    float acc2 = 0.0f;
    float acc3 = 0.0f;
    uint k = 0;
    for (; k + 3u < K; k += 4u) {
        float b0 = float(B[(k + 0u) * N + col]);
        float b1 = float(B[(k + 1u) * N + col]);
        float b2 = float(B[(k + 2u) * N + col]);
        float b3 = float(B[(k + 3u) * N + col]);
        float4 bv = float4(b0, b1, b2, b3);
        {
            float4 a = *(device const float4*)(A + k);
            acc0 += a.x * bv.x + a.y * bv.y + a.z * bv.z + a.w * bv.w;
        }
        if (M > 1u) {
            float4 a = *(device const float4*)(A + K + k);
            acc1 += a.x * bv.x + a.y * bv.y + a.z * bv.z + a.w * bv.w;
        }
        if (M > 2u) {
            float4 a = *(device const float4*)(A + 2u * K + k);
            acc2 += a.x * bv.x + a.y * bv.y + a.z * bv.z + a.w * bv.w;
        }
        if (M > 3u) {
            float4 a = *(device const float4*)(A + 3u * K + k);
            acc3 += a.x * bv.x + a.y * bv.y + a.z * bv.z + a.w * bv.w;
        }
    }
    for (; k < K; ++k) {
        float b = float(B[k * N + col]);
        acc0 += A[k] * b;
        if (M > 1u) acc1 += A[K + k] * b;
        if (M > 2u) acc2 += A[2u * K + k] * b;
        if (M > 3u) acc3 += A[3u * K + k] * b;
    }
    C[col] = acc0;
    if (M > 1u) C[N + col] = acc1;
    if (M > 2u) C[2u * N + col] = acc2;
    if (M > 3u) C[3u * N + col] = acc3;
}

// M=1 F16-weight GEMV with K-splitting. `sgemm_f16w_small_m` launches one
// thread per output column — for the decode projections (N ~1-3k) that is only
// ~1-3k threads, too few to saturate memory bandwidth (~37 GB/s of ~273 peak).
// Here KSPLIT threads cooperate per column: each strides k by KSPLIT and sums a
// K/KSPLIT slice, then the partials reduce through threadgroup memory. The
// threadgroup is (32 cols × KSPLIT), and a simdgroup is the 32 columns for one
// `ks`, so `B[k*N + col .. col+31]` stays fully coalesced (KSPLIT× the threads
// at the same DRAM traffic). A: [1,K] f32, B: [K,N] f16, C: [1,N] f32.
kernel void gemv_f16w_splitk(
    device const float* A [[buffer(0)]],
    device const half*  B [[buffer(1)]],
    device float*       C [[buffer(2)]],
    constant uint& K      [[buffer(3)]],
    constant uint& N      [[buffer(4)]],
    uint2 tid  [[thread_position_in_threadgroup]],
    uint2 tgid [[threadgroup_position_in_grid]]
) {
    constexpr uint KSPLIT = 32u;
    threadgroup float2 partial[32][32];
    // Two columns per thread via half2 loads: 32 threads × 4 B = 128-byte
    // coalesced transactions (full Apple cache line) vs 64 B for scalar half.
    // Requires N even & col-pair aligned — the dispatch only routes here for
    // even N (all transformer projection widths); odd N falls to scalar.
    uint col = tgid.x * 64u + tid.x * 2u;
    uint ks = tid.y;
    float2 acc = float2(0.0f);
    if (col < N) {
        device const half2* Bp = (device const half2*)(B + col);
        for (uint k = ks; k < K; k += KSPLIT) {
            float a = A[k];
            half2 b = Bp[k * (N / 2u)];
            acc.x += a * float(b.x);
            acc.y += a * float(b.y);
        }
    }
    partial[tid.x][ks] = acc;
    threadgroup_barrier(mem_flags::mem_threadgroup);
    if (ks == 0u && col < N) {
        float2 s = float2(0.0f);
        for (uint j = 0u; j < KSPLIT; ++j) s += partial[tid.x][j];
        C[col] = s.x;
        if (col + 1u < N) {
            C[col + 1u] = s.y;
        }
    }
}

// K-partitioned F16 GEMV — CEILING PROBE for small-N decode projections.
// gemv_f16w_splitk launches only N/64 threadgroups (o_proj/down_proj n=1024 →
// 16 tgs), under-occupying the GPU. This variant adds a Kparts z-axis so each
// (col-block, k-partition) tg attends a K-slice and atomic-adds its partial
// into a pre-zeroed C — raising the tg count to (N/64)*Kparts. NOTE: float
// atomic-add is order-nondeterministic, so this is a measurement probe; a
// production version must reduce partials deterministically (scratch + fixed
// order) to stay token-identical.
kernel void gemv_f16w_kpart(
    device const float* A [[buffer(0)]],
    device const half*  B [[buffer(1)]],
    device atomic_float* C [[buffer(2)]],
    constant uint& K      [[buffer(3)]],
    constant uint& N      [[buffer(4)]],
    constant uint& Kparts [[buffer(5)]],
    uint3 tid  [[thread_position_in_threadgroup]],
    uint3 tgid [[threadgroup_position_in_grid]]
) {
    constexpr uint KSPLIT = 32u;
    threadgroup float2 partial[32][32];
    uint col = tgid.x * 64u + tid.x * 2u;
    uint ks = tid.y;
    uint kchunk = (K + Kparts - 1u) / Kparts;
    uint kstart = tgid.z * kchunk;
    uint kend = min(kstart + kchunk, K);
    float2 acc = float2(0.0f);
    if (col < N) {
        device const half2* Bp = (device const half2*)(B + col);
        for (uint k = kstart + ks; k < kend; k += KSPLIT) {
            float a = A[k];
            half2 b = Bp[k * (N / 2u)];
            acc.x += a * float(b.x);
            acc.y += a * float(b.y);
        }
    }
    partial[tid.x][ks] = acc;
    threadgroup_barrier(mem_flags::mem_threadgroup);
    if (ks == 0u && col < N) {
        float2 s = float2(0.0f);
        for (uint j = 0u; j < KSPLIT; ++j) s += partial[tid.x][j];
        atomic_fetch_add_explicit(&C[col], s.x, memory_order_relaxed);
        if (col + 1u < N) atomic_fetch_add_explicit(&C[col + 1u], s.y, memory_order_relaxed);
    }
}

// Zero N floats of an atomic_float region (pre-zero for gemv_f16w_kpart).
kernel void gemv_zero_f32(
    device atomic_float* C [[buffer(0)]],
    constant uint& N      [[buffer(1)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid < N) atomic_store_explicit(&C[gid], 0.0f, memory_order_relaxed);
}

// Padded simdgroup with F16 B (same staging as sgemm_simd_padded).
kernel void sgemm_simd_padded_f16w(
    device const float* A [[buffer(0)]],
    device const half* B  [[buffer(1)]],
    device float* C       [[buffer(2)]],
    constant uint& M      [[buffer(3)]],
    constant uint& K      [[buffer(4)]],
    constant uint& N      [[buffer(5)]],
    uint2 tgid [[threadgroup_position_in_grid]],
    uint sgid  [[simdgroup_index_in_threadgroup]],
    uint slid  [[thread_index_in_simdgroup]]
) {
    uint row_base = tgid.y * 8;
    uint col_base = tgid.x * 8;
    threadgroup float A_pad[64];
    threadgroup float B_pad[64];
    simdgroup_float8x8 a, b, c;
    c = simdgroup_float8x8(0.0f);
    for (uint k = 0; k < K; k += 8) {
        for (uint i = 0; i < 2; ++i) {
            uint idx = i * 32 + slid;
            uint ar = idx / 8;
            uint ac = idx % 8;
            uint src_row = row_base + ar;
            uint src_col = k + ac;
            float v = (src_row < M && src_col < K) ? A[src_row * K + src_col] : 0.0f;
            A_pad[idx] = v;
        }
        for (uint i = 0; i < 2; ++i) {
            uint idx = i * 32 + slid;
            uint br = idx / 8;
            uint bc = idx % 8;
            uint src_row = k + br;
            uint src_col = col_base + bc;
            float v = (src_row < K && src_col < N) ? float(B[src_row * N + src_col]) : 0.0f;
            B_pad[idx] = v;
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);
        simdgroup_load(a, A_pad, 8);
        simdgroup_load(b, B_pad, 8);
        simdgroup_multiply_accumulate(c, a, b, c);
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }
    // Write back with bounds (same as sgemm_simd_padded).
    threadgroup float C_pad[64];
    simdgroup_store(c, C_pad, 8);
    threadgroup_barrier(mem_flags::mem_threadgroup);
    for (uint i = 0; i < 2; ++i) {
        uint idx = i * 32 + slid;
        uint cr = idx / 8;
        uint cc = idx % 8;
        uint out_row = row_base + cr;
        uint out_col = col_base + cc;
        if (out_row < M && out_col < N) {
            C[out_row * N + out_col] = C_pad[idx];
        }
    }
}

// 64×64 F16-weight GEMM tile (8 simdgroups) — the prefill fix.
// sgemm_simd_padded_f16w computes one 8×8 output tile per threadgroup (1 MMA
// per K-step, A re-loaded per column block) → ~650 GFLOPS, 4–10× slower than
// MPS per the mps_re_bench RE. This computes a 64×64 tile per threadgroup:
// 8 simdgroups × 8 column-block accumulators, with half→float A/B staged
// cooperatively by 256 threads into threadgroup memory (bounds-zeroed, so any
// M/N/K works) — each staged A/B slab feeds 64 MMAs/K-step, the register-reuse
// MPS exploits. F32 accumulate (more accurate than MPS's f16). Grid
// (ceil(N/64), ceil(M/64)); dispatch with 256 threads/threadgroup.
kernel void sgemm_wide8x64_f16w(
    device const float* A [[buffer(0)]],
    device const half*  B [[buffer(1)]],
    device float* C       [[buffer(2)]],
    constant uint& M      [[buffer(3)]],
    constant uint& K      [[buffer(4)]],
    constant uint& N      [[buffer(5)]],
    uint2 tgid [[threadgroup_position_in_grid]],
    uint sgid  [[simdgroup_index_in_threadgroup]],
    uint slid  [[thread_index_in_simdgroup]]
) {
    constexpr uint TR = 64u, TC = 64u, NACC = 8u;
    uint row0 = tgid.y * TR;
    uint col0 = tgid.x * TC;
    uint tix = sgid * 32u + slid; // 0..255
    threadgroup float As[64u * 8u]; // 64 rows(M) × 8 (K)
    threadgroup float Bs[8u * 64u]; // 8 (K) × 64 cols(N)
    threadgroup float Cs[64u * 64u]; // 64×64 output tile
    simdgroup_float8x8 acc[NACC];
    for (uint j = 0; j < NACC; ++j) acc[j] = make_filled_simdgroup_matrix<float, 8, 8>(0.0f);
    for (uint kk = 0; kk < K; kk += 8u) {
        // Stage A [64×8] and B [8×64], 512 elems each, 256 threads → 2 each.
        for (uint i = 0; i < 2u; ++i) {
            uint idx = i * 256u + tix;
            uint ar = idx / 8u, ac = idx % 8u;
            uint sr = row0 + ar, sc = kk + ac;
            As[idx] = (sr < M && sc < K) ? A[sr * K + sc] : 0.0f;
            uint br = idx / 64u, bc = idx % 64u;
            uint tr = kk + br, tc = col0 + bc;
            Bs[idx] = (tr < K && tc < N) ? float(B[tr * N + tc]) : 0.0f;
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);
        simdgroup_float8x8 a, b;
        simdgroup_load(a, As + sgid * 8u * 8u, 8); // this simdgroup's 8 rows
        for (uint j = 0; j < NACC; ++j) {
            simdgroup_load(b, Bs + j * 8u, 64);
            simdgroup_multiply_accumulate(acc[j], a, b, acc[j]);
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }
    for (uint j = 0; j < NACC; ++j) simdgroup_store(acc[j], Cs + (sgid * 8u) * 64u + j * 8u, 64);
    threadgroup_barrier(mem_flags::mem_threadgroup);
    // Guarded write Cs → C: 4096 elems / 256 threads = 16 each.
    for (uint i = 0; i < 16u; ++i) {
        uint idx = i * 256u + tix;
        uint cr = idx / 64u, cc = idx % 64u;
        uint orow = row0 + cr, ocol = col0 + cc;
        if (orow < M && ocol < N) C[orow * N + ocol] = Cs[idx];
    }
}

// ── Half-precision (f16) variants ──────────────────────────────────────
// Apple Silicon supports simdgroup_half8x8 — same tensor unit pipeline
// but 2× peak FLOPs and ½ memory bandwidth vs simdgroup_float8x8.

// Tiled half-precision matmul: 32x32 output per TG, 16 simdgroups cooperate.
// Inputs A, B and output C all in f16; bias also f16 if provided.
kernel void hgemm_simd_4x4(
    device const half* A [[buffer(0)]],
    device const half* B [[buffer(1)]],
    device half* C       [[buffer(2)]],
    constant uint& M     [[buffer(3)]],
    constant uint& K     [[buffer(4)]],
    constant uint& N     [[buffer(5)]],
    uint2 tgid [[threadgroup_position_in_grid]],
    uint sgid  [[simdgroup_index_in_threadgroup]],
    uint slid  [[thread_index_in_simdgroup]]
) {
    uint sg_row = sgid / 4;
    uint sg_col = sgid % 4;
    uint tg_row_base = tgid.y * 32;
    uint tg_col_base = tgid.x * 32;

    threadgroup half A_tg[32 * 32];
    threadgroup half B_tg[32 * 32];

    simdgroup_half8x8 a, b;
    simdgroup_half8x8 c = simdgroup_half8x8(0.0h);

    for (uint kk = 0; kk < K; kk += 32) {
        uint linear = sgid * 32 + slid;
        for (uint i = 0; i < 2; ++i) {
            uint idx = i * 512 + linear;
            uint ar = idx / 32, ac = idx % 32;
            A_tg[idx] = A[(tg_row_base + ar) * K + (kk + ac)];
        }
        for (uint i = 0; i < 2; ++i) {
            uint idx = i * 512 + linear;
            uint br = idx / 32, bc = idx % 32;
            B_tg[idx] = B[(kk + br) * N + (tg_col_base + bc)];
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);

        for (uint k_inner = 0; k_inner < 32; k_inner += 8) {
            simdgroup_load(a, &A_tg[sg_row * 8 * 32 + k_inner], 32);
            simdgroup_load(b, &B_tg[k_inner * 32 + sg_col * 8], 32);
            simdgroup_multiply_accumulate(c, a, b, c);
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }

    uint out_row = tg_row_base + sg_row * 8;
    uint out_col = tg_col_base + sg_col * 8;
    simdgroup_store(c, &C[out_row * N + out_col], N);
}

// Half-precision matmul + bias + activation fused.
kernel void hgemm_simd_4x4_bias(
    device const half* A     [[buffer(0)]],
    device const half* B     [[buffer(1)]],
    device const half* bias  [[buffer(2)]],
    device half* C           [[buffer(3)]],
    constant uint& M         [[buffer(4)]],
    constant uint& K         [[buffer(5)]],
    constant uint& N         [[buffer(6)]],
    constant uint& act_kind  [[buffer(7)]],
    uint2 tgid [[threadgroup_position_in_grid]],
    uint sgid  [[simdgroup_index_in_threadgroup]],
    uint slid  [[thread_index_in_simdgroup]]
) {
    uint sg_row = sgid / 4;
    uint sg_col = sgid % 4;
    uint tg_row_base = tgid.y * 32;
    uint tg_col_base = tgid.x * 32;

    threadgroup half A_tg[32 * 32];
    threadgroup half B_tg[32 * 32];

    simdgroup_half8x8 a, b;
    simdgroup_half8x8 c = simdgroup_half8x8(0.0h);

    for (uint kk = 0; kk < K; kk += 32) {
        uint linear = sgid * 32 + slid;
        for (uint i = 0; i < 2; ++i) {
            uint idx = i * 512 + linear;
            uint ar = idx / 32, ac = idx % 32;
            A_tg[idx] = A[(tg_row_base + ar) * K + (kk + ac)];
        }
        for (uint i = 0; i < 2; ++i) {
            uint idx = i * 512 + linear;
            uint br = idx / 32, bc = idx % 32;
            B_tg[idx] = B[(kk + br) * N + (tg_col_base + bc)];
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);

        for (uint k_inner = 0; k_inner < 32; k_inner += 8) {
            simdgroup_load(a, &A_tg[sg_row * 8 * 32 + k_inner], 32);
            simdgroup_load(b, &B_tg[k_inner * 32 + sg_col * 8], 32);
            simdgroup_multiply_accumulate(c, a, b, c);
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }

    threadgroup half tile[16 * 64];
    simdgroup_store(c, &tile[sgid * 64], 8);
    threadgroup_barrier(mem_flags::mem_threadgroup);

    uint out_row_base = tg_row_base + sg_row * 8;
    uint out_col_base = tg_col_base + sg_col * 8;
    for (uint i = 0; i < 2; ++i) {
        uint idx = i * 32 + slid;
        uint r = idx / 8;
        uint cc = idx % 8;
        // Promote to fp32 for activation math (more accurate)
        float v = float(tile[sgid * 64 + idx]) + float(bias[out_col_base + cc]);
        if (act_kind == 1) {
            v = rlx_gelu_scalar(v);
        } else if (act_kind == 2) {
            v = rlx_silu_scalar(v);
        }
        C[(out_row_base + r) * N + (out_col_base + cc)] = half(v);
    }
}

// ── Half-precision element-wise + reduction kernels ─────────────────

kernel void bias_add_h(
    device half* data       [[buffer(0)]],
    device const half* bias [[buffer(1)]],
    constant uint& m        [[buffer(2)]],
    constant uint& n        [[buffer(3)]],
    uint2 gid [[thread_position_in_grid]]
) {
    uint row = gid.y, col = gid.x;
    if (row >= m || col >= n) return;
    data[row * n + col] += bias[col];
}

// Core scalar-activation f16 in-place kernels — generated once from the shared
// rlxsl manifest (was hand-written MSL re-inlining the A&S erf polynomial with
// drift: unclamped erf arg + ties-away `round`). Injected by msl_source().
// @@RLX_ACT_INPLACE_H@@

// f16 input, f32 reduction, f16 output (mixed precision LayerNorm)
kernel void layer_norm_h(
    device const half* input [[buffer(0)]],
    device const half* gamma [[buffer(1)]],
    device const half* beta  [[buffer(2)]],
    device half* output      [[buffer(3)]],
    constant uint& h         [[buffer(4)]],
    constant float& eps      [[buffer(5)]],
    uint row [[threadgroup_position_in_grid]],
    uint tid [[thread_position_in_threadgroup]],
    uint tsize [[threads_per_threadgroup]]
) {
    threadgroup float partial_sum[256];
    threadgroup float partial_sumsq[256];

    float local_sum = 0.0f, local_sumsq = 0.0f;
    for (uint i = tid; i < h; i += tsize) {
        float v = float(input[row * h + i]);
        local_sum += v;
        local_sumsq += v * v;
    }
    partial_sum[tid] = local_sum;
    partial_sumsq[tid] = local_sumsq;
    threadgroup_barrier(mem_flags::mem_threadgroup);

    for (uint stride = tsize / 2; stride > 0; stride /= 2) {
        if (tid < stride) {
            partial_sum[tid] += partial_sum[tid + stride];
            partial_sumsq[tid] += partial_sumsq[tid + stride];
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }

    float mean = partial_sum[0] / float(h);
    float var = fmax(0.0f, partial_sumsq[0] / float(h) - mean * mean);
    float inv_std = rsqrt(var + eps);

    for (uint i = tid; i < h; i += tsize) {
        float v = float(input[row * h + i]);
        output[row * h + i] = half((v - mean) * inv_std * float(gamma[i]) + float(beta[i]));
    }
}

kernel void fused_residual_ln_h(
    device const half* x      [[buffer(0)]],
    device const half* res    [[buffer(1)]],
    device const half* gamma  [[buffer(2)]],
    device const half* beta   [[buffer(3)]],
    device half* out          [[buffer(4)]],
    constant uint& h          [[buffer(5)]],
    constant float& eps       [[buffer(6)]],
    uint row [[threadgroup_position_in_grid]],
    uint tid [[thread_position_in_threadgroup]],
    uint tsize [[threads_per_threadgroup]]
) {
    threadgroup float partial_sum[256];
    threadgroup float partial_sumsq[256];

    float local_sum = 0.0f, local_sumsq = 0.0f;
    for (uint i = tid; i < h; i += tsize) {
        float v = float(x[row * h + i]) + float(res[row * h + i]);
        local_sum += v;
        local_sumsq += v * v;
    }
    partial_sum[tid] = local_sum;
    partial_sumsq[tid] = local_sumsq;
    threadgroup_barrier(mem_flags::mem_threadgroup);

    for (uint stride = tsize / 2; stride > 0; stride /= 2) {
        if (tid < stride) {
            partial_sum[tid] += partial_sum[tid + stride];
            partial_sumsq[tid] += partial_sumsq[tid + stride];
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }

    float mean = partial_sum[0] / float(h);
    float var = fmax(0.0f, partial_sumsq[0] / float(h) - mean * mean);
    float inv_std = rsqrt(var + eps);

    for (uint i = tid; i < h; i += tsize) {
        float v = float(x[row * h + i]) + float(res[row * h + i]);
        out[row * h + i] = half((v - mean) * inv_std * float(gamma[i]) + float(beta[i]));
    }
}

kernel void fused_residual_rms_norm_h(
    device const char* arena  [[buffer(0)]],
    constant ulong& x_off     [[buffer(1)]],
    constant ulong& res_off   [[buffer(2)]],
    constant ulong& g_off     [[buffer(3)]],
    constant ulong& b_off     [[buffer(4)]],
    constant ulong& out_off   [[buffer(5)]],
    constant uint& h          [[buffer(6)]],
    constant float& eps       [[buffer(7)]],
    uint row [[threadgroup_position_in_grid]],
    uint tid [[thread_position_in_threadgroup]],
    uint tsize [[threads_per_threadgroup]]
) {
    device const half* x = (device const half*)(arena + x_off);
    device const half* res = (device const half*)(arena + res_off);
    device const half* gamma = (device const half*)(arena + g_off);
    device const half* beta = (device const half*)(arena + b_off);
    device half* out = (device half*)(arena + out_off);
    threadgroup float partial_sumsq[256];
    float local_sumsq = 0.0f;
    for (uint i = tid; i < h; i += tsize) {
        float v = float(x[row * h + i]) + float(res[row * h + i]);
        local_sumsq += v * v;
    }
    partial_sumsq[tid] = local_sumsq;
    threadgroup_barrier(mem_flags::mem_threadgroup);
    for (uint stride = tsize / 2; stride > 0; stride /= 2) {
        if (tid < stride) {
            partial_sumsq[tid] += partial_sumsq[tid + stride];
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }
    float inv_rms = rsqrt(partial_sumsq[0] / float(h) + eps);
    for (uint i = tid; i < h; i += tsize) {
        float v = float(x[row * h + i]) + float(res[row * h + i]);
        out[row * h + i] = half(v * inv_rms * float(gamma[i]) + float(beta[i]));
    }
}

kernel void elem_add_h(
    device const half* a [[buffer(0)]],
    device const half* b [[buffer(1)]],
    device half* c       [[buffer(2)]],
    constant uint& len   [[buffer(3)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= len) return;
    c[gid] = a[gid] + b[gid];
}

kernel void elem_mul_h(
    device const half* a [[buffer(0)]],
    device const half* b [[buffer(1)]],
    device half* c       [[buffer(2)]],
    constant uint& len   [[buffer(3)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= len) return;
    c[gid] = a[gid] * b[gid];
}

kernel void elem_sub_h(
    device const half* a [[buffer(0)]],
    device const half* b [[buffer(1)]],
    device half* c       [[buffer(2)]],
    constant uint& len   [[buffer(3)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= len) return;
    c[gid] = a[gid] - b[gid];
}

kernel void elem_div_h(
    device const half* a [[buffer(0)]],
    device const half* b [[buffer(1)]],
    device half* c       [[buffer(2)]],
    constant uint& len   [[buffer(3)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= len) return;
    c[gid] = a[gid] / b[gid];
}

kernel void elem_max_h(
    device const half* a [[buffer(0)]],
    device const half* b [[buffer(1)]],
    device half* c       [[buffer(2)]],
    constant uint& len   [[buffer(3)]],
    uint gid [[thread_position_in_grid]]
) { if (gid >= len) return; c[gid] = max(a[gid], b[gid]); }

kernel void elem_min_h(
    device const half* a [[buffer(0)]],
    device const half* b [[buffer(1)]],
    device half* c       [[buffer(2)]],
    constant uint& len   [[buffer(3)]],
    uint gid [[thread_position_in_grid]]
) { if (gid >= len) return; c[gid] = min(a[gid], b[gid]); }

kernel void elem_pow_h(
    device const half* a [[buffer(0)]],
    device const half* b [[buffer(1)]],
    device half* c       [[buffer(2)]],
    constant uint& len   [[buffer(3)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= len) return;
    c[gid] = half(pow(float(a[gid]), float(b[gid])));
}

kernel void gather_axis0_h(
    device const half* table [[buffer(0)]],
    device const half* idx   [[buffer(1)]],
    device half* out         [[buffer(2)]],
    constant uint& num_idx   [[buffer(3)]],
    constant uint& trailing  [[buffer(4)]],
    uint2 gid [[thread_position_in_grid]]
) {
    uint i = gid.y, j = gid.x;
    if (i >= num_idx || j >= trailing) return;
    uint row = uint(float(idx[i]));
    out[i * trailing + j] = table[row * trailing + j];
}

kernel void narrow_lastax_h(
    device const char* arena_src [[buffer(0)]],
    device char* arena_dst       [[buffer(1)]],
    constant uint& outer     [[buffer(2)]],
    constant uint& src_axis  [[buffer(3)]],
    constant uint& start     [[buffer(4)]],
    constant uint& len       [[buffer(5)]],
    constant ulong& src_byte_off [[buffer(6)]],
    constant ulong& dst_byte_off [[buffer(7)]],
    uint2 gid [[thread_position_in_grid]]
) {
    device const half* src = (device const half*)(arena_src + src_byte_off);
    device half* dst       = (device half*)(arena_dst + dst_byte_off);
    uint i = gid.y, j = gid.x;
    if (i >= outer || j >= len) return;
    dst[i * len + j] = src[i * src_axis + start + j];
}

kernel void sdpa_h(
    device const half* Q    [[buffer(0)]],
    device const half* K    [[buffer(1)]],
    device const half* V    [[buffer(2)]],
    device const half* M    [[buffer(3)]],
    device half* OUT        [[buffer(4)]],
    constant uint& batch      [[buffer(5)]],
    constant uint& seq        [[buffer(6)]],
    constant uint& heads      [[buffer(7)]],
    constant uint& head_dim   [[buffer(8)]],
    constant uint& seq_stride [[buffer(9)]],
    constant uint& mask_kind  [[buffer(10)]],
    constant uint& seq_k      [[buffer(11)]],  // unused; mirrors sdpa signature
    constant uint& k_stride   [[buffer(12)]],  // unused; mirrors sdpa signature
    constant uint& bhsd       [[buffer(13)]],  // unused; mirrors sdpa signature
    constant uint& window     [[buffer(14)]],
    uint tgid_x [[threadgroup_position_in_grid]],
    uint tid    [[thread_position_in_threadgroup]],
    uint tsize  [[threads_per_threadgroup]]
) {
    (void)seq_k; (void)k_stride; (void)bhsd;  // accepted to share encode_sdpa layout
    threadgroup float scores[64 * 64];
    threadgroup float row_max;
    threadgroup float row_sum;

    uint bi = tgid_x / heads;
    uint hi = tgid_x % heads;
    if (bi >= batch) return;

    uint hs = heads * head_dim;
    float scale = rsqrt(float(head_dim));
    uint per_batch_stride = seq_stride * hs;

    uint total = seq * seq;
    for (uint idx = tid; idx < total; idx += tsize) {
        uint qi = idx / seq;
        uint ki = idx % seq;
        float dot = 0.0f;
        uint q_base = bi * per_batch_stride + qi * hs + hi * head_dim;
        uint k_base = bi * per_batch_stride + ki * hs + hi * head_dim;
        for (uint d = 0; d < head_dim; ++d) {
            dot += float(Q[q_base + d]) * float(K[k_base + d]);
        }
        float s = dot * scale;
        if (mask_kind == 1u) {
            if (ki > qi) s = -1e9f;
        } else if (mask_kind == 2u) {
            if (float(M[bi * seq_stride + ki]) < 0.5f) s = -1e9f;
        } else if (mask_kind == 4u) {
            // Lq == Lk here (sdpa_h is prefill-only), so abs_q == qi.
            uint lo = qi > window ? qi - window : 0u;
            if (ki < lo || ki > qi) s = -1e9f;
        }
        scores[qi * seq + ki] = s;
    }
    threadgroup_barrier(mem_flags::mem_threadgroup);

    for (uint qi = 0; qi < seq; ++qi) {
        if (tid == 0) {
            float mx = -1e30f;
            for (uint ki = 0; ki < seq; ++ki) {
                mx = max(mx, scores[qi * seq + ki]);
            }
            row_max = mx;
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);

        if (tid == 0) {
            float sum = 0.0f;
            for (uint ki = 0; ki < seq; ++ki) {
                float e = precise::exp(scores[qi * seq + ki] - row_max);
                scores[qi * seq + ki] = e;
                sum += e;
            }
            row_sum = sum;
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);

        for (uint ki = tid; ki < seq; ki += tsize) {
            scores[qi * seq + ki] /= row_sum;
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }

    uint out_total = seq * head_dim;
    for (uint idx = tid; idx < out_total; idx += tsize) {
        uint qi = idx / head_dim;
        uint d = idx % head_dim;
        float acc = 0.0f;
        for (uint ki = 0; ki < seq; ++ki) {
            uint v_base = bi * per_batch_stride + ki * hs + hi * head_dim;
            acc += scores[qi * seq + ki] * float(V[v_base + d]);
        }
        uint o_base = bi * per_batch_stride + qi * hs + hi * head_dim;
        OUT[o_base + d] = half(acc);
    }
}

kernel void rope_h(
    device const half* x   [[buffer(0)]],
    device const half* cos [[buffer(1)]],
    device const half* sin [[buffer(2)]],
    device half* out       [[buffer(3)]],
    constant uint& batch          [[buffer(4)]],
    constant uint& seq            [[buffer(5)]],
    constant uint& hidden         [[buffer(6)]],
    constant uint& head_dim       [[buffer(7)]],
    constant uint& src_row_stride [[buffer(8)]],
    constant uint& seq_stride     [[buffer(9)]],
    constant uint& n_rot          [[buffer(10)]],
    constant uint& cos_per_token  [[buffer(11)]],
    constant uint& interleaved    [[buffer(12)]],
    uint3 gid [[thread_position_in_grid]]
) {
    uint rot_half = n_rot / 2;
    // The cos/sin table stores exactly the rotation angles — `n_rot/2` per
    // token, NOT head_dim/2. For PARTIAL rope (n_rot < head_dim) a head_dim/2
    // row stride overruns into the next token's angles from position 1 onward,
    // which is why this indexes by `rot_half`. Full rope has n_rot == head_dim,
    // so rot_half == head_dim/2 and nothing changes there.
    if (gid.x >= head_dim) return;

    uint bs = gid.z;
    uint bi = bs / seq;
    uint si = bs % seq;
    if (bi >= batch || si >= seq) return;

    uint nh = hidden / head_dim;
    uint hi = gid.y;
    if (hi >= nh) return;

    // Per-seq-position table by default; per global token for ragged decode.
    uint cos_row = (cos_per_token != 0u) ? bs : si;

    // PLAN L1 — `seq_stride` is the compile-time full extent for buffer
    // offsets; `seq` is the (possibly scaled) iteration bound.
    uint src_base = bi * seq_stride * src_row_stride + si * src_row_stride + hi * head_dim;
    uint dst_base = bi * seq_stride * hidden + si * hidden + hi * head_dim;
    uint d = gid.x;
    if (interleaved != 0u) {
        // GPT-J / llama.cpp-NORM: pairs are adjacent (2d, 2d+1). cos/sin
        // row index is the freq d (0..rot_half).
        if (d < rot_half) {
            uint a = 2u * d;
            uint b = 2u * d + 1u;
            float x1 = float(x[src_base + a]);
            float x2 = float(x[src_base + b]);
            float c = float(cos[cos_row * rot_half + d]);
            float s = float(sin[cos_row * rot_half + d]);
            out[dst_base + a] = half(x1 * c - x2 * s);
            out[dst_base + b] = half(x2 * c + x1 * s);
        } else if (d >= n_rot) {
            out[dst_base + d] = x[src_base + d];
        }
    } else if (d < rot_half) {
        float x1 = float(x[src_base + d]);
        float x2 = float(x[src_base + rot_half + d]);
        float c = float(cos[cos_row * rot_half + d]);
        float s = float(sin[cos_row * rot_half + d]);
        out[dst_base + d] = half(x1 * c - x2 * s);
        out[dst_base + rot_half + d] = half(x2 * c + x1 * s);
    } else if (d >= n_rot) {
        out[dst_base + d] = x[src_base + d];
    }
}

// Native f32 fused-attention core for `Op::FusedAttentionBlock` (no-bias
// path). Reads the PACKED QKV projection `[B,S,3*inner]` (per token
// `[Q(inner)|K(inner)|V(inner)]`, heads interleaved), applies optional NeoX
// RoPE to Q/K inline, runs softmax SDPA with the score matrix resident in
// threadgroup memory (one threadgroup per batch·head), and writes the
// attention output `[B,S,inner]`. Collapses narrow×3 + transpose×3 + rope×2
// + attention into a single dispatch; the QKV / out projections stay GEMMs.
// mask_kind: 0=None, 1=Causal, 2=Custom (binary [B,S], <0.5 ⇒ drop).
kernel void fused_attn_block(
    device const float* QKV  [[buffer(0)]],
    device const float* M    [[buffer(1)]],
    device const float* COS  [[buffer(2)]],
    device const float* SIN  [[buffer(3)]],
    device float* OUT        [[buffer(4)]],
    constant uint& batch     [[buffer(5)]],
    constant uint& seq       [[buffer(6)]],
    constant uint& heads     [[buffer(7)]],
    constant uint& head_dim  [[buffer(8)]],
    constant uint& mask_kind [[buffer(9)]],
    constant uint& scale_bits[[buffer(10)]],
    constant uint& has_rope  [[buffer(11)]],
    uint tgid  [[threadgroup_position_in_grid]],
    uint tid   [[thread_position_in_threadgroup]],
    uint tsize [[threads_per_threadgroup]]
) {
    threadgroup float scores[64 * 64];   // seq ≤ 64 (gated in rlx-metal)
    uint inner = heads * head_dim;
    uint bi = tgid / heads;
    uint hi = tgid % heads;
    if (bi >= batch) return;
    float scale = as_type<float>(scale_bits);
    uint half_d = head_dim / 2;
    uint tok = 3u * inner;                // per-token stride in QKV

    uint total = seq * seq;
    for (uint idx = tid; idx < total; idx += tsize) {
        uint qi = idx / seq;
        uint ki = idx % seq;
        uint qb = (bi * seq + qi) * tok + hi * head_dim;            // Q
        uint kb = (bi * seq + ki) * tok + inner + hi * head_dim;    // K
        float dot = 0.0f;
        if (has_rope != 0u) {
            uint qc = qi * half_d;
            uint kc = ki * half_d;
            for (uint i = 0; i < half_d; ++i) {
                float q1 = QKV[qb + i],        q2 = QKV[qb + half_d + i];
                float k1 = QKV[kb + i],        k2 = QKV[kb + half_d + i];
                float cq = COS[qc + i], sq = SIN[qc + i];
                float ck = COS[kc + i], sk = SIN[kc + i];
                float qr1 = q1 * cq - q2 * sq, qr2 = q2 * cq + q1 * sq;
                float kr1 = k1 * ck - k2 * sk, kr2 = k2 * ck + k1 * sk;
                dot += qr1 * kr1 + qr2 * kr2;
            }
        } else {
            for (uint d = 0; d < head_dim; ++d) dot += QKV[qb + d] * QKV[kb + d];
        }
        float s = dot * scale;
        if (mask_kind == 1u) {
            if (ki > qi) s = -1e9f;
        } else if (mask_kind == 2u) {
            if (M[bi * seq + ki] < 0.5f) s = -1e9f;
        }
        scores[qi * seq + ki] = s;
    }
    threadgroup_barrier(mem_flags::mem_threadgroup);

    for (uint qi = tid; qi < seq; qi += tsize) {
        float mx = -1e30f;
        for (uint ki = 0; ki < seq; ++ki) mx = max(mx, scores[qi * seq + ki]);
        float sum = 0.0f;
        for (uint ki = 0; ki < seq; ++ki) {
            float e = precise::exp(scores[qi * seq + ki] - mx);
            scores[qi * seq + ki] = e;
            sum += e;
        }
        float inv = (sum > 0.0f) ? (1.0f / sum) : 0.0f;
        for (uint ki = 0; ki < seq; ++ki) scores[qi * seq + ki] *= inv;
    }
    threadgroup_barrier(mem_flags::mem_threadgroup);

    uint otot = seq * head_dim;
    for (uint idx = tid; idx < otot; idx += tsize) {
        uint qi = idx / head_dim;
        uint d = idx % head_dim;
        float acc = 0.0f;
        for (uint ki = 0; ki < seq; ++ki) {
            uint vb = (bi * seq + ki) * tok + 2u * inner + hi * head_dim;
            acc += scores[qi * seq + ki] * QKV[vb + d];
        }
        OUT[(bi * seq + qi) * inner + hi * head_dim + d] = acc;
    }
}

// Cast f32 → f16 (used at I/O boundary)
kernel void cast_f32_to_f16(
    device const float* src [[buffer(0)]],
    device half* dst        [[buffer(1)]],
    constant uint& len      [[buffer(2)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= len) return;
    dst[gid] = half(src[gid]);
}

// Cast f16 → f32 (used at I/O boundary)
kernel void cast_f16_to_f32(
    device const half* src [[buffer(0)]],
    device float* dst      [[buffer(1)]],
    constant uint& len     [[buffer(2)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= len) return;
    dst[gid] = float(src[gid]);
}

// Plain f32 buffer copy — used for Reshape/Expand thunks when we want
// to stay on the shared compute encoder instead of switching to a blit
// encoder (encoder-switch overhead dominates for small ops).
kernel void copy_f32(
    device const char* arena [[buffer(0)]],
    constant ulong& src_byte_off [[buffer(1)]],
    constant ulong& dst_byte_off [[buffer(2)]],
    constant uint& len [[buffer(3)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= len) return;
    device const float* src = (device const float*)(arena + src_byte_off);
    device float* dst = (device float*)(arena + dst_byte_off);
    dst[gid] = src[gid];
}

kernel void copy4(
    device const char* arena [[buffer(0)]],
    constant ulong& src_byte_off [[buffer(1)]],
    constant ulong& dst_byte_off [[buffer(2)]],
    constant uint& len4 [[buffer(3)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= len4) return;
    device const packed_float4* src = (device const packed_float4*)(arena + src_byte_off);
    device packed_float4* dst = (device packed_float4*)(arena + dst_byte_off);
    dst[gid] = src[gid];
}

// SIMD-group matrix sgemm: uses Apple Silicon's dedicated tensor units.
// One simdgroup computes an 8x8 output tile via simdgroup_multiply_accumulate.
// Threadgroup has 32 threads = 1 simdgroup, computing one 8x8 tile of C.
// For larger output, dispatch more threadgroups.
//
// All dimensions must be multiples of 8 for this kernel. Caller is responsible
// for routing non-multiple-of-8 cases to the scalar tiled fallback.
kernel void sgemm_simd(
    device const float* A [[buffer(0)]],
    device const float* B [[buffer(1)]],
    device float* C       [[buffer(2)]],
    constant uint& M      [[buffer(3)]],
    constant uint& K      [[buffer(4)]],
    constant uint& N      [[buffer(5)]],
    uint2 tgid [[threadgroup_position_in_grid]],
    uint sgid  [[simdgroup_index_in_threadgroup]]
) {
    uint row_base = tgid.y * 8;
    uint col_base = tgid.x * 8;
    if (row_base >= M || col_base >= N) return;

    simdgroup_float8x8 a;
    simdgroup_float8x8 b;
    simdgroup_float8x8 c;
    c = simdgroup_float8x8(0.0f);

    for (uint k = 0; k < K; k += 8) {
        simdgroup_load(a, A + row_base * K + k, K);
        simdgroup_load(b, B + k * N + col_base, N);
        simdgroup_multiply_accumulate(c, a, b, c);
    }

    simdgroup_store(c, C + row_base * N + col_base, N);
}

// High-throughput simdgroup matmul: 32x32 output per threadgroup,
// 4x4 = 16 simdgroups cooperate through threadgroup memory.
// Each B element is reused 4× across rows of simdgroups; each A element 4× across cols.
// K loaded in 32-wide stripes into threadgroup memory.
//
// Requires M%32==K%32==N%32==0. Falls back to sgemm_simd for smaller dims.
kernel void sgemm_simd_4x4(
    device const float* A [[buffer(0)]],
    device const float* B [[buffer(1)]],
    device float* C       [[buffer(2)]],
    constant uint& M      [[buffer(3)]],
    constant uint& K      [[buffer(4)]],
    constant uint& N      [[buffer(5)]],
    uint2 tgid [[threadgroup_position_in_grid]],
    uint sgid  [[simdgroup_index_in_threadgroup]],
    uint slid  [[thread_index_in_simdgroup]]
) {
    // 4x4 simdgroup grid within threadgroup
    uint sg_row = sgid / 4;  // 0..3
    uint sg_col = sgid % 4;  // 0..3

    uint tg_row_base = tgid.y * 32;
    uint tg_col_base = tgid.x * 32;

    threadgroup float A_tg[32 * 32];  // 4 KB
    threadgroup float B_tg[32 * 32];  // 4 KB

    simdgroup_float8x8 a, b, c;
    c = simdgroup_float8x8(0.0f);

    for (uint kk = 0; kk < K; kk += 32) {
        // Cooperative load: 16 simdgroups × 32 threads = 512 threads
        // load 32×32 A tile and 32×32 B tile (1024 floats each = 4 elements per thread)
        uint linear = sgid * 32 + slid; // 0..511
        for (uint i = 0; i < 2; ++i) {
            uint idx = i * 512 + linear;
            uint ar = idx / 32;
            uint ac = idx % 32;
            A_tg[idx] = A[(tg_row_base + ar) * K + (kk + ac)];
        }
        for (uint i = 0; i < 2; ++i) {
            uint idx = i * 512 + linear;
            uint br = idx / 32;
            uint bc = idx % 32;
            B_tg[idx] = B[(kk + br) * N + (tg_col_base + bc)];
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);

        // 4 inner-K steps of 8 each, accumulating into c
        for (uint k_inner = 0; k_inner < 32; k_inner += 8) {
            simdgroup_load(a, &A_tg[sg_row * 8 * 32 + k_inner], 32);
            simdgroup_load(b, &B_tg[k_inner * 32 + sg_col * 8], 32);
            simdgroup_multiply_accumulate(c, a, b, c);
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }

    uint out_row = tg_row_base + sg_row * 8;
    uint out_col = tg_col_base + sg_col * 8;
    simdgroup_store(c, &C[out_row * N + out_col], N);
}

// Register-blocked simdgroup GEMM family (plain + split-K) — generated per tile
// config by `sgemm_tile_variant!` / `sgemm_splitk_variant!` (see msl_source). Each
// config is TROWS×TCOLS output/threadgroup, TROWS/8 simdgroups × NACC 8×8-col
// accumulators. Measured: beats MPS on aligned shapes (plain on tall/short-K,
// split-K on fat-K). NO edge handling — `pick_sgemm` gates to 64/8 alignment.
// @@RLX_SGEMM_TILES@@

// Zero an f32 buffer region (for split-K accumulation init).
kernel void zero_f32(
    device float* C [[buffer(0)]],
    constant uint& n [[buffer(1)]],
    uint i [[thread_position_in_grid]]
) {
    if (i < n) C[i] = 0.0f;
}

// 32x32-tiled with bias + optional activation fused.
kernel void sgemm_simd_4x4_bias(
    device const float* A [[buffer(0)]],
    device const float* B [[buffer(1)]],
    device const float* bias [[buffer(2)]],
    device float* C       [[buffer(3)]],
    constant uint& M      [[buffer(4)]],
    constant uint& K      [[buffer(5)]],
    constant uint& N      [[buffer(6)]],
    constant uint& act_kind [[buffer(7)]],
    uint2 tgid [[threadgroup_position_in_grid]],
    uint sgid  [[simdgroup_index_in_threadgroup]],
    uint slid  [[thread_index_in_simdgroup]]
) {
    uint sg_row = sgid / 4;
    uint sg_col = sgid % 4;
    uint tg_row_base = tgid.y * 32;
    uint tg_col_base = tgid.x * 32;

    threadgroup float A_tg[32 * 32];
    threadgroup float B_tg[32 * 32];

    simdgroup_float8x8 a, b, c;
    c = simdgroup_float8x8(0.0f);

    for (uint kk = 0; kk < K; kk += 32) {
        uint linear = sgid * 32 + slid;
        for (uint i = 0; i < 2; ++i) {
            uint idx = i * 512 + linear;
            uint ar = idx / 32;
            uint ac = idx % 32;
            A_tg[idx] = A[(tg_row_base + ar) * K + (kk + ac)];
        }
        for (uint i = 0; i < 2; ++i) {
            uint idx = i * 512 + linear;
            uint br = idx / 32;
            uint bc = idx % 32;
            B_tg[idx] = B[(kk + br) * N + (tg_col_base + bc)];
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);

        for (uint k_inner = 0; k_inner < 32; k_inner += 8) {
            simdgroup_load(a, &A_tg[sg_row * 8 * 32 + k_inner], 32);
            simdgroup_load(b, &B_tg[k_inner * 32 + sg_col * 8], 32);
            simdgroup_multiply_accumulate(c, a, b, c);
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }

    // Stage 8x8 output, apply bias + activation per element
    threadgroup float tile[16 * 64]; // 16 simdgroups × 64 elements each
    simdgroup_store(c, &tile[sgid * 64], 8);
    threadgroup_barrier(mem_flags::mem_threadgroup);

    uint out_row_base = tg_row_base + sg_row * 8;
    uint out_col_base = tg_col_base + sg_col * 8;
    for (uint i = 0; i < 2; ++i) {
        uint idx = i * 32 + slid;
        uint r = idx / 8;
        uint cc = idx % 8;
        float v = tile[sgid * 64 + idx] + bias[out_col_base + cc];
        if (act_kind == 1) {
            v = rlx_gelu_scalar(v);
        } else if (act_kind == 2) {
            v = rlx_silu_scalar(v);
        }
        C[(out_row_base + r) * N + (out_col_base + cc)] = v;
    }
}

// sgemm + bias (broadcast per column) fused into one kernel.
// Dispatched same as sgemm_simd: 1 threadgroup per 8x8 output tile.
kernel void sgemm_simd_bias(
    device const float* A [[buffer(0)]],
    device const float* B [[buffer(1)]],
    device const float* bias [[buffer(2)]],
    device float* C       [[buffer(3)]],
    constant uint& M      [[buffer(4)]],
    constant uint& K      [[buffer(5)]],
    constant uint& N      [[buffer(6)]],
    constant uint& act_kind [[buffer(7)]],  // 0=none, 1=gelu, 2=silu
    uint2 tgid [[threadgroup_position_in_grid]],
    uint slid  [[thread_index_in_simdgroup]]
) {
    uint row_base = tgid.y * 8;
    uint col_base = tgid.x * 8;
    if (row_base >= M || col_base >= N) return;

    simdgroup_float8x8 a, b, c;
    c = simdgroup_float8x8(0.0f);

    for (uint k = 0; k < K; k += 8) {
        simdgroup_load(a, A + row_base * K + k, K);
        simdgroup_load(b, B + k * N + col_base, N);
        simdgroup_multiply_accumulate(c, a, b, c);
    }

    // Stage tile in threadgroup memory, then apply bias + activation per element
    threadgroup float tile[64];
    simdgroup_store(c, tile, 8);
    threadgroup_barrier(mem_flags::mem_threadgroup);

    // 32 threads × 2 elements each cover the 8x8 tile
    for (uint i = 0; i < 2; ++i) {
        uint idx = i * 32 + slid;
        uint r = idx / 8;
        uint cc = idx % 8;
        float v = tile[idx] + bias[col_base + cc];
        if (act_kind == 1) {
            v = rlx_gelu_scalar(v);
        } else if (act_kind == 2) {
            v = rlx_silu_scalar(v);
        }
        C[(row_base + r) * N + (col_base + cc)] = v;
    }
}

// Padded variant: arbitrary M with bounds-checked stores + bias + optional act.
kernel void sgemm_simd_padded_bias(
    device const float* A [[buffer(0)]],
    device const float* B [[buffer(1)]],
    device const float* bias [[buffer(2)]],
    device float* C       [[buffer(3)]],
    constant uint& M      [[buffer(4)]],
    constant uint& K      [[buffer(5)]],
    constant uint& N      [[buffer(6)]],
    constant uint& act_kind [[buffer(7)]],
    uint2 tgid [[threadgroup_position_in_grid]],
    uint slid  [[thread_index_in_simdgroup]]
) {
    uint row_base = tgid.y * 8;
    uint col_base = tgid.x * 8;

    threadgroup float A_pad[64];
    threadgroup float B_pad[64];

    simdgroup_float8x8 a, b, c;
    c = simdgroup_float8x8(0.0f);

    for (uint k = 0; k < K; k += 8) {
        for (uint i = 0; i < 2; ++i) {
            uint idx = i * 32 + slid;
            uint ar = idx / 8, ac = idx % 8;
            uint sr = row_base + ar, sc = k + ac;
            A_pad[idx] = (sr < M && sc < K) ? A[sr * K + sc] : 0.0f;
        }
        for (uint i = 0; i < 2; ++i) {
            uint idx = i * 32 + slid;
            uint br = idx / 8, bc = idx % 8;
            uint sr = k + br, sc = col_base + bc;
            B_pad[idx] = (sr < K && sc < N) ? B[sr * N + sc] : 0.0f;
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);

        simdgroup_load(a, A_pad, 8);
        simdgroup_load(b, B_pad, 8);
        simdgroup_multiply_accumulate(c, a, b, c);
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }

    threadgroup float C_pad[64];
    simdgroup_store(c, C_pad, 8);
    threadgroup_barrier(mem_flags::mem_threadgroup);

    for (uint i = 0; i < 2; ++i) {
        uint idx = i * 32 + slid;
        uint r = idx / 8;
        uint cc = idx % 8;
        uint dst_row = row_base + r;
        uint dst_col = col_base + cc;
        if (dst_row < M && dst_col < N) {
            float v = C_pad[idx] + bias[dst_col];
            if (act_kind == 1) {
                v = rlx_gelu_scalar(v);
            } else if (act_kind == 2) {
                v = rlx_silu_scalar(v);
            }
            C[dst_row * N + dst_col] = v;
        }
    }
}

// Padded simdgroup sgemm: handles arbitrary M/K/N by zero-padding.
// Reads A row-by-row with bounds checks, computes 8x8 simdgroup tiles,
// writes back row-by-row with bounds checks. Slower than sgemm_simd for
// aligned dims but works for the common batch=1 case (m=6).
//
// Strategy: pre-stage A's relevant rows into threadgroup memory (zero-pad
// missing rows), then use simdgroup ops on the padded tile. Same for B's
// columns.
kernel void sgemm_simd_padded(
    device const float* A [[buffer(0)]],
    device const float* B [[buffer(1)]],
    device float* C       [[buffer(2)]],
    constant uint& M      [[buffer(3)]],
    constant uint& K      [[buffer(4)]],
    constant uint& N      [[buffer(5)]],
    uint2 tgid [[threadgroup_position_in_grid]],
    uint sgid  [[simdgroup_index_in_threadgroup]],
    uint slid  [[thread_index_in_simdgroup]]
) {
    uint row_base = tgid.y * 8;
    uint col_base = tgid.x * 8;

    // Per-tile staging in threadgroup memory: 8x8 A tile, 8x8 B tile.
    // 32 threads collaborate to stage; reuse the simdgroup_load API for
    // the multiply once data is in threadgroup or device memory.
    threadgroup float A_pad[64];
    threadgroup float B_pad[64];

    simdgroup_float8x8 a, b, c;
    c = simdgroup_float8x8(0.0f);

    for (uint k = 0; k < K; k += 8) {
        // Stage 8x8 A tile with bounds-checked loads (32 threads cover 64 elements: 2 each)
        for (uint i = 0; i < 2; ++i) {
            uint idx = i * 32 + slid;
            uint ar = idx / 8;
            uint ac = idx % 8;
            uint src_row = row_base + ar;
            uint src_col = k + ac;
            float v = (src_row < M && src_col < K) ? A[src_row * K + src_col] : 0.0f;
            A_pad[idx] = v;
        }
        // Stage 8x8 B tile
        for (uint i = 0; i < 2; ++i) {
            uint idx = i * 32 + slid;
            uint br = idx / 8;
            uint bc = idx % 8;
            uint src_row = k + br;
            uint src_col = col_base + bc;
            float v = (src_row < K && src_col < N) ? B[src_row * N + src_col] : 0.0f;
            B_pad[idx] = v;
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);

        simdgroup_load(a, A_pad, 8);
        simdgroup_load(b, B_pad, 8);
        simdgroup_multiply_accumulate(c, a, b, c);
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }

    // Bounds-checked store of the 8x8 C tile (32 threads × 2 elements each)
    threadgroup float C_pad[64];
    simdgroup_store(c, C_pad, 8);
    threadgroup_barrier(mem_flags::mem_threadgroup);
    for (uint i = 0; i < 2; ++i) {
        uint idx = i * 32 + slid;
        uint cr = idx / 8;
        uint cc = idx % 8;
        uint dst_row = row_base + cr;
        uint dst_col = col_base + cc;
        if (dst_row < M && dst_col < N) {
            C[dst_row * N + dst_col] = C_pad[idx];
        }
    }
}

// sgemm_simd_padded with a fused full-tensor residual add in the epilogue:
// C = A·B + R (R same shape as C, [M,N]). Emitted for Op::FusedMatMulResidual
// so the transformer residual `add(skip, matmul_out)` folds into the matmul's
// store instead of a separate elementwise-add dispatch — one fewer launch on a
// launch-latency-bound decode. Body is identical to sgemm_simd_padded; only the
// store adds R[idx].
kernel void sgemm_simd_padded_residual(
    device const float* A [[buffer(0)]],
    device const float* B [[buffer(1)]],
    device float* C       [[buffer(2)]],
    constant uint& M      [[buffer(3)]],
    constant uint& K      [[buffer(4)]],
    constant uint& N      [[buffer(5)]],
    device const float* R [[buffer(6)]],
    uint2 tgid [[threadgroup_position_in_grid]],
    uint sgid  [[simdgroup_index_in_threadgroup]],
    uint slid  [[thread_index_in_simdgroup]]
) {
    uint row_base = tgid.y * 8;
    uint col_base = tgid.x * 8;

    threadgroup float A_pad[64];
    threadgroup float B_pad[64];

    simdgroup_float8x8 a, b, c;
    c = simdgroup_float8x8(0.0f);

    for (uint k = 0; k < K; k += 8) {
        for (uint i = 0; i < 2; ++i) {
            uint idx = i * 32 + slid;
            uint ar = idx / 8;
            uint ac = idx % 8;
            uint src_row = row_base + ar;
            uint src_col = k + ac;
            float v = (src_row < M && src_col < K) ? A[src_row * K + src_col] : 0.0f;
            A_pad[idx] = v;
        }
        for (uint i = 0; i < 2; ++i) {
            uint idx = i * 32 + slid;
            uint br = idx / 8;
            uint bc = idx % 8;
            uint src_row = k + br;
            uint src_col = col_base + bc;
            float v = (src_row < K && src_col < N) ? B[src_row * N + src_col] : 0.0f;
            B_pad[idx] = v;
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);

        simdgroup_load(a, A_pad, 8);
        simdgroup_load(b, B_pad, 8);
        simdgroup_multiply_accumulate(c, a, b, c);
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }

    threadgroup float C_pad[64];
    simdgroup_store(c, C_pad, 8);
    threadgroup_barrier(mem_flags::mem_threadgroup);
    for (uint i = 0; i < 2; ++i) {
        uint idx = i * 32 + slid;
        uint cr = idx / 8;
        uint cc = idx % 8;
        uint dst_row = row_base + cr;
        uint dst_col = col_base + cc;
        if (dst_row < M && dst_col < N) {
            uint o = dst_row * N + dst_col;
            C[o] = C_pad[idx] + R[o];
        }
    }
}

// Tiled sgemm: TILExTILE output blocks, K loaded in TILE-wide stripes
// into threadgroup memory. Used for non-multiple-of-8 dimensions.
constant uint TILE = 16;

kernel void sgemm_tiled(
    device const float* A [[buffer(0)]],
    device const float* B [[buffer(1)]],
    device float* C       [[buffer(2)]],
    constant uint& M      [[buffer(3)]],
    constant uint& K      [[buffer(4)]],
    constant uint& N      [[buffer(5)]],
    uint2 gid  [[thread_position_in_grid]],
    uint2 tid  [[thread_position_in_threadgroup]],
    uint2 tgid [[threadgroup_position_in_grid]]
) {
    threadgroup float Asub[16][16];
    threadgroup float Bsub[16][16];

    uint row = tgid.y * TILE + tid.y;
    uint col = tgid.x * TILE + tid.x;

    float sum = 0.0;
    uint num_tiles = (K + TILE - 1) / TILE;

    for (uint t = 0; t < num_tiles; ++t) {
        uint a_col = t * TILE + tid.x;
        uint b_row = t * TILE + tid.y;
        Asub[tid.y][tid.x] = (row < M && a_col < K) ? A[row * K + a_col] : 0.0;
        Bsub[tid.y][tid.x] = (b_row < K && col < N) ? B[b_row * N + col] : 0.0;
        threadgroup_barrier(mem_flags::mem_threadgroup);

        for (uint k = 0; k < TILE; ++k) {
            sum += Asub[tid.y][k] * Bsub[k][tid.x];
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }

    if (row < M && col < N) {
        C[row * N + col] = sum;
    }
}

// out = bias_add(data, bias, m, n)
kernel void bias_add(
    device float* data [[buffer(0)]],
    device const float* bias [[buffer(1)]],
    constant uint& m [[buffer(2)]],
    constant uint& n [[buffer(3)]],
    uint2 gid [[thread_position_in_grid]]
) {
    uint row = gid.y;
    uint col = gid.x;
    if (row >= m || col >= n) return;
    data[row * n + col] += bias[col];
}

// in-place GELU using Abramowitz & Stegun erf approximation
// (matches CPU NEON kernel for parity)
// f32 gelu / gelu_approx in-place (arena form) — generated from rlxsl. The tuned
// vec4 `*_inplace4` variants below stay hand-written (peak-perf hot path).
// @@RLX_GELU_INPLACE_F32@@

kernel void gelu_approx_inplace4(
    device char* arena [[buffer(0)]],
    constant ulong& data_byte_off [[buffer(1)]],
    constant uint& len4 [[buffer(2)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= len4) return;
    device packed_float4* data = (device packed_float4*)(arena + data_byte_off);
    packed_float4 px = data[gid];
    packed_float4 out;
    for (uint c = 0; c < 4; ++c) {
        out[c] = rlx_gelu_approx_scalar(px[c]);
    }
    data[gid] = out;
}

kernel void gelu_approx_out4(
    device const char* arena [[buffer(0)]],
    constant ulong& src_off [[buffer(1)]],
    constant ulong& dst_off [[buffer(2)]],
    constant uint& len4 [[buffer(3)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= len4) return;
    device const packed_float4* src = (device const packed_float4*)(arena + src_off);
    device packed_float4* dst = (device packed_float4*)(arena + dst_off);
    packed_float4 px = src[gid];
    packed_float4 out;
    for (uint c = 0; c < 4; ++c) {
        out[c] = rlx_gelu_approx_scalar(px[c]);
    }
    dst[gid] = out;
}

kernel void gelu_inplace4(
    device char* arena [[buffer(0)]],
    constant ulong& data_byte_off [[buffer(1)]],
    constant uint& len4 [[buffer(2)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= len4) return;
    device packed_float4* data = (device packed_float4*)(arena + data_byte_off);
    packed_float4 px = data[gid];
    packed_float4 out;
    for (uint c = 0; c < 4; ++c) {
        out[c] = rlx_gelu_scalar(px[c]);
    }
    data[gid] = out;
}

kernel void silu_inplace4(
    device char* arena [[buffer(0)]],
    constant ulong& data_byte_off [[buffer(1)]],
    constant uint& len4 [[buffer(2)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= len4) return;
    device packed_float4* data = (device packed_float4*)(arena + data_byte_off);
    packed_float4 px = data[gid];
    packed_float4 out;
    for (uint c = 0; c < 4; ++c) {
        out[c] = rlx_silu_scalar(px[c]);
    }
    data[gid] = out;
}

// Out-of-place SiLU: dst = src * sigmoid(src) — skips a prior Copy.
kernel void silu_out4(
    device char* arena [[buffer(0)]],
    constant ulong& src_byte_off [[buffer(1)]],
    constant ulong& dst_byte_off [[buffer(2)]],
    constant uint& len4 [[buffer(3)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= len4) return;
    device const packed_float4* src = (device const packed_float4*)(arena + src_byte_off);
    device packed_float4* dst = (device packed_float4*)(arena + dst_byte_off);
    packed_float4 px = src[gid];
    packed_float4 out;
    for (uint c = 0; c < 4; ++c) {
        out[c] = rlx_silu_scalar(px[c]);
    }
    dst[gid] = out;
}

// rhs [cols] broadcast across rows: out[m, n] = lhs[m*cols+n] op rhs[n]
kernel void binary_broadcast_rhs_col_f32(
    device const float* lhs [[buffer(0)]],
    device const float* rhs [[buffer(1)]],
    device float* dst       [[buffer(2)]],
    constant uint& rows     [[buffer(3)]],
    constant uint& cols     [[buffer(4)]],
    constant uint& op       [[buffer(5)]],
    uint2 gid [[thread_position_in_grid]]
) {
    uint m = gid.y;
    uint n = gid.x;
    if (m >= rows || n >= cols) return;
    float lv = lhs[m * cols + n];
    float rv = rhs[n];
    float out;
    switch (op) {
        case 0: out = lv + rv; break;
        case 1: out = lv - rv; break;
        case 2: out = lv * rv; break;
        case 3: out = lv / rv; break;
        case 4: out = max(lv, rv); break;
        case 5: out = min(lv, rv); break;
        default: out = rlx_pow_scalar(lv, rv); break;
    }
    dst[m * cols + n] = out;
}

kernel void binary_broadcast_rhs_col4(
    device const float* lhs [[buffer(0)]],
    device const float* rhs [[buffer(1)]],
    device float* dst       [[buffer(2)]],
    constant uint& rows     [[buffer(3)]],
    constant uint& cols4    [[buffer(4)]],
    constant uint& op       [[buffer(5)]],
    uint2 gid [[thread_position_in_grid]]
) {
    uint m = gid.y;
    uint n4 = gid.x;
    uint cols = cols4 * 4u;
    if (m >= rows || n4 >= cols4) return;
    device const packed_float4* lhs4 =
        (device const packed_float4*)(lhs + m * cols);
    device const packed_float4* rhs4 = (device const packed_float4*)(rhs);
    device packed_float4* dst4 = (device packed_float4*)(dst + m * cols);
    packed_float4 lv = lhs4[n4];
    packed_float4 rv = rhs4[n4];
    packed_float4 out;
    switch (op) {
        case 0: out = lv + rv; break;
        case 1: out = lv - rv; break;
        case 2: out = lv * rv; break;
        case 3: out = lv / rv; break;
        case 4: out = max(lv, rv); break;
        case 5: out = min(lv, rv); break;
        default: out = rlx_pow_scalar(lv, rv); break;
    }
    dst4[n4] = out;
}

// Dense lhs + rhs row vector broadcast on the last axis (e.g. `[rows, cols] op [rows, 1]`).
kernel void binary_broadcast_rhs_row_f32(
    device const float* lhs [[buffer(0)]],
    device const float* rhs [[buffer(1)]],
    device float* dst       [[buffer(2)]],
    constant uint& rows     [[buffer(3)]],
    constant uint& cols     [[buffer(4)]],
    constant uint& op       [[buffer(5)]],
    uint2 gid [[thread_position_in_grid]]
) {
    uint m = gid.y;
    uint n = gid.x;
    if (m >= rows || n >= cols) return;
    float lv = lhs[m * cols + n];
    float rv = rhs[m];
    float out;
    switch (op) {
        case 0: out = lv + rv; break;
        case 1: out = lv - rv; break;
        case 2: out = lv * rv; break;
        case 3: out = lv / rv; break;
        case 4: out = max(lv, rv); break;
        case 5: out = min(lv, rv); break;
        default: out = rlx_pow_scalar(lv, rv); break;
    }
    dst[m * cols + n] = out;
}

kernel void binary_broadcast_rhs_row4(
    device const float* lhs [[buffer(0)]],
    device const float* rhs [[buffer(1)]],
    device float* dst       [[buffer(2)]],
    constant uint& rows     [[buffer(3)]],
    constant uint& cols4    [[buffer(4)]],
    constant uint& op       [[buffer(5)]],
    uint2 gid [[thread_position_in_grid]]
) {
    uint m = gid.y;
    uint n4 = gid.x;
    uint cols = cols4 * 4u;
    if (m >= rows || n4 >= cols4) return;
    device const packed_float4* lhs4 =
        (device const packed_float4*)(lhs + m * cols);
    float rv = rhs[m];
    packed_float4 rv4 = packed_float4(rv);
    device packed_float4* dst4 = (device packed_float4*)(dst + m * cols);
    packed_float4 lv = lhs4[n4];
    packed_float4 out;
    switch (op) {
        case 0: out = lv + rv4; break;
        case 1: out = lv - rv4; break;
        case 2: out = lv * rv4; break;
        case 3: out = lv / rv4; break;
        case 4: out = max(lv, rv4); break;
        case 5: out = min(lv, rv4); break;
        default: out = rlx_pow_scalar(lv, rv4); break;
    }
    dst4[n4] = out;
}

// Dense lhs + scalar rhs (all broadcast strides zero).
kernel void binary_broadcast_rhs_scalar_f32(
    device const char* arena [[buffer(0)]],
    constant ulong& lhs_off [[buffer(1)]],
    constant ulong& rhs_off [[buffer(2)]],
    constant ulong& dst_off [[buffer(3)]],
    constant uint& len      [[buffer(4)]],
    constant uint& op       [[buffer(5)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= len) return;
    device const float* lhs = (device const float*)(arena + lhs_off);
    device const float* rhs = (device const float*)(arena + rhs_off);
    device float* dst = (device float*)(arena + dst_off);
    float lv = lhs[gid];
    float rv = rhs[0];
    float out;
    switch (op) {
        case 0: out = lv + rv; break;
        case 1: out = lv - rv; break;
        case 2: out = lv * rv; break;
        case 3: out = lv / rv; break;
        case 4: out = max(lv, rv); break;
        case 5: out = min(lv, rv); break;
        default: out = rlx_pow_scalar(lv, rv); break;
    }
    dst[gid] = out;
}

kernel void binary_broadcast_rhs_scalar4(
    device const char* arena [[buffer(0)]],
    constant ulong& lhs_off [[buffer(1)]],
    constant ulong& rhs_off [[buffer(2)]],
    constant ulong& dst_off [[buffer(3)]],
    constant uint& len4             [[buffer(4)]],
    constant uint& op               [[buffer(5)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= len4) return;
    device const packed_float4* lhs = (device const packed_float4*)(arena + lhs_off);
    device const float* rhs = (device const float*)(arena + rhs_off);
    device packed_float4* dst = (device packed_float4*)(arena + dst_off);
    float rv = rhs[0];
    packed_float4 rv4 = packed_float4(rv);
    packed_float4 lv = lhs[gid];
    packed_float4 out;
    switch (op) {
        case 0: out = lv + rv4; break;
        case 1: out = lv - rv4; break;
        case 2: out = lv * rv4; break;
        case 3: out = lv / rv4; break;
        case 4: out = max(lv, rv4); break;
        case 5: out = min(lv, rv4); break;
        default: out = rlx_pow_scalar(lv, rv4); break;
    }
    dst[gid] = out;
}

// Dense lhs + rhs broadcast on exactly one axis (e.g. `[B, T, H] op [B, 1, H]`).
kernel void binary_broadcast_1ax_f32(
    device const float* lhs [[buffer(0)]],
    device const float* rhs [[buffer(1)]],
    device float* dst       [[buffer(2)]],
    constant uint& rows     [[buffer(3)]],
    constant uint& cols     [[buffer(4)]],
    constant uint& mid      [[buffer(5)]],
    constant uint& op       [[buffer(6)]],
    uint2 gid [[thread_position_in_grid]]
) {
    uint col = gid.x;
    uint row = gid.y;
    if (col >= cols || row >= rows) return;
    uint pre_i = row / mid;
    uint li = row * cols + col;
    uint ri = pre_i * cols + col;
    float lv = lhs[li];
    float rv = rhs[ri];
    float out;
    switch (op) {
        case 0: out = lv + rv; break;
        case 1: out = lv - rv; break;
        case 2: out = lv * rv; break;
        case 3: out = lv / rv; break;
        case 4: out = max(lv, rv); break;
        case 5: out = min(lv, rv); break;
        default: out = rlx_pow_scalar(lv, rv); break;
    }
    dst[li] = out;
}

kernel void binary_broadcast_1ax4(
    device const float* lhs [[buffer(0)]],
    device const float* rhs [[buffer(1)]],
    device float* dst       [[buffer(2)]],
    constant uint& rows     [[buffer(3)]],
    constant uint& cols4    [[buffer(4)]],
    constant uint& mid      [[buffer(5)]],
    constant uint& op       [[buffer(6)]],
    uint2 gid [[thread_position_in_grid]]
) {
    uint col4 = gid.x;
    uint row = gid.y;
    uint cols = cols4 * 4u;
    if (col4 >= cols4 || row >= rows) return;
    uint pre_i = row / mid;
    device const packed_float4* lhs4 =
        (device const packed_float4*)(lhs + row * cols);
    device const float* rhs_row = rhs + pre_i * cols;
    device const packed_float4* rhs4 =
        (device const packed_float4*)(rhs_row);
    device packed_float4* dst4 =
        (device packed_float4*)(dst + row * cols);
    packed_float4 lv = lhs4[col4];
    packed_float4 rv = rhs4[col4];
    packed_float4 out;
    switch (op) {
        case 0: out = lv + rv; break;
        case 1: out = lv - rv; break;
        case 2: out = lv * rv; break;
        case 3: out = lv / rv; break;
        case 4: out = max(lv, rv); break;
        case 5: out = min(lv, rv); break;
        default: out = rlx_pow_scalar(lv, rv); break;
    }
    dst4[col4] = out;
}

// Binary-op math generated once from the shared rlxsl manifest (fixes the
// negative-base `pow` and 32-bit-bitwise drift the hand-written switch had).
// @@RLX_BINARY_FN@@
inline float fused_bin(float lv, float rv, uint op) { return rlx_binary_apply(op, lv, rv); }

// Same-size element-wise binary by opcode (Mod/bitwise: no dedicated elem_*).
kernel void elem_binop(
    device const float* a [[buffer(0)]],
    device const float* b [[buffer(1)]],
    device float* c        [[buffer(2)]],
    constant uint& n       [[buffer(3)]],
    constant uint& op      [[buffer(4)]],
    uint gid [[thread_position_in_grid]]
) { if (gid >= n) return; c[gid] = fused_bin(a[gid], b[gid], op); }

kernel void elem_binop_h(
    device const half* a [[buffer(0)]],
    device const half* b [[buffer(1)]],
    device half* c       [[buffer(2)]],
    constant uint& n     [[buffer(3)]],
    constant uint& op    [[buffer(4)]],
    uint gid [[thread_position_in_grid]]
) { if (gid >= n) return; c[gid] = half(fused_bin(float(a[gid]), float(b[gid]), op)); }

// Fused-region activation dispatch. Its mini opcode scheme (0=gelu 1=silu
// 2=relu 3=sigmoid 4=tanh) routes to the shared `rlx_<name>_scalar` functions
// generated from rlxsl — so the A&S erf polynomial (and its drift) is no longer
// re-inlined here. Metal inlines these, so the fused epilogue stays as fast.
inline float fused_act(float x, uint act) {
    switch (act) {
        case 0: return rlx_gelu_scalar(x);
        case 1: return rlx_silu_scalar(x);
        case 2: return rlx_relu_scalar(x);
        case 3: return rlx_sigmoid_scalar(x);
        case 4: return rlx_tanh_scalar(x);
        default: return x;
    }
}

kernel void fused_binary_activation_f32(
    device const float* lhs [[buffer(0)]],
    device const float* rhs [[buffer(1)]],
    device float* dst       [[buffer(2)]],
    constant uint& len      [[buffer(3)]],
    constant uint& bin_op   [[buffer(4)]],
    constant uint& act_op   [[buffer(5)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= len) return;
    float v = fused_bin(lhs[gid], rhs[gid], bin_op);
    dst[gid] = fused_act(v, act_op);
}

kernel void fused_binary_activation4(
    device const packed_float4* lhs [[buffer(0)]],
    device const packed_float4* rhs [[buffer(1)]],
    device packed_float4* dst       [[buffer(2)]],
    constant uint& len4             [[buffer(3)]],
    constant uint& bin_op           [[buffer(4)]],
    constant uint& act_op           [[buffer(5)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= len4) return;
    packed_float4 lv = lhs[gid];
    packed_float4 rv = rhs[gid];
    packed_float4 out;
    for (uint c = 0; c < 4; ++c) {
        float v = fused_bin(lv[c], rv[c], bin_op);
        out[c] = fused_act(v, act_op);
    }
    dst[gid] = out;
}

kernel void fused_ternary_activation_f32(
    device const float* lhs [[buffer(0)]],
    device const float* rhs0 [[buffer(1)]],
    device const float* rhs1 [[buffer(2)]],
    device float* dst       [[buffer(3)]],
    constant uint& len      [[buffer(4)]],
    constant uint& bin_op0  [[buffer(5)]],
    constant uint& bin_op1  [[buffer(6)]],
    constant uint& act_op   [[buffer(7)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= len) return;
    float v = fused_bin(lhs[gid], rhs0[gid], bin_op0);
    v = fused_bin(v, rhs1[gid], bin_op1);
    dst[gid] = fused_act(v, act_op);
}

kernel void fused_ternary_activation4(
    device const packed_float4* lhs [[buffer(0)]],
    device const packed_float4* rhs0 [[buffer(1)]],
    device const packed_float4* rhs1 [[buffer(2)]],
    device packed_float4* dst       [[buffer(3)]],
    constant uint& len4             [[buffer(4)]],
    constant uint& bin_op0          [[buffer(5)]],
    constant uint& bin_op1          [[buffer(6)]],
    constant uint& act_op           [[buffer(7)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= len4) return;
    packed_float4 lv = lhs[gid];
    packed_float4 r0 = rhs0[gid];
    packed_float4 r1 = rhs1[gid];
    packed_float4 out;
    for (uint c = 0; c < 4; ++c) {
        float v = fused_bin(lv[c], r0[c], bin_op0);
        v = fused_bin(v, r1[c], bin_op1);
        out[c] = fused_act(v, act_op);
    }
    dst[gid] = out;
}

// Element-wise add: c = a + b (same length)
kernel void elem_add(
    device const char* arena [[buffer(0)]],
    constant ulong& a_off [[buffer(1)]],
    constant ulong& b_off [[buffer(2)]],
    constant ulong& c_off [[buffer(3)]],
    constant uint& len [[buffer(4)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= len) return;
    device const float* a = (device const float*)(arena + a_off);
    device const float* b = (device const float*)(arena + b_off);
    device float* c = (device float*)(arena + c_off);
    c[gid] = a[gid] + b[gid];
}

kernel void elem_add4(
    device const char* arena [[buffer(0)]],
    constant ulong& a_off [[buffer(1)]],
    constant ulong& b_off [[buffer(2)]],
    constant ulong& c_off [[buffer(3)]],
    constant uint& len4 [[buffer(4)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= len4) return;
    device const packed_float4* a = (device const packed_float4*)(arena + a_off);
    device const packed_float4* b = (device const packed_float4*)(arena + b_off);
    device packed_float4* c = (device packed_float4*)(arena + c_off);
    c[gid] = a[gid] + b[gid];
}

kernel void elem_sub4(
    device const char* arena [[buffer(0)]],
    constant ulong& a_off [[buffer(1)]],
    constant ulong& b_off [[buffer(2)]],
    constant ulong& c_off [[buffer(3)]],
    constant uint& len4 [[buffer(4)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= len4) return;
    device const packed_float4* a = (device const packed_float4*)(arena + a_off);
    device const packed_float4* b = (device const packed_float4*)(arena + b_off);
    device packed_float4* c = (device packed_float4*)(arena + c_off);
    c[gid] = a[gid] - b[gid];
}

// Rank-2 broadcast without a per-element rank loop (fallback after vec fast paths).
kernel void binary_broadcast_rank2_f32(
    device const float* lhs       [[buffer(0)]],
    device const float* rhs       [[buffer(1)]],
    device float* dst             [[buffer(2)]],
    constant uint& len            [[buffer(3)]],
    constant uint& dim0           [[buffer(4)]],
    constant uint& dim1           [[buffer(5)]],
    constant uint& lhs_stride0    [[buffer(6)]],
    constant uint& lhs_stride1    [[buffer(7)]],
    constant uint& rhs_stride0    [[buffer(8)]],
    constant uint& rhs_stride1    [[buffer(9)]],
    constant uint& op             [[buffer(10)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= len) return;
    uint j = gid % dim1;
    uint i = gid / dim1;
    uint li = i * lhs_stride0 + j * lhs_stride1;
    uint ri = i * rhs_stride0 + j * rhs_stride1;
    float lv = lhs[li];
    float rv = rhs[ri];
    float out;
    switch (op) {
        case 0: out = lv + rv; break;
        case 1: out = lv - rv; break;
        case 2: out = lv * rv; break;
        case 3: out = lv / rv; break;
        case 4: out = max(lv, rv); break;
        case 5: out = min(lv, rv); break;
        default: out = rlx_pow_scalar(lv, rv); break;
    }
    dst[gid] = out;
}

kernel void binary_broadcast_rank24(
    device const float* lhs       [[buffer(0)]],
    device const float* rhs       [[buffer(1)]],
    device float* dst             [[buffer(2)]],
    constant uint& len4           [[buffer(3)]],
    constant uint& dim0           [[buffer(4)]],
    constant uint& dim1           [[buffer(5)]],
    constant uint& lhs_stride0    [[buffer(6)]],
    constant uint& lhs_stride1    [[buffer(7)]],
    constant uint& rhs_stride0    [[buffer(8)]],
    constant uint& rhs_stride1    [[buffer(9)]],
    constant uint& op             [[buffer(10)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= len4) return;
    uint cols4 = dim1 / 4u;
    uint j4 = gid % cols4;
    uint i = gid / cols4;
    uint j = j4 * 4u;
    device const packed_float4* lhs4 =
        (device const packed_float4*)(lhs + i * lhs_stride0 + j * lhs_stride1);
    device const packed_float4* rhs4 =
        (device const packed_float4*)(rhs + i * rhs_stride0 + j * rhs_stride1);
    device packed_float4* dst4 = (device packed_float4*)(dst + i * dim1 + j);
    packed_float4 lv = *lhs4;
    packed_float4 rv = *rhs4;
    packed_float4 out;
    switch (op) {
        case 0: out = lv + rv; break;
        case 1: out = lv - rv; break;
        case 2: out = lv * rv; break;
        case 3: out = lv / rv; break;
        case 4: out = max(lv, rv); break;
        case 5: out = min(lv, rv); break;
        default: out = rlx_pow_scalar(lv, rv); break;
    }
    *dst4 = out;
}

// Shape-aware broadcast binary op. Each thread computes one output
// element by decomposing gid into coords against `out_dims` (row-major)
// and walking `lhs_strides`/`rhs_strides` (stride 0 ⇒ broadcast).
// Op encoding matches `rlx_ir::op::BinaryOp` discriminant order:
//   0=Add, 1=Sub, 2=Mul, 3=Div, 4=Max, 5=Min, 6=Pow. Rank capped at 8.
kernel void binary_broadcast_f32(
    device const float* lhs       [[buffer(0)]],
    device const float* rhs       [[buffer(1)]],
    device float* dst             [[buffer(2)]],
    constant uint& len            [[buffer(3)]],
    constant uint& rank           [[buffer(4)]],
    constant uint* out_dims       [[buffer(5)]],
    constant uint* lhs_strides    [[buffer(6)]],
    constant uint* rhs_strides    [[buffer(7)]],
    constant uint& op             [[buffer(8)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= len) return;
    uint rem = gid;
    uint li = 0;
    uint ri = 0;
    // Walk from innermost dim to outermost (matches row-major decomposition).
    for (uint ax_rev = 0; ax_rev < rank; ++ax_rev) {
        uint ax = rank - 1 - ax_rev;
        uint sz = out_dims[ax];
        uint coord = rem % sz;
        rem /= sz;
        li += coord * lhs_strides[ax];
        ri += coord * rhs_strides[ax];
    }
    float lv = lhs[li];
    float rv = rhs[ri];
    float out;
    switch (op) {
        case 0: out = lv + rv; break;
        case 1: out = lv - rv; break;
        case 2: out = lv * rv; break;
        case 3: out = lv / rv; break;
        case 4: out = max(lv, rv); break;
        case 5: out = min(lv, rv); break;
        default: out = rlx_pow_scalar(lv, rv); break;
    }
    dst[gid] = out;
}

// Element-wise multiply: c = a * b
kernel void elem_mul(
    device const char* arena [[buffer(0)]],
    constant ulong& a_off [[buffer(1)]],
    constant ulong& b_off [[buffer(2)]],
    constant ulong& c_off [[buffer(3)]],
    constant uint& len [[buffer(4)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= len) return;
    device const float* a = (device const float*)(arena + a_off);
    device const float* b = (device const float*)(arena + b_off);
    device float* c = (device float*)(arena + c_off);
    c[gid] = a[gid] * b[gid];
}

kernel void elem_mul4(
    device const char* arena [[buffer(0)]],
    constant ulong& a_off [[buffer(1)]],
    constant ulong& b_off [[buffer(2)]],
    constant ulong& c_off [[buffer(3)]],
    constant uint& len4 [[buffer(4)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= len4) return;
    device const packed_float4* a = (device const packed_float4*)(arena + a_off);
    device const packed_float4* b = (device const packed_float4*)(arena + b_off);
    device packed_float4* c = (device packed_float4*)(arena + c_off);
    c[gid] = a[gid] * b[gid];
}

kernel void elem_div4(
    device const char* arena [[buffer(0)]],
    constant ulong& a_off [[buffer(1)]],
    constant ulong& b_off [[buffer(2)]],
    constant ulong& c_off [[buffer(3)]],
    constant uint& len4 [[buffer(4)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= len4) return;
    device const packed_float4* a = (device const packed_float4*)(arena + a_off);
    device const packed_float4* b = (device const packed_float4*)(arena + b_off);
    device packed_float4* c = (device packed_float4*)(arena + c_off);
    c[gid] = a[gid] / b[gid];
}

// Element-wise subtract: c = a - b
kernel void elem_sub(
    device const char* arena [[buffer(0)]],
    constant ulong& a_off [[buffer(1)]],
    constant ulong& b_off [[buffer(2)]],
    constant ulong& c_off [[buffer(3)]],
    constant uint& len [[buffer(4)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= len) return;
    device const float* a = (device const float*)(arena + a_off);
    device const float* b = (device const float*)(arena + b_off);
    device float* c = (device float*)(arena + c_off);
    c[gid] = a[gid] - b[gid];
}

// Element-wise divide: c = a / b
kernel void elem_div(
    device const char* arena [[buffer(0)]],
    constant ulong& a_off [[buffer(1)]],
    constant ulong& b_off [[buffer(2)]],
    constant ulong& c_off [[buffer(3)]],
    constant uint& len [[buffer(4)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= len) return;
    device const float* a = (device const float*)(arena + a_off);
    device const float* b = (device const float*)(arena + b_off);
    device float* c = (device float*)(arena + c_off);
    c[gid] = a[gid] / b[gid];
}

kernel void elem_max(
    device const float* a [[buffer(0)]],
    device const float* b [[buffer(1)]],
    device float* c       [[buffer(2)]],
    constant uint& len    [[buffer(3)]],
    uint gid [[thread_position_in_grid]]
) { if (gid >= len) return; c[gid] = max(a[gid], b[gid]); }

kernel void elem_min(
    device const float* a [[buffer(0)]],
    device const float* b [[buffer(1)]],
    device float* c       [[buffer(2)]],
    constant uint& len    [[buffer(3)]],
    uint gid [[thread_position_in_grid]]
) { if (gid >= len) return; c[gid] = min(a[gid], b[gid]); }

kernel void elem_pow(
    device const float* a [[buffer(0)]],
    device const float* b [[buffer(1)]],
    device float* c       [[buffer(2)]],
    constant uint& len    [[buffer(3)]],
    uint gid [[thread_position_in_grid]]
) { if (gid >= len) return; c[gid] = rlx_pow_scalar(a[gid], b[gid]); }

// Element-wise compare: writes 1.0 / 0.0 per element. `op_kind` selects:
//   0=Eq 1=Ne 2=Lt 3=Le 4=Gt 5=Ge
// One kernel for all six variants keeps the binary-shaped dispatch path
// uniform — the encoder picks op_kind at submit time.
// Compare-op math generated once from the shared rlxsl manifest.
// @@RLX_COMPARE_FN@@
kernel void elem_compare(
    device const float* a    [[buffer(0)]],
    device const float* b    [[buffer(1)]],
    device float* c          [[buffer(2)]],
    constant uint& len       [[buffer(3)]],
    constant uint& op_kind   [[buffer(4)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= len) return;
    c[gid] = rlx_compare_apply(op_kind, a[gid], b[gid]);
}

/// Compare with optional scalar broadcast (`flags`: bit0=lhs scalar, bit1=rhs).
kernel void elem_compare_bcast(
    device const float* a    [[buffer(0)]],
    device const float* b    [[buffer(1)]],
    device float* c          [[buffer(2)]],
    constant uint& len       [[buffer(3)]],
    constant uint& op_kind   [[buffer(4)]],
    constant uint& flags     [[buffer(5)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= len) return;
    float x = (flags & 1u) ? a[0] : a[gid];
    float y = (flags & 2u) ? b[0] : b[gid];
    c[gid] = rlx_compare_apply(op_kind, x, y);
}

// 2D convolution (naive direct, NCHW input). One thread per output
// element. Supports groups, dilation. Bias is a separate Op (matches the
// IR's two-input Conv shape). Two u32-arrays of dims pack into one
// constant buffer; an `aux` buffer carries the param triplets.
kernel void conv2d(
    device const float* src    [[buffer(0)]],
    device const float* wt     [[buffer(1)]],
    device float* dst          [[buffer(2)]],
    constant uint4& nch        [[buffer(3)]],   // [N, C_in, H, W]
    constant uint4& out_dims   [[buffer(4)]],   // [C_out, H_out, W_out, groups]
    constant uint4& kshape     [[buffer(5)]],   // [kh, kw, sh, sw]
    constant uint4& padd       [[buffer(6)]],   // [ph, pw, dh, dw]
    uint3 gid [[thread_position_in_grid]]
) {
    uint nco = gid.z;            // n * c_out + co
    uint ho = gid.y;
    uint wo = gid.x;
    uint c_out = out_dims.x;
    uint h_out = out_dims.y;
    uint w_out = out_dims.z;
    uint groups = out_dims.w;
    if (ho >= h_out || wo >= w_out || nco >= nch.x * c_out) return;
    uint n = nco / c_out;
    uint co = nco % c_out;
    uint c_in = nch.y;
    uint h = nch.z;
    uint w = nch.w;
    uint c_in_per_g = c_in / groups;
    uint c_out_per_g = c_out / groups;
    uint g = co / c_out_per_g;
    uint ci_start = g * c_in_per_g;
    uint kh = kshape.x; uint kw = kshape.y;
    uint sh = kshape.z; uint sw = kshape.w;
    uint ph = padd.x; uint pw = padd.y;
    uint dh = padd.z; uint dw = padd.w;

    float acc = 0.0f;
    for (uint ci_off = 0; ci_off < c_in_per_g; ++ci_off) {
        uint ci = ci_start + ci_off;
        uint in_chan = ((n * c_in) + ci) * h * w;
        uint wt_chan = ((co * c_in_per_g) + ci_off) * kh * kw;
        for (uint ki = 0; ki < kh; ++ki) {
            for (uint kj = 0; kj < kw; ++kj) {
                int hi = (int)(ho * sh + ki * dh) - (int)ph;
                int wi = (int)(wo * sw + kj * dw) - (int)pw;
                if (hi < 0 || wi < 0 || hi >= (int)h || wi >= (int)w) continue;
                acc += src[in_chan + (uint)hi * w + (uint)wi]
                     * wt[wt_chan + ki * kw + kj];
            }
        }
    }
    dst[((n * c_out) + co) * h_out * w_out + ho * w_out + wo] = acc;
}

// Depthwise causal 1-D conv on BSC layout `[B, W, C]` → `[B, out_seq, C]`.
// Weight is NCHW-packed `[C, 1, 1, K]` (same as `Op::Conv` depthwise). Optional
// fused SiLU. Replaces Transpose→Copy→Conv2D→Copy→Transpose(+Silu) for GDN.
kernel void depthwise_conv1d_bsc(
    device float* arena            [[buffer(0)]],
    constant ulong& src_off        [[buffer(1)]],
    constant ulong& wt_off         [[buffer(2)]],
    constant ulong& dst_off        [[buffer(3)]],
    constant uint4& dims           [[buffer(4)]], // batch, width, out_seq, channels
    constant uint2& k_silu         [[buffer(5)]], // k, silu!=0
    device const float* wt_buf     [[buffer(7)]],
    uint gid                       [[thread_position_in_grid]]
) {
    const uint batch = dims.x;
    const uint width = dims.y;
    const uint out_seq = dims.z;
    const uint channels = dims.w;
    const uint k = k_silu.x;
    const bool do_silu = k_silu.y != 0u;
    const uint total = batch * out_seq * channels;
    if (gid >= total || k == 0u) return;

    const uint c = gid % channels;
    const uint tmp = gid / channels;
    const uint t = tmp % out_seq;
    const uint b = tmp / out_seq;

    device const float* src = (device const float*)((device char*)arena + src_off);
    device float* dst = (device float*)((device char*)arena + dst_off);
    device const float* wt = (device const float*)((device const char*)wt_buf + wt_off);

    const uint src_base = b * width * channels + t * channels + c;
    const uint wt_base = c * k;
    float acc = 0.f;
    if (k == 4u) {
        acc += src[src_base + 0u * channels] * wt[wt_base + 0u];
        acc += src[src_base + 1u * channels] * wt[wt_base + 1u];
        acc += src[src_base + 2u * channels] * wt[wt_base + 2u];
        acc += src[src_base + 3u * channels] * wt[wt_base + 3u];
    } else {
        for (uint ki = 0u; ki < k; ++ki) {
            acc += src[src_base + ki * channels] * wt[wt_base + ki];
        }
    }
    if (do_silu) {
        acc = acc / (1.0f + exp(-acc));
    }
    dst[b * out_seq * channels + t * channels + c] = acc;
}

// 1-D conv (W_in = W_out = 1) — Voxtral codec layout `[N,C,T,1]`.
kernel void conv2d_w1(
    device const float* src [[buffer(0)]],
    device const float* wt [[buffer(1)]],
    device float* dst [[buffer(2)]],
    constant uint4& nch [[buffer(3)]],       // [N, C_in, H, 1]
    constant uint4& out_dims [[buffer(4)]],  // [C_out, H_out, 1, groups]
    constant uint4& kshape [[buffer(5)]],
    constant uint4& padd [[buffer(6)]],
    uint3 gid [[thread_position_in_grid]]
) {
    uint nco = gid.z;
    uint ho = gid.y;
    uint h_out = out_dims.y;
    uint c_out = out_dims.x;
    uint groups = out_dims.w;
    if (ho >= h_out || nco >= nch.x * c_out) return;
    uint n = nco / c_out;
    uint co = nco % c_out;
    uint c_in = nch.y;
    uint h = nch.z;
    uint c_in_per_g = c_in / groups;
    uint c_out_per_g = c_out / groups;
    uint g = co / c_out_per_g;
    uint ci_start = g * c_in_per_g;
    uint kh = kshape.x;
    uint kw = kshape.y;
    uint sh = kshape.z;
    uint ph = padd.x;
    uint pw = padd.y;
    uint dh = padd.z;
    uint dw = padd.w;

    float acc = 0.0f;
    for (uint ci_off = 0; ci_off < c_in_per_g; ++ci_off) {
        uint ci = ci_start + ci_off;
        uint in_chan = ((n * c_in) + ci) * h;
        uint wt_chan = ((co * c_in_per_g) + ci_off) * kh * kw;
        for (uint ki = 0; ki < kh; ++ki) {
            for (uint kj = 0; kj < kw; ++kj) {
                int hi = (int)(ho * sh + ki * dh) - (int)ph;
                int wi = (int)(kj * dw) - (int)pw;
                if (hi < 0 || wi < 0 || hi >= (int)h || wi >= 1) continue;
                acc += src[in_chan + (uint)hi] * wt[wt_chan + ki * kw + kj];
            }
        }
    }
    dst[((n * c_out) + co) * h_out + ho] = acc;
}

// LayerNorm2d (candle / SAM semantics): normalize across channels at each
// spatial position. One thread per (batch, ho, wo). gamma/beta are [C].
kernel void layer_norm2d(
    device const float* src    [[buffer(0)]],
    device const float* gamma [[buffer(1)]],
    device const float* beta  [[buffer(2)]],
    device float* dst          [[buffer(3)]],
    constant uint4& nchw      [[buffer(4)]],   // [N, C, H, W]
    constant float& eps       [[buffer(5)]],
    uint3 gid [[thread_position_in_grid]]
) {
    uint n = gid.z;
    uint ho = gid.y;
    uint wo = gid.x;
    uint batch = nchw.x;
    uint c = nchw.y;
    uint h = nchw.z;
    uint w = nchw.w;
    if (n >= batch || ho >= h || wo >= w) return;

    float mean = 0.0f;
    for (uint ch = 0; ch < c; ++ch) {
        mean += src[((n * c + ch) * h + ho) * w + wo];
    }
    mean /= (float)c;
    float var = 0.0f;
    for (uint ch = 0; ch < c; ++ch) {
        float d = src[((n * c + ch) * h + ho) * w + wo] - mean;
        var += d * d;
    }
    var /= (float)c;
    float inv = rsqrt(var + eps);
    for (uint ch = 0; ch < c; ++ch) {
        uint idx = ((n * c + ch) * h + ho) * w + wo;
        float v = (src[idx] - mean) * inv;
        dst[idx] = v * gamma[ch] + beta[ch];
    }
}

// Transposed 2D convolution (NCHW, PyTorch ConvTranspose2d, no bias).
// Weight layout [C_in, C_out/groups, kH, kW]. One thread per output
// element; accumulates in-register (no output zero pass).
kernel void conv_transpose2d(
    device const float* src    [[buffer(0)]],
    device const float* wt     [[buffer(1)]],
    device float* dst          [[buffer(2)]],
    constant uint4& nch        [[buffer(3)]],   // [N, C_in, H, W]
    constant uint4& out_dims   [[buffer(4)]],   // [C_out, H_out, W_out, groups]
    constant uint4& kshape     [[buffer(5)]],   // [kh, kw, sh, sw]
    constant uint4& padd       [[buffer(6)]],   // [ph, pw, dh, dw]
    uint3 gid [[thread_position_in_grid]]
) {
    uint nco = gid.z;
    uint ho = gid.y;
    uint wo = gid.x;
    uint c_out = out_dims.x;
    uint h_out = out_dims.y;
    uint w_out = out_dims.z;
    uint groups = out_dims.w;
    if (ho >= h_out || wo >= w_out || nco >= nch.x * c_out) return;
    uint n = nco / c_out;
    uint co = nco % c_out;
    uint c_in = nch.y;
    uint h = nch.z;
    uint w = nch.w;
    uint c_in_per_g = c_in / groups;
    uint c_out_per_g = c_out / groups;
    uint g = co / c_out_per_g;
    uint oc_off = co % c_out_per_g;
    uint kh = kshape.x; uint kw = kshape.y;
    uint sh = kshape.z; uint sw = kshape.w;
    uint ph = padd.x; uint pw = padd.y;
    uint dh = padd.z; uint dw = padd.w;

    float acc = 0.0f;
    for (uint ci_off = 0; ci_off < c_in_per_g; ++ci_off) {
        uint ci = g * c_in_per_g + ci_off;
        for (uint ky = 0; ky < kh; ++ky) {
            int t_h = (int)ho + (int)ph - (int)ky * (int)dh;
            if (t_h < 0 || t_h % (int)sh != 0) continue;
            int iy = t_h / (int)sh;
            if (iy < 0 || iy >= (int)h) continue;
            for (uint kx = 0; kx < kw; ++kx) {
                int t_w = (int)wo + (int)pw - (int)kx * (int)dw;
                if (t_w < 0 || t_w % (int)sw != 0) continue;
                int ix = t_w / (int)sw;
                if (ix < 0 || ix >= (int)w) continue;
                uint w_idx = ((ci * c_out_per_g + oc_off) * kh + ky) * kw + kx;
                float v = src[((n * c_in + ci) * h + (uint)iy) * w + (uint)ix];
                acc += v * wt[w_idx];
            }
        }
    }
    dst[((n * c_out) + co) * h_out * w_out + ho * w_out + wo] = acc;
}

// 3D NCDHW convolution. Weight: [C_out, C_in/groups, kD, kH, kW].
// One thread per output element; grid (w_out, h_out, n*c_out*d_out).
kernel void conv3d(
    device const float* src    [[buffer(0)]],
    device const float* wt     [[buffer(1)]],
    device float* dst          [[buffer(2)]],
    constant uint4& a          [[buffer(3)]],  // [N, C_in, D, H]
    constant uint4& b          [[buffer(4)]],  // [W, C_out, D_out, H_out]
    constant uint4& c          [[buffer(5)]],  // [W_out, kd, kh, kw]
    constant uint4& d          [[buffer(6)]],  // [sd, sh, sw, groups]
    constant uint4& e          [[buffer(7)]],  // [pd, ph, pw, dd]
    constant uint4& f          [[buffer(8)]],  // [dh, dw, 0, 0]
    uint3 gid [[thread_position_in_grid]]
) {
    uint n = a.x; uint c_in = a.y; uint din = a.z; uint hin = a.w;
    uint win = b.x; uint c_out = b.y; uint d_out = b.z; uint h_out = b.w;
    uint w_out = c.x; uint kd = c.y; uint kh = c.z; uint kw = c.w;
    uint sd = d.x; uint sh = d.y; uint sw = d.z; uint groups = d.w;
    uint pd = e.x; uint ph = e.y; uint pw = e.z; uint dd = e.w;
    uint dh = f.x; uint dw = f.y;

    uint wo = gid.x;
    uint ho = gid.y;
    uint ndco = gid.z;
    if (wo >= w_out || ho >= h_out || ndco >= n * c_out * d_out) return;
    uint d_o = ndco % d_out;
    uint nco = ndco / d_out;
    uint nn = nco / c_out;
    uint co = nco % c_out;

    uint c_in_per_g = c_in / groups;
    uint c_out_per_g = c_out / groups;
    uint g = co / c_out_per_g;
    uint ci_start = g * c_in_per_g;

    float acc = 0.0f;
    for (uint ci_off = 0; ci_off < c_in_per_g; ++ci_off) {
        uint ci = ci_start + ci_off;
        for (uint kz = 0; kz < kd; ++kz) {
            for (uint ky = 0; ky < kh; ++ky) {
                for (uint kx = 0; kx < kw; ++kx) {
                    int in_d = (int)(d_o * sd + kz * dd) - (int)pd;
                    int in_h = (int)(ho * sh + ky * dh) - (int)ph;
                    int in_w = (int)(wo * sw + kx * dw) - (int)pw;
                    if (in_d < 0 || in_h < 0 || in_w < 0
                        || in_d >= (int)din || in_h >= (int)hin || in_w >= (int)win) {
                        continue;
                    }
                    uint in_idx = (((nn * c_in + ci) * din + (uint)in_d) * hin + (uint)in_h) * win
                                  + (uint)in_w;
                    uint w_idx = (((co * c_in_per_g + ci_off) * kd + kz) * kh + ky) * kw + kx;
                    acc += src[in_idx] * wt[w_idx];
                }
            }
        }
    }
    dst[(((nn * c_out + co) * d_out + d_o) * h_out + ho) * w_out + wo] = acc;
}

// 3D NCDHW transposed conv (output-centric gather).
// Weight: [C_in, C_out/groups, kD, kH, kW] (PyTorch ConvTranspose3d).
kernel void conv_transpose3d(
    device const float* src    [[buffer(0)]],
    device const float* wt     [[buffer(1)]],
    device float* dst          [[buffer(2)]],
    constant uint4& a          [[buffer(3)]],  // [N, C_in, D, H]
    constant uint4& b          [[buffer(4)]],  // [W, C_out, D_out, H_out]
    constant uint4& c          [[buffer(5)]],  // [W_out, kd, kh, kw]
    constant uint4& d          [[buffer(6)]],  // [sd, sh, sw, groups]
    constant uint4& e          [[buffer(7)]],  // [pd, ph, pw, dd]
    constant uint4& f          [[buffer(8)]],  // [dh, dw, 0, 0]
    uint3 gid [[thread_position_in_grid]]
) {
    uint n = a.x; uint c_in = a.y; uint din = a.z; uint hin = a.w;
    uint win = b.x; uint c_out = b.y; uint d_out = b.z; uint h_out = b.w;
    uint w_out = c.x; uint kd = c.y; uint kh = c.z; uint kw = c.w;
    uint sd = d.x; uint sh = d.y; uint sw = d.z; uint groups = d.w;
    uint pd = e.x; uint ph = e.y; uint pw = e.z; uint dd = e.w;
    uint dh = f.x; uint dw = f.y;

    uint wo = gid.x;
    uint ho = gid.y;
    uint ndco = gid.z;
    if (wo >= w_out || ho >= h_out || ndco >= n * c_out * d_out) return;
    uint d_o = ndco % d_out;
    uint nco = ndco / d_out;
    uint nn = nco / c_out;
    uint co = nco % c_out;

    uint c_in_per_g = c_in / groups;
    uint c_out_per_g = c_out / groups;
    uint g = co / c_out_per_g;
    uint oc_off = co % c_out_per_g;
    uint ci_start = g * c_in_per_g;

    float acc = 0.0f;
    for (uint kz = 0; kz < kd; ++kz) {
        int num_d = (int)d_o + (int)pd - (int)(kz * dd);
        if (num_d < 0 || (num_d % (int)sd) != 0) continue;
        uint id = (uint)(num_d / (int)sd);
        if (id >= din) continue;
        for (uint ky = 0; ky < kh; ++ky) {
            int num_h = (int)ho + (int)ph - (int)(ky * dh);
            if (num_h < 0 || (num_h % (int)sh) != 0) continue;
            uint ih = (uint)(num_h / (int)sh);
            if (ih >= hin) continue;
            for (uint kx = 0; kx < kw; ++kx) {
                int num_w = (int)wo + (int)pw - (int)(kx * dw);
                if (num_w < 0 || (num_w % (int)sw) != 0) continue;
                uint iw = (uint)(num_w / (int)sw);
                if (iw >= win) continue;
                for (uint ci_off = 0; ci_off < c_in_per_g; ++ci_off) {
                    uint ci = ci_start + ci_off;
                    uint in_idx = (((nn * c_in + ci) * din + id) * hin + ih) * win + iw;
                    uint w_idx = (((ci * c_out_per_g + oc_off) * kd + kz) * kh + ky) * kw + kx;
                    acc += src[in_idx] * wt[w_idx];
                }
            }
        }
    }
    dst[(((nn * c_out + co) * d_out + d_o) * h_out + ho) * w_out + wo] = acc;
}

// NCHW group norm: normalize each (C/G)×H×W block. One threadgroup per
// (batch, group); 256-wide reduction then normalize.
kernel void group_norm(
    device const float* src    [[buffer(0)]],
    device const float* gamma [[buffer(1)]],
    device const float* beta  [[buffer(2)]],
    device float* dst          [[buffer(3)]],
    constant uint4& nchw      [[buffer(4)]],   // [N, C, H, W]
    constant uint& num_groups [[buffer(5)]],
    constant float& eps       [[buffer(6)]],
    uint ng [[threadgroup_position_in_grid]],
    uint tid [[thread_position_in_threadgroup]],
    uint tsize [[threads_per_threadgroup]]
) {
    uint batch = nchw.x;
    uint c = nchw.y;
    uint h = nchw.z;
    uint w = nchw.w;
    if (ng >= batch * num_groups) return;
    uint n = ng / num_groups;
    uint g = ng % num_groups;
    uint cpg = c / num_groups;
    uint c0 = g * cpg;
    uint plane = h * w;
    uint count = cpg * plane;

    float local_sum = 0.0f;
    float local_sumsq = 0.0f;
    for (uint i = tid; i < count; i += tsize) {
        uint c_off = i / plane;
        uint s = i % plane;
        uint ch = c0 + c_off;
        float v = src[((n * c + ch) * plane) + s];
        local_sum += v;
        local_sumsq += v * v;
    }
    threadgroup float partial_sum[256];
    threadgroup float partial_sumsq[256];
    partial_sum[tid] = local_sum;
    partial_sumsq[tid] = local_sumsq;
    threadgroup_barrier(mem_flags::mem_threadgroup);
    for (uint stride = tsize / 2; stride > 0; stride /= 2) {
        if (tid < stride) {
            partial_sum[tid] += partial_sum[tid + stride];
            partial_sumsq[tid] += partial_sumsq[tid + stride];
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }
    float mean = partial_sum[0] / float(count);
    float var = fmax(0.0f, partial_sumsq[0] / float(count) - mean * mean);
    float inv = rsqrt(var + eps);

    for (uint i = tid; i < count; i += tsize) {
        uint c_off = i / plane;
        uint s = i % plane;
        uint ch = c0 + c_off;
        uint idx = ((n * c + ch) * plane) + s;
        float v = (src[idx] - mean) * inv;
        dst[idx] = v * gamma[ch] + beta[ch];
    }
}

// Nearest-neighbor 2× upsample on planar NCHW. One thread per output pixel.
kernel void resize_nearest_2x(
    device const float* src [[buffer(0)]],
    device float* dst       [[buffer(1)]],
    constant uint4& nchw    [[buffer(2)]],   // [N, C, H, W] input
    uint3 gid [[thread_position_in_grid]]
) {
    uint wo = gid.x;
    uint ho = gid.y;
    uint nc = gid.z;
    uint n = nchw.x;
    uint c = nchw.y;
    uint h = nchw.z;
    uint w = nchw.w;
    uint h2 = h * 2u;
    uint w2 = w * 2u;
    if (nc >= n * c || ho >= h2 || wo >= w2) return;
    uint ni = nc / c;
    uint ci = nc % c;
    uint hi = ho / 2u;
    uint wi = wo / 2u;
    float v = src[((ni * c + ci) * h + hi) * w + wi];
    dst[((ni * c + ci) * h2 + ho) * w2 + wo] = v;
}

// 2D pooling. One thread per output element (n, c, ho, wo). Padding is
// implicit-zero; Mean divides by the full kernel area to match torch's
// `count_include_pad=True`. `kind`: 0=Mean (catch-all), 2=Max.
kernel void pool2d(
    device const float* src   [[buffer(0)]],
    device float* dst         [[buffer(1)]],
    constant uint4& nchw      [[buffer(2)]],   // [N, C, H, W]
    constant uint2& hw_out    [[buffer(3)]],   // [H_out, W_out]
    constant uint4& khsw      [[buffer(4)]],   // [kh, kw, sh, sw]
    constant uint2& pad       [[buffer(5)]],   // [ph, pw]
    constant uint& kind       [[buffer(6)]],
    uint3 gid [[thread_position_in_grid]]
) {
    uint nc = gid.z;
    uint ho = gid.y;
    uint wo = gid.x;
    uint n_total = nchw.x;
    uint c_total = nchw.y;
    if (nc >= n_total * c_total || ho >= hw_out.x || wo >= hw_out.y) return;
    uint n = nc / c_total;
    uint c = nc % c_total;
    uint h = nchw.z;
    uint w = nchw.w;
    uint h_out = hw_out.x;
    uint w_out = hw_out.y;
    uint kh = khsw.x; uint kw = khsw.y;
    uint sh = khsw.z; uint sw = khsw.w;
    uint ph = pad.x; uint pw = pad.y;

    float acc = (kind == 2) ? -INFINITY : 0.0f;
    uint in_chan = ((n * c_total) + c) * h * w;
    for (uint ki = 0; ki < kh; ++ki) {
        for (uint kj = 0; kj < kw; ++kj) {
            int hi = (int)(ho * sh + ki) - (int)ph;
            int wi = (int)(wo * sw + kj) - (int)pw;
            if (hi < 0 || wi < 0 || hi >= (int)h || wi >= (int)w) continue;
            float v = src[in_chan + (uint)hi * w + (uint)wi];
            if (kind == 2) acc = max(acc, v); else acc += v;
        }
    }
    if (kind == 0 || kind == 1) acc /= (float)(kh * kw);  // Mean
    dst[((n * c_total) + c) * h_out * w_out + ho * w_out + wo] = acc;
}

// ──────────────────────────────────────────────────────────────────────
// Training backward kernels. All three are OUTPUT-PARALLEL: each thread owns
// exactly one output element and writes it once — no atomics, no pre-zeroing,
// no scratch buffers, and (critically) no GPU→CPU sync. They mirror the CPU
// reference (crates/rlx-cpu/src/{conv_bwd,training_bwd}.rs) bit-for-bit,
// including the max-pool strict-`>` first-in-scan arg-max tie-break.
// ──────────────────────────────────────────────────────────────────────

// Max-pool backward. One thread per INPUT element (n,c,ih,iw); it accumulates
// dy from every output window in which it is the arg-max. Handles overlapping
// windows and padding. Race-free: distinct threads write distinct dx.
kernel void maxpool2d_backward(
    device const float* x   [[buffer(0)]],
    device const float* dy  [[buffer(1)]],
    device float* dx        [[buffer(2)]],
    constant uint4& p0      [[buffer(3)]],  // [N, C, H, W]
    constant uint4& p1      [[buffer(4)]],  // [H_out, W_out, kh, kw]
    constant uint4& p2      [[buffer(5)]],  // [sh, sw, ph, pw]
    uint3 gid [[thread_position_in_grid]]
) {
    uint N = p0.x, C = p0.y, H = p0.z, W = p0.w;
    uint h_out = p1.x, w_out = p1.y, kh = p1.z, kw = p1.w;
    uint sh = p2.x, sw = p2.y, ph = p2.z, pw = p2.w;
    uint iw = gid.x, ih = gid.y, nc = gid.z;
    if (nc >= N * C || ih >= H || iw >= W) return;

    int p_h = (int)ih + (int)ph;
    int p_w = (int)iw + (int)pw;
    int oh_max = p_h / (int)sh;
    int ow_max = p_w / (int)sw;
    if (oh_max >= (int)h_out) oh_max = (int)h_out - 1;
    if (ow_max >= (int)w_out) ow_max = (int)w_out - 1;
    // floor((p - k)/s)+1, clamped: integer div is floor only for non-neg args.
    int oh_min = (p_h - (int)kh < 0) ? 0 : (p_h - (int)kh) / (int)sh + 1;
    int ow_min = (p_w - (int)kw < 0) ? 0 : (p_w - (int)kw) / (int)sw + 1;

    uint in_chan = nc * H * W;
    uint out_chan = nc * h_out * w_out;
    float acc = 0.0f;
    for (int oh = oh_min; oh <= oh_max; ++oh) {
        for (int ow = ow_min; ow <= ow_max; ++ow) {
            float best_v = -INFINITY;
            int best_h = -1, best_w = -1;
            for (uint ki = 0; ki < kh; ++ki) {
                int hh = oh * (int)sh + (int)ki - (int)ph;
                if (hh < 0 || hh >= (int)H) continue;
                for (uint kj = 0; kj < kw; ++kj) {
                    int ww = ow * (int)sw + (int)kj - (int)pw;
                    if (ww < 0 || ww >= (int)W) continue;
                    float v = x[in_chan + (uint)hh * W + (uint)ww];
                    if (v > best_v) { best_v = v; best_h = hh; best_w = ww; }
                }
            }
            if (best_h == (int)ih && best_w == (int)iw)
                acc += dy[out_chan + (uint)oh * w_out + (uint)ow];
        }
    }
    dx[in_chan + ih * W + iw] = acc;
}

// Conv2d backward-input (transposed-conv gather). One thread per dx element
// (n, ci, ih, iw); gathers from every (co,ki,kj) whose forward map lands here.
kernel void conv2d_backward_input(
    device const float* dy  [[buffer(0)]],
    device const float* wt  [[buffer(1)]],
    device float* dx        [[buffer(2)]],
    constant uint4& a       [[buffer(3)]],  // [N, C_in, H, W_in]
    constant uint4& b       [[buffer(4)]],  // [C_out, H_out, W_out, kh]
    constant uint4& cc      [[buffer(5)]],  // [kw, sh, sw, ph]
    constant uint4& d       [[buffer(6)]],  // [pw, dh, dw, groups]
    uint3 gid [[thread_position_in_grid]]
) {
    uint N=a.x, C_in=a.y, H=a.z, W_in=a.w;
    uint C_out=b.x, H_out=b.y, W_out=b.z, kh=b.w;
    uint kw=cc.x, sh=cc.y, sw=cc.z, ph=cc.w;
    uint pw=d.x, dh=d.y, dw=d.z, groups=d.w;

    uint iw = gid.x, ih = gid.y, nci = gid.z;
    if (nci >= N * C_in || ih >= H || iw >= W_in) return;
    uint n = nci / C_in;
    uint ci = nci % C_in;
    uint c_in_per_g = C_in / groups;
    uint c_out_per_g = C_out / groups;
    uint g = ci / c_in_per_g;
    uint ci_local = ci % c_in_per_g;

    float acc = 0.0f;
    for (uint ki = 0; ki < kh; ++ki) {
        int num_h = (int)ih + (int)ph - (int)(ki * dh);
        if (num_h < 0 || (num_h % (int)sh) != 0) continue;
        int ho = num_h / (int)sh;
        if (ho >= (int)H_out) continue;
        for (uint kj = 0; kj < kw; ++kj) {
            int num_w = (int)iw + (int)pw - (int)(kj * dw);
            if (num_w < 0 || (num_w % (int)sw) != 0) continue;
            int wo = num_w / (int)sw;
            if (wo >= (int)W_out) continue;
            for (uint col = 0; col < c_out_per_g; ++col) {
                uint co = g * c_out_per_g + col;
                uint w_idx = ((co * c_in_per_g + ci_local) * kh + ki) * kw + kj;
                uint dy_idx = ((n * C_out + co) * H_out + (uint)ho) * W_out + (uint)wo;
                acc += wt[w_idx] * dy[dy_idx];
            }
        }
    }
    dx[((n * C_in + ci) * H + ih) * W_in + iw] = acc;
}

// Conv2d backward-weight (direct, batch-reduced). One thread per dw element
// (co, ci_local, ki, kj); sums dy*x over (n, ho, wo).
kernel void conv2d_backward_weight(
    device const float* x   [[buffer(0)]],
    device const float* dy  [[buffer(1)]],
    device float* dw        [[buffer(2)]],
    constant uint4& a       [[buffer(3)]],  // [N, C_in, H, W]
    constant uint4& b       [[buffer(4)]],  // [C_out, H_out, W_out, kh]
    constant uint4& cc      [[buffer(5)]],  // [kw, sh, sw, ph]
    constant uint4& d       [[buffer(6)]],  // [pw, dh, dw_dil, groups]
    uint3 gid [[thread_position_in_grid]]
) {
    uint N=a.x, C_in=a.y, H=a.z, W=a.w;
    uint C_out=b.x, H_out=b.y, W_out=b.z, kh=b.w;
    uint kw=cc.x, sh=cc.y, sw=cc.z, ph=cc.w;
    uint pw=d.x, dh=d.y, dwd=d.z, groups=d.w;

    uint kj = gid.x, ki = gid.y, coci = gid.z;
    uint c_in_per_g = C_in / groups;
    uint c_out_per_g = C_out / groups;
    if (kj >= kw || ki >= kh || coci >= C_out * c_in_per_g) return;
    uint co = coci / c_in_per_g;
    uint ci_local = coci % c_in_per_g;
    uint g = co / c_out_per_g;
    uint ci = g * c_in_per_g + ci_local;

    float acc = 0.0f;
    for (uint n = 0; n < N; ++n) {
        for (uint ho = 0; ho < H_out; ++ho) {
            int hh = (int)(ho * sh + ki * dh) - (int)ph;
            if (hh < 0 || hh >= (int)H) continue;
            for (uint wo = 0; wo < W_out; ++wo) {
                int ww = (int)(wo * sw + kj * dwd) - (int)pw;
                if (ww < 0 || ww >= (int)W) continue;
                uint x_idx = ((n * C_in + ci) * H + (uint)hh) * W + (uint)ww;
                uint dy_idx = ((n * C_out + co) * H_out + ho) * W_out + wo;
                acc += x[x_idx] * dy[dy_idx];
            }
        }
    }
    dw[((co * c_in_per_g + ci_local) * kh + ki) * kw + kj] = acc;
}

// MaxPool3d backward (NCDHW). One thread per input element; accumulates dy
// from every window whose argmax (strict `>`) lands here.
kernel void maxpool3d_backward(
    device const float* x   [[buffer(0)]],
    device const float* dy  [[buffer(1)]],
    device float* dx        [[buffer(2)]],
    constant uint4& p0      [[buffer(3)]],  // [N, C, D, H]
    constant uint4& p1      [[buffer(4)]],  // [W, D_out, H_out, W_out]
    constant uint4& p2      [[buffer(5)]],  // [kd, kh, kw, sd]
    constant uint4& p3      [[buffer(6)]],  // [sh, sw, pd, ph]
    constant uint&  pw      [[buffer(7)]],
    uint gid [[thread_position_in_grid]]
) {
    uint N = p0.x, C = p0.y, D = p0.z, H = p0.w;
    uint W = p1.x, d_out = p1.y, h_out = p1.z, w_out = p1.w;
    uint kd = p2.x, kh = p2.y, kw = p2.z, sd = p2.w;
    uint sh = p3.x, sw = p3.y, pd = p3.z, ph = p3.w;
    uint total = N * C * D * H * W;
    if (gid >= total) return;

    uint iw = gid % W;
    uint q1 = gid / W;
    uint ih = q1 % H;
    uint q2 = q1 / H;
    uint id = q2 % D;
    uint q3 = q2 / D;
    uint cc = q3 % C;
    uint nn = q3 / C;
    uint base_nc = (nn * C + cc) * D * H * W;

    int do_lo = (int)id + (int)pd - (int)kd + 1;
    do_lo = do_lo <= 0 ? 0 : (do_lo + (int)sd - 1) / (int)sd;
    int do_hi = ((int)id + (int)pd) / (int)sd;
    int ho_lo = (int)ih + (int)ph - (int)kh + 1;
    ho_lo = ho_lo <= 0 ? 0 : (ho_lo + (int)sh - 1) / (int)sh;
    int ho_hi = ((int)ih + (int)ph) / (int)sh;
    int wo_lo = (int)iw + (int)pw - (int)kw + 1;
    wo_lo = wo_lo <= 0 ? 0 : (wo_lo + (int)sw - 1) / (int)sw;
    int wo_hi = ((int)iw + (int)pw) / (int)sw;

    float acc = 0.0f;
    for (int do_ = do_lo; do_ <= do_hi && do_ < (int)d_out; ++do_) {
        int dstart = do_ * (int)sd - (int)pd;
        for (int ho = ho_lo; ho <= ho_hi && ho < (int)h_out; ++ho) {
            int hstart = ho * (int)sh - (int)ph;
            for (int wo = wo_lo; wo <= wo_hi && wo < (int)w_out; ++wo) {
                int wstart = wo * (int)sw - (int)pw;
                float best = -INFINITY;
                int best_idx = -1;
                for (uint kz = 0; kz < kd; ++kz) {
                    int irz = dstart + (int)kz;
                    if (irz < 0 || irz >= (int)D) continue;
                    for (uint i = 0; i < kh; ++i) {
                        int ir = hstart + (int)i;
                        if (ir < 0 || ir >= (int)H) continue;
                        for (uint j = 0; j < kw; ++j) {
                            int ic = wstart + (int)j;
                            if (ic < 0 || ic >= (int)W) continue;
                            uint id3 = base_nc + ((uint)irz * H + (uint)ir) * W + (uint)ic;
                            float v = x[id3];
                            if (v > best) { best = v; best_idx = (int)id3; }
                        }
                    }
                }
                if (best_idx == (int)gid)
                    acc += dy[((((nn * C + cc) * d_out + (uint)do_) * h_out + (uint)ho) * w_out + (uint)wo)];
            }
        }
    }
    dx[gid] = acc;
}

// Conv3d backward-input (NCDHW gather). Weight [C_out, C_in/groups, kD, kH, kW].
kernel void conv3d_backward_input(
    device const float* dy  [[buffer(0)]],
    device const float* wt  [[buffer(1)]],
    device float* dx        [[buffer(2)]],
    constant uint4& a       [[buffer(3)]],  // [N, C_in, D, H]
    constant uint4& b       [[buffer(4)]],  // [W, C_out, D_out, H_out]
    constant uint4& cc      [[buffer(5)]],  // [W_out, kd, kh, kw]
    constant uint4& d       [[buffer(6)]],  // [sd, sh, sw, pd]
    constant uint4& e       [[buffer(7)]],  // [ph, pw, dd, dh]
    constant uint2& f       [[buffer(8)]],  // [dw, groups]
    uint gid [[thread_position_in_grid]]
) {
    uint N=a.x, C_in=a.y, D=a.z, H=a.w;
    uint W=b.x, C_out=b.y, D_out=b.z, H_out=b.w;
    uint W_out=cc.x, kd=cc.y, kh=cc.z, kw=cc.w;
    uint sd=d.x, sh=d.y, sw=d.z, pd=d.w;
    uint ph=e.x, pw=e.y, dd=e.z, dh=e.w;
    uint dw=f.x, groups=f.y;

    uint total = N * C_in * D * H * W;
    if (gid >= total) return;
    uint iw = gid % W;
    uint q1 = gid / W;
    uint ih = q1 % H;
    uint q2 = q1 / H;
    uint id = q2 % D;
    uint q3 = q2 / D;
    uint ci = q3 % C_in;
    uint nn = q3 / C_in;

    uint c_in_per_g = C_in / groups;
    uint c_out_per_g = C_out / groups;
    uint g = ci / c_in_per_g;
    uint ci_off = ci - g * c_in_per_g;
    uint co_start = g * c_out_per_g;

    float acc = 0.0f;
    for (uint kz = 0; kz < kd; ++kz) {
        int num_d = (int)id + (int)pd - (int)(kz * dd);
        if (num_d < 0 || (num_d % (int)sd) != 0) continue;
        int do_ = num_d / (int)sd;
        if (do_ >= (int)D_out) continue;
        for (uint ki = 0; ki < kh; ++ki) {
            int num_h = (int)ih + (int)ph - (int)(ki * dh);
            if (num_h < 0 || (num_h % (int)sh) != 0) continue;
            int ho = num_h / (int)sh;
            if (ho >= (int)H_out) continue;
            for (uint kj = 0; kj < kw; ++kj) {
                int num_w = (int)iw + (int)pw - (int)(kj * dw);
                if (num_w < 0 || (num_w % (int)sw) != 0) continue;
                int wo = num_w / (int)sw;
                if (wo >= (int)W_out) continue;
                for (uint co_off = 0; co_off < c_out_per_g; ++co_off) {
                    uint co = co_start + co_off;
                    float dyv = dy[((((nn * C_out + co) * D_out + (uint)do_) * H_out + (uint)ho) * W_out + (uint)wo)];
                    float wv = wt[((((co * c_in_per_g + ci_off) * kd + kz) * kh + ki) * kw + kj)];
                    acc += dyv * wv;
                }
            }
        }
    }
    dx[gid] = acc;
}

// Conv3d backward-weight (NCDHW). One thread per dw element.
kernel void conv3d_backward_weight(
    device const float* x   [[buffer(0)]],
    device const float* dy  [[buffer(1)]],
    device float* dw        [[buffer(2)]],
    constant uint4& a       [[buffer(3)]],  // [N, C_in, D, H]
    constant uint4& b       [[buffer(4)]],  // [W, C_out, D_out, H_out]
    constant uint4& cc      [[buffer(5)]],  // [W_out, kd, kh, kw]
    constant uint4& d       [[buffer(6)]],  // [sd, sh, sw, pd]
    constant uint4& e       [[buffer(7)]],  // [ph, pw, dd, dh]
    constant uint2& f       [[buffer(8)]],  // [dw, groups]
    uint gid [[thread_position_in_grid]]
) {
    uint N=a.x, C_in=a.y, D=a.z, H=a.w;
    uint W=b.x, C_out=b.y, D_out=b.z, H_out=b.w;
    uint W_out=cc.x, kd=cc.y, kh=cc.z, kw=cc.w;
    uint sd=d.x, sh=d.y, sw=d.z, pd=d.w;
    uint ph=e.x, pw=e.y, dd=e.z, dh=e.w;
    uint dw_dil=f.x, groups=f.y;

    uint c_in_per_g = C_in / groups;
    uint c_out_per_g = C_out / groups;
    uint total = C_out * c_in_per_g * kd * kh * kw;
    if (gid >= total) return;

    uint kj = gid % kw;
    uint q1 = gid / kw;
    uint ki = q1 % kh;
    uint q2 = q1 / kh;
    uint kz = q2 % kd;
    uint q3 = q2 / kd;
    uint ci_off = q3 % c_in_per_g;
    uint co = q3 / c_in_per_g;
    uint g = co / c_out_per_g;
    uint ci = g * c_in_per_g + ci_off;

    float acc = 0.0f;
    for (uint nn = 0; nn < N; ++nn) {
        for (uint do_ = 0; do_ < D_out; ++do_) {
            int id = (int)(do_ * sd + kz * dd) - (int)pd;
            if (id < 0 || id >= (int)D) continue;
            for (uint ho = 0; ho < H_out; ++ho) {
                int ih = (int)(ho * sh + ki * dh) - (int)ph;
                if (ih < 0 || ih >= (int)H) continue;
                for (uint wo = 0; wo < W_out; ++wo) {
                    int iw = (int)(wo * sw + kj * dw_dil) - (int)pw;
                    if (iw < 0 || iw >= (int)W) continue;
                    float dyv = dy[((((nn * C_out + co) * D_out + do_) * H_out + ho) * W_out + wo)];
                    float xv = x[((((nn * C_in + ci) * D + (uint)id) * H + (uint)ih) * W + (uint)iw)];
                    acc += dyv * xv;
                }
            }
        }
    }
    dw[gid] = acc;
}

// Conv2d backward-weight, pass 1 (batch-parallel). One thread per
// (n, co, ci_local, ki, kj) writes a per-sample partial sum into `part`,
// laid out [N, C_out, c_in_per_g, kh, kw]. Threads scale with N, so small
// kernels (conv1: 288 dw elems) no longer starve the GPU.
kernel void conv2d_backward_weight_partial(
    device const float* x   [[buffer(0)]],
    device const float* dy  [[buffer(1)]],
    device float* part      [[buffer(2)]],
    constant uint4& a       [[buffer(3)]],  // [N, C_in, H, W]
    constant uint4& b       [[buffer(4)]],  // [C_out, H_out, W_out, kh]
    constant uint4& cc      [[buffer(5)]],  // [kw, sh, sw, ph]
    constant uint4& d       [[buffer(6)]],  // [pw, dh, dw_dil, groups]
    uint3 gid [[thread_position_in_grid]]
) {
    uint N=a.x, C_in=a.y, H=a.z, W=a.w;
    uint C_out=b.x, H_out=b.y, W_out=b.z, kh=b.w;
    uint kw=cc.x, sh=cc.y, sw=cc.z, ph=cc.w;
    uint pw=d.x, dh=d.y, dwd=d.z, groups=d.w;
    uint c_in_per_g = C_in / groups;
    uint c_out_per_g = C_out / groups;
    uint wsz = c_in_per_g * kh * kw;   // per-(n,co) weight slab

    uint j = gid.x, co = gid.y, n = gid.z;
    if (j >= wsz || co >= C_out || n >= N) return;
    uint kj = j % kw;
    uint ki = (j / kw) % kh;
    uint ci_local = j / (kw * kh);
    uint g = co / c_out_per_g;
    uint ci = g * c_in_per_g + ci_local;

    float acc = 0.0f;
    for (uint ho = 0; ho < H_out; ++ho) {
        int hh = (int)(ho * sh + ki * dh) - (int)ph;
        if (hh < 0 || hh >= (int)H) continue;
        for (uint wo = 0; wo < W_out; ++wo) {
            int ww = (int)(wo * sw + kj * dwd) - (int)pw;
            if (ww < 0 || ww >= (int)W) continue;
            uint x_idx = ((n * C_in + ci) * H + (uint)hh) * W + (uint)ww;
            uint dy_idx = ((n * C_out + co) * H_out + ho) * W_out + wo;
            acc += x[x_idx] * dy[dy_idx];
        }
    }
    part[(n * C_out + co) * wsz + j] = acc;
}

// Conv2d backward-weight, pass 2: sum the per-sample partials over the batch.
// One thread per dw element. `wslab` = C_out * c_in_per_g * kh * kw.
kernel void conv2d_backward_weight_reduce(
    device const float* part [[buffer(0)]],
    device float* dw         [[buffer(1)]],
    constant uint2& dims     [[buffer(2)]],   // [N, wslab]
    uint gid [[thread_position_in_grid]]
) {
    uint N = dims.x, wslab = dims.y;
    if (gid >= wslab) return;
    float acc = 0.0f;
    for (uint n = 0; n < N; ++n) acc += part[n * wslab + gid];
    dw[gid] = acc;
}

// Gather along an arbitrary axis. One thread per output element. Output
// is laid out as [outer, num_idx, trailing]; source as [outer, axis_dim, trailing].
kernel void gather_axis(
    device const float* table [[buffer(0)]],
    device const float* idx   [[buffer(1)]],
    device float* dst         [[buffer(2)]],
    constant uint& outer      [[buffer(3)]],
    constant uint& axis_dim   [[buffer(4)]],
    constant uint& num_idx    [[buffer(5)]],
    constant uint& trailing   [[buffer(6)]],
    uint3 gid [[thread_position_in_grid]]
) {
    uint o = gid.z;
    uint k = gid.y;
    uint t = gid.x;
    if (o >= outer || k >= num_idx || t >= trailing) return;
    uint row = (uint)(idx[k]);
    dst[(o * num_idx + k) * trailing + t] =
        table[(o * axis_dim + row) * trailing + t];
}

// General N-D transpose. One thread per output element. The encoder packs
// out_dims and in_strides into a single u32 buffer of length 2*rank:
//   buffer = [out_dim_0, ..., out_dim_{r-1}, in_stride_0, ..., in_stride_{r-1}]
// Rank is bounded at 8 (sufficient for current models).
kernel void transpose_nd(
    device const float* src [[buffer(0)]],
    device float* dst       [[buffer(1)]],
    constant uint& rank     [[buffer(2)]],
    constant uint& total    [[buffer(3)]],
    constant uint* meta     [[buffer(4)]],   // [out_dims..., in_strides...]
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= total) return;
    uint src_idx = 0;
    uint remaining = gid;
    // Decompose flat output index into multi-dim coords (outer-to-inner)
    // using stride math, then accumulate the source index from in_strides.
    // Compute denominators on the fly to avoid a separate divisor table.
    uint stride_rem = total;
    for (uint d = 0; d < rank; ++d) {
        uint dim = meta[d];
        stride_rem /= dim;
        uint coord = remaining / stride_rem;
        remaining = remaining - coord * stride_rem;
        src_idx += coord * meta[rank + d];
    }
    dst[gid] = src[src_idx];
}

// F16 reindex — same index math as `transpose_nd`, half elements. Emitted for
// Expand/Transpose/repeat_kv on an F16-resident KV cache (`RLX_QWEN3_F16_KV`).
kernel void transpose_nd_h(
    device const half* src [[buffer(0)]],
    device half* dst       [[buffer(1)]],
    constant uint& rank    [[buffer(2)]],
    constant uint& total   [[buffer(3)]],
    constant uint* meta    [[buffer(4)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= total) return;
    uint src_idx = 0;
    uint remaining = gid;
    uint stride_rem = total;
    for (uint d = 0; d < rank; ++d) {
        uint dim = meta[d];
        stride_rem /= dim;
        uint coord = remaining / stride_rem;
        remaining = remaining - coord * stride_rem;
        src_idx += coord * meta[rank + d];
    }
    dst[gid] = src[src_idx];
}

// Rank-2 swap (row-major [rows, cols] → [cols, rows]). Cheaper than transpose_nd
// for attention/layout reshapes — 2D thread grid, coalesced reads.
kernel void transpose_2d_f32(
    device const float* src [[buffer(0)]],
    device float* dst       [[buffer(1)]],
    constant uint& rows     [[buffer(2)]],
    constant uint& cols     [[buffer(3)]],
    uint2 gid [[thread_position_in_grid]]
) {
    uint r = gid.x;
    uint c = gid.y;
    if (r >= rows || c >= cols) return;
    dst[c * rows + r] = src[r * cols + c];
}

// Tiled transpose for large matrices (32x32 tile staged through threadgroup memory).
// Threadgroup size: (32, 8, 1). Each thread loads 4 rows of the tile.
kernel void transpose_2d_tiled_f32(
    device const float* src [[buffer(0)]],
    device float* dst       [[buffer(1)]],
    constant uint& rows     [[buffer(2)]],
    constant uint& cols     [[buffer(3)]],
    ushort2 tid [[thread_position_in_threadgroup]],
    ushort2 tgp [[threadgroup_position_in_grid]]
) {
    // Coalesced both ways: tid.x (the 32-wide fast lane) indexes the CONTIGUOUS
    // dimension on BOTH the global load (src col) and the store (dst row); the
    // transpose happens in the padded [32][33] threadgroup tile (+1 avoids bank
    // conflicts). The prior version indexed the ROW with tid.x on both sides, so
    // both global accesses were strided and the tile delivered ZERO coalescing —
    // profiling measured this rewrite at 1.1–2.7× (2.7× on the
    // common 192² weight-grad transpose). Same grid (tgp.x=row-block, tgp.y=col-block).
    threadgroup float tile[32][33];
    uint row_base = (uint)tgp.x * 32u;
    uint col_base = (uint)tgp.y * 32u;
    uint col = col_base + (uint)tid.x;
    for (uint i = 0; i < 32; i += 8) {
        uint row = row_base + (uint)tid.y + i;
        if (row < rows && col < cols) {
            tile[tid.y + i][tid.x] = src[row * cols + col];
        }
    }
    threadgroup_barrier(mem_flags::mem_threadgroup);
    uint drow = row_base + (uint)tid.x;
    for (uint i = 0; i < 32; i += 8) {
        uint dcol = col_base + (uint)tid.y + i;
        if (drow < rows && dcol < cols) {
            dst[dcol * rows + drow] = tile[tid.x][tid.y + i];
        }
    }
}

// Batched swap of the last two dims: src [batch, rows, cols] -> dst [batch, cols, rows]
kernel void transpose_last2_batched_f32(
    device const float* src [[buffer(0)]],
    device float* dst       [[buffer(1)]],
    constant uint& batch    [[buffer(2)]],
    constant uint& rows     [[buffer(3)]],
    constant uint& cols     [[buffer(4)]],
    uint3 gid [[thread_position_in_grid]]
) {
    uint b = gid.z;
    uint r = gid.x;
    uint c = gid.y;
    if (b >= batch || r >= rows || c >= cols) return;
    uint src_base = b * rows * cols;
    uint dst_base = b * rows * cols;
    dst[dst_base + c * rows + r] = src[src_base + r * cols + c];
}

// Tiled batched last2 transpose. Dispatch threadgroups over (rows, cols, batch).
// Threadgroup size: (32, 8, 1).
kernel void transpose_last2_batched_tiled_f32(
    device const float* src [[buffer(0)]],
    device float* dst       [[buffer(1)]],
    constant uint& batch    [[buffer(2)]],
    constant uint& rows     [[buffer(3)]],
    constant uint& cols     [[buffer(4)]],
    uint3 tid [[thread_position_in_threadgroup]],
    uint3 tgp [[threadgroup_position_in_grid]]
) {
    threadgroup float tile[32][33];
    uint b = tgp.z;
    if (b >= batch) return;
    uint r0 = tgp.x * 32u + tid.x;
    uint c0 = tgp.y * 32u + tid.y;
    uint base = b * rows * cols;
    for (uint i = 0; i < 32; i += 8) {
        uint c = c0 + i;
        if (r0 < rows && c < cols) {
            tile[tid.x][tid.y + i] = src[base + r0 * cols + c];
        }
    }
    threadgroup_barrier(mem_flags::mem_threadgroup);
    uint rr0 = tgp.y * 32u + tid.x; // out row (col index)
    uint cc0 = tgp.x * 32u + tid.y; // out col (row index)
    for (uint i = 0; i < 32; i += 8) {
        uint rr = rr0;
        uint cc = cc0 + i;
        if (rr < cols && cc < rows) {
            dst[base + rr * rows + cc] = tile[tid.y + i][tid.x];
        }
    }
}

// `[B, A, C, D] → [B, C, A, D]` — swap axes 1 and 2 with trailing axis contiguous.
kernel void transpose_swap12_batched_trail_f32(
    device const float* src [[buffer(0)]],
    device float* dst       [[buffer(1)]],
    constant uint& batch    [[buffer(2)]],
    constant uint& rows     [[buffer(3)]],
    constant uint& cols     [[buffer(4)]],
    constant uint& trail    [[buffer(5)]],
    uint3 gid [[thread_position_in_grid]]
) {
    uint bd = gid.z;
    uint b = bd / trail;
    uint d = bd % trail;
    uint r = gid.x;
    uint c = gid.y;
    if (b >= batch || r >= rows || c >= cols) return;
    uint block = rows * cols * trail;
    uint src_idx = b * block + c * rows * trail + r * trail + d;
    uint dst_idx = b * block + r * cols * trail + c * trail + d;
    dst[dst_idx] = src[src_idx];
}

kernel void transpose_swap12_batched_trail_tiled_f32(
    device const float* src [[buffer(0)]],
    device float* dst       [[buffer(1)]],
    constant uint& batch    [[buffer(2)]],
    constant uint& rows     [[buffer(3)]],
    constant uint& cols     [[buffer(4)]],
    constant uint& trail    [[buffer(5)]],
    uint3 tid [[thread_position_in_threadgroup]],
    uint3 tgp [[threadgroup_position_in_grid]]
) {
    threadgroup float tile[32][33];
    uint bd = tgp.z;
    uint b = bd / trail;
    uint d = bd % trail;
    if (b >= batch) return;
    uint block = rows * cols * trail;
    uint plane = b * block + d;
    uint r0 = tgp.x * 32u + tid.x;
    uint c0 = tgp.y * 32u + tid.y;
    for (uint i = 0; i < 32; i += 8) {
        uint c = c0 + i;
        if (r0 < rows && c < cols) {
            tile[tid.x][tid.y + i] =
                src[plane + c * rows * trail + r0 * trail];
        }
    }
    threadgroup_barrier(mem_flags::mem_threadgroup);
    uint rr0 = tgp.y * 32u + tid.x;
    uint cc0 = tgp.x * 32u + tid.y;
    for (uint i = 0; i < 32; i += 8) {
        uint rr = rr0;          // output column index (in `cols` range)
        uint cc = cc0 + i;      // output row index    (in `rows` range)
        if (rr < cols && cc < rows) {
            // Output layout [batch, rows, cols, trail]: element (row=cc, col=rr)
            // lives at cc*cols*trail + rr*trail (+ plane carries batch & trail).
            dst[plane + cc * cols * trail + rr * trail] = tile[tid.y + i][tid.x];
        }
    }
}

// Two-phase scatter-add: phase 0 zeros the output buffer, phase 1
// accumulates updates atomically. Atomic add is required because
// multiple updates may target the same destination row from different
// threads. `op_phase`: 0 = zero, 1 = accumulate.
//
// Each phase is a single dispatch: phase 0 runs over `out_total` threads,
// phase 1 over `num_updates * trailing` threads. The encoder fires both
// in sequence within one command buffer.
kernel void scatter_add_zero(
    device atomic_float* dst [[buffer(0)]],
    constant uint& out_total [[buffer(1)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= out_total) return;
    atomic_store_explicit(&dst[gid], 0.0f, memory_order_relaxed);
}

kernel void scatter_add_accumulate(
    device const float* updates [[buffer(0)]],
    device const float* indices [[buffer(1)]],
    device atomic_float* dst    [[buffer(2)]],
    constant uint& trailing     [[buffer(3)]],
    constant uint& num_updates  [[buffer(4)]],
    constant uint& out_dim      [[buffer(5)]],
    uint2 gid [[thread_position_in_grid]]
) {
    uint i = gid.y;     // which update
    uint j = gid.x;     // which trailing element
    if (i >= num_updates || j >= trailing) return;
    uint row = (uint)indices[i];
    if (row >= out_dim) return;            // OOB safety
    float v = updates[i * trailing + j];
    // Native hardware-arbitrated float atomic-add. Replaces the old f32
    // CAS-retry loop (as_type<uint> + compare_exchange_weak); profiling
    // measured 10.5× under codebook-grad contention (~36 threads/entry, where
    // the CAS spun). `atomic_float` compiles on metal3.1+ (all Apple-silicon
    // Metal targets); same bits + same unordered-fadd (non-)determinism as the
    // CAS.
    atomic_fetch_add_explicit(&dst[row * trailing + j], v, memory_order_relaxed);
}

// Indexed batched matmul (MoE GEMM). One thread per output element
// (i, j). Token i looks up its expert via expert_idx, then dot-products
// the row of `input` against the column of `weight[expert_idx[i]]`.
// F32-weight GEMV with K-splitting — the plain-`sgemm` counterpart of
// `gemv_f16w_splitk` and `grouped_gemv_splitk`. `sgemm` launches one thread per
// output element, so at decode (M == 1) that is only N threads: not enough
// outstanding loads to use the bandwidth. KSPLIT threads cooperate per column,
// each striding `k` and summing a K/KSPLIT slice, then partials reduce through
// threadgroup memory. Scalar columns already give a full 128-byte coalesced line
// for f32 (32 threads x 4 B), so no vector loads are needed.
//
// Deterministic: partials are summed in fixed order (unlike the atomic-accumulate
// `Simd64SplitK` variant). Reassociating K moves results ~1 ulp from the
// sequential kernel.
kernel void gemv_f32_splitk(
    device const float* A [[buffer(0)]],
    device const float* B [[buffer(1)]],
    device float*       C [[buffer(2)]],
    constant uint& K      [[buffer(3)]],
    constant uint& N      [[buffer(4)]],
    uint2 tid  [[thread_position_in_threadgroup]],
    uint2 tgid [[threadgroup_position_in_grid]]
) {
    constexpr uint KSPLIT = 32u;
    threadgroup float partial[32][KSPLIT];
    uint col = tgid.x * 32u + tid.x;
    uint ks  = tid.y;
    float acc = 0.0f;
    if (col < N) {
        for (uint k = ks; k < K; k += KSPLIT) {
            acc += A[k] * B[k * N + col];
        }
    }
    partial[tid.x][ks] = acc;
    threadgroup_barrier(mem_flags::mem_threadgroup);
    if (ks == 0u && col < N) {
        float s = 0.0f;
        for (uint j = 0u; j < KSPLIT; ++j) s += partial[tid.x][j];
        C[col] = s;
    }
}

// MoE GEMV with K-splitting — the `grouped_matmul` counterpart of
// `gemv_f16w_splitk`, and for the same reason. `grouped_matmul` launches one
// thread per output element, so at DECODE (m == 1) that is only `n` threads
// (~1-1.5k for MoE expert projections): far too few outstanding loads to use the
// bandwidth. Here KSPLIT threads cooperate per column — each strides `k` by
// KSPLIT and sums a K/KSPLIT slice, then partials reduce through threadgroup
// memory. The threadgroup is (32 cols x KSPLIT) and a simdgroup is the 32 columns
// for one `ks`, so `weight[e*K*N + k*N + col .. col+31]` is a fully coalesced
// 128-byte line — 32x the threads at identical DRAM traffic.
//
// Split-K reassociates the K reduction, so results differ from the sequential
// kernel by ~1 ulp (partials are summed in a fixed order, so it stays
// deterministic and run-to-run identical).
kernel void grouped_gemv_splitk(
    device const float* input      [[buffer(0)]],
    device const float* weight     [[buffer(1)]],
    device const float* expert_idx [[buffer(2)]],
    device float* dst              [[buffer(3)]],
    constant uint& k_dim           [[buffer(4)]],
    constant uint& n               [[buffer(5)]],
    constant uint& num_experts     [[buffer(6)]],
    uint2 tid  [[thread_position_in_threadgroup]],
    uint2 tgid [[threadgroup_position_in_grid]]
) {
    constexpr uint KSPLIT = 32u;
    threadgroup float partial[32][KSPLIT];
    uint col = tgid.x * 32u + tid.x;
    uint row = tgid.y;
    uint ks  = tid.y;
    uint e = (uint)(expert_idx[row]);
    float acc = 0.0f;
    bool live = (col < n) && (e < num_experts);
    if (live) {
        uint w_base = e * k_dim * n;
        uint in_base = row * k_dim;
        for (uint kk = ks; kk < k_dim; kk += KSPLIT) {
            acc += input[in_base + kk] * weight[w_base + kk * n + col];
        }
    }
    partial[tid.x][ks] = acc;
    threadgroup_barrier(mem_flags::mem_threadgroup);
    if (ks == 0u && live) {
        float s = 0.0f;
        for (uint j = 0u; j < KSPLIT; ++j) s += partial[tid.x][j];
        dst[row * n + col] = s;
    }
}

kernel void grouped_matmul(
    device const float* input      [[buffer(0)]],
    device const float* weight     [[buffer(1)]],
    device const float* expert_idx [[buffer(2)]],
    device float* dst              [[buffer(3)]],
    constant uint& m               [[buffer(4)]],
    constant uint& k_dim           [[buffer(5)]],
    constant uint& n               [[buffer(6)]],
    constant uint& num_experts     [[buffer(7)]],
    uint2 gid [[thread_position_in_grid]]
) {
    uint i = gid.y;
    uint j = gid.x;
    if (i >= m || j >= n) return;
    uint e = (uint)(expert_idx[i]);
    if (e >= num_experts) return;          // OOB safety
    uint w_base = e * k_dim * n;
    uint in_base = i * k_dim;
    // Four independent accumulators. The single-chain version serialises on the
    // FMA dependency, so with only `n` threads at decode (m == 1) there are never
    // enough loads in flight to use the bandwidth — this kernel measured ~21 GB/s
    // where Metal's tuned sgemm gets ~60 GB/s on the same shapes. Unrolling breaks
    // the chain and quadruples outstanding loads; `weight` reads stay coalesced
    // across `j` exactly as before.
    float acc0 = 0.0f, acc1 = 0.0f, acc2 = 0.0f, acc3 = 0.0f;
    uint kk = 0;
    for (; kk + 4u <= k_dim; kk += 4u) {
        acc0 += input[in_base + kk + 0u] * weight[w_base + (kk + 0u) * n + j];
        acc1 += input[in_base + kk + 1u] * weight[w_base + (kk + 1u) * n + j];
        acc2 += input[in_base + kk + 2u] * weight[w_base + (kk + 2u) * n + j];
        acc3 += input[in_base + kk + 3u] * weight[w_base + (kk + 3u) * n + j];
    }
    float acc = (acc0 + acc1) + (acc2 + acc3);
    for (; kk < k_dim; ++kk) {
        acc += input[in_base + kk] * weight[w_base + kk * n + j];
    }
    dst[i * n + j] = acc;
}

// Top-K indices along the last axis. One thread per output row. Repeated
// argmax with masking — O(k * axis_dim) per row; fine for small k (MoE
// typical k=2–8). Each thread maintains its own scratch space in private
// memory, no threadgroup coordination needed.
//
// Important: rlx writes float32-encoded indices; downstream Gather reads
// them via `(uint)idx[k]`. Cast on store mirrors that.
kernel void topk_lastax(
    device const float* src [[buffer(0)]],
    device float* dst       [[buffer(1)]],
    constant uint& axis_dim [[buffer(2)]],
    constant uint& k        [[buffer(3)]],
    uint o [[thread_position_in_grid]]
) {
    // Hard cap on axis_dim — guards the on-chip scratch. MoE expert
    // counts top out around 256 in practice; raise this if a real
    // workload needs more.
    const uint MAX_AXIS = 1024;
    if (axis_dim > MAX_AXIS) return;

    float scratch[MAX_AXIS];
    uint base = o * axis_dim;
    for (uint i = 0; i < axis_dim; ++i) scratch[i] = src[base + i];

    uint out_base = o * k;
    for (uint ki = 0; ki < k; ++ki) {
        float best_v = scratch[0];
        uint  best_i = 0;
        for (uint i = 1; i < axis_dim; ++i) {
            float v = scratch[i];
            if (v > best_v) { best_v = v; best_i = i; }
        }
        dst[out_base + ki] = (float)best_i;
        scratch[best_i] = -INFINITY;
    }
}

// Reduce over a contiguous axis range. Input layout [outer, reduced, inner];
// output [outer, inner]. One thread per output element walks `reduced`
// values with stride `inner`. `op_kind`: 0=Sum 1=Mean 2=Max 3=Min 4=Prod.
//
// Trade-off: a serial reduction loop per thread is slower than threadgroup
// reduction when `reduced` is large, but it generalises trivially to any
// axis range and avoids the per-row threadgroup setup cost. For the shapes
// we care about (Reduce::Sum on 60×768 is 22 µs CPU vs 135 µs Metal — the
// wait latency dominates either way), kernel choice barely moves the
// needle. Revisit if a launch-bound reduction shows up.
kernel void reduce_axes(
    device const float* src [[buffer(0)]],
    device float* dst       [[buffer(1)]],
    constant uint& reduced  [[buffer(2)]],
    constant uint& inner    [[buffer(3)]],
    constant uint& op_kind  [[buffer(4)]],
    uint2 gid [[thread_position_in_grid]]
) {
    uint i = gid.x;            // inner axis index
    uint o = gid.y;            // outer axis index
    if (i >= inner) return;
    float acc;
    if      (op_kind == 2) acc = -INFINITY;
    else if (op_kind == 3) acc =  INFINITY;
    else if (op_kind == 4) acc =  1.0f;
    else                   acc =  0.0f;        // Sum / Mean

    uint base = o * reduced * inner + i;
    for (uint r = 0; r < reduced; ++r) {
        float v = src[base + r * inner];
        if      (op_kind == 0 || op_kind == 1) acc += v;
        else if (op_kind == 2) acc = max(acc, v);
        else if (op_kind == 3) acc = min(acc, v);
        else                   acc *= v;
    }
    if (op_kind == 1) acc /= float(reduced);
    dst[o * inner + i] = acc;
}

// SIMD-parallel Sum/Mean reduction: one 32-wide threadgroup (== 1 SIMD group)
// per output element; the 32 lanes split the `reduced` axis and combine with
// simd_sum. `reduce_axes` above dispatches ONE thread per output, each serially
// summing the whole reduced axis — for grad-sums (bias/beta: reduced = batch·seq
// ≈ 1024, few outputs) that is a serial reduction at low occupancy. Sum/Mean
// only (op_kind 0/1); max/min/prod stay on the scalar kernel.
kernel void reduce_axes_sum_simd(
    device const float* src [[buffer(0)]],
    device float* dst       [[buffer(1)]],
    constant uint& reduced  [[buffer(2)]],
    constant uint& inner    [[buffer(3)]],
    constant uint& op_kind  [[buffer(4)]],
    uint out_idx [[threadgroup_position_in_grid]],
    uint tid     [[thread_position_in_threadgroup]],
    uint tsize   [[threads_per_threadgroup]]
) {
    uint i = out_idx % inner;
    uint o = out_idx / inner;
    uint base = o * reduced * inner + i;
    float local = 0.0f;
    for (uint r = tid; r < reduced; r += tsize) {
        local += src[base + r * inner];
    }
    float total = simd_sum(local);
    if (tid == 0) {
        dst[out_idx] = (op_kind == 1u) ? total / float(reduced) : total;
    }
}

// Ternary select: cond != 0 ? a : b. cond is treated as bool via != 0.
kernel void elem_where(
    device const float* cond [[buffer(0)]],
    device const float* a    [[buffer(1)]],
    device const float* b    [[buffer(2)]],
    device float* out        [[buffer(3)]],
    constant uint& len       [[buffer(4)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= len) return;
    out[gid] = cond[gid] != 0.0f ? a[gid] : b[gid];
}

/// Where with optional scalar broadcast on any operand.
/// `flags`: bit0=cond scalar, bit1=a scalar, bit2=b scalar.
kernel void elem_where_bcast(
    device const float* cond [[buffer(0)]],
    device const float* a    [[buffer(1)]],
    device const float* b    [[buffer(2)]],
    device float* out        [[buffer(3)]],
    constant uint& len       [[buffer(4)]],
    constant uint& flags     [[buffer(5)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= len) return;
    float c = (flags & 1u) ? cond[0] : cond[gid];
    float x = (flags & 2u) ? a[0] : a[gid];
    float y = (flags & 4u) ? b[0] : b[gid];
    out[gid] = c != 0.0f ? x : y;
}

// Single-rounded fused multiply-add: out = fma(a, b, c). MSL `fma` is a true
// fused op (one rounding) — required for compensated / error-free-transform math.
kernel void elem_fma(
    device const float* a [[buffer(0)]],
    device const float* b [[buffer(1)]],
    device const float* c [[buffer(2)]],
    device float* out     [[buffer(3)]],
    constant uint& len    [[buffer(4)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= len) return;
    out[gid] = fma(a[gid], b[gid], c[gid]);
}

// Element-wise ReLU / activation backward. `op` matches CUDA
// `activation_op_id` / unary forward ids (0=relu … 16=atan).
// Formulas mirror rlx-cpu `activation_backward_kernel`.
kernel void relu_backward(
    device const float* x  [[buffer(0)]],
    device const float* dy [[buffer(1)]],
    device float* dx       [[buffer(2)]],
    constant uint& len     [[buffer(3)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= len) return;
    float xv = x[gid];
    float dyv = dy[gid];
    dx[gid] = (xv > 0.0f) ? dyv : 0.0f;
}

// @@RLX_ACTIVATION_BACKWARD@@
kernel void activation_backward(
    device const float* x  [[buffer(0)]],
    device const float* dy [[buffer(1)]],
    device float* dx       [[buffer(2)]],
    constant uint& len     [[buffer(3)]],
    constant uint& op      [[buffer(4)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= len) return;
    float xv = x[gid];
    float dyv = dy[gid];
    // Derivative dispatch generated from the rlxsl manifest (auto-differentiated
    // from the forward); definition substituted for the marker below at runtime.
    dx[gid] = rlx_activation_backward(op, xv, dyv);
}

// C64 Wirtinger surface on interleaved [re, im] f32 pairs (mirrors
// `complex_wirtinger.cu` / rlx-cpu `exec_complex_norm_sq{,_backward}_f32` /
// `exec_conjugate_c64`). Dispatched over the complex-element index `k ∈ [0, n)`.
// Buffer pointers are already advanced to each tensor's byte offset.
kernel void complex_norm_sq(
    device const float* in [[buffer(0)]],
    device float* out      [[buffer(1)]],
    constant uint& n       [[buffer(2)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= n) return;
    float re = in[2u * gid];
    float im = in[2u * gid + 1u];
    out[gid] = re * re + im * im;
}

kernel void complex_norm_sq_backward(
    device const float* z  [[buffer(0)]],
    device const float* g  [[buffer(1)]],
    device float* dz       [[buffer(2)]],
    constant uint& n       [[buffer(3)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= n) return;
    float re = z[2u * gid];
    float im = z[2u * gid + 1u];
    float gv = g[gid];
    dz[2u * gid]      = gv * re;
    dz[2u * gid + 1u] = gv * im;
}

kernel void conjugate_c64(
    device const float* in [[buffer(0)]],
    device float* out      [[buffer(1)]],
    constant uint& n       [[buffer(2)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= n) return;
    out[2u * gid]      =  in[2u * gid];
    out[2u * gid + 1u] = -in[2u * gid + 1u];
}

// Ternary-pruned radix-2 butterfly stage (interleaved C64 [batch, n_fft, 2]).
// Mirrors CUDA `fft_butterfly_stage.cu` / CPU `execute_fft_butterfly_stage_f32`.
// One thread per (batch, butterfly); gate=0 copies the pair, else twiddle + optional rev.
struct FftButterflyStageParams {
    uint batch;
    uint n_fft;
    uint stage;
    uint n_half;
};

kernel void fft_butterfly_stage(
    device const float* state [[buffer(0)]],
    device float* out         [[buffer(1)]],
    device const float* gate  [[buffer(2)]],
    device const float* rev   [[buffer(3)]],
    device const float* tw_re [[buffer(4)]],
    device const float* tw_im [[buffer(5)]],
    constant FftButterflyStageParams& p [[buffer(6)]],
    uint2 gid [[thread_position_in_grid]]
) {
    uint bf = gid.x;
    uint b = gid.y;
    if (b >= p.batch || bf >= p.n_half) return;

    uint stride = 1u << p.stage;
    uint row_elems = p.n_fft * 2u;
    device const float* inp = state + b * row_elems;
    device float* o = out + b * row_elems;

    uint group = bf / stride;
    uint k = bf % stride;
    uint i0 = group * 2u * stride + k;
    uint i1 = i0 + stride;

    if (gate[bf] == 0.0f) {
        o[i0 * 2u]      = inp[i0 * 2u];
        o[i0 * 2u + 1u] = inp[i0 * 2u + 1u];
        o[i1 * 2u]      = inp[i1 * 2u];
        o[i1 * 2u + 1u] = inp[i1 * 2u + 1u];
        return;
    }

    float w_re = tw_re[bf];
    float w_im = tw_im[bf];
    float in_a_re = inp[i0 * 2u];
    float in_a_im = inp[i0 * 2u + 1u];
    float in_b_re = inp[i1 * 2u];
    float in_b_im = inp[i1 * 2u + 1u];

    float b_re = in_b_re * w_re - in_b_im * w_im;
    float b_im = in_b_re * w_im + in_b_im * w_re;
    float top_re = in_a_re + b_re;
    float top_im = in_a_im + b_im;
    float bot_re = in_a_re - b_re;
    float bot_im = in_a_im - b_im;

    float oa_re, oa_im, ob_re, ob_im;
    if (rev[bf] >= 0.5f) {
        oa_re = bot_re; oa_im = bot_im;
        ob_re = top_re; ob_im = top_im;
    } else {
        oa_re = top_re; oa_im = top_im;
        ob_re = bot_re; ob_im = bot_im;
    }
    o[i0 * 2u]      = oa_re;
    o[i0 * 2u + 1u] = oa_im;
    o[i1 * 2u]      = ob_re;
    o[i1 * 2u + 1u] = ob_im;
}

// FakeQuantize forward: clamp(round(x / s), -q_max, q_max) * s.
// Matches `rlx_cpu::thunk::ops::quant::exec_fake_quantize` for Fixed and
// PerBatch (EMA stays on HostOp). Channel layout: c = (i / inner) % chan_dim.
// Rounding: Rust `f32::round` (half away from zero), not ties-to-even.
struct FakeQuantizeParams {
    uint n;
    uint chan_dim;
    uint inner;
    float q_max;
};

inline float apply_fq(float x, float s, float q_max) {
    float scaled = x / s;
    float rounded = sign(scaled) * floor(fabs(scaled) + 0.5f);
    float qv = clamp(rounded, -q_max, q_max);
    return qv * s;
}

inline uint fq_channel_of(uint i, uint chan_dim, uint inner) {
    if (chan_dim <= 1u) return 0u;
    return (i / inner) % chan_dim;
}

// One thread per element. Scale from `scale[c]` (Fixed).
kernel void fake_quantize_fixed(
    device const float* in [[buffer(0)]],
    device const float* scale [[buffer(1)]],
    device float* out [[buffer(2)]],
    constant FakeQuantizeParams& p [[buffer(3)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= p.n) return;
    uint c = fq_channel_of(gid, p.chan_dim, p.inner);
    float s = max(scale[c], 1e-12f);
    out[gid] = apply_fq(in[gid], s, p.q_max);
}

// One thread per channel. s = max(|x|) / q_max, then quantize that channel
// (PerBatch).
kernel void fake_quantize_perbatch(
    device const float* in [[buffer(0)]],
    device float* out [[buffer(1)]],
    constant FakeQuantizeParams& p [[buffer(2)]],
    uint gid [[thread_position_in_grid]]
) {
    uint c = gid;
    if (c >= p.chan_dim) return;

    float max_abs = 0.0f;
    uint stride = p.chan_dim * p.inner;
    uint outer = p.n / max(stride, 1u);
    for (uint o = 0u; o < outer; o++) {
        uint base = o * stride + c * p.inner;
        for (uint j = 0u; j < p.inner; j++) {
            max_abs = max(max_abs, fabs(in[base + j]));
        }
    }
    // axis=None: chan_dim=1, inner=n → outer=1, single scan of all elements.
    if (outer * stride != p.n) {
        for (uint i = 0u; i < p.n; i++) {
            if (fq_channel_of(i, p.chan_dim, p.inner) == c) {
                max_abs = max(max_abs, fabs(in[i]));
            }
        }
    }

    float s = max(max_abs / p.q_max, 1e-12f);

    for (uint o = 0u; o < outer; o++) {
        uint base = o * stride + c * p.inner;
        for (uint j = 0u; j < p.inner; j++) {
            uint idx = base + j;
            out[idx] = apply_fq(in[idx], s, p.q_max);
        }
    }
    if (outer * stride != p.n) {
        for (uint i = 0u; i < p.n; i++) {
            if (fq_channel_of(i, p.chan_dim, p.inner) == c) {
                out[i] = apply_fq(in[i], s, p.q_max);
            }
        }
    }
}

// In-place ReLU: data = max(0, data)
kernel void relu_inplace(
    device float* data [[buffer(0)]],
    constant uint& len [[buffer(1)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= len) return;
    data[gid] = max(0.0f, data[gid]);
}

// In-place sigmoid: 1 / (1 + exp(-x))
kernel void sigmoid_inplace(
    device float* data [[buffer(0)]],
    constant uint& len [[buffer(1)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= len) return;
    data[gid] = 1.0f / (1.0f + exp(-data[gid]));
}

// In-place tan
kernel void tan_inplace(
    device float* data [[buffer(0)]],
    constant uint& len [[buffer(1)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= len) return;
    data[gid] = tan(data[gid]);
}

// In-place atan
kernel void atan_inplace(
    device float* data [[buffer(0)]],
    constant uint& len [[buffer(1)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= len) return;
    data[gid] = atan(data[gid]);
}

// In-place sin
kernel void sin_inplace(
    device float* data [[buffer(0)]],
    constant uint& len [[buffer(1)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= len) return;
    data[gid] = sin(data[gid]);
}

// In-place cos
kernel void cos_inplace(
    device float* data [[buffer(0)]],
    constant uint& len [[buffer(1)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= len) return;
    data[gid] = cos(data[gid]);
}

// In-place tanh
kernel void tanh_inplace(
    device float* data [[buffer(0)]],
    constant uint& len [[buffer(1)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= len) return;
    // Clamp to the tanh saturation range: Metal's fast-math `tanh` returns NaN for
    // large |x| (it evaluates via exp, which overflows) — e.g. VITS posterior
    // WaveNet pre-activations reach ~300. tanh(±15)≈±1 to f32 precision, matching
    // CPU's stable tanh; clamp also folds ±inf to ±15.
    data[gid] = tanh(clamp(data[gid], -15.0f, 15.0f));
}

// In-place exp / log / sqrt / rsqrt / neg / abs — one kernel each so the
// dispatch path stays uniform with the existing `*_inplace` family.
kernel void exp_inplace(
    device float* data [[buffer(0)]],
    constant uint& len [[buffer(1)]],
    uint gid [[thread_position_in_grid]]
) { if (gid >= len) return; data[gid] = exp(data[gid]); }

kernel void log_inplace(
    device float* data [[buffer(0)]],
    constant uint& len [[buffer(1)]],
    uint gid [[thread_position_in_grid]]
) { if (gid >= len) return; data[gid] = log(data[gid]); }

kernel void sqrt_inplace(
    device float* data [[buffer(0)]],
    constant uint& len [[buffer(1)]],
    uint gid [[thread_position_in_grid]]
) { if (gid >= len) return; data[gid] = sqrt(data[gid]); }

kernel void rsqrt_inplace(
    device float* data [[buffer(0)]],
    constant uint& len [[buffer(1)]],
    uint gid [[thread_position_in_grid]]
) { if (gid >= len) return; data[gid] = rsqrt(data[gid]); }

kernel void rec_inplace(
    device float* data [[buffer(0)]],
    constant uint& len [[buffer(1)]],
    uint gid [[thread_position_in_grid]]
) { if (gid >= len) return; data[gid] = 1.0f / data[gid]; }

kernel void neg_inplace(
    device float* data [[buffer(0)]],
    constant uint& len [[buffer(1)]],
    uint gid [[thread_position_in_grid]]
) { if (gid >= len) return; data[gid] = -data[gid]; }

kernel void abs_inplace(
    device float* data [[buffer(0)]],
    constant uint& len [[buffer(1)]],
    uint gid [[thread_position_in_grid]]
) { if (gid >= len) return; data[gid] = abs(data[gid]); }

kernel void round_inplace(
    device float* data [[buffer(0)]],
    constant uint& len [[buffer(1)]],
    uint gid [[thread_position_in_grid]]
) { if (gid >= len) return; data[gid] = round(data[gid]); }

// Standalone softmax along the last axis. One threadgroup per row,
// reduces max + exp-sum across the row, then normalizes. tg_size is
// the actual number of threads per group (passed via threads_per_threadgroup).
kernel void softmax_lastax(
    device float* data    [[buffer(0)]],
    constant uint& cols   [[buffer(1)]],
    uint row [[threadgroup_position_in_grid]],
    uint tid [[thread_position_in_threadgroup]],
    uint tsize [[threads_per_threadgroup]]
) {
    threadgroup float partial[256];
    uint base = row * cols;

    // Pass 1: find row max for numerical stability.
    float local_max = -INFINITY;
    for (uint i = tid; i < cols; i += tsize) {
        local_max = max(local_max, data[base + i]);
    }
    partial[tid] = (float)local_max;
    threadgroup_barrier(mem_flags::mem_threadgroup);
    for (uint stride = tsize / 2; stride > 0; stride /= 2) {
        if (tid < stride) {
            partial[tid] = max(partial[tid], partial[tid + stride]);
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }
    float row_max = partial[0];

    // Pass 2: exp(x - max) and sum.
    float local_sum = 0.0f;
    for (uint i = tid; i < cols; i += tsize) {
        float e = exp(data[base + i] - row_max);
        data[base + i] = e;
        local_sum += e;
    }
    partial[tid] = (float)local_sum;
    threadgroup_barrier(mem_flags::mem_threadgroup);
    for (uint stride = tsize / 2; stride > 0; stride /= 2) {
        if (tid < stride) {
            partial[tid] += partial[tid + stride];
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }
    float inv_sum = 1.0f / partial[0];

    // Pass 3: normalize.
    for (uint i = tid; i < cols; i += tsize) {
        data[base + i] *= inv_sum;
    }
}

// Causal-bounded row softmax for the attention-backward score recompute. Row
// r = slot*sq + qi softmaxes over only its causal band [0, qi]; cols ki>qi are
// masked sentinels (-1e4) whose exp underflows to EXACTLY 0, so they contribute
// nothing to the row max/sum — making this bit-identical to `softmax_lastax` for
// the band that the consumers (attn_bwd dv/ds) actually read. Cols>qi are left
// untouched (never read). ~2× less work per row for causal prefill.
kernel void softmax_lastax_causal(
    device float* data    [[buffer(0)]],
    constant uint& cols   [[buffer(1)]],
    constant uint& sq     [[buffer(2)]],
    uint row [[threadgroup_position_in_grid]],
    uint tid [[thread_position_in_threadgroup]],
    uint tsize [[threads_per_threadgroup]]
) {
    threadgroup float partial[256];
    uint qi = row % sq;
    uint active = min(qi + 1u, cols);   // causal band [0, qi]
    uint base = row * cols;

    float local_max = -INFINITY;
    for (uint i = tid; i < active; i += tsize) {
        local_max = max(local_max, data[base + i]);
    }
    partial[tid] = local_max;
    threadgroup_barrier(mem_flags::mem_threadgroup);
    for (uint stride = tsize / 2; stride > 0; stride /= 2) {
        if (tid < stride) partial[tid] = max(partial[tid], partial[tid + stride]);
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }
    float row_max = partial[0];

    float local_sum = 0.0f;
    for (uint i = tid; i < active; i += tsize) {
        float e = exp(data[base + i] - row_max);
        data[base + i] = e;
        local_sum += e;
    }
    partial[tid] = local_sum;
    threadgroup_barrier(mem_flags::mem_threadgroup);
    for (uint stride = tsize / 2; stride > 0; stride /= 2) {
        if (tid < stride) partial[tid] += partial[tid + stride];
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }
    float inv_sum = 1.0f / partial[0];

    for (uint i = tid; i < active; i += tsize) {
        data[base + i] *= inv_sum;
    }
}

// Fused dense / soft-label softmax cross-entropy along the last axis.
// One threadgroup per row computes, numerically stably,
//   loss[n] = logsumexp(logits[n]) - Σ_c targets[n,c]·logits[n,c]
// via three threadgroup reductions (row max, Σexp, Σtargets·logits).
// `cols` is the class count C; output is one scalar per row.
kernel void softmax_cross_entropy_dense(
    device const float* logits  [[buffer(0)]],
    device const float* targets [[buffer(1)]],
    device float* out           [[buffer(2)]],
    constant uint& cols         [[buffer(3)]],
    uint row [[threadgroup_position_in_grid]],
    uint tid [[thread_position_in_threadgroup]],
    uint tsize [[threads_per_threadgroup]]
) {
    threadgroup float partial[256];
    uint base = row * cols;

    // Pass 1: row max for numerical stability.
    float local_max = -INFINITY;
    for (uint i = tid; i < cols; i += tsize) {
        local_max = max(local_max, logits[base + i]);
    }
    partial[tid] = local_max;
    threadgroup_barrier(mem_flags::mem_threadgroup);
    for (uint stride = tsize / 2; stride > 0; stride /= 2) {
        if (tid < stride) {
            partial[tid] = max(partial[tid], partial[tid + stride]);
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }
    float row_max = partial[0];
    threadgroup_barrier(mem_flags::mem_threadgroup);

    // Pass 2: Σ exp(x - max) and Σ targets·logits in one sweep.
    float local_sum = 0.0f;
    float local_dot = 0.0f;
    for (uint i = tid; i < cols; i += tsize) {
        float v = logits[base + i];
        local_sum += exp(v - row_max);
        local_dot += targets[base + i] * v;
    }
    partial[tid] = local_sum;
    threadgroup_barrier(mem_flags::mem_threadgroup);
    for (uint stride = tsize / 2; stride > 0; stride /= 2) {
        if (tid < stride) {
            partial[tid] += partial[tid + stride];
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }
    float sum_exp = partial[0];
    threadgroup_barrier(mem_flags::mem_threadgroup);

    partial[tid] = local_dot;
    threadgroup_barrier(mem_flags::mem_threadgroup);
    for (uint stride = tsize / 2; stride > 0; stride /= 2) {
        if (tid < stride) {
            partial[tid] += partial[tid + stride];
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }
    float dot = partial[0];

    if (tid == 0) {
        out[row] = (row_max + log(sum_exp)) - dot;
    }
}

// Softmax cross-entropy with integer labels (forward). One threadgroup per row.
// loss[row] = logsumexp(logits[row]) - logits[row, label]. Replaces the
// softmax + one-hot(compare/where) + gather decomposition on Metal.
kernel void softmax_cross_entropy_with_logits(
    device const float* logits [[buffer(0)]],
    device const float* labels [[buffer(1)]],
    device float* out          [[buffer(2)]],
    constant uint& cols        [[buffer(3)]],
    uint row [[threadgroup_position_in_grid]],
    uint tid [[thread_position_in_threadgroup]],
    uint tsize [[threads_per_threadgroup]]
) {
    threadgroup float partial[256];
    uint base = row * cols;

    float local_max = -INFINITY;
    for (uint i = tid; i < cols; i += tsize) local_max = max(local_max, logits[base + i]);
    partial[tid] = local_max;
    threadgroup_barrier(mem_flags::mem_threadgroup);
    for (uint stride = tsize / 2; stride > 0; stride /= 2) {
        if (tid < stride) partial[tid] = max(partial[tid], partial[tid + stride]);
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }
    float row_max = partial[0];
    threadgroup_barrier(mem_flags::mem_threadgroup);

    float local_sum = 0.0f;
    for (uint i = tid; i < cols; i += tsize) local_sum += exp(logits[base + i] - row_max);
    partial[tid] = local_sum;
    threadgroup_barrier(mem_flags::mem_threadgroup);
    for (uint stride = tsize / 2; stride > 0; stride /= 2) {
        if (tid < stride) partial[tid] += partial[tid + stride];
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }
    if (tid == 0) {
        uint label = (uint)labels[row];
        out[row] = (row_max + log(partial[0])) - logits[base + label];
    }
}

// Softmax cross-entropy backward (integer labels). One threadgroup per row.
// dlogits[row,k] = (softmax(logits[row])[k] - [k==label]) * d_loss[row].
kernel void softmax_cross_entropy_backward(
    device const float* logits [[buffer(0)]],
    device const float* labels [[buffer(1)]],
    device const float* d_loss [[buffer(2)]],
    device float* dlogits      [[buffer(3)]],
    constant uint& cols        [[buffer(4)]],
    uint row [[threadgroup_position_in_grid]],
    uint tid [[thread_position_in_threadgroup]],
    uint tsize [[threads_per_threadgroup]]
) {
    threadgroup float partial[256];
    uint base = row * cols;

    float local_max = -INFINITY;
    for (uint i = tid; i < cols; i += tsize) local_max = max(local_max, logits[base + i]);
    partial[tid] = local_max;
    threadgroup_barrier(mem_flags::mem_threadgroup);
    for (uint stride = tsize / 2; stride > 0; stride /= 2) {
        if (tid < stride) partial[tid] = max(partial[tid], partial[tid + stride]);
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }
    float row_max = partial[0];
    threadgroup_barrier(mem_flags::mem_threadgroup);

    float local_sum = 0.0f;
    for (uint i = tid; i < cols; i += tsize) local_sum += exp(logits[base + i] - row_max);
    partial[tid] = local_sum;
    threadgroup_barrier(mem_flags::mem_threadgroup);
    for (uint stride = tsize / 2; stride > 0; stride /= 2) {
        if (tid < stride) partial[tid] += partial[tid + stride];
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }
    float inv_sum = 1.0f / partial[0];
    float scale = d_loss[row];
    uint label = (uint)labels[row];
    for (uint k = tid; k < cols; k += tsize) {
        float p = exp(logits[base + k] - row_max) * inv_sum;
        dlogits[base + k] = (p - (k == label ? 1.0f : 0.0f)) * scale;
    }
}

// Embedding lookup: out[i, .] = table[idx[i], .]
// table: [vocab, trailing], idx: [num_idx], out: [num_idx, trailing]
kernel void gather_axis0(
    device const float* table [[buffer(0)]],
    device const float* idx   [[buffer(1)]],
    device float* out         [[buffer(2)]],
    constant uint& num_idx    [[buffer(3)]],
    constant uint& trailing   [[buffer(4)]],
    uint2 gid [[thread_position_in_grid]]
) {
    uint i = gid.y;
    uint j = gid.x;
    if (i >= num_idx || j >= trailing) return;
    uint row = uint(idx[i]);
    out[i * trailing + j] = table[row * trailing + j];
}

// Narrow / slice along last axis. src is [outer, src_axis], dst is [outer, len].
// Each invocation copies one (outer, j) element.
kernel void narrow_lastax(
    device const char* arena_src [[buffer(0)]],
    device char* arena_dst       [[buffer(1)]],
    constant uint& outer    [[buffer(2)]],
    constant uint& src_axis [[buffer(3)]],
    constant uint& start    [[buffer(4)]],
    constant uint& len      [[buffer(5)]],
    constant ulong& src_byte_off [[buffer(6)]],
    constant ulong& dst_byte_off [[buffer(7)]],
    uint2 gid [[thread_position_in_grid]]
) {
    // Task #50: > 4 GB activations need ulong byte offsets.
    device const float* src = (device const float*)(arena_src + src_byte_off);
    device float* dst       = (device float*)(arena_dst + dst_byte_off);
    uint i = gid.y;
    uint j = gid.x;
    if (i >= outer || j >= len) return;
    dst[i * len + j] = src[i * src_axis + start + j];
}

// Vectorized narrow for aligned shapes: src/dst treated as packed_float4.
// Requirements (enforced in encoder): start, src_axis, len are divisible by 4.
kernel void narrow_lastax4(
    device const char* arena_src    [[buffer(0)]],
    device char* arena_dst          [[buffer(1)]],
    constant uint& outer            [[buffer(2)]],
    constant uint& src_axis4        [[buffer(3)]],
    constant uint& start4           [[buffer(4)]],
    constant uint& len4             [[buffer(5)]],
    constant ulong& src_byte_off    [[buffer(6)]],
    constant ulong& dst_byte_off    [[buffer(7)]],
    uint2 gid [[thread_position_in_grid]]
) {
    device const packed_float4* src = (device const packed_float4*)(arena_src + src_byte_off);
    device packed_float4* dst       = (device packed_float4*)(arena_dst + dst_byte_off);
    uint i = gid.y;
    uint j4 = gid.x;
    if (i >= outer || j4 >= len4) return;
    dst[i * len4 + j4] = src[i * src_axis4 + start4 + j4];
}

// `dst` widened to ulong (task #50: ≥4 GB models have activation byte
// offsets that exceed u32 — truncated wrap-around made K_rep narrows
// write to the wrong slot and SDPA saw all-zero K_rep).
struct NarrowSeg {
    ulong dst;
    uint start;
    uint len;
};

// Concat VJP: multiple last-axis slices from one source in one dispatch.
kernel void split_lastax(
    device const char* arena_src [[buffer(0)]],
    device char* arena    [[buffer(1)]],
    constant uint& outer    [[buffer(2)]],
    constant uint& src_axis [[buffer(3)]],
    constant uint& num_seg  [[buffer(4)]],
    constant NarrowSeg* segs [[buffer(5)]],
    constant ulong& src_byte_off [[buffer(6)]],
    uint3 gid [[thread_position_in_grid]]
) {
    device const float* src = (device const float*)(arena_src + src_byte_off);
    uint s = gid.z;
    uint i = gid.y;
    uint j = gid.x;
    if (s >= num_seg) return;
    NarrowSeg seg = segs[s];
    if (i >= outer || j >= seg.len) return;
    device float* dst = (device float*)(arena + seg.dst);
    dst[i * seg.len + j] = src[i * src_axis + seg.start + j];
}

kernel void split_lastax4(
    device const char* arena_src    [[buffer(0)]],
    device char* arena            [[buffer(1)]],
    constant uint& outer            [[buffer(2)]],
    constant uint& src_axis4        [[buffer(3)]],
    constant uint& num_seg          [[buffer(4)]],
    constant NarrowSeg* segs        [[buffer(5)]],
    constant ulong& src_byte_off    [[buffer(6)]],
    uint3 gid [[thread_position_in_grid]]
) {
    device const packed_float4* src = (device const packed_float4*)(arena_src + src_byte_off);
    uint s = gid.z;
    uint i = gid.y;
    uint j4 = gid.x;
    if (s >= num_seg) return;
    NarrowSeg seg = segs[s];
    uint len4 = seg.len / 4u;
    if (i >= outer || j4 >= len4) return;
    device packed_float4* dst = (device packed_float4*)(arena + seg.dst);
    uint start4 = seg.start / 4u;
    dst[i * len4 + j4] = src[i * src_axis4 + start4 + j4];
}

// Concat segment: copy one [outer, src_axis] tensor into [outer, dst_axis]
// at the column slice [dst_col .. dst_col + src_axis]. Multi-input concat
// = N dispatches of this kernel, one per source. Mirror of narrow_lastax.
kernel void concat_segment_lastax(
    device const char* arena_src [[buffer(0)]],
    device char* arena_dst       [[buffer(1)]],
    constant uint& outer    [[buffer(2)]],
    constant uint& src_axis [[buffer(3)]],
    constant uint& dst_axis [[buffer(4)]],
    constant uint& dst_col  [[buffer(5)]],
    constant ulong& src_byte_off [[buffer(6)]],
    constant ulong& dst_byte_off [[buffer(7)]],
    uint2 gid [[thread_position_in_grid]]
) {
    // Task #50: large set_buffer offsets silently lose kernel writes on
    // M-series — bind arena base and apply byte offsets here.
    device const float* src = (device const float*)(arena_src + src_byte_off);
    device float* dst       = (device float*)(arena_dst + dst_byte_off);
    uint i = gid.y;
    uint j = gid.x;
    if (i >= outer || j >= src_axis) return;
    dst[i * dst_axis + dst_col + j] = src[i * src_axis + j];
}

kernel void concat_segment_lastax4(
    device const char* arena_src [[buffer(0)]],
    device char* arena_dst       [[buffer(1)]],
    constant uint& outer            [[buffer(2)]],
    constant uint& src_axis4        [[buffer(3)]],
    constant uint& dst_axis4        [[buffer(4)]],
    constant uint& dst_col4         [[buffer(5)]],
    constant ulong& src_byte_off [[buffer(6)]],
    constant ulong& dst_byte_off [[buffer(7)]],
    uint2 gid [[thread_position_in_grid]]
) {
    device const packed_float4* src = (device const packed_float4*)(arena_src + src_byte_off);
    device packed_float4* dst       = (device packed_float4*)(arena_dst + dst_byte_off);
    uint i = gid.y;
    uint j4 = gid.x;
    if (i >= outer || j4 >= src_axis4) return;
    dst[i * dst_axis4 + dst_col4 + j4] = src[i * src_axis4 + j4];
}

// `src` widened to ulong (task #50: ≥4 GB models have activation byte
// offsets > u32 — truncated wrap-around made `repeat_kv` write the wrong
// slot and SDPA saw all-zero K_rep).
struct ConcatSeg {
    ulong src;
    uint dst_col;
    uint len;
};

kernel void concat_lastax_multi(
    device char* arena       [[buffer(0)]],
    constant ulong& dst_byte [[buffer(1)]],
    constant uint& outer     [[buffer(2)]],
    constant uint& dst_axis  [[buffer(3)]],
    constant uint& num_seg   [[buffer(4)]],
    constant ConcatSeg* segs [[buffer(5)]],
    uint3 gid [[thread_position_in_grid]]
) {
    uint s = gid.z;
    uint i = gid.y;
    uint j = gid.x;
    if (s >= num_seg) return;
    ConcatSeg seg = segs[s];
    if (i >= outer || j >= seg.len) return;
    device const float* src = (device const float*)(arena + seg.src);
    device float* dst = (device float*)(arena + dst_byte);
    dst[i * dst_axis + seg.dst_col + j] = src[i * seg.len + j];
}

kernel void concat_lastax_multi4(
    device char* arena       [[buffer(0)]],
    constant ulong& dst_byte [[buffer(1)]],
    constant uint& outer     [[buffer(2)]],
    constant uint& dst_axis4 [[buffer(3)]],
    constant uint& num_seg   [[buffer(4)]],
    constant ConcatSeg* segs [[buffer(5)]],
    uint3 gid [[thread_position_in_grid]]
) {
    uint s = gid.z;
    uint i = gid.y;
    uint j4 = gid.x;
    if (s >= num_seg) return;
    ConcatSeg seg = segs[s];
    uint len4 = seg.len / 4u;
    if (i >= outer || j4 >= len4) return;
    device const packed_float4* src =
        (device const packed_float4*)(arena + seg.src);
    device packed_float4* dst =
        (device packed_float4*)(arena + dst_byte);
    uint dst_col4 = seg.dst_col / 4u;
    dst[i * dst_axis4 + dst_col4 + j4] = src[i * len4 + j4];
}

kernel void concat_segment_lastax_h(
    device const char* arena_src [[buffer(0)]],
    device char* arena_dst       [[buffer(1)]],
    constant uint& outer    [[buffer(2)]],
    constant uint& src_axis [[buffer(3)]],
    constant uint& dst_axis [[buffer(4)]],
    constant uint& dst_col  [[buffer(5)]],
    constant ulong& src_byte_off [[buffer(6)]],
    constant ulong& dst_byte_off [[buffer(7)]],
    uint2 gid [[thread_position_in_grid]]
) {
    device const half* src = (device const half*)(arena_src + src_byte_off);
    device half* dst       = (device half*)(arena_dst + dst_byte_off);
    uint i = gid.y;
    uint j = gid.x;
    if (i >= outer || j >= src_axis) return;
    dst[i * dst_axis + dst_col + j] = src[i * src_axis + j];
}

// Mid-axis concat (inner > 1): copy one segment src[outer][src_axis][inner]
// into dst[outer][dst_axis][inner] starting at axis offset `dst_col`. One 1D
// dispatch per segment, encoded into the live command buffer (NO commit/wait)
// — the host-copy fallback used to commit+wait per concat, serializing a
// decode step into ~100 tiny GPU submissions (the dominant cost on GGUF KV
// caches). `arena` bound at 0 + ulong byte offsets so it is correct on
// >4 GiB arenas (task #50). Generic: subsumes last-axis concat when inner==1.
// Mid-axis concat segment kernels, one per (input, output) precision pair —
// generated from CONCAT_MIDAXIS_SEG_TMPL by `concat_midaxis_variant!`. The
// converting variants (f32→f16 / f16→f32) let a concat with a mismatched-dtype
// input (e.g. an f32 `k_rope` into the f16 KV concat) convert on write instead
// of reading the source bytes at the wrong width (→ inf/NaN saturation).
// @@RLX_CONCAT_MIDAXIS@@

// Fused residual + LN: out = LN(x + residual + bias, gamma, beta)
// (bias is broadcast per row; pass empty/null offset for no-bias variant)
kernel void fused_residual_ln(
    device const float* x      [[buffer(0)]],
    device const float* res    [[buffer(1)]],
    device const float* gamma  [[buffer(2)]],
    device const float* beta   [[buffer(3)]],
    device float* out          [[buffer(4)]],
    constant uint& h           [[buffer(5)]],
    constant float& eps        [[buffer(6)]],
    uint row [[threadgroup_position_in_grid]],
    uint tid [[thread_position_in_threadgroup]],
    uint tsize [[threads_per_threadgroup]]
) {
    threadgroup float partial_sum[256];
    threadgroup float partial_sumsq[256];

    // Pass 1: compute (x + res) on the fly, accumulate sum/sumsq
    float local_sum = 0.0;
    float local_sumsq = 0.0;
    for (uint i = tid; i < h; i += tsize) {
        float v = x[row * h + i] + res[row * h + i];
        local_sum += v;
        local_sumsq += v * v;
    }
    partial_sum[tid] = local_sum;
    partial_sumsq[tid] = local_sumsq;
    threadgroup_barrier(mem_flags::mem_threadgroup);

    for (uint stride = tsize / 2; stride > 0; stride /= 2) {
        if (tid < stride) {
            partial_sum[tid] += partial_sum[tid + stride];
            partial_sumsq[tid] += partial_sumsq[tid + stride];
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }

    float mean = partial_sum[0] / float(h);
    float var = fmax(0.0f, partial_sumsq[0] / float(h) - mean * mean);
    float inv_std = rsqrt(var + eps);

    // Pass 2: write normalized output
    for (uint i = tid; i < h; i += tsize) {
        float v = x[row * h + i] + res[row * h + i];
        out[row * h + i] = (v - mean) * inv_std * gamma[i] + beta[i];
    }
}

// DiT adaLN-Zero: out = norm(x) * (1 + scale) + shift
// scale/shift broadcast over leading dims (typically [B,1,D] over [B,S,D]).
// `layer_norm != 0` → mean-subtract; else RMS only.
// Packed lead dims: [lead_rank, x_lead[8], mod_lead[8]] as 17 uints.
kernel void ada_layer_norm(
    device const float* x      [[buffer(0)]],
    device const float* scale  [[buffer(1)]],
    device const float* shift  [[buffer(2)]],
    device float* out          [[buffer(3)]],
    constant uint& h           [[buffer(4)]],
    constant float& eps        [[buffer(5)]],
    constant uint& layer_norm  [[buffer(6)]],
    constant uint* lead_pack   [[buffer(7)]],
    uint row [[threadgroup_position_in_grid]],
    uint tid [[thread_position_in_threadgroup]],
    uint tsize [[threads_per_threadgroup]]
) {
    uint lead_rank = lead_pack[0];
    // Decode `row` into a multi-index over x_lead; fold into mod base offset.
    uint rem = row;
    uint mod_base = 0;
    uint mod_stride = h;
    for (int j = int(lead_rank) - 1; j >= 0; --j) {
        uint xd = lead_pack[1 + j];
        if (xd == 0u) { xd = 1u; }
        uint xi = rem % xd;
        rem /= xd;
        uint md = lead_pack[9 + j];
        if (md == 0u) { md = 1u; }
        if (md != 1u) {
            mod_base += xi * mod_stride;
        }
        mod_stride *= md;
    }

    threadgroup float partial_sum[256];
    threadgroup float partial_sumsq[256];

    float local_sum = 0.0;
    float local_sumsq = 0.0;
    for (uint i = tid; i < h; i += tsize) {
        float v = x[row * h + i];
        local_sum += v;
        local_sumsq += v * v;
    }
    partial_sum[tid] = local_sum;
    partial_sumsq[tid] = local_sumsq;
    threadgroup_barrier(mem_flags::mem_threadgroup);

    for (uint stride = tsize / 2; stride > 0; stride /= 2) {
        if (tid < stride) {
            partial_sum[tid] += partial_sum[tid + stride];
            partial_sumsq[tid] += partial_sumsq[tid + stride];
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }

    float mean = 0.0;
    float inv;
    if (layer_norm != 0u) {
        mean = partial_sum[0] / float(h);
        float var = fmax(0.0f, partial_sumsq[0] / float(h) - mean * mean);
        inv = rsqrt(var + eps);
    } else {
        float ms = partial_sumsq[0] / float(h);
        inv = rsqrt(ms + eps);
    }

    for (uint i = tid; i < h; i += tsize) {
        float n = (x[row * h + i] - mean) * inv;
        out[row * h + i] = n * (1.0f + scale[mod_base + i]) + shift[mod_base + i];
    }
}

// f16 DiT adaLN — accumulate in f32, store half.
kernel void ada_layer_norm_h(
    device const half* x      [[buffer(0)]],
    device const half* scale  [[buffer(1)]],
    device const half* shift  [[buffer(2)]],
    device half* out          [[buffer(3)]],
    constant uint& h           [[buffer(4)]],
    constant float& eps        [[buffer(5)]],
    constant uint& layer_norm  [[buffer(6)]],
    constant uint* lead_pack   [[buffer(7)]],
    uint row [[threadgroup_position_in_grid]],
    uint tid [[thread_position_in_threadgroup]],
    uint tsize [[threads_per_threadgroup]]
) {
    uint lead_rank = lead_pack[0];
    uint rem = row;
    uint mod_base = 0;
    uint mod_stride = h;
    for (int j = int(lead_rank) - 1; j >= 0; --j) {
        uint xd = lead_pack[1 + j];
        if (xd == 0u) { xd = 1u; }
        uint xi = rem % xd;
        rem /= xd;
        uint md = lead_pack[9 + j];
        if (md == 0u) { md = 1u; }
        if (md != 1u) {
            mod_base += xi * mod_stride;
        }
        mod_stride *= md;
    }

    threadgroup float partial_sum[256];
    threadgroup float partial_sumsq[256];

    float local_sum = 0.0;
    float local_sumsq = 0.0;
    for (uint i = tid; i < h; i += tsize) {
        float v = float(x[row * h + i]);
        local_sum += v;
        local_sumsq += v * v;
    }
    partial_sum[tid] = local_sum;
    partial_sumsq[tid] = local_sumsq;
    threadgroup_barrier(mem_flags::mem_threadgroup);

    for (uint stride = tsize / 2; stride > 0; stride /= 2) {
        if (tid < stride) {
            partial_sum[tid] += partial_sum[tid + stride];
            partial_sumsq[tid] += partial_sumsq[tid + stride];
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }

    float mean = 0.0;
    float inv;
    if (layer_norm != 0u) {
        mean = partial_sum[0] / float(h);
        float var = fmax(0.0f, partial_sumsq[0] / float(h) - mean * mean);
        inv = rsqrt(var + eps);
    } else {
        float ms = partial_sumsq[0] / float(h);
        inv = rsqrt(ms + eps);
    }

    for (uint i = tid; i < h; i += tsize) {
        float n = (float(x[row * h + i]) - mean) * inv;
        out[row * h + i] = half(n * (1.0f + float(scale[mod_base + i])) + float(shift[mod_base + i]));
    }
}

// DiT gated residual: out = x + gate * y  (gate broadcasts like adaLN scale).
// lead_pack: [lead_rank, x_lead[8], gate_lead[8]].
kernel void gated_residual(
    device const float* x      [[buffer(0)]],
    device const float* y      [[buffer(1)]],
    device const float* gate   [[buffer(2)]],
    device float* out          [[buffer(3)]],
    constant uint& h           [[buffer(4)]],
    constant uint* lead_pack   [[buffer(5)]],
    uint gid [[thread_position_in_grid]]
) {
    uint lead_rank = lead_pack[0];
    uint row = gid / h;
    uint col = gid % h;
    uint rem = row;
    uint gate_base = 0;
    uint gate_stride = h;
    for (int j = int(lead_rank) - 1; j >= 0; --j) {
        uint xd = lead_pack[1 + j];
        if (xd == 0u) { xd = 1u; }
        uint xi = rem % xd;
        rem /= xd;
        uint gd = lead_pack[9 + j];
        if (gd == 0u) { gd = 1u; }
        if (gd != 1u) {
            gate_base += xi * gate_stride;
        }
        gate_stride *= gd;
    }
    uint i = row * h + col;
    out[i] = x[i] + gate[gate_base + col] * y[i];
}

kernel void gated_residual_h(
    device const half* x      [[buffer(0)]],
    device const half* y      [[buffer(1)]],
    device const half* gate   [[buffer(2)]],
    device half* out          [[buffer(3)]],
    constant uint& h           [[buffer(4)]],
    constant uint* lead_pack   [[buffer(5)]],
    uint gid [[thread_position_in_grid]]
) {
    uint lead_rank = lead_pack[0];
    uint row = gid / h;
    uint col = gid % h;
    uint rem = row;
    uint gate_base = 0;
    uint gate_stride = h;
    for (int j = int(lead_rank) - 1; j >= 0; --j) {
        uint xd = lead_pack[1 + j];
        if (xd == 0u) { xd = 1u; }
        uint xi = rem % xd;
        rem /= xd;
        uint gd = lead_pack[9 + j];
        if (gd == 0u) { gd = 1u; }
        if (gd != 1u) {
            gate_base += xi * gate_stride;
        }
        gate_stride *= gd;
    }
    uint i = row * h + col;
    out[i] = half(float(x[i]) + float(gate[gate_base + col]) * float(y[i]));
}

// Packed DiT gated residual backward: out = [dx ∥ dy ∥ dgate] (1-D).
// Threadgroups = mod_rows (unique gate rows); each TG owns one gate slice
// and loops `seq_per_mod` x-rows that share it (DiT [B,S,D] / [B,1,D]).
kernel void gated_residual_backward(
    device const float* y       [[buffer(0)]],
    device const float* gate    [[buffer(1)]],
    device const float* dy      [[buffer(2)]],
    device float* packed        [[buffer(3)]],
    constant uint& h            [[buffer(4)]],
    constant uint& seq_per_mod  [[buffer(5)]],
    constant uint& mod_rows     [[buffer(6)]],
    uint m [[threadgroup_position_in_grid]],
    uint tid [[thread_position_in_threadgroup]],
    uint tsize [[threads_per_threadgroup]]
) {
    if (m >= mod_rows) return;
    uint nx = mod_rows * seq_per_mod * h;
    uint gate_base = m * h;
    device float* dx = packed;
    device float* dy_out = packed + nx;
    device float* dgate = packed + 2 * nx;

    for (uint i = tid; i < h; i += tsize) {
        float acc = 0.0f;
        for (uint s = 0; s < seq_per_mod; s++) {
            uint row = m * seq_per_mod + s;
            uint idx = row * h + i;
            float g = gate[gate_base + i];
            float d = dy[idx];
            dx[idx] = d;
            dy_out[idx] = d * g;
            acc += d * y[idx];
        }
        dgate[gate_base + i] = acc;
    }
}

kernel void gated_residual_backward_h(
    device const half* y       [[buffer(0)]],
    device const half* gate    [[buffer(1)]],
    device const half* dy      [[buffer(2)]],
    device half* packed        [[buffer(3)]],
    constant uint& h            [[buffer(4)]],
    constant uint& seq_per_mod  [[buffer(5)]],
    constant uint& mod_rows     [[buffer(6)]],
    uint m [[threadgroup_position_in_grid]],
    uint tid [[thread_position_in_threadgroup]],
    uint tsize [[threads_per_threadgroup]]
) {
    if (m >= mod_rows) return;
    uint nx = mod_rows * seq_per_mod * h;
    uint gate_base = m * h;
    device half* dx = packed;
    device half* dy_out = packed + nx;
    device half* dgate = packed + 2 * nx;

    for (uint i = tid; i < h; i += tsize) {
        float acc = 0.0f;
        for (uint s = 0; s < seq_per_mod; s++) {
            uint row = m * seq_per_mod + s;
            uint idx = row * h + i;
            float g = float(gate[gate_base + i]);
            float d = float(dy[idx]);
            dx[idx] = half(d);
            dy_out[idx] = half(d * g);
            acc += d * float(y[idx]);
        }
        dgate[gate_base + i] = half(acc);
    }
}

// Packed AdaLayerNorm backward: out = [dx ∥ dscale ∥ dshift] (1-D).
// Same launch geometry as gated_residual_backward.
kernel void ada_layer_norm_backward(
    device const float* x       [[buffer(0)]],
    device const float* scale   [[buffer(1)]],
    device const float* dy      [[buffer(2)]],
    device float* packed        [[buffer(3)]],
    constant uint& h            [[buffer(4)]],
    constant float& eps         [[buffer(5)]],
    constant uint& layer_norm   [[buffer(6)]],
    constant uint& seq_per_mod  [[buffer(7)]],
    constant uint& mod_rows     [[buffer(8)]],
    uint m [[threadgroup_position_in_grid]],
    uint tid [[thread_position_in_threadgroup]],
    uint tsize [[threads_per_threadgroup]]
) {
    if (m >= mod_rows) return;
    uint nx = mod_rows * seq_per_mod * h;
    uint mod_len = mod_rows * h;
    uint mod_base = m * h;
    device float* dx = packed;
    device float* dscale = packed + nx;
    device float* dshift = packed + nx + mod_len;

    threadgroup float partial_sum[256];
    threadgroup float partial_sumsq[256];

    // Zero modulation grads for this slice.
    for (uint i = tid; i < h; i += tsize) {
        dscale[mod_base + i] = 0.0f;
        dshift[mod_base + i] = 0.0f;
    }
    threadgroup_barrier(mem_flags::mem_device);

    float inv_h = 1.0f / float(h);
    for (uint s = 0; s < seq_per_mod; s++) {
        uint row = m * seq_per_mod + s;

        float local_sum = 0.0f;
        float local_sumsq = 0.0f;
        for (uint i = tid; i < h; i += tsize) {
            float v = x[row * h + i];
            local_sum += v;
            local_sumsq += v * v;
        }
        partial_sum[tid] = local_sum;
        partial_sumsq[tid] = local_sumsq;
        threadgroup_barrier(mem_flags::mem_threadgroup);
        for (uint stride = tsize / 2; stride > 0; stride /= 2) {
            if (tid < stride) {
                partial_sum[tid] += partial_sum[tid + stride];
                partial_sumsq[tid] += partial_sumsq[tid + stride];
            }
            threadgroup_barrier(mem_flags::mem_threadgroup);
        }
        float mean = 0.0f;
        float inv;
        if (layer_norm != 0u) {
            mean = partial_sum[0] * inv_h;
            float var = fmax(0.0f, partial_sumsq[0] * inv_h - mean * mean);
            inv = rsqrt(var + eps);
        } else {
            inv = rsqrt(partial_sumsq[0] * inv_h + eps);
        }

        // First pass: accumulate dscale/dshift and reduction stats for dx.
        float local_sy = 0.0f;
        float local_sxh = 0.0f;
        for (uint i = tid; i < h; i += tsize) {
            float n = (x[row * h + i] - mean) * inv;
            float d = dy[row * h + i];
            float sc = scale[mod_base + i];
            float sy = d * (1.0f + sc);
            dscale[mod_base + i] += d * n;
            dshift[mod_base + i] += d;
            local_sy += sy;
            local_sxh += sy * n;
        }
        partial_sum[tid] = local_sy;
        partial_sumsq[tid] = local_sxh;
        threadgroup_barrier(mem_flags::mem_threadgroup);
        for (uint stride = tsize / 2; stride > 0; stride /= 2) {
            if (tid < stride) {
                partial_sum[tid] += partial_sum[tid + stride];
                partial_sumsq[tid] += partial_sumsq[tid + stride];
            }
            threadgroup_barrier(mem_flags::mem_threadgroup);
        }
        float m_sy = partial_sum[0] * inv_h;
        float m_sxh = partial_sumsq[0] * inv_h;

        for (uint i = tid; i < h; i += tsize) {
            float n = (x[row * h + i] - mean) * inv;
            float d = dy[row * h + i];
            float sc = scale[mod_base + i];
            float sy = d * (1.0f + sc);
            if (layer_norm != 0u) {
                dx[row * h + i] = inv * (sy - m_sy - n * m_sxh);
            } else {
                float n_rms = x[row * h + i] * inv;
                dx[row * h + i] = inv * (sy - n_rms * m_sxh);
            }
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }
}

kernel void ada_layer_norm_backward_h(
    device const half* x       [[buffer(0)]],
    device const half* scale   [[buffer(1)]],
    device const half* dy      [[buffer(2)]],
    device half* packed        [[buffer(3)]],
    constant uint& h            [[buffer(4)]],
    constant float& eps         [[buffer(5)]],
    constant uint& layer_norm   [[buffer(6)]],
    constant uint& seq_per_mod  [[buffer(7)]],
    constant uint& mod_rows     [[buffer(8)]],
    uint m [[threadgroup_position_in_grid]],
    uint tid [[thread_position_in_threadgroup]],
    uint tsize [[threads_per_threadgroup]]
) {
    if (m >= mod_rows) return;
    uint nx = mod_rows * seq_per_mod * h;
    uint mod_len = mod_rows * h;
    uint mod_base = m * h;
    device half* dx = packed;
    device half* dscale = packed + nx;
    device half* dshift = packed + nx + mod_len;

    threadgroup float partial_sum[256];
    threadgroup float partial_sumsq[256];

    for (uint i = tid; i < h; i += tsize) {
        dscale[mod_base + i] = half(0.0h);
        dshift[mod_base + i] = half(0.0h);
    }
    threadgroup_barrier(mem_flags::mem_device);

    float inv_h = 1.0f / float(h);
    for (uint s = 0; s < seq_per_mod; s++) {
        uint row = m * seq_per_mod + s;

        float local_sum = 0.0f;
        float local_sumsq = 0.0f;
        for (uint i = tid; i < h; i += tsize) {
            float v = float(x[row * h + i]);
            local_sum += v;
            local_sumsq += v * v;
        }
        partial_sum[tid] = local_sum;
        partial_sumsq[tid] = local_sumsq;
        threadgroup_barrier(mem_flags::mem_threadgroup);
        for (uint stride = tsize / 2; stride > 0; stride /= 2) {
            if (tid < stride) {
                partial_sum[tid] += partial_sum[tid + stride];
                partial_sumsq[tid] += partial_sumsq[tid + stride];
            }
            threadgroup_barrier(mem_flags::mem_threadgroup);
        }
        float mean = 0.0f;
        float inv;
        if (layer_norm != 0u) {
            mean = partial_sum[0] * inv_h;
            float var = fmax(0.0f, partial_sumsq[0] * inv_h - mean * mean);
            inv = rsqrt(var + eps);
        } else {
            inv = rsqrt(partial_sumsq[0] * inv_h + eps);
        }

        float local_sy = 0.0f;
        float local_sxh = 0.0f;
        for (uint i = tid; i < h; i += tsize) {
            float n = (float(x[row * h + i]) - mean) * inv;
            float d = float(dy[row * h + i]);
            float sc = float(scale[mod_base + i]);
            float sy = d * (1.0f + sc);
            dscale[mod_base + i] = half(float(dscale[mod_base + i]) + d * n);
            dshift[mod_base + i] = half(float(dshift[mod_base + i]) + d);
            local_sy += sy;
            local_sxh += sy * n;
        }
        partial_sum[tid] = local_sy;
        partial_sumsq[tid] = local_sxh;
        threadgroup_barrier(mem_flags::mem_threadgroup);
        for (uint stride = tsize / 2; stride > 0; stride /= 2) {
            if (tid < stride) {
                partial_sum[tid] += partial_sum[tid + stride];
                partial_sumsq[tid] += partial_sumsq[tid + stride];
            }
            threadgroup_barrier(mem_flags::mem_threadgroup);
        }
        float m_sy = partial_sum[0] * inv_h;
        float m_sxh = partial_sumsq[0] * inv_h;

        for (uint i = tid; i < h; i += tsize) {
            float n = (float(x[row * h + i]) - mean) * inv;
            float d = float(dy[row * h + i]);
            float sc = float(scale[mod_base + i]);
            float sy = d * (1.0f + sc);
            if (layer_norm != 0u) {
                dx[row * h + i] = half(inv * (sy - m_sy - n * m_sxh));
            } else {
                float n_rms = float(x[row * h + i]) * inv;
                dx[row * h + i] = half(inv * (sy - n_rms * m_sxh));
            }
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }
}

// Fused residual + RMSNorm: out = RmsNorm(x + residual, gamma, beta).
// Arena + byte offsets (task #50) — `set_buffer(..., large_offset)` silently
// drops writes on M-series for big Qwen3.5 / Bonsai arenas.
kernel void fused_residual_rms_norm(
    device const char* arena   [[buffer(0)]],
    constant ulong& x_off      [[buffer(1)]],
    constant ulong& res_off    [[buffer(2)]],
    constant ulong& g_off      [[buffer(3)]],
    constant ulong& b_off      [[buffer(4)]],
    constant ulong& out_off    [[buffer(5)]],
    constant uint& h           [[buffer(6)]],
    constant float& eps        [[buffer(7)]],
    constant ulong& sum_off    [[buffer(8)]],   // dual output: pre-norm sum (x+res), 0 = disabled
    uint row [[threadgroup_position_in_grid]],
    uint tid [[thread_position_in_threadgroup]],
    uint tsize [[threads_per_threadgroup]]
) {
    device const float* x = (device const float*)(arena + x_off);
    device const float* res = (device const float*)(arena + res_off);
    device const float* gamma = (device const float*)(arena + g_off);
    device const float* beta = (device const float*)(arena + b_off);
    device float* out = (device float*)(arena + out_off);
    device float* sum = (sum_off != 0) ? (device float*)(arena + sum_off) : (device float*)0;
    threadgroup float partial_sumsq[256];
    float local_sumsq = 0.0;
    for (uint i = tid; i < h; i += tsize) {
        float v = x[row * h + i] + res[row * h + i];
        local_sumsq += v * v;
        if (sum != 0) sum[row * h + i] = v;   // emit the residual sum for the skip stream
    }
    partial_sumsq[tid] = local_sumsq;
    threadgroup_barrier(mem_flags::mem_threadgroup);
    for (uint stride = tsize / 2; stride > 0; stride /= 2) {
        if (tid < stride) {
            partial_sumsq[tid] += partial_sumsq[tid + stride];
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }
    float inv_rms = rsqrt(partial_sumsq[0] / float(h) + eps);
    for (uint i = tid; i < h; i += tsize) {
        float v = x[row * h + i] + res[row * h + i];
        out[row * h + i] = v * inv_rms * gamma[i] + beta[i];
    }
}

// Packed SDPA byte offsets (task #50: lets the kernel reach activations
// that sit past 4 GB in the arena without needing `set_buffer(... ,large)`,
// which silently dropped kernel writes on M-series).
struct SdpaOffsets {
    ulong q;
    ulong k;
    ulong v;
    ulong m;
    ulong o;
    // GQA/MQA: K/V head count (equals `heads` for MHA). Packed here so we
    // don't need a 7th set_bytes slot past the Metal arg-table quirk.
    uint kv_heads;
    // V/output per-head width. Equals `head_dim` for symmetric SDPA; smaller
    // for asymmetric MLA (DeepSeek/Kimi qk=192, v=128). Packed into the high
    // 32 bits of the kv_heads slot on the host side. 0 ⇒ fall back to head_dim.
    uint v_head_dim;
};

// Q/K/V offset helpers — BSNH [B, L, H*D] vs BHSD [B, H, L, D].
static inline uint qkv_q_offset(
    uint bi, uint hi, uint qi,
    uint heads, uint seq_q, uint head_dim, uint q_stride, uint bhsd
) {
    if (bhsd != 0u) {
        return bi * heads * seq_q * head_dim + hi * seq_q * head_dim + qi * head_dim;
    }
    uint hs = heads * head_dim;
    return bi * q_stride * hs + qi * hs + hi * head_dim;
}
static inline uint qkv_kv_offset(
    uint bi, uint hi, uint ki,
    uint heads, uint kv_heads, uint seq_k, uint head_dim, uint k_stride, uint bhsd
) {
    // Map query head → shared KV head (GQA/MQA). MHA: kv_heads == heads.
    uint nkv = (kv_heads == 0u) ? heads : kv_heads;
    uint group = heads / nkv;
    uint hi_kv = (group > 1u) ? (hi / group) : hi;
    if (bhsd != 0u) {
        return bi * nkv * seq_k * head_dim + hi_kv * seq_k * head_dim + ki * head_dim;
    }
    uint hs = nkv * head_dim;
    return bi * k_stride * hs + ki * hs + hi_kv * head_dim;
}
// V read offset — like qkv_kv_offset but the per-head width is `v_head_dim`
// (asymmetric MLA). Reduces exactly to qkv_kv_offset when v_head_dim==head_dim.
static inline uint qkv_v_offset(
    uint bi, uint hi, uint ki,
    uint heads, uint kv_heads, uint seq_k, uint v_head_dim, uint k_stride, uint bhsd
) {
    uint nkv = (kv_heads == 0u) ? heads : kv_heads;
    uint group = heads / nkv;
    uint hi_kv = (group > 1u) ? (hi / group) : hi;
    if (bhsd != 0u) {
        return bi * nkv * seq_k * v_head_dim + hi_kv * seq_k * v_head_dim + ki * v_head_dim;
    }
    uint hs = nkv * v_head_dim;
    return bi * k_stride * hs + ki * hs + hi_kv * v_head_dim;
}
// Output write offset — the attention output row is `heads * v_head_dim` wide
// (asymmetric MLA). Reduces exactly to qkv_q_offset when v_head_dim==head_dim.
static inline uint qkv_out_offset(
    uint bi, uint hi, uint qi,
    uint heads, uint seq_q, uint v_head_dim, uint q_stride, uint bhsd
) {
    if (bhsd != 0u) {
        return bi * heads * seq_q * v_head_dim + hi * seq_q * v_head_dim + qi * v_head_dim;
    }
    uint hs = heads * v_head_dim;
    return bi * q_stride * hs + qi * hs + hi * v_head_dim;
}

// Multi-head SDPA: attention(Q, K, V, mask) → out
// Shapes: Q/out [batch, seq_q, heads*head_dim]; K/V [batch, seq_k, heads*head_dim]
// One threadgroup per (batch, head). Each TG computes [seq_q, seq_k] scores
// in threadgroup memory (seq_q * seq_k ≤ 64*64), applies softmax, then
// accumulates scores @ V.
kernel void sdpa(
    device const float* arena_q   [[buffer(0)]],
    device const float* arena_k   [[buffer(1)]],
    device const float* arena_v   [[buffer(2)]],
    device const float* arena_m   [[buffer(3)]],
    device float*       arena_o   [[buffer(4)]],
    constant uint& batch      [[buffer(5)]],
    constant uint& seq_q      [[buffer(6)]],
    constant uint& heads      [[buffer(7)]],
    constant uint& head_dim   [[buffer(8)]],
    constant uint& q_stride   [[buffer(9)]],
    constant uint& mask_kind  [[buffer(10)]],
    constant uint& seq_k      [[buffer(11)]],
    constant uint& k_stride   [[buffer(12)]],
    constant uint& bhsd       [[buffer(13)]],
    constant uint& window     [[buffer(14)]],
    constant float& score_scale  [[buffer(15)]],
    constant float& attn_softcap [[buffer(16)]],
    // Byte offsets relative to the arena buffer, packed as one struct.
    // For ≥4 GB models the activation byte offsets exceed u32 — and Metal
    // silently drops kernel writes when `set_buffer` is called with
    // `offset > 4 GB`. Binding all five buffers to `offset=0` and adding
    // the offsets here is the workaround proven by the dequant kernel
    // (works for offsets ≥ 14 GB). One inline-constant slot for all five
    // (task #50).
    constant SdpaOffsets& byte_offs [[buffer(17)]],
    uint tgid_x [[threadgroup_position_in_grid]],
    uint tid    [[thread_position_in_threadgroup]],
    uint tsize  [[threads_per_threadgroup]]
) {
    ulong q_byte_off = byte_offs.q;
    ulong k_byte_off = byte_offs.k;
    ulong v_byte_off = byte_offs.v;
    ulong m_byte_off = byte_offs.m;
    ulong o_byte_off = byte_offs.o;
    device const float* Q = (device const float*)((device const char*)arena_q + q_byte_off);
    device const float* K = (device const float*)((device const char*)arena_k + k_byte_off);
    device const float* V = (device const float*)((device const char*)arena_v + v_byte_off);
    device const float* M = (device const float*)((device const char*)arena_m + m_byte_off);
    device float* OUT     = (device float*)((device char*)arena_o + o_byte_off);
    // mask_kind:
    //   0 = None           (no masking)
    //   1 = Causal         (mask ki > (seq_k - seq_q) + qi)
    //   2 = Custom         (column-wise binary mask buffer M; 0 = padded)
    //   4 = SlidingWindow  (visible range [abs_q - window, abs_q],
    //                       absolute positions so decode w/ cached K/V works)
    threadgroup float scores[64 * 64];   // up to seq_q * seq_k = 4096
    threadgroup float row_max;
    threadgroup float row_sum;

    // Linearized: tgid_x = bi * heads + hi
    uint bi = tgid_x / heads;
    uint hi = tgid_x % heads;
    if (bi >= batch) return;

    // `score_scale` is the host-provided multiplier (Gemma 4 sets 1.0
    // because Q is per-head RMS-normed before attention). Sentinel `0.0`
    // means "use the relaxed-precision default `1/sqrt(head_dim)`".
    float scale = (score_scale > 0.0f) ? score_scale : rsqrt(float(head_dim));
    // Gemma 2 carries an attention-logit softcap (50.0); Gemma 4 sets 0.
    float softcap_inv = (attn_softcap > 0.0f) ? (1.0f / attn_softcap) : 0.0f;
    uint q_offset = seq_k - seq_q;


    // 1. Compute scores[qi, ki] = scale * (Q[bi, qi, hi*dh:] · K[bi, ki, hi*dh:]) + mask.
    uint total = seq_q * seq_k;
    for (uint idx = tid; idx < total; idx += tsize) {
        uint qi = idx / seq_k;
        uint ki = idx % seq_k;
        float dot = 0.0;
        uint q_base = qkv_q_offset(bi, hi, qi, heads, seq_q, head_dim, q_stride, bhsd);
        uint k_base = qkv_kv_offset(bi, hi, ki, heads, byte_offs.kv_heads, seq_k, head_dim, k_stride, bhsd);
        for (uint d = 0; d < head_dim; ++d) {
            dot += Q[q_base + d] * K[k_base + d];
        }
        float s = dot * scale;
        if (softcap_inv > 0.0f) {
            s = precise::tanh(s * softcap_inv) * attn_softcap;
        }
        if (mask_kind == 1u) {
            if (ki > q_offset + qi) s = -1e9;
        } else if (mask_kind == 2u) {
            if (M[bi * k_stride + ki] < 0.5) s = -1e9;
        } else if (mask_kind == 4u) {
            uint abs_q = q_offset + qi;
            uint lo = abs_q > window ? abs_q - window : 0u;
            if (ki < lo || ki > abs_q) s = -1e9;
        }
        scores[qi * seq_k + ki] = s;
    }
    threadgroup_barrier(mem_flags::mem_threadgroup);

    // 2. Softmax row-by-row over scores[seq_q, seq_k]. `precise::exp`
    // matches CPU `f32::exp` to within 1 ULP; the default fast-math
    // `exp` accumulates several ULPs of error per token, which the
    // softcap + LM head amplify into visible logit drift.
    for (uint qi = 0; qi < seq_q; ++qi) {
        if (tid == 0) {
            float mx = -1e30;
            for (uint ki = 0; ki < seq_k; ++ki) {
                mx = max(mx, scores[qi * seq_k + ki]);
            }
            row_max = mx;
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);

        if (tid == 0) {
            float sum = 0.0;
            for (uint ki = 0; ki < seq_k; ++ki) {
                float e = precise::exp(scores[qi * seq_k + ki] - row_max);
                scores[qi * seq_k + ki] = e;
                sum += e;
            }
            row_sum = sum;
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);

        for (uint ki = tid; ki < seq_k; ki += tsize) {
            scores[qi * seq_k + ki] /= row_sum;
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }

    // 3. Output[qi, d] = sum_ki scores[qi, ki] * V[bi, ki, hi*vdh + d]
    // V is read and the output is written `v_head_dim`-wide (asymmetric MLA;
    // == head_dim for symmetric SDPA). Q/K scores above stay head_dim-wide.
    uint vdh = (byte_offs.v_head_dim == 0u) ? head_dim : byte_offs.v_head_dim;
    uint out_total = seq_q * vdh;
    for (uint idx = tid; idx < out_total; idx += tsize) {
        uint qi = idx / vdh;
        uint d = idx % vdh;
        float acc = 0.0;
        for (uint ki = 0; ki < seq_k; ++ki) {
            uint v_base = qkv_v_offset(bi, hi, ki, heads, byte_offs.kv_heads, seq_k, vdh, k_stride, bhsd);
            acc += scores[qi * seq_k + ki] * V[v_base + d];
        }
        uint o_base = qkv_out_offset(bi, hi, qi, heads, seq_q, vdh, q_stride, bhsd);
        OUT[o_base + d] = acc;
    }
}

// SIMD-group-parallel SDPA (seq<=64 prefill). Identical math to `sdpa`, but the
// per-row softmax reduction uses simd_max / simd_sum across the 32-wide
// threadgroup (== one Apple SIMD group) instead of a serial `tid==0` scan. This
// removes the single-thread softmax bottleneck and all per-row threadgroup
// barriers; the score-matmul and A·V steps are already thread-parallel and are
// kept verbatim. Dispatched with exactly 32 threads/threadgroup so the SIMD
// reductions cover the whole threadgroup.
kernel void sdpa_simd(
    device const float* arena_q   [[buffer(0)]],
    device const float* arena_k   [[buffer(1)]],
    device const float* arena_v   [[buffer(2)]],
    device const float* arena_m   [[buffer(3)]],
    device float*       arena_o   [[buffer(4)]],
    constant uint& batch      [[buffer(5)]],
    constant uint& seq_q      [[buffer(6)]],
    constant uint& heads      [[buffer(7)]],
    constant uint& head_dim   [[buffer(8)]],
    constant uint& q_stride   [[buffer(9)]],
    constant uint& mask_kind  [[buffer(10)]],
    constant uint& seq_k      [[buffer(11)]],
    constant uint& k_stride   [[buffer(12)]],
    constant uint& bhsd       [[buffer(13)]],
    constant uint& window     [[buffer(14)]],
    constant float& score_scale  [[buffer(15)]],
    constant float& attn_softcap [[buffer(16)]],
    constant SdpaOffsets& byte_offs [[buffer(17)]],
    uint tgid_x [[threadgroup_position_in_grid]],
    uint tid    [[thread_position_in_threadgroup]],
    uint tsize  [[threads_per_threadgroup]]
) {
    device const float* Q = (device const float*)((device const char*)arena_q + byte_offs.q);
    device const float* K = (device const float*)((device const char*)arena_k + byte_offs.k);
    device const float* V = (device const float*)((device const char*)arena_v + byte_offs.v);
    device const float* M = (device const float*)((device const char*)arena_m + byte_offs.m);
    device float* OUT     = (device float*)((device char*)arena_o + byte_offs.o);
    threadgroup float scores[64 * 64];

    uint bi = tgid_x / heads;
    uint hi = tgid_x % heads;
    if (bi >= batch) return;

    float scale = (score_scale > 0.0f) ? score_scale : rsqrt(float(head_dim));
    float softcap_inv = (attn_softcap > 0.0f) ? (1.0f / attn_softcap) : 0.0f;
    uint q_offset = seq_k - seq_q;

    // 1. scores[qi,ki] = scale·(Q·K) + mask  (thread-parallel, verbatim from sdpa).
    uint total = seq_q * seq_k;
    for (uint idx = tid; idx < total; idx += tsize) {
        uint qi = idx / seq_k;
        uint ki = idx % seq_k;
        float dot = 0.0;
        uint q_base = qkv_q_offset(bi, hi, qi, heads, seq_q, head_dim, q_stride, bhsd);
        uint k_base = qkv_kv_offset(bi, hi, ki, heads, byte_offs.kv_heads, seq_k, head_dim, k_stride, bhsd);
        for (uint d = 0; d < head_dim; ++d) {
            dot += Q[q_base + d] * K[k_base + d];
        }
        float s = dot * scale;
        if (softcap_inv > 0.0f) {
            s = precise::tanh(s * softcap_inv) * attn_softcap;
        }
        if (mask_kind == 1u) {
            if (ki > q_offset + qi) s = -1e9;
        } else if (mask_kind == 2u) {
            if (M[bi * k_stride + ki] < 0.5) s = -1e9;
        } else if (mask_kind == 4u) {
            uint abs_q = q_offset + qi;
            uint lo = abs_q > window ? abs_q - window : 0u;
            if (ki < lo || ki > abs_q) s = -1e9;
        }
        scores[qi * seq_k + ki] = s;
    }
    threadgroup_barrier(mem_flags::mem_threadgroup);

    // 2. Softmax per row via SIMD-group reductions. Each thread owns the strided
    // columns ki = tid, tid+32, …; simd_max / simd_sum combine across the 32
    // lanes. Each thread only reads/writes its own columns, so no barrier is
    // needed between the phases (the SIMD ops synchronize the reduction).
    for (uint qi = 0; qi < seq_q; ++qi) {
        float lmax = -1e30;
        for (uint ki = tid; ki < seq_k; ki += tsize) {
            lmax = max(lmax, scores[qi * seq_k + ki]);
        }
        float mx = simd_max(lmax);
        float lsum = 0.0;
        for (uint ki = tid; ki < seq_k; ki += tsize) {
            float e = precise::exp(scores[qi * seq_k + ki] - mx);
            scores[qi * seq_k + ki] = e;
            lsum += e;
        }
        float inv = 1.0f / simd_sum(lsum);
        for (uint ki = tid; ki < seq_k; ki += tsize) {
            scores[qi * seq_k + ki] *= inv;
        }
    }
    threadgroup_barrier(mem_flags::mem_threadgroup);

    // 3. Output[qi,d] = Σ_ki scores[qi,ki]·V[…]  (thread-parallel, verbatim).
    uint vdh = (byte_offs.v_head_dim == 0u) ? head_dim : byte_offs.v_head_dim;
    uint out_total = seq_q * vdh;
    for (uint idx = tid; idx < out_total; idx += tsize) {
        uint qi = idx / vdh;
        uint d = idx % vdh;
        float acc = 0.0;
        for (uint ki = 0; ki < seq_k; ++ki) {
            uint v_base = qkv_v_offset(bi, hi, ki, heads, byte_offs.kv_heads, seq_k, vdh, k_stride, bhsd);
            acc += scores[qi * seq_k + ki] * V[v_base + d];
        }
        uint o_base = qkv_out_offset(bi, hi, qi, heads, seq_q, vdh, q_stride, bhsd);
        OUT[o_base + d] = acc;
    }
}

// f16-scores variant of `sdpa_simd`: the threadgroup scores matrix is stored as
// `half` (8 KiB vs 16 KiB) so 2× as many threadgroups fit per core — the one real
// occupancy limiter the shader trace found. ALL arithmetic stays f32 (Q·K dot,
// softmax max/exp/sum, A·V accumulate); only the on-chip *storage* of scores /
// probabilities is half. The scores are O(1) after scaling and softmax is
// range-robust, so the accuracy cost is ~1 half-ULP in the probabilities.
kernel void sdpa_simd_h16(
    device const float* arena_q   [[buffer(0)]],
    device const float* arena_k   [[buffer(1)]],
    device const float* arena_v   [[buffer(2)]],
    device const float* arena_m   [[buffer(3)]],
    device float*       arena_o   [[buffer(4)]],
    constant uint& batch      [[buffer(5)]],
    constant uint& seq_q      [[buffer(6)]],
    constant uint& heads      [[buffer(7)]],
    constant uint& head_dim   [[buffer(8)]],
    constant uint& q_stride   [[buffer(9)]],
    constant uint& mask_kind  [[buffer(10)]],
    constant uint& seq_k      [[buffer(11)]],
    constant uint& k_stride   [[buffer(12)]],
    constant uint& bhsd       [[buffer(13)]],
    constant uint& window     [[buffer(14)]],
    constant float& score_scale  [[buffer(15)]],
    constant float& attn_softcap [[buffer(16)]],
    constant SdpaOffsets& byte_offs [[buffer(17)]],
    uint tgid_x [[threadgroup_position_in_grid]],
    uint tid    [[thread_position_in_threadgroup]],
    uint tsize  [[threads_per_threadgroup]]
) {
    device const float* Q = (device const float*)((device const char*)arena_q + byte_offs.q);
    device const float* K = (device const float*)((device const char*)arena_k + byte_offs.k);
    device const float* V = (device const float*)((device const char*)arena_v + byte_offs.v);
    device const float* M = (device const float*)((device const char*)arena_m + byte_offs.m);
    device float* OUT     = (device float*)((device char*)arena_o + byte_offs.o);
    threadgroup half scores[64 * 64];   // 8 KiB (half of the f32 version)

    uint bi = tgid_x / heads;
    uint hi = tgid_x % heads;
    if (bi >= batch) return;

    float scale = (score_scale > 0.0f) ? score_scale : rsqrt(float(head_dim));
    float softcap_inv = (attn_softcap > 0.0f) ? (1.0f / attn_softcap) : 0.0f;
    uint q_offset = seq_k - seq_q;

    uint total = seq_q * seq_k;
    for (uint idx = tid; idx < total; idx += tsize) {
        uint qi = idx / seq_k, ki = idx % seq_k;
        float dot = 0.0;
        uint q_base = qkv_q_offset(bi, hi, qi, heads, seq_q, head_dim, q_stride, bhsd);
        uint k_base = qkv_kv_offset(bi, hi, ki, heads, byte_offs.kv_heads, seq_k, head_dim, k_stride, bhsd);
        // AIR/AGX opt: vectorized float4 loads (4× fewer load ops) + explicit
        // fma (1 fused instr, single-rounding ⇒ *more* accurate than mul+add).
        // float4 needs 16-byte-aligned bases; head_dim/hs are multiples of 4 for
        // real head dims, so the fast path applies. Scalar+fma fallback otherwise.
        if ((head_dim & 3u) == 0u) {
            device const float4* Q4 = (device const float4*)(Q + q_base);
            device const float4* K4 = (device const float4*)(K + k_base);
            for (uint d4 = 0; d4 < (head_dim >> 2); ++d4) {
                float4 qv = Q4[d4], kv = K4[d4];
                dot = fma(qv.x, kv.x, fma(qv.y, kv.y, fma(qv.z, kv.z, fma(qv.w, kv.w, dot))));
            }
        } else {
            for (uint d = 0; d < head_dim; ++d) {
                dot = fma(Q[q_base + d], K[k_base + d], dot);
            }
        }
        float s = dot * scale;
        if (softcap_inv > 0.0f) {
            s = precise::tanh(s * softcap_inv) * attn_softcap;
        }
        if (mask_kind == 1u) {
            if (ki > q_offset + qi) s = -65504.0f;
        } else if (mask_kind == 2u) {
            if (M[bi * k_stride + ki] < 0.5) s = -65504.0f;
        } else if (mask_kind == 4u) {
            uint abs_q = q_offset + qi;
            uint lo = abs_q > window ? abs_q - window : 0u;
            if (ki < lo || ki > abs_q) s = -65504.0f;
        }
        scores[qi * seq_k + ki] = half(s);
    }
    threadgroup_barrier(mem_flags::mem_threadgroup);

    for (uint qi = 0; qi < seq_q; ++qi) {
        float lmax = -1e30;
        for (uint ki = tid; ki < seq_k; ki += tsize) {
            lmax = max(lmax, float(scores[qi * seq_k + ki]));
        }
        float mx = simd_max(lmax);
        float lsum = 0.0;
        for (uint ki = tid; ki < seq_k; ki += tsize) {
            float e = precise::exp(float(scores[qi * seq_k + ki]) - mx);
            scores[qi * seq_k + ki] = half(e);
            lsum += e;
        }
        float inv = 1.0f / simd_sum(lsum);
        for (uint ki = tid; ki < seq_k; ki += tsize) {
            scores[qi * seq_k + ki] = half(float(scores[qi * seq_k + ki]) * inv);
        }
    }
    threadgroup_barrier(mem_flags::mem_threadgroup);

    uint vdh = (byte_offs.v_head_dim == 0u) ? head_dim : byte_offs.v_head_dim;
    uint out_total = seq_q * vdh;
    for (uint idx = tid; idx < out_total; idx += tsize) {
        uint qi = idx / vdh, d = idx % vdh;
        float acc = 0.0;
        for (uint ki = 0; ki < seq_k; ++ki) {
            uint v_base = qkv_v_offset(bi, hi, ki, heads, byte_offs.kv_heads, seq_k, vdh, k_stride, bhsd);
            acc += float(scores[qi * seq_k + ki]) * V[v_base + d];
        }
        OUT[qkv_out_offset(bi, hi, qi, heads, seq_q, vdh, q_stride, bhsd) + d] = acc;
    }
}

// Online-softmax SDPA (FlashAttention v1 inner-row form). Same algorithm
// as `wgpu/src/kernels/attention.wgsl` and `cpu/src/thunk.rs` Attention.
// One thread per (batch, head, q_row); each thread walks the K dimension
// exactly once, maintaining a running (m, l, O[D]) tuple — no scores
// matrix in threadgroup memory, so it scales to arbitrary seq length.
//
// The plain `sdpa` kernel above uses `threadgroup float scores[64*64]`;
// for vision (seq=257) that overflows. This kernel handles seq > 64.
//
// Mask layout (vision constant all-ones is `[batch, seq_stride]`):
// reads M[bi * seq_stride + ki] just like `sdpa`.
//
// MAX_HEAD_DIM = 128 covers BERT/Nomic/Vision (head_dim ≤ 128); larger
// head dims would need a per-thread spill buffer.
kernel void sdpa_long(
    device const float* arena_q   [[buffer(0)]],
    device const float* arena_k   [[buffer(1)]],
    device const float* arena_v   [[buffer(2)]],
    device const float* arena_m   [[buffer(3)]],
    device float*       arena_o   [[buffer(4)]],
    constant uint& batch       [[buffer(5)]],
    constant uint& seq_q       [[buffer(6)]],   // query length Lq
    constant uint& heads       [[buffer(7)]],
    constant uint& head_dim    [[buffer(8)]],
    constant uint& q_stride    [[buffer(9)]],   // per-batch Q row stride (= Lq for dense)
    constant uint& mask_kind   [[buffer(10)]],
    constant uint& seq_k       [[buffer(11)]],  // key/value length Lk
    constant uint& k_stride    [[buffer(12)]],  // per-batch K/V row stride (= Lk for dense)
    constant uint& bhsd        [[buffer(13)]],  // 1 = [B,H,S,D]
    constant uint& window      [[buffer(14)]],  // SlidingWindow lookback (0 otherwise)
    constant float& score_scale  [[buffer(15)]],
    constant float& attn_softcap [[buffer(16)]],
    // Task #50: > 4 GB activations need offsets in inline constants.
    constant SdpaOffsets& byte_offs [[buffer(17)]],
    uint tid_x [[thread_position_in_grid]]
) {
    device const float* Q = (device const float*)((device const char*)arena_q + byte_offs.q);
    device const float* K = (device const float*)((device const char*)arena_k + byte_offs.k);
    device const float* V = (device const float*)((device const char*)arena_v + byte_offs.v);
    device const float* M = (device const float*)((device const char*)arena_m + byte_offs.m);
    device float* OUT     = (device float*)((device char*)arena_o + byte_offs.o);
    // mask_kind:
    //   0 = None
    //   1 = Causal           (prefill — Lq == Lk required)
    //   2 = Custom            (binary key-padding mask M[B, Lk])
    //   3 = Bias              (additive per-head bias M[B, H, Lq, Lk])
    //   4 = SlidingWindow     (visible range [abs_q - window, abs_q])
    //
    // Gemma 4 12B's SWA layers use head_dim=256 and the FULL layers use
    // head_dim=512. The previous 128 cap silently overflowed q_reg/o_acc
    // and produced all-NaN logits when decode picked this kernel.
    constexpr uint MAX_HEAD_DIM = 512u;
    uint total = batch * heads * seq_q;
    if (tid_x >= total) return;

    uint qi = tid_x % seq_q;
    uint bh = tid_x / seq_q;
    uint hi = bh % heads;
    uint bi = bh / heads;

    // Precise scale; honour caller's `score_scale` (Gemma 4 = 1.0).
    float scale = (score_scale > 0.0f) ? score_scale : rsqrt(float(head_dim));
    float softcap_inv = (attn_softcap > 0.0f) ? (1.0f / attn_softcap) : 0.0f;

    // V/output per-head width (asymmetric MLA; == head_dim for symmetric).
    uint vdh = (byte_offs.v_head_dim == 0u) ? head_dim : byte_offs.v_head_dim;

    // Cache Q[qi, hi*dh : (hi+1)*dh] in registers — read seq_k times below.
    float q_reg[MAX_HEAD_DIM];
    uint q_base = qkv_q_offset(bi, hi, qi, heads, seq_q, head_dim, q_stride, bhsd);
    for (uint d = 0; d < head_dim; ++d) q_reg[d] = Q[q_base + d];

    // Bias base offset (only read when mask_kind == 3).
    uint bias_row_base = ((bi * heads + hi) * seq_q + qi) * seq_k;
    uint q_offset = seq_k - seq_q;

    // Online softmax accumulators. O is v_head_dim-wide.
    float m_acc = -1e30;
    float l_acc = 0.0;
    float o_acc[MAX_HEAD_DIM];
    for (uint d = 0; d < vdh; ++d) o_acc[d] = 0.0;

    for (uint ki = 0; ki < seq_k; ++ki) {
        // Causal early-exit: keys ki > q_offset+qi are fully masked and contribute
        // EXACTLY zero to the online softmax (m unchanged, e_cur = exp(-1e9-m) = 0,
        // l/o += 0). Since ki increases monotonically, once past the diagonal every
        // remaining key is masked — stop. Bit-identical; ~2× fewer iterations for
        // causal prefill. Only fires for mask_kind==1 (non-causal graphs — vision
        // seq=257, BERT, cross-attn — keep the full loop).
        if (mask_kind == 1u && ki > q_offset + qi) break;
        // Score: scale * (Q · K[ki]) + mask
        uint k_base = qkv_kv_offset(bi, hi, ki, heads, byte_offs.kv_heads, seq_k, head_dim, k_stride, bhsd);
        float dot = 0.0;
        for (uint d = 0; d < head_dim; ++d) dot += q_reg[d] * K[k_base + d];
        float s = dot * scale;
        if (softcap_inv > 0.0f) {
            s = precise::tanh(s * softcap_inv) * attn_softcap;
        }
        if (mask_kind == 1u) {
            if (ki > q_offset + qi) s = -1e9;
        } else if (mask_kind == 2u) {
            if (M[bi * k_stride + ki] < 0.5) s = -1e9;
        } else if (mask_kind == 3u) {
            s += M[bias_row_base + ki];
        } else if (mask_kind == 4u) {
            uint abs_q = q_offset + qi;
            uint lo = abs_q > window ? abs_q - window : 0u;
            if (ki < lo || ki > abs_q) s = -1e9;
        }

        // Online softmax update with precise exp.
        float m_new = max(m_acc, s);
        float e_old = precise::exp(m_acc - m_new);
        float e_cur = precise::exp(s - m_new);
        l_acc = e_old * l_acc + e_cur;
        uint v_base = qkv_v_offset(bi, hi, ki, heads, byte_offs.kv_heads, seq_k, vdh, k_stride, bhsd);
        for (uint d = 0; d < vdh; ++d) {
            o_acc[d] = e_old * o_acc[d] + e_cur * V[v_base + d];
        }
        m_acc = m_new;
    }

    // Normalize and emit (v_head_dim-wide output).
    float inv_l = 1.0 / l_acc;
    uint o_base = qkv_out_offset(bi, hi, qi, heads, seq_q, vdh, q_stride, bhsd);
    for (uint d = 0; d < vdh; ++d) {
        OUT[o_base + d] = o_acc[d] * inv_l;
    }
}

// ── Occupancy-isolation probe (benchmark only) ──────────────────────────────
// Byte-for-byte identical WORK to `sdpa_long` (same memory traffic, same
// instruction stream) but declares a 20 KB dummy threadgroup array — matching
// sdpa_fa2/attn_bwd_fused's tgMem footprint — so the compiler's
// staticThreadgroupMemoryLength (hence concurrent-threadgroups-per-core /
// occupancy) drops to their level while everything else is held constant. If
// this runs ~2-3× slower than `sdpa_long`, threadgroup-memory-limited occupancy
// is proven the cause of the flash-kernel slowdowns, independent of cache/roofline.
kernel void sdpa_long_occpad(
    device const float* arena_q   [[buffer(0)]],
    device const float* arena_k   [[buffer(1)]],
    device const float* arena_v   [[buffer(2)]],
    device const float* arena_m   [[buffer(3)]],
    device float*       arena_o   [[buffer(4)]],
    constant uint& batch       [[buffer(5)]],
    constant uint& seq_q       [[buffer(6)]],
    constant uint& heads       [[buffer(7)]],
    constant uint& head_dim    [[buffer(8)]],
    constant uint& q_stride    [[buffer(9)]],
    constant uint& mask_kind   [[buffer(10)]],
    constant uint& seq_k       [[buffer(11)]],
    constant uint& k_stride    [[buffer(12)]],
    constant uint& bhsd        [[buffer(13)]],
    constant uint& window      [[buffer(14)]],
    constant float& score_scale  [[buffer(15)]],
    constant float& attn_softcap [[buffer(16)]],
    constant SdpaOffsets& byte_offs [[buffer(17)]],
    uint tid_x [[thread_position_in_grid]],
    uint tlid  [[thread_index_in_threadgroup]]
) {
    // 20 KB of threadgroup memory — declared (so it counts against occupancy)
    // and lightly touched (so the compiler can't eliminate it), but it plays no
    // role in the math below.
    threadgroup float occ_pad[5120];
    device const float* Q = (device const float*)((device const char*)arena_q + byte_offs.q);
    device const float* K = (device const float*)((device const char*)arena_k + byte_offs.k);
    device const float* V = (device const float*)((device const char*)arena_v + byte_offs.v);
    device const float* M = (device const float*)((device const char*)arena_m + byte_offs.m);
    device float* OUT     = (device float*)((device char*)arena_o + byte_offs.o);
    constexpr uint MAX_HEAD_DIM = 512u;
    uint total = batch * heads * seq_q;
    if (tid_x >= total) return;
    uint qi = tid_x % seq_q;
    uint bh = tid_x / seq_q;
    uint hi = bh % heads;
    uint bi = bh / heads;
    float scale = (score_scale > 0.0f) ? score_scale : rsqrt(float(head_dim));
    float softcap_inv = (attn_softcap > 0.0f) ? (1.0f / attn_softcap) : 0.0f;
    uint vdh = (byte_offs.v_head_dim == 0u) ? head_dim : byte_offs.v_head_dim;
    occ_pad[tlid] = scale;
    threadgroup_barrier(mem_flags::mem_threadgroup);
    float q_reg[MAX_HEAD_DIM];
    uint q_base = qkv_q_offset(bi, hi, qi, heads, seq_q, head_dim, q_stride, bhsd);
    for (uint d = 0; d < head_dim; ++d) q_reg[d] = Q[q_base + d];
    uint bias_row_base = ((bi * heads + hi) * seq_q + qi) * seq_k;
    uint q_offset = seq_k - seq_q;
    float m_acc = -1e30;
    float l_acc = 0.0;
    float o_acc[MAX_HEAD_DIM];
    for (uint d = 0; d < vdh; ++d) o_acc[d] = 0.0;
    for (uint ki = 0; ki < seq_k; ++ki) {
        if (mask_kind == 1u && ki > q_offset + qi) break;
        uint k_base = qkv_kv_offset(bi, hi, ki, heads, byte_offs.kv_heads, seq_k, head_dim, k_stride, bhsd);
        float dot = 0.0;
        for (uint d = 0; d < head_dim; ++d) dot += q_reg[d] * K[k_base + d];
        float s = dot * scale;
        if (softcap_inv > 0.0f) s = precise::tanh(s * softcap_inv) * attn_softcap;
        if (mask_kind == 1u) { if (ki > q_offset + qi) s = -1e9; }
        else if (mask_kind == 2u) { if (M[bi * k_stride + ki] < 0.5) s = -1e9; }
        else if (mask_kind == 3u) { s += M[bias_row_base + ki]; }
        else if (mask_kind == 4u) { uint abs_q = q_offset + qi; uint lo = abs_q > window ? abs_q - window : 0u; if (ki < lo || ki > abs_q) s = -1e9; }
        float m_new = max(m_acc, s);
        float e_old = precise::exp(m_acc - m_new);
        float e_cur = precise::exp(s - m_new);
        l_acc = e_old * l_acc + e_cur;
        uint v_base = qkv_v_offset(bi, hi, ki, heads, byte_offs.kv_heads, seq_k, vdh, k_stride, bhsd);
        for (uint d = 0; d < vdh; ++d) o_acc[d] = e_old * o_acc[d] + e_cur * V[v_base + d];
        m_acc = m_new;
    }
    float inv_l = 1.0 / l_acc;
    uint o_base = qkv_out_offset(bi, hi, qi, heads, seq_q, vdh, q_stride, bhsd);
    // The dummy tgMem feeds a never-true branch so it can't be optimized away.
    float pad_guard = occ_pad[tlid];
    for (uint d = 0; d < vdh; ++d) {
        float o = o_acc[d] * inv_l;
        if (pad_guard > 1e38f) o += occ_pad[(tlid + d) % 5120];
        OUT[o_base + d] = o;
    }
}

// Decode-step SDPA fast path (Lq == 1, Lk arbitrary).
//
// One threadgroup per (batch, head); threads split the K axis and merge
// online-softmax states. The old 1-thread-per-head launch left Apple GPUs
// nearly idle on GQA decode (Zonos: batch=2 × 16 heads = 32 threads).
// @@RLX_SDPA_DECODE_M1@@

// Flash-attention tile kernel with optional additive bias mask.
//
// Targets the SAM3 detector decoder image cross-attention where the
// scalar `sdpa_long` is bandwidth-bound (each query thread re-reads K
// and V for all 5184 positions). This kernel processes Br=8 query
// rows per threadgroup with K, V, and bias tiles loaded cooperatively
// into threadgroup memory — each K/V/bias element is read once per
// row tile instead of once per query.
//
// Layout matches `sdpa_long`: Q/K/V are [B, Lq_or_Lk, heads*head_dim],
// bias is [B, H, Lq, Lk]. head_dim is dynamic but capped at 128 for
// the per-thread output accumulator.
kernel void sdpa_fa_f32(
    device const float* Q   [[buffer(0)]],
    device const float* K   [[buffer(1)]],
    device const float* V   [[buffer(2)]],
    device const float* M   [[buffer(3)]],
    device float* OUT       [[buffer(4)]],
    constant uint& batch       [[buffer(5)]],
    constant uint& seq_q       [[buffer(6)]],
    constant uint& heads       [[buffer(7)]],
    constant uint& head_dim    [[buffer(8)]],
    constant uint& q_stride    [[buffer(9)]],
    constant uint& mask_kind   [[buffer(10)]],
    constant uint& seq_k       [[buffer(11)]],
    constant uint& k_stride    [[buffer(12)]],
    constant uint& bhsd        [[buffer(13)]],
    constant uint& window      [[buffer(14)]],  // reserved; not yet wired in FA tile path
    uint3 tgid [[threadgroup_position_in_grid]],
    uint tid_in_tg [[thread_index_in_threadgroup]]
) {
    (void)window;  // SlidingWindow falls through to sdpa_long today
    // Tile sizes — tuned for SAM3 image CA (dh=16) but kernel is
    // generic. With Br=8, Bc=64, the per-TG threadgroup memory is
    // 8*128 (Q) + 64*128 (K) + 64*128 (V) + 8*64 (S/M) ≈ 71KB at
    // dh=128; well under the 32–64KB per-TG hard limit at dh=16
    // (where it's ~10KB).
    // Tile sizes — the threadgroup-memory cap on Apple7/8 (32KB) and
    // Apple9 (64KB) bounds `MAX_DH`. At MAX_DH=32 we use ~20KB,
    // leaving headroom for larger Bc later. dh up to 32 covers SAM
    // family models (dh=16) and DETR-style detectors. Larger dh
    // (LLM 64–128) falls back to scalar sdpa_long via the dispatch
    // guard in `encode_sdpa`.
    constexpr uint Br = 8u;
    constexpr uint Bc = 64u;
    constexpr uint MAX_DH = 32u;
    constexpr uint THREADS = 64u;

    threadgroup float Q_tg[Br * MAX_DH];     // 1 KB
    threadgroup float K_tg[Bc * MAX_DH];     // 8 KB
    threadgroup float V_tg[Bc * MAX_DH];     // 8 KB
    threadgroup float S_tg[Br * Bc];         // 2 KB

    // Per-row online softmax state.
    threadgroup float m_row[Br];
    threadgroup float l_row[Br];
    threadgroup float o_row[Br * MAX_DH];    // 1 KB

    uint q_tile = tgid.x;          // index over Lq / Br
    uint hi     = tgid.y;          // head
    uint bi     = tgid.z;          // batch
    uint q_start = q_tile * Br;

    float scale = rsqrt(float(head_dim));

    // ── Load Q tile cooperatively ────────────────────────────────────
    for (uint i = tid_in_tg; i < Br * head_dim; i += THREADS) {
        uint qi = i / head_dim;
        uint di = i % head_dim;
        uint pos = q_start + qi;
        Q_tg[qi * MAX_DH + di] = (pos < seq_q)
            ? Q[qkv_q_offset(bi, hi, pos, heads, seq_q, head_dim, q_stride, bhsd) + di]
            : 0.0f;
    }

    // Initialize per-row state.
    if (tid_in_tg < Br) {
        m_row[tid_in_tg] = -1e30f;
        l_row[tid_in_tg] = 0.0f;
    }
    for (uint i = tid_in_tg; i < Br * head_dim; i += THREADS) {
        o_row[(i / head_dim) * MAX_DH + (i % head_dim)] = 0.0f;
    }
    threadgroup_barrier(mem_flags::mem_threadgroup);

    // ── Iterate K/V tiles ─────────────────────────────────────────────
    uint bias_row_base = (bi * heads + hi) * seq_q * seq_k;

    for (uint kt = 0; kt < seq_k; kt += Bc) {
        // Load K and V tiles (Bc * head_dim elements each).
        for (uint i = tid_in_tg; i < Bc * head_dim; i += THREADS) {
            uint ki = i / head_dim;
            uint di = i % head_dim;
            uint pos = kt + ki;
            uint kv_off = qkv_kv_offset(bi, hi, pos, heads, heads, seq_k, head_dim, k_stride, bhsd);
            bool in_range = pos < seq_k;
            K_tg[ki * MAX_DH + di] = in_range ? K[kv_off + di] : 0.0f;
            V_tg[ki * MAX_DH + di] = in_range ? V[kv_off + di] : 0.0f;
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);

        // Compute scores S[Br, Bc] = Q_tg @ K_tg^T, scaled, +bias, +pad-mask.
        // Each thread covers Br*Bc/THREADS = 8*64/64 = 8 cells.
        for (uint c = tid_in_tg; c < Br * Bc; c += THREADS) {
            uint qi = c / Bc;
            uint ki = c % Bc;
            uint pos = kt + ki;
            bool valid = (q_start + qi) < seq_q && pos < seq_k;
            float s = 0.0f;
            if (valid) {
                for (uint di = 0; di < head_dim; ++di) {
                    s += Q_tg[qi * MAX_DH + di] * K_tg[ki * MAX_DH + di];
                }
                s *= scale;
                if (mask_kind == 1u) {
                    uint q_offset = seq_k - seq_q;
                    if (pos > q_offset + q_start + qi) s = -1e9f;
                } else if (mask_kind == 2u) {
                    if (M[bi * k_stride + pos] < 0.5f) s = -1e9f;
                } else if (mask_kind == 3u) {
                    s += M[bias_row_base + (q_start + qi) * seq_k + pos];
                }
            } else {
                s = -1e9f;
            }
            S_tg[c] = s;
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);

        // Online softmax update — one thread per row (Br threads).
        if (tid_in_tg < Br) {
            uint qi = tid_in_tg;
            float m_new = m_row[qi];
            for (uint ki = 0; ki < Bc; ++ki) {
                m_new = max(m_new, S_tg[qi * Bc + ki]);
            }
            float e_old = exp(m_row[qi] - m_new);
            float l_new = e_old * l_row[qi];
            for (uint ki = 0; ki < Bc; ++ki) {
                float p = exp(S_tg[qi * Bc + ki] - m_new);
                S_tg[qi * Bc + ki] = p;
                l_new += p;
            }
            // O ← e_old * O + P @ V
            for (uint di = 0; di < head_dim; ++di) {
                float o = o_row[qi * MAX_DH + di] * e_old;
                for (uint ki = 0; ki < Bc; ++ki) {
                    o += S_tg[qi * Bc + ki] * V_tg[ki * MAX_DH + di];
                }
                o_row[qi * MAX_DH + di] = o;
            }
            m_row[qi] = m_new;
            l_row[qi] = l_new;
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }

    // ── Normalize + emit ─────────────────────────────────────────────
    for (uint i = tid_in_tg; i < Br * head_dim; i += THREADS) {
        uint qi = i / head_dim;
        uint di = i % head_dim;
        uint pos = q_start + qi;
        if (pos < seq_q) {
            float o = o_row[qi * MAX_DH + di] / l_row[qi];
            OUT[qkv_q_offset(bi, hi, pos, heads, seq_q, head_dim, q_stride, bhsd) + di] = o;
        }
    }
}

// ── Variant 1: split-K SIMD prefill SDPA ────────────────────────────────────
// One SIMD group (32 threads) per (batch, head, query-row). The 32 lanes split
// the key axis (ki = lane, lane+32, …), each maintaining a partial online-softmax
// state (m,l,O) over its strided keys, then merge across lanes via simd_max /
// simd_sum. This parallelizes `sdpa_long`'s serial K walk 32-way while keeping
// the same O(D)-register / no-scores-matrix footprint (scales to any seq).
// head_dim, v_head_dim ≤ 128 (per-lane register accumulators). MUST be launched
// with exactly 32 threads/threadgroup (== one Apple SIMD group).
kernel void sdpa_splitk(
    device const float* arena_q   [[buffer(0)]],
    device const float* arena_k   [[buffer(1)]],
    device const float* arena_v   [[buffer(2)]],
    device const float* arena_m   [[buffer(3)]],
    device float*       arena_o   [[buffer(4)]],
    constant uint& batch       [[buffer(5)]],
    constant uint& seq_q       [[buffer(6)]],
    constant uint& heads       [[buffer(7)]],
    constant uint& head_dim    [[buffer(8)]],
    constant uint& q_stride    [[buffer(9)]],
    constant uint& mask_kind   [[buffer(10)]],
    constant uint& seq_k       [[buffer(11)]],
    constant uint& k_stride    [[buffer(12)]],
    constant uint& bhsd        [[buffer(13)]],
    constant uint& window      [[buffer(14)]],
    constant float& score_scale  [[buffer(15)]],
    constant float& attn_softcap [[buffer(16)]],
    constant SdpaOffsets& byte_offs [[buffer(17)]],
    uint tgid_x [[threadgroup_position_in_grid]],
    uint lane   [[thread_index_in_threadgroup]]
) {
    device const float* Q = (device const float*)((device const char*)arena_q + byte_offs.q);
    device const float* K = (device const float*)((device const char*)arena_k + byte_offs.k);
    device const float* V = (device const float*)((device const char*)arena_v + byte_offs.v);
    device const float* M = (device const float*)((device const char*)arena_m + byte_offs.m);
    device float* OUT     = (device float*)((device char*)arena_o + byte_offs.o);

    constexpr uint MAX_DH = 128u;
    constexpr uint LANES = 32u;

    uint total_rows = batch * heads * seq_q;
    if (tgid_x >= total_rows) return;
    uint qi = tgid_x % seq_q;
    uint bh = tgid_x / seq_q;
    uint hi = bh % heads;
    uint bi = bh / heads;

    float scale = (score_scale > 0.0f) ? score_scale : rsqrt(float(head_dim));
    float softcap_inv = (attn_softcap > 0.0f) ? (1.0f / attn_softcap) : 0.0f;
    uint vdh = (byte_offs.v_head_dim == 0u) ? head_dim : byte_offs.v_head_dim;
    uint q_offset = seq_k - seq_q;

    // Q row into per-lane registers (small; head_dim ≤ 128).
    float q_reg[MAX_DH];
    uint q_base = qkv_q_offset(bi, hi, qi, heads, seq_q, head_dim, q_stride, bhsd);
    for (uint d = 0; d < head_dim; ++d) q_reg[d] = Q[q_base + d];

    // Per-lane online softmax over strided keys.
    float m_i = -1e30f;
    float l_i = 0.0f;
    float o_i[MAX_DH];
    for (uint d = 0; d < vdh; ++d) o_i[d] = 0.0f;
    uint bias_row_base = ((bi * heads + hi) * seq_q + qi) * seq_k;

    for (uint ki = lane; ki < seq_k; ki += LANES) {
        uint k_base = qkv_kv_offset(bi, hi, ki, heads, byte_offs.kv_heads, seq_k, head_dim, k_stride, bhsd);
        float dot = 0.0f;
        for (uint d = 0; d < head_dim; ++d) dot = fma(q_reg[d], K[k_base + d], dot);
        float s = dot * scale;
        if (softcap_inv > 0.0f) s = precise::tanh(s * softcap_inv) * attn_softcap;
        bool masked = false;
        if (mask_kind == 1u) {
            if (ki > q_offset + qi) masked = true;
        } else if (mask_kind == 2u) {
            if (M[bi * k_stride + ki] < 0.5f) masked = true;
        } else if (mask_kind == 3u) {
            s += M[bias_row_base + ki];
        } else if (mask_kind == 4u) {
            uint abs_q = q_offset + qi;
            uint lo = abs_q > window ? abs_q - window : 0u;
            if (ki < lo || ki > abs_q) masked = true;
        }
        if (masked) continue;
        float m_new = max(m_i, s);
        float e_old = precise::exp(m_i - m_new);
        float e_cur = precise::exp(s - m_new);
        l_i = e_old * l_i + e_cur;
        uint v_base = qkv_v_offset(bi, hi, ki, heads, byte_offs.kv_heads, seq_k, vdh, k_stride, bhsd);
        for (uint d = 0; d < vdh; ++d) o_i[d] = e_old * o_i[d] + e_cur * V[v_base + d];
        m_i = m_new;
    }

    // Merge the 32 partial states across the SIMD group. Empty lanes keep
    // m_i = -1e30 ⇒ scale_i = exp(-inf) = 0, so they contribute nothing.
    float m_g = simd_max(m_i);
    float scale_i = precise::exp(m_i - m_g);
    float l_g = simd_sum(l_i * scale_i);
    float inv_l = (l_g > 0.0f) ? (1.0f / l_g) : 0.0f;
    uint o_base = qkv_out_offset(bi, hi, qi, heads, seq_q, vdh, q_stride, bhsd);
    for (uint d = 0; d < vdh; ++d) {
        float o_d = simd_sum(o_i[d] * scale_i);
        if (lane == 0u) OUT[o_base + d] = o_d * inv_l;
    }
}

// ── Variant 2: improved flash-attention tile (full thread utilization) ───────
// Rewrite of `sdpa_fa_f32`: Br=8 query rows per threadgroup with K/V loaded
// cooperatively into threadgroup memory (each K/V element read once per row-tile,
// not once per query), BUT — unlike sdpa_fa_f32, whose softmax + O update ran on
// only Br of the 64 threads — the score matmul AND the O=P·V update both run
// across all 64 threads. Only the tiny per-row softmax-stat reduction is Br-wide.
// Uses proper SdpaOffsets byte offsets (sdpa_fa_f32 ignored them). head_dim,
// v_head_dim ≤ 64 (MAX_DH); larger falls back to sdpa_long via the dispatch gate.
kernel void sdpa_fa2(
    device const float* arena_q   [[buffer(0)]],
    device const float* arena_k   [[buffer(1)]],
    device const float* arena_v   [[buffer(2)]],
    device const float* arena_m   [[buffer(3)]],
    device float*       arena_o   [[buffer(4)]],
    constant uint& batch       [[buffer(5)]],
    constant uint& seq_q       [[buffer(6)]],
    constant uint& heads       [[buffer(7)]],
    constant uint& head_dim    [[buffer(8)]],
    constant uint& q_stride    [[buffer(9)]],
    constant uint& mask_kind   [[buffer(10)]],
    constant uint& seq_k       [[buffer(11)]],
    constant uint& k_stride    [[buffer(12)]],
    constant uint& bhsd        [[buffer(13)]],
    constant uint& window      [[buffer(14)]],
    constant float& score_scale  [[buffer(15)]],
    constant float& attn_softcap [[buffer(16)]],
    constant SdpaOffsets& byte_offs [[buffer(17)]],
    uint3 tgid [[threadgroup_position_in_grid]],
    uint tid   [[thread_index_in_threadgroup]]
) {
    device const float* Q = (device const float*)((device const char*)arena_q + byte_offs.q);
    device const float* K = (device const float*)((device const char*)arena_k + byte_offs.k);
    device const float* V = (device const float*)((device const char*)arena_v + byte_offs.v);
    device const float* M = (device const float*)((device const char*)arena_m + byte_offs.m);
    device float* OUT     = (device float*)((device char*)arena_o + byte_offs.o);

    constexpr uint Br = 8u;
    constexpr uint Bc = 32u;
    constexpr uint MAX_DH = 64u;
    constexpr uint THREADS = 64u;

    threadgroup float Q_tg[Br * MAX_DH];
    threadgroup float K_tg[Bc * MAX_DH];
    threadgroup float V_tg[Bc * MAX_DH];
    threadgroup float S_tg[Br * Bc];
    threadgroup float O_tg[Br * MAX_DH];
    threadgroup float m_row[Br];
    threadgroup float l_row[Br];
    threadgroup float eold_row[Br];

    uint q_tile = tgid.x;
    uint hi     = tgid.y;
    uint bi     = tgid.z;
    uint q_start = q_tile * Br;

    float scale = (score_scale > 0.0f) ? score_scale : rsqrt(float(head_dim));
    float softcap_inv = (attn_softcap > 0.0f) ? (1.0f / attn_softcap) : 0.0f;
    uint vdh = (byte_offs.v_head_dim == 0u) ? head_dim : byte_offs.v_head_dim;
    uint q_offset = seq_k - seq_q;

    // Load Q tile cooperatively.
    for (uint i = tid; i < Br * head_dim; i += THREADS) {
        uint qi = i / head_dim, di = i % head_dim;
        uint pos = q_start + qi;
        Q_tg[qi * MAX_DH + di] = (pos < seq_q)
            ? Q[qkv_q_offset(bi, hi, pos, heads, seq_q, head_dim, q_stride, bhsd) + di]
            : 0.0f;
    }
    if (tid < Br) { m_row[tid] = -1e30f; l_row[tid] = 0.0f; }
    for (uint i = tid; i < Br * MAX_DH; i += THREADS) O_tg[i] = 0.0f;
    threadgroup_barrier(mem_flags::mem_threadgroup);

    for (uint kt = 0; kt < seq_k; kt += Bc) {
        // Load K and V tiles cooperatively.
        for (uint i = tid; i < Bc * head_dim; i += THREADS) {
            uint ki = i / head_dim, di = i % head_dim;
            uint pos = kt + ki;
            K_tg[ki * MAX_DH + di] = (pos < seq_k)
                ? K[qkv_kv_offset(bi, hi, pos, heads, byte_offs.kv_heads, seq_k, head_dim, k_stride, bhsd) + di]
                : 0.0f;
        }
        for (uint i = tid; i < Bc * vdh; i += THREADS) {
            uint ki = i / vdh, di = i % vdh;
            uint pos = kt + ki;
            V_tg[ki * MAX_DH + di] = (pos < seq_k)
                ? V[qkv_v_offset(bi, hi, pos, heads, byte_offs.kv_heads, seq_k, vdh, k_stride, bhsd) + di]
                : 0.0f;
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);

        // Scores S[Br,Bc] = scale·(Q·Kᵀ) + mask — all 64 threads (Br·Bc/64 cells each).
        for (uint c = tid; c < Br * Bc; c += THREADS) {
            uint qi = c / Bc, ki = c % Bc;
            uint pos = kt + ki;
            bool valid = (q_start + qi) < seq_q && pos < seq_k;
            float s = -1e30f;
            if (valid) {
                float acc = 0.0f;
                for (uint di = 0; di < head_dim; ++di)
                    acc = fma(Q_tg[qi * MAX_DH + di], K_tg[ki * MAX_DH + di], acc);
                s = acc * scale;
                if (softcap_inv > 0.0f) s = precise::tanh(s * softcap_inv) * attn_softcap;
                uint abs_q = q_offset + q_start + qi;
                if (mask_kind == 1u) {
                    if (pos > abs_q) s = -1e30f;
                } else if (mask_kind == 2u) {
                    if (M[bi * k_stride + pos] < 0.5f) s = -1e30f;
                } else if (mask_kind == 3u) {
                    s += M[((bi * heads + hi) * seq_q + (q_start + qi)) * seq_k + pos];
                } else if (mask_kind == 4u) {
                    uint lo = abs_q > window ? abs_q - window : 0u;
                    if (pos < lo || pos > abs_q) s = -1e30f;
                }
            }
            S_tg[c] = s;
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);

        // Per-row online-softmax stats (Br threads): update m,l; rewrite S←P.
        if (tid < Br) {
            uint qi = tid;
            float m_old = m_row[qi];
            float m_new = m_old;
            for (uint ki = 0; ki < Bc; ++ki) m_new = max(m_new, S_tg[qi * Bc + ki]);
            float e_old = precise::exp(m_old - m_new);
            float l_new = e_old * l_row[qi];
            for (uint ki = 0; ki < Bc; ++ki) {
                float p = precise::exp(S_tg[qi * Bc + ki] - m_new);
                S_tg[qi * Bc + ki] = p;
                l_new += p;
            }
            m_row[qi] = m_new;
            l_row[qi] = l_new;
            eold_row[qi] = e_old;
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);

        // O ← e_old·O + P·V — all 64 threads over (qi,di) = Br·vdh work items.
        for (uint c = tid; c < Br * vdh; c += THREADS) {
            uint qi = c / vdh, di = c % vdh;
            float o = O_tg[qi * MAX_DH + di] * eold_row[qi];
            for (uint ki = 0; ki < Bc; ++ki)
                o = fma(S_tg[qi * Bc + ki], V_tg[ki * MAX_DH + di], o);
            O_tg[qi * MAX_DH + di] = o;
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }

    // Normalize + emit.
    for (uint c = tid; c < Br * vdh; c += THREADS) {
        uint qi = c / vdh, di = c % vdh;
        uint pos = q_start + qi;
        if (pos < seq_q) {
            float inv = (l_row[qi] > 0.0f) ? (1.0f / l_row[qi]) : 0.0f;
            OUT[qkv_out_offset(bi, hi, pos, heads, seq_q, vdh, q_stride, bhsd) + di]
                = O_tg[qi * MAX_DH + di] * inv;
        }
    }
}

// ── Variant 3: simdgroup-matrix (float8x8 tensor-unit) flash attention ───────
// One SIMD group (32 threads) per (Br=8 query rows, head, batch). QK^T and P·V
// run on Apple's simdgroup_float8x8 matrix units (8×8×8 MMAs) instead of scalar
// dot loops; the online-softmax rescale of the O accumulator is kept in scalar
// threadgroup memory (simdgroup registers can't be row-scaled). Small Bc=8 blocks
// keep threadgroup memory ~5 KB (vs sdpa_fa2's ~21 KB) so occupancy stays high.
// Requires head_dim % 8 == 0 and head_dim, v_head_dim ≤ 64 (dispatch-gated).
kernel void sdpa_mma(
    device const float* arena_q   [[buffer(0)]],
    device const float* arena_k   [[buffer(1)]],
    device const float* arena_v   [[buffer(2)]],
    device const float* arena_m   [[buffer(3)]],
    device float*       arena_o   [[buffer(4)]],
    constant uint& batch       [[buffer(5)]],
    constant uint& seq_q       [[buffer(6)]],
    constant uint& heads       [[buffer(7)]],
    constant uint& head_dim    [[buffer(8)]],
    constant uint& q_stride    [[buffer(9)]],
    constant uint& mask_kind   [[buffer(10)]],
    constant uint& seq_k       [[buffer(11)]],
    constant uint& k_stride    [[buffer(12)]],
    constant uint& bhsd        [[buffer(13)]],
    constant uint& window      [[buffer(14)]],
    constant float& score_scale  [[buffer(15)]],
    constant float& attn_softcap [[buffer(16)]],
    constant SdpaOffsets& byte_offs [[buffer(17)]],
    uint3 tgid [[threadgroup_position_in_grid]],
    uint tid   [[thread_index_in_threadgroup]]
) {
    device const float* Q = (device const float*)((device const char*)arena_q + byte_offs.q);
    device const float* K = (device const float*)((device const char*)arena_k + byte_offs.k);
    device const float* V = (device const float*)((device const char*)arena_v + byte_offs.v);
    device const float* M = (device const float*)((device const char*)arena_m + byte_offs.m);
    device float* OUT     = (device float*)((device char*)arena_o + byte_offs.o);

    constexpr uint Br = 8u;
    constexpr uint Bc = 8u;
    constexpr uint MAX_DH = 64u;
    constexpr uint THREADS = 32u;

    threadgroup float Q_tg[Br * MAX_DH];
    threadgroup float K_tg[Bc * MAX_DH];
    threadgroup float V_tg[Bc * MAX_DH];
    threadgroup float S_tg[Br * Bc];
    threadgroup float PV_tg[Br * MAX_DH];
    threadgroup float O_tg[Br * MAX_DH];
    threadgroup float m_row[Br];
    threadgroup float l_row[Br];
    threadgroup float eold_row[Br];

    uint q_tile = tgid.x, hi = tgid.y, bi = tgid.z;
    uint q_start = q_tile * Br;
    float scale = (score_scale > 0.0f) ? score_scale : rsqrt(float(head_dim));
    float softcap_inv = (attn_softcap > 0.0f) ? (1.0f / attn_softcap) : 0.0f;
    uint vdh = (byte_offs.v_head_dim == 0u) ? head_dim : byte_offs.v_head_dim;
    uint q_offset = seq_k - seq_q;

    // Load Q tile (Br × head_dim), zero-pad rows past seq_q.
    for (uint i = tid; i < Br * head_dim; i += THREADS) {
        uint qi = i / head_dim, di = i % head_dim;
        uint pos = q_start + qi;
        Q_tg[qi * MAX_DH + di] = (pos < seq_q)
            ? Q[qkv_q_offset(bi, hi, pos, heads, seq_q, head_dim, q_stride, bhsd) + di]
            : 0.0f;
    }
    if (tid < Br) { m_row[tid] = -1e30f; l_row[tid] = 0.0f; }
    for (uint i = tid; i < Br * MAX_DH; i += THREADS) O_tg[i] = 0.0f;
    threadgroup_barrier(mem_flags::mem_threadgroup);

    uint dh_chunks = head_dim / 8u;   // exact (dispatch-gated to head_dim%8==0)
    uint vd_chunks = vdh / 8u;

    for (uint kt = 0; kt < seq_k; kt += Bc) {
        // Load K, V blocks (Bc × head_dim / vdh), zero-pad past seq_k.
        for (uint i = tid; i < Bc * head_dim; i += THREADS) {
            uint ki = i / head_dim, di = i % head_dim;
            uint pos = kt + ki;
            K_tg[ki * MAX_DH + di] = (pos < seq_k)
                ? K[qkv_kv_offset(bi, hi, pos, heads, byte_offs.kv_heads, seq_k, head_dim, k_stride, bhsd) + di]
                : 0.0f;
        }
        for (uint i = tid; i < Bc * vdh; i += THREADS) {
            uint ki = i / vdh, di = i % vdh;
            uint pos = kt + ki;
            V_tg[ki * MAX_DH + di] = (pos < seq_k)
                ? V[qkv_v_offset(bi, hi, pos, heads, byte_offs.kv_heads, seq_k, vdh, k_stride, bhsd) + di]
                : 0.0f;
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);

        // S[Br,Bc] = Q @ Kᵀ via simdgroup MMA (accumulate over head_dim chunks).
        simdgroup_float8x8 sacc = simdgroup_float8x8(0.0f);
        for (uint c = 0; c < dh_chunks; ++c) {
            simdgroup_float8x8 qmat, kmat;
            simdgroup_load(qmat, &Q_tg[c * 8u], MAX_DH);
            simdgroup_load(kmat, &K_tg[c * 8u], MAX_DH, ulong2(0, 0), /*transpose=*/true);
            simdgroup_multiply_accumulate(sacc, qmat, kmat, sacc);
        }
        simdgroup_store(sacc, &S_tg[0], Bc);
        threadgroup_barrier(mem_flags::mem_threadgroup);

        // Scale + mask + online-softmax stats (Br threads); rewrite S ← P.
        if (tid < Br) {
            uint qi = tid;
            uint abs_q = q_offset + q_start + qi;
            bool row_valid = (q_start + qi) < seq_q;
            float m_old = m_row[qi];
            float m_new = m_old;
            for (uint ki = 0; ki < Bc; ++ki) {
                uint pos = kt + ki;
                float s = S_tg[qi * Bc + ki] * scale;
                if (softcap_inv > 0.0f) s = precise::tanh(s * softcap_inv) * attn_softcap;
                bool ok = row_valid && pos < seq_k;
                if (ok) {
                    if (mask_kind == 1u) { if (pos > abs_q) ok = false; }
                    else if (mask_kind == 2u) { if (M[bi * k_stride + pos] < 0.5f) ok = false; }
                    else if (mask_kind == 3u) { s += M[((bi * heads + hi) * seq_q + (q_start + qi)) * seq_k + pos]; }
                    else if (mask_kind == 4u) { uint lo = abs_q > window ? abs_q - window : 0u; if (pos < lo || pos > abs_q) ok = false; }
                }
                if (!ok) s = -1e30f;
                S_tg[qi * Bc + ki] = s;
                m_new = max(m_new, s);
            }
            float e_old = precise::exp(m_old - m_new);
            float l_new = e_old * l_row[qi];
            for (uint ki = 0; ki < Bc; ++ki) {
                float p = precise::exp(S_tg[qi * Bc + ki] - m_new);
                S_tg[qi * Bc + ki] = p;
                l_new += p;
            }
            m_row[qi] = m_new;
            l_row[qi] = l_new;
            eold_row[qi] = e_old;
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);

        // PV[Br,vdh] = P @ V via simdgroup MMA (P is 8×8; V in 8-col chunks).
        for (uint dc = 0; dc < vd_chunks; ++dc) {
            simdgroup_float8x8 pmat, vmat;
            simdgroup_load(pmat, &S_tg[0], Bc);
            simdgroup_load(vmat, &V_tg[dc * 8u], MAX_DH);
            simdgroup_float8x8 pvacc = simdgroup_float8x8(0.0f);
            simdgroup_multiply_accumulate(pvacc, pmat, vmat, pvacc);
            simdgroup_store(pvacc, &PV_tg[dc * 8u], MAX_DH);
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);

        // O ← e_old·O + PV  (scalar rescale, all 32 threads).
        for (uint i = tid; i < Br * vdh; i += THREADS) {
            uint qi = i / vdh, di = i % vdh;
            O_tg[qi * MAX_DH + di] = eold_row[qi] * O_tg[qi * MAX_DH + di] + PV_tg[qi * MAX_DH + di];
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }

    // Normalize + emit.
    for (uint i = tid; i < Br * vdh; i += THREADS) {
        uint qi = i / vdh, di = i % vdh;
        uint pos = q_start + qi;
        if (pos < seq_q) {
            float inv = (l_row[qi] > 0.0f) ? (1.0f / l_row[qi]) : 0.0f;
            OUT[qkv_out_offset(bi, hi, pos, heads, seq_q, vdh, q_stride, bhsd) + di]
                = O_tg[qi * MAX_DH + di] * inv;
        }
    }
}

// RoPE: apply rotary position embeddings to one tensor (Q or K).
// x: [batch, seq, hidden], hidden = num_heads * head_dim
// cos/sin: [max_pos, head_dim/2]
// Out-of-place into out (or in-place via aliasing).
kernel void rope(
    device const float* x   [[buffer(0)]],
    device const float* cos [[buffer(1)]],
    device const float* sin [[buffer(2)]],
    device float* out       [[buffer(3)]],
    constant uint& batch          [[buffer(4)]],
    constant uint& seq            [[buffer(5)]],
    constant uint& hidden         [[buffer(6)]],
    constant uint& head_dim       [[buffer(7)]],
    constant uint& src_row_stride [[buffer(8)]],
    constant uint& seq_stride     [[buffer(9)]],
    constant uint& n_rot          [[buffer(10)]],
    constant uint& cos_per_token  [[buffer(11)]],
    constant uint& interleaved    [[buffer(12)]],
    uint3 gid [[thread_position_in_grid]]
) {
    // gid.x = dim index within head (0..head_dim)
    // gid.y = head index
    // gid.z = batch * seq + seq pos (linearized)
    uint rot_half = n_rot / 2;
    // The cos/sin table stores exactly the rotation angles — `n_rot/2` per
    // token, NOT head_dim/2. For PARTIAL rope (n_rot < head_dim) a head_dim/2
    // row stride overruns into the next token's angles from position 1 onward,
    // which is why this indexes by `rot_half`. Full rope has n_rot == head_dim,
    // so rot_half == head_dim/2 and nothing changes there.
    if (gid.x >= head_dim) return;

    uint bs = gid.z;
    uint bi = bs / seq;
    uint si = bs % seq;
    if (bi >= batch || si >= seq) return;

    uint nh = hidden / head_dim;
    uint hi = gid.y;
    if (hi >= nh) return;

    // RoPE table row: per-seq-position by default; per global (batch·seq)
    // token for ragged batched decode, where each sequence sits at its own
    // absolute position.
    uint cos_row = (cos_per_token != 0u) ? bs : si;

    // PLAN L1 — `seq_stride` is the compile-time full extent for buffer
    // offsets; `seq` is the (possibly scaled) iteration bound. This
    // separation lets active-extent dispatch shrink the loop without
    // corrupting per-batch strides.
    uint src_base = bi * seq_stride * src_row_stride + si * src_row_stride + hi * head_dim;
    uint dst_base = bi * seq_stride * hidden + si * hidden + hi * head_dim;
    uint d = gid.x;
    if (interleaved != 0u) {
        // GPT-J / llama.cpp-NORM: rotated pairs are adjacent (2d, 2d+1);
        // cos/sin indexed by freq d. GGUF Llama weights need this flavor.
        if (d < rot_half) {
            uint a = 2u * d;
            uint b = 2u * d + 1u;
            float x1 = x[src_base + a];
            float x2 = x[src_base + b];
            float c = cos[cos_row * rot_half + d];
            float s = sin[cos_row * rot_half + d];
            out[dst_base + a] = x1 * c - x2 * s;
            out[dst_base + b] = x2 * c + x1 * s;
        } else if (d >= n_rot) {
            out[dst_base + d] = x[src_base + d];
        }
    } else if (d < rot_half) {
        float x1 = x[src_base + d];
        float x2 = x[src_base + rot_half + d];
        float c = cos[cos_row * rot_half + d];
        float s = sin[cos_row * rot_half + d];
        out[dst_base + d] = x1 * c - x2 * s;
        out[dst_base + rot_half + d] = x2 * c + x1 * s;
    } else if (d >= n_rot) {
        out[dst_base + d] = x[src_base + d];
    }
}

// ArgMax / ArgMin along the middle axis of [outer, reduced, inner], emitting
// the winning index (f32). One thread per (outer, inner) output element. Strict
// comparison with first-best tie-break — matches rlx-cpu execute_argreduce_f32.
kernel void argreduce(
    device const float* src [[buffer(0)]],
    device float* out       [[buffer(1)]],
    constant uint& outer    [[buffer(2)]],
    constant uint& reduced  [[buffer(3)]],
    constant uint& inner    [[buffer(4)]],
    constant uint& is_max   [[buffer(5)]],
    uint gid [[thread_position_in_grid]]
) {
    uint total = outer * inner;
    if (gid >= total) return;
    uint o = gid / inner;
    uint i = gid % inner;
    uint base = o * reduced * inner + i;
    float best = src[base];
    uint best_idx = 0u;
    for (uint r = 1u; r < reduced; ++r) {
        float v = src[base + r * inner];
        bool better = (is_max != 0u) ? (v > best) : (v < best);
        if (better) { best = v; best_idx = r; }
    }
    out[o * inner + i] = float(best_idx);
}

// Cooperative last-axis ArgMax/ArgMin: one threadgroup reduces one `outer`
// row over the `reduced` axis (inner == 1 — the decode logits case, where the
// naive one-thread-per-output `argreduce` would loop a 128k-vocab row on a
// single GPU lane). Tie-break = lowest index wins, matching the strict `>`/`<`
// in rlx-cpu execute_argreduce_f32. Threadgroup size must be a power of two.
kernel void argreduce_lastaxis(
    device const float* src [[buffer(0)]],
    device float* out       [[buffer(1)]],
    constant uint& outer    [[buffer(2)]],
    constant uint& reduced  [[buffer(3)]],
    constant uint& is_max   [[buffer(4)]],
    uint tg       [[threadgroup_position_in_grid]],
    uint tid      [[thread_position_in_threadgroup]],
    uint nthreads [[threads_per_threadgroup]]
) {
    if (tg >= outer) return;
    device const float* row = src + (ulong)tg * (ulong)reduced;

    threadgroup float sval[256];
    threadgroup uint  sidx[256];

    float best = (is_max != 0u) ? -INFINITY : INFINITY;
    uint  bidx = 0u;
    // Strided scan: within a lane, strict comparison keeps the lowest index.
    for (uint r = tid; r < reduced; r += nthreads) {
        float v = row[r];
        bool better = (is_max != 0u) ? (v > best) : (v < best);
        if (better) { best = v; bidx = r; }
    }
    sval[tid] = best;
    sidx[tid] = bidx;
    threadgroup_barrier(mem_flags::mem_threadgroup);

    for (uint s = nthreads >> 1; s > 0u; s >>= 1) {
        if (tid < s) {
            float v  = sval[tid + s];
            uint  i  = sidx[tid + s];
            float cv = sval[tid];
            uint  ci = sidx[tid];
            bool better = (is_max != 0u) ? (v > cv) : (v < cv);
            // Equal value → keep the lower source index (CPU first-best).
            if (better || (v == cv && i < ci)) { sval[tid] = v; sidx[tid] = i; }
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }
    if (tid == 0u) out[tg] = float(sidx[0]);
}

// Fused vector-quantization: one threadgroup per input row cooperatively finds
// the nearest codebook entry over the reduced K axis — no [N,K] materialization,
// no D2H copy (the host-callback custom-op path was ~100× slower on Metal).
// metric 0 = L2 (argmin ‖x−C‖²), 1 = cosine (argmax x·C/‖C‖). Output is the
// f32-encoded code index (ready for gather). Lowest index wins ties.
kernel void vq_assign(
    device const float* x   [[buffer(0)]],   // [N, D]
    device const float* cb  [[buffer(1)]],   // [K, D]
    device float* out       [[buffer(2)]],   // [N]
    constant uint& n        [[buffer(3)]],
    constant uint& d        [[buffer(4)]],
    constant uint& k        [[buffer(5)]],
    constant uint& metric   [[buffer(6)]],
    uint tg       [[threadgroup_position_in_grid]],
    uint tid      [[thread_position_in_threadgroup]],
    uint nthreads [[threads_per_threadgroup]]
) {
    if (tg >= n) return;
    device const float* xi = x + (ulong)tg * (ulong)d;
    threadgroup float sval[256];
    threadgroup uint  sidx[256];

    // float4 fast path when D is a multiple of 4 (the common case); the tail
    // loop handles any remainder. Vectorizing the dot closes most of the gap to
    // MPS's matrix units.
    const uint d4 = d >> 2;
    const uint drem = d & 3u;
    device const float4* xi4 = (device const float4*)xi;

    float best = (metric == 0u) ? INFINITY : -INFINITY;
    uint  bidx = 0u;
    for (uint j = tid; j < k; j += nthreads) {
        device const float* cj = cb + (ulong)j * (ulong)d;
        device const float4* cj4 = (device const float4*)cj;
        if (metric == 0u) {
            float4 acc = 0.0f;
            for (uint t = 0u; t < d4; ++t) { float4 df = xi4[t] - cj4[t]; acc += df * df; }
            float dist = acc.x + acc.y + acc.z + acc.w;
            for (uint t = d - drem; t < d; ++t) { float df = xi[t] - cj[t]; dist += df * df; }
            if (dist < best) { best = dist; bidx = j; }
        } else {
            float4 dacc = 0.0f, nacc = 0.0f;
            for (uint t = 0u; t < d4; ++t) { dacc += xi4[t] * cj4[t]; nacc += cj4[t] * cj4[t]; }
            float dot = dacc.x + dacc.y + dacc.z + dacc.w;
            float nc = nacc.x + nacc.y + nacc.z + nacc.w;
            for (uint t = d - drem; t < d; ++t) { dot += xi[t] * cj[t]; nc += cj[t] * cj[t]; }
            float sim = (nc > 0.0f) ? dot * rsqrt(nc) : 0.0f;
            if (sim > best) { best = sim; bidx = j; }
        }
    }
    sval[tid] = best;
    sidx[tid] = bidx;
    threadgroup_barrier(mem_flags::mem_threadgroup);
    for (uint s = nthreads >> 1; s > 0u; s >>= 1) {
        if (tid < s) {
            bool take = (metric == 0u) ? (sval[tid + s] < sval[tid]) : (sval[tid + s] > sval[tid]);
            if (take || (sval[tid + s] == sval[tid] && sidx[tid + s] < sidx[tid])) {
                sval[tid] = sval[tid + s];
                sidx[tid] = sidx[tid + s];
            }
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }
    if (tid == 0u) out[tg] = float(sidx[0]);
}

// On-GPU logit sampling: temperature -> top-k -> softmax -> top-p -> Philox
// inverse-CDF draw. One threadgroup per batch row. Mirrors the CPU algorithm
// in rlx-cpu `sample_row` / `execute_sample_f32` exactly, including the
// Philox4x32 stream (rlx_ir::rng), so a fixed seed is bit-comparable.
//
// top-k / top-p cutoffs are found by parallel bisection on the value/prob
// range rather than a full sort: for distinct logits this selects the
// identical kept set (the cutoff lands strictly between the two order
// statistics that bracket it). The final inverse-CDF walk (thread 0) recomputes
// each token's filtered probability in original index order so the sequential
// float accumulation matches the CPU reference element-for-element.
//
// Threadgroup size must be a power of two (dispatched at 256).
kernel void sample_logits(
    device float* arena         [[buffer(0)]],
    constant ulong& logits_off  [[buffer(1)]],
    constant ulong& dst_off     [[buffer(2)]],
    constant uint& batch        [[buffer(3)]],
    constant uint& vocab        [[buffer(4)]],
    constant uint& top_k        [[buffer(5)]],
    constant float& top_p       [[buffer(6)]],
    constant float& temperature [[buffer(7)]],
    constant ulong& seed        [[buffer(8)]],
    uint tg       [[threadgroup_position_in_grid]],
    uint tid      [[thread_position_in_threadgroup]],
    uint nthreads [[threads_per_threadgroup]]
) {
    if (tg >= batch) return;
    device const float* logits =
        (device const float*)((device char*)arena + logits_off);
    device float* dst = (device float*)((device char*)arena + dst_off);
    device const float* row = logits + (ulong)tg * (ulong)vocab;

    const uint v = vocab;
    const float MIN_POS = 1.1754944e-38f;          // f32::MIN_POSITIVE
    const float temp = max(temperature, 1e-6f);
    const uint  kk   = min(top_k, v);
    const bool use_topk = (kk > 0u) && (kk < v);
    const bool use_topp = (top_p < 1.0f);

    if (v == 0u) { if (tid == 0u) dst[tg] = 0.0f; return; }

    // `red` is the reduction scratch; `bounds[0..1]` carries the bisection
    // lo/hi (an array, so per-element-init warnings don't fire). All cross-lane
    // values are read back from `red[0]` after the reduction barrier.
    threadgroup float red[256];
    threadgroup float bounds[2];

    // ── max(scaled) ────────────────────────────────────────────────
    float lmax = -INFINITY;
    for (uint i = tid; i < v; i += nthreads) lmax = max(lmax, row[i]);
    red[tid] = lmax;
    threadgroup_barrier(mem_flags::mem_threadgroup);
    for (uint s = nthreads >> 1; s > 0u; s >>= 1) {
        if (tid < s) red[tid] = max(red[tid], red[tid + s]);
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }
    const float max_l = red[0] / temp;
    threadgroup_barrier(mem_flags::mem_threadgroup);

    // ── min(scaled) (bisection lower bound) ────────────────────────
    float lmin = INFINITY;
    for (uint i = tid; i < v; i += nthreads) lmin = min(lmin, row[i]);
    red[tid] = lmin;
    threadgroup_barrier(mem_flags::mem_threadgroup);
    for (uint s = nthreads >> 1; s > 0u; s >>= 1) {
        if (tid < s) red[tid] = min(red[tid], red[tid + s]);
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }
    const float min_l = red[0] / temp;
    threadgroup_barrier(mem_flags::mem_threadgroup);

    // ── top-k cutoff = kk-th largest scaled value (bisection) ──────
    float cutoff = -INFINITY;
    if (use_topk) {
        if (tid == 0u) { bounds[0] = min_l; bounds[1] = max_l; }
        threadgroup_barrier(mem_flags::mem_threadgroup);
        for (uint it = 0u; it < 50u; ++it) {
            float mid = 0.5f * (bounds[0] + bounds[1]);
            float cnt = 0.0f;
            for (uint i = tid; i < v; i += nthreads)
                if (row[i] / temp >= mid) cnt += 1.0f;
            red[tid] = cnt;
            threadgroup_barrier(mem_flags::mem_threadgroup);
            for (uint s = nthreads >> 1; s > 0u; s >>= 1) {
                if (tid < s) red[tid] += red[tid + s];
                threadgroup_barrier(mem_flags::mem_threadgroup);
            }
            if (tid == 0u) {
                if (red[0] >= float(kk)) bounds[0] = mid; else bounds[1] = mid;
            }
            threadgroup_barrier(mem_flags::mem_threadgroup);
        }
        cutoff = bounds[0];
    }

    // ── softmax denom over the top-k set ───────────────────────────
    float s1 = 0.0f;
    for (uint i = tid; i < v; i += nthreads) {
        float sc = row[i] / temp;
        if (!use_topk || sc >= cutoff) s1 += exp(sc - max_l);
    }
    red[tid] = s1;
    threadgroup_barrier(mem_flags::mem_threadgroup);
    for (uint s = nthreads >> 1; s > 0u; s >>= 1) {
        if (tid < s) red[tid] += red[tid + s];
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }
    const float sum1 = red[0];
    const float inv1 = 1.0f / max(sum1, MIN_POS);
    threadgroup_barrier(mem_flags::mem_threadgroup);

    // ── top-p prob cutoff (bisection over [0,1]) ───────────────────
    float pcut = 0.0f;
    if (use_topp) {
        if (tid == 0u) { bounds[0] = 0.0f; bounds[1] = 1.0f; }
        threadgroup_barrier(mem_flags::mem_threadgroup);
        for (uint it = 0u; it < 60u; ++it) {
            float mid = 0.5f * (bounds[0] + bounds[1]);
            float psum = 0.0f;
            for (uint i = tid; i < v; i += nthreads) {
                float sc = row[i] / temp;
                if (use_topk && sc < cutoff) continue;
                float p = exp(sc - max_l) * inv1;
                if (p >= mid) psum += p;
            }
            red[tid] = psum;
            threadgroup_barrier(mem_flags::mem_threadgroup);
            for (uint s = nthreads >> 1; s > 0u; s >>= 1) {
                if (tid < s) red[tid] += red[tid + s];
                threadgroup_barrier(mem_flags::mem_threadgroup);
            }
            if (tid == 0u) {
                if (red[0] >= top_p) bounds[0] = mid; else bounds[1] = mid;
            }
            threadgroup_barrier(mem_flags::mem_threadgroup);
        }
        pcut = bounds[0];
    }

    // ── renorm denom over the top-p set ────────────────────────────
    float sum2 = 1.0f;
    if (use_topp) {
        float s2 = 0.0f;
        for (uint i = tid; i < v; i += nthreads) {
            float sc = row[i] / temp;
            if (use_topk && sc < cutoff) continue;
            float p = exp(sc - max_l) * inv1;
            if (p >= pcut) s2 += p;
        }
        red[tid] = s2;
        threadgroup_barrier(mem_flags::mem_threadgroup);
        for (uint s = nthreads >> 1; s > 0u; s >>= 1) {
            if (tid < s) red[tid] += red[tid + s];
            threadgroup_barrier(mem_flags::mem_threadgroup);
        }
        sum2 = red[0];
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }

    // ── thread 0: Philox draw + sequential inverse-CDF in index order ─
    if (tid == 0u) {
        ulong sd  = (seed == 0ul) ? 0xDEADBEEFul : seed;
        uint  k0  = (uint)(sd & 0xFFFFFFFFul);
        uint  k1  = (uint)(sd >> 32);
        uint  st0 = (uint)(tg / 4u), st1 = 0u, st2 = 0u, st3 = 0u;
        for (uint rr = 0u; rr < 10u; ++rr) {
            ulong p0 = (ulong)st0 * 0xD2561A75ul;
            ulong p1 = (ulong)st2 * 0xCD9E8D57ul;
            uint hi0 = (uint)(p0 >> 32), lo0 = (uint)(p0 & 0xFFFFFFFFul);
            uint hi1 = (uint)(p1 >> 32), lo1 = (uint)(p1 & 0xFFFFFFFFul);
            uint n0 = hi1 ^ st1 ^ k0;
            uint n1 = lo1;
            uint n2 = hi0 ^ st3 ^ k1;
            uint n3 = lo0;
            st0 = n0; st1 = n1; st2 = n2; st3 = n3;
            k0 += 0x9E3779B9u; k1 += 0xBB67AE85u;
        }
        uint lane = tg % 4u;
        uint bits = (lane == 0u ? st0 : (lane == 1u ? st1 : (lane == 2u ? st2 : st3))) >> 8;
        float r = (float)bits / 16777216.0f;

        float inv2 = use_topp ? (1.0f / max(sum2, MIN_POS)) : 1.0f;
        float acc = 0.0f;
        uint chosen = v - 1u;
        for (uint i = 0u; i < v; ++i) {
            float sc = row[i] / temp;
            float p;
            if (use_topk && sc < cutoff) {
                p = 0.0f;
            } else {
                p = exp(sc - max_l) * inv1;
                if (use_topp) { p = (p >= pcut) ? (p * inv2) : 0.0f; }
            }
            acc += p;
            if (r <= acc) { chosen = i; break; }
        }
        dst[tg] = float(chosen);
    }
}

// Block-quantized int8 weight matmul: out[m,n] = x[m,k] @ dequant(wq[k,n]).
// Per-(block-of-k, n) scale (+ optional zero-point). One thread per output
// element. Matches rlx-cpu dequant_matmul_int8.
kernel void dequant_matmul_int8(
    device const float* x      [[buffer(0)]],
    device const char*  wq     [[buffer(1)]],
    device const float* scales [[buffer(2)]],
    device const float* zps    [[buffer(3)]],
    device float* out          [[buffer(4)]],
    constant uint& m           [[buffer(5)]],
    constant uint& k           [[buffer(6)]],
    constant uint& n           [[buffer(7)]],
    constant uint& block_size  [[buffer(8)]],
    constant uint& asym        [[buffer(9)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= m * n) return;
    uint i = gid / n;
    uint j = gid % n;
    // Accumulate one block at a time and fold in that block's scale ONCE, not
    // per element (see rlx_cpu dequant_matmul_int8). Reassociation only —
    // fewer multiplies, tighter running-sum dynamic range, bit-consistent
    // with the CPU oracle.
    uint nblk = (k + block_size - 1u) / block_size;
    float acc = 0.0f;
    for (uint b = 0u; b < nblk; ++b) {
        float s = scales[b * n + j];
        float z = (asym != 0u) ? zps[b * n + j] : 0.0f;
        uint lo = b * block_size;
        uint hi = min(lo + block_size, k);
        float bacc = 0.0f;
        for (uint p = lo; p < hi; ++p) {
            float q = float(wq[p * n + j]);
            bacc += x[i * k + p] * (q - z);
        }
        acc += bacc * s;
    }
    out[i * n + j] = acc;
}

// Block-quantized int4 (two nibbles per byte) weight matmul. Low nibble first.
kernel void dequant_matmul_int4(
    device const float* x      [[buffer(0)]],
    device const uchar* wq     [[buffer(1)]],
    device const float* scales [[buffer(2)]],
    device const float* zps    [[buffer(3)]],
    device float* out          [[buffer(4)]],
    constant uint& m           [[buffer(5)]],
    constant uint& k           [[buffer(6)]],
    constant uint& n           [[buffer(7)]],
    constant uint& block_size  [[buffer(8)]],
    constant uint& asym        [[buffer(9)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= m * n) return;
    uint i = gid / n;
    uint j = gid % n;
    // Block-at-a-time accumulation, scale folded once per block (see int8).
    uint nblk = (k + block_size - 1u) / block_size;
    float acc = 0.0f;
    for (uint b = 0u; b < nblk; ++b) {
        float s = scales[b * n + j];
        float z = (asym != 0u) ? zps[b * n + j] : 0.0f;
        uint lo = b * block_size;
        uint hi = min(lo + block_size, k);
        float bacc = 0.0f;
        for (uint p = lo; p < hi; ++p) {
            uint idx = p * n + j;
            uchar byte = wq[idx >> 1];
            uint nib = ((idx & 1u) == 0u) ? (uint(byte) & 0x0Fu) : (uint(byte) >> 4);
            bacc += x[i * k + p] * (float(nib) - z);
        }
        acc += bacc * s;
    }
    out[i * n + j] = acc;
}

inline float dq_fp8_e4m3(uchar byte) {
    // Match rlx-mlx-io `dequant_scale_fp8_e4m3` (bit-exact OCP E4M3).
    uint sign = (uint(byte) >> 7) & 1u;
    int exp_v = int((uint(byte) >> 3) & 0x0Fu);
    uint mant = uint(byte) & 0x7u;
    if (exp_v == 0x0f && mant == 0x7u) {
        return NAN; // Match rlx-mlx-io host decode
    }
    if (exp_v == 0) {
        if (mant == 0u) {
            return (sign != 0u) ? -0.0f : 0.0f;
        }
        uint m = mant;
        int e = -6;
        while ((m & 0x8u) == 0u) {
            m <<= 1;
            e -= 1;
        }
        m &= 0x7u;
        uint bits = (sign << 31) | (uint(e + 127) << 23) | (m << 20);
        return as_type<float>(bits);
    }
    uint bits = (sign << 31) | (uint(exp_v - 7 + 127) << 23) | (mant << 20);
    return as_type<float>(bits);
}

inline float dq_fp8_e5m2(uchar byte) {
    uint sign = (uint(byte) >> 7) & 1u;
    uint exp_v = (uint(byte) >> 2) & 0x1Fu;
    uint mant = uint(byte) & 0x3u;
    float v;
    if (exp_v == 0u) {
        v = (mant == 0u) ? 0.0f : (float(mant) / 4.0f) * exp2(-14.0f);
    } else if (exp_v == 0x1Fu) {
        v = 0.0f;
    } else {
        v = (1.0f + float(mant) / 4.0f) * exp2(float(int(exp_v) - 15));
    }
    return (sign != 0u) ? -v : v;
}

constant float DQ_FP4_E2M1[16] = {
    0.0f, 0.5f, 1.0f, 1.5f, 2.0f, 3.0f, 4.0f, 6.0f,
    -0.0f, -0.5f, -1.0f, -1.5f, -2.0f, -3.0f, -4.0f, -6.0f
};

kernel void dequant_matmul_fp8(
    device const float* x      [[buffer(0)]],
    device const uchar* wq     [[buffer(1)]],
    device const float* scales [[buffer(2)]],
    device float* out          [[buffer(4)]],
    constant uint& m           [[buffer(5)]],
    constant uint& k           [[buffer(6)]],
    constant uint& n           [[buffer(7)]],
    constant uint& e5m2        [[buffer(8)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= m * n) return;
    uint i = gid / n;
    uint j = gid % n;
    // The per-column scale is loop-invariant: apply it once to the finished dot
    // instead of every term. Reassociation only.
    float col_scale = scales[j];
    float acc = 0.0f;
    for (uint p = 0u; p < k; ++p) {
        uchar byte = wq[p * n + j];
        float w = (e5m2 != 0u) ? dq_fp8_e5m2(byte) : dq_fp8_e4m3(byte);
        acc += x[i * k + p] * w;
    }
    out[i * n + j] = acc * col_scale;
}

kernel void dequant_matmul_nvfp4(
    device const float* x      [[buffer(0)]],
    device const uchar* wq     [[buffer(1)]],
    device const uchar* scales [[buffer(2)]],
    device const float* gs_ptr [[buffer(3)]],
    device float* out          [[buffer(4)]],
    constant uint& m           [[buffer(5)]],
    constant uint& k           [[buffer(6)]],
    constant uint& n           [[buffer(7)]],
    constant uint& group_size  [[buffer(8)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= m * n) return;
    uint i = gid / n;
    uint j = gid % n;
    // Fold the per-group E4M3 scale in once per group and the single global
    // scale in once at the end (see rlx_cpu dequant_matmul_nvfp4).
    float gs = gs_ptr[0];
    uint nblk = (k + group_size - 1u) / group_size;
    float acc = 0.0f;
    for (uint b = 0u; b < nblk; ++b) {
        float s = dq_fp8_e4m3(scales[b * n + j]);
        uint lo = b * group_size;
        uint hi = min(lo + group_size, k);
        float bacc = 0.0f;
        for (uint p = lo; p < hi; ++p) {
            uint idx = p * n + j;
            uint byte_idx = idx >> 1;
            uint nib = ((idx & 1u) == 0u) ? (uint(wq[byte_idx]) & 0x0Fu) : (uint(wq[byte_idx]) >> 4);
            bacc += x[i * k + p] * DQ_FP4_E2M1[nib];
        }
        acc += bacc * s;
    }
    out[i * n + j] = acc * gs;
}

// MxFp4x2 two-level residual E2M1 fused decode-matmul. wq = [plane0|plane1]
// (E2M1 nibbles packed 2/byte over the [k,n] grid); scales = [s0|s1] f32 per
// (block = k/group_size, n). out[i,j] = sum_p x[i,p]·(s0·LUT[q0] + s1·LUT[q1]).
// Same math + layout as rlx_cpu::dequant_matmul_mxfp4x2.
kernel void dequant_matmul_mxfp4x2(
    device const float* x      [[buffer(0)]],
    device const uchar* wq     [[buffer(1)]],
    device const float* scales [[buffer(2)]],
    device float* out          [[buffer(3)]],
    constant uint& m           [[buffer(4)]],
    constant uint& k           [[buffer(5)]],
    constant uint& n           [[buffer(6)]],
    constant uint& group_size  [[buffer(7)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= m * n) return;
    uint i = gid / n;
    uint j = gid % n;
    uint total = k * n;
    uint plane = (total + 1u) >> 1;                     // bytes per nibble plane
    uint g = (group_size == 0u) ? 1u : group_size;
    uint nblk = (k + g - 1u) / g;
    // Accumulate each residual plane's dot per block, then fold each plane's
    // block scale in once (s0·Σq0 + s1·Σq1). Reassociation only.
    float acc = 0.0f;
    for (uint b = 0u; b < nblk; ++b) {
        float s0 = scales[b * n + j];
        float s1 = scales[nblk * n + b * n + j];
        uint lo = b * g;
        uint hi = min(lo + g, k);
        float a0 = 0.0f;
        float a1 = 0.0f;
        for (uint p = lo; p < hi; ++p) {
            uint idx = p * n + j;
            uint byte_idx = idx >> 1;
            uint sh = ((idx & 1u) == 0u) ? 0u : 4u;
            uint q0 = (uint(wq[byte_idx]) >> sh) & 0x0Fu;
            uint q1 = (uint(wq[plane + byte_idx]) >> sh) & 0x0Fu;
            float xv = x[i * k + p];
            a0 += xv * DQ_FP4_E2M1[q0];
            a1 += xv * DQ_FP4_E2M1[q1];
        }
        acc += a0 * s0 + a1 * s1;
    }
    out[i * n + j] = acc;
}

// MLX Linear packs: w [n,k] along K. kind 0=affine, 1=mxfp4, 2=mxfp8.
inline float mlx_e8m0(uint s) {
    if (s == 0u) return as_type<float>(uint(0x0040u) << 16);
    return as_type<float>(s << 23);
}
inline float mlx_group_scale(uint s, uint gs) {
    return (gs == 16u) ? dq_fp8_e4m3(uchar(s)) : mlx_e8m0(s);
}
inline uint mlx_pack_factor(uint bits) {
    if (bits == 2u || bits == 4u || bits == 8u) return 8u / bits;
    if (bits == 3u || bits == 5u) return 8u;
    if (bits == 6u) return 4u;
    return 1u;
}
inline uint mlx_bpp(uint bits) {
    if (bits == 2u || bits == 4u || bits == 8u) return 1u;
    if (bits == 3u || bits == 6u) return 3u;
    if (bits == 5u) return 5u;
    return 1u;
}

// Decode GEMV (m==1): one threadgroup per output column; threads split K.
kernel void dequant_matmul_mlx_gemv(
    device const float* x      [[buffer(0)]],
    device const uchar* wq     [[buffer(1)]],
    device const float* scales [[buffer(2)]],
    device const float* biases [[buffer(3)]],
    device float* out          [[buffer(4)]],
    constant uint& k           [[buffer(5)]],
    constant uint& n           [[buffer(6)]],
    constant uint& kind        [[buffer(7)]],
    constant uint& bits        [[buffer(8)]],
    constant uint& group_size  [[buffer(9)]],
    uint tid [[thread_index_in_threadgroup]],
    uint j [[threadgroup_position_in_grid]],
    uint tpg [[threads_per_threadgroup]]
) {
    if (j >= n) return;
    uint gs = group_size;
    uint n_groups = k / gs;
    device const uchar* scale_u = (device const uchar*)scales;
    threadgroup float smem[256];
    float acc = 0.0f;
    for (uint p = tid; p < k; p += tpg) {
        uint g = p / gs;
        float w_dq;
        if (kind == 0u) {
            uint pf = mlx_pack_factor(bits);
            uint bpp = mlx_bpp(bits);
            uint packs_in_group = gs / pf;
            uint local = p % gs;
            uint row_base = j * n_groups * packs_in_group * bpp + g * packs_in_group * bpp;
            uint code = 0u;
            if (bits == 2u || bits == 4u || bits == 8u) {
                uint pack_idx = local / pf;
                uint in_pack = local % pf;
                uchar byte = wq[row_base + pack_idx];
                uint mask = (1u << bits) - 1u;
                code = (uint(byte) >> (in_pack * bits)) & mask;
            } else if (bits == 3u) {
                uint pack_idx = local / 8u;
                uint in_pack = local % 8u;
                uint bo = row_base + pack_idx * 3u;
                uchar b0 = wq[bo], b1 = wq[bo + 1], b2 = wq[bo + 2];
                uint codes[8] = {
                    uint(b0) & 0x7u,
                    (uint(b0) & 0x38u) >> 3,
                    ((uint(b0) & 0xc0u) >> 6) + ((uint(b1) & 0x1u) << 2),
                    (uint(b1) & 0xeu) >> 1,
                    (uint(b1) & 0x70u) >> 4,
                    ((uint(b1) & 0x80u) >> 7) + ((uint(b2) & 0x3u) << 1),
                    (uint(b2) & 0x1cu) >> 2,
                    (uint(b2) & 0xe0u) >> 5
                };
                code = codes[in_pack];
            } else if (bits == 5u) {
                uint pack_idx = local / 8u;
                uint in_pack = local % 8u;
                uint bo = row_base + pack_idx * 5u;
                uchar b0 = wq[bo], b1 = wq[bo+1], b2 = wq[bo+2], b3 = wq[bo+3], b4 = wq[bo+4];
                uint codes[8] = {
                    uint(b0) & 0x1fu,
                    ((uint(b0) & 0xe0u) >> 5) + ((uint(b1) & 0x3u) << 3),
                    (uint(b1) & 0x7cu) >> 2,
                    ((uint(b1) & 0x80u) >> 7) + ((uint(b2) & 0xfu) << 1),
                    ((uint(b2) & 0xf0u) >> 4) + ((uint(b3) & 0x1u) << 4),
                    (uint(b3) & 0x3eu) >> 1,
                    ((uint(b3) & 0xc0u) >> 6) + ((uint(b4) & 0x7u) << 2),
                    (uint(b4) & 0xf8u) >> 3
                };
                code = codes[in_pack];
            } else {
                uint pack_idx = local / 4u;
                uint in_pack = local % 4u;
                uint bo = row_base + pack_idx * 3u;
                uchar b0 = wq[bo], b1 = wq[bo+1], b2 = wq[bo+2];
                uint codes[4] = {
                    uint(b0) & 0x3fu,
                    ((uint(b0) >> 6) & 0x03u) + ((uint(b1) & 0x0fu) << 2),
                    ((uint(b1) >> 4) & 0x0fu) + ((uint(b2) & 0x03u) << 4),
                    (uint(b2) >> 2) & 0x3fu
                };
                code = codes[in_pack];
            }
            w_dq = scales[j * n_groups + g] * float(code) + biases[j * n_groups + g];
        } else if (kind == 1u) {
            uint bidx = j * (k / 2u) + (p / 2u);
            uchar byte = wq[bidx];
            uint nib = ((p & 1u) == 0u) ? (uint(byte) & 0x0Fu) : (uint(byte) >> 4);
            w_dq = DQ_FP4_E2M1[nib] * mlx_group_scale(uint(scale_u[j * n_groups + g]), gs);
        } else {
            w_dq = dq_fp8_e4m3(wq[j * k + p]) * mlx_group_scale(uint(scale_u[j * n_groups + g]), gs);
        }
        acc += x[p] * w_dq;
    }
    smem[tid] = acc;
    threadgroup_barrier(mem_flags::mem_threadgroup);
    for (uint s = tpg >> 1u; s > 0u; s >>= 1u) {
        if (tid < s) smem[tid] += smem[tid + s];
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }
    if (tid == 0u) out[j] = smem[0];
}

inline float mlx_bf16(ushort b) { return as_type<float>(uint(b) << 16); }

// Grouped decode GEMV (m==1) — the native MoE expert kernel. Identical dequant
// math to dequant_matmul_mlx_gemv, but the (single) row's expert e = e_idx[0]
// offsets into the stacked [E,n,k] weight slab (wq + e*slab_bytes) and the
// [E,n,n_groups] scales/biases (+ e*n*n_groups). Replaces the CPU host-delegate
// for the DeepSeek-V4 MoE — reads packed codes straight from unified memory and
// dequant+matmuls on-GPU. `scale_bf16`: scales/biases are BF16 (2B, the model's
// per-expert slab format — already e8m0-decoded) rather than f32/e8m0-uchar.
// One threadgroup per output column; threads split K.
kernel void grouped_dequant_matmul_mlx_gemv(
    device const float* x       [[buffer(0)]],
    device const uchar* wq      [[buffer(1)]],
    device const float* scales  [[buffer(2)]],
    device const float* biases  [[buffer(3)]],
    device float* out           [[buffer(4)]],
    device const float* e_idx   [[buffer(5)]],
    constant uint& k            [[buffer(6)]],
    constant uint& n            [[buffer(7)]],
    constant uint& kind         [[buffer(8)]],
    constant uint& bits         [[buffer(9)]],
    constant uint& group_size   [[buffer(10)]],
    constant uint& slab_bytes   [[buffer(11)]],
    constant uint& scale_bf16   [[buffer(12)]],
    uint tid [[thread_index_in_threadgroup]],
    uint j [[threadgroup_position_in_grid]],
    uint tpg [[threads_per_threadgroup]]
) {
    if (j >= n) return;
    uint gs = group_size;
    uint n_groups = k / gs;
    uint e = uint(e_idx[0] + 0.5f);              // this row's expert
    device const uchar* wqe = wq + e * slab_bytes;   // expert weight slab
    uint sc_off = e * n * n_groups;                  // expert scale/bias base
    device const uchar* scale_u = (device const uchar*)scales;
    device const ushort* scale_bf = (device const ushort*)scales;
    device const ushort* bias_bf = (device const ushort*)biases;
    threadgroup float smem[256];
    float acc = 0.0f;
    for (uint p = tid; p < k; p += tpg) {
        uint g = p / gs;
        float w_dq;
        if (kind == 0u) {
            uint pf = mlx_pack_factor(bits);
            uint bpp = mlx_bpp(bits);
            uint packs_in_group = gs / pf;
            uint local = p % gs;
            uint row_base = j * n_groups * packs_in_group * bpp + g * packs_in_group * bpp;
            uint code = 0u;
            if (bits == 2u || bits == 4u || bits == 8u) {
                uint pack_idx = local / pf;
                uint in_pack = local % pf;
                uchar byte = wqe[row_base + pack_idx];
                uint mask = (1u << bits) - 1u;
                code = (uint(byte) >> (in_pack * bits)) & mask;
            } else if (bits == 3u) {
                uint pack_idx = local / 8u;
                uint in_pack = local % 8u;
                uint bo = row_base + pack_idx * 3u;
                uchar b0 = wqe[bo], b1 = wqe[bo + 1], b2 = wqe[bo + 2];
                uint codes[8] = {
                    uint(b0) & 0x7u,
                    (uint(b0) & 0x38u) >> 3,
                    ((uint(b0) & 0xc0u) >> 6) + ((uint(b1) & 0x1u) << 2),
                    (uint(b1) & 0xeu) >> 1,
                    (uint(b1) & 0x70u) >> 4,
                    ((uint(b1) & 0x80u) >> 7) + ((uint(b2) & 0x3u) << 1),
                    (uint(b2) & 0x1cu) >> 2,
                    (uint(b2) & 0xe0u) >> 5
                };
                code = codes[in_pack];
            } else if (bits == 5u) {
                uint pack_idx = local / 8u;
                uint in_pack = local % 8u;
                uint bo = row_base + pack_idx * 5u;
                uchar b0 = wqe[bo], b1 = wqe[bo+1], b2 = wqe[bo+2], b3 = wqe[bo+3], b4 = wqe[bo+4];
                uint codes[8] = {
                    uint(b0) & 0x1fu,
                    ((uint(b0) & 0xe0u) >> 5) + ((uint(b1) & 0x3u) << 3),
                    (uint(b1) & 0x7cu) >> 2,
                    ((uint(b1) & 0x80u) >> 7) + ((uint(b2) & 0xfu) << 1),
                    ((uint(b2) & 0xf0u) >> 4) + ((uint(b3) & 0x1u) << 4),
                    (uint(b3) & 0x3eu) >> 1,
                    ((uint(b3) & 0xc0u) >> 6) + ((uint(b4) & 0x7u) << 2),
                    (uint(b4) & 0xf8u) >> 3
                };
                code = codes[in_pack];
            } else {
                uint pack_idx = local / 4u;
                uint in_pack = local % 4u;
                uint bo = row_base + pack_idx * 3u;
                uchar b0 = wqe[bo], b1 = wqe[bo+1], b2 = wqe[bo+2];
                uint codes[4] = {
                    uint(b0) & 0x3fu,
                    ((uint(b0) >> 6) & 0x03u) + ((uint(b1) & 0x0fu) << 2),
                    ((uint(b1) >> 4) & 0x0fu) + ((uint(b2) & 0x03u) << 4),
                    (uint(b2) >> 2) & 0x3fu
                };
                code = codes[in_pack];
            }
            uint si = sc_off + j * n_groups + g;
            float sv = scale_bf16 ? mlx_bf16(scale_bf[si]) : scales[si];
            float bv = scale_bf16 ? mlx_bf16(bias_bf[si]) : biases[si];
            w_dq = sv * float(code) + bv;
        } else if (kind == 1u) {
            uint bidx = j * (k / 2u) + (p / 2u);
            uchar byte = wqe[bidx];
            uint nib = ((p & 1u) == 0u) ? (uint(byte) & 0x0Fu) : (uint(byte) >> 4);
            uint si = sc_off + j * n_groups + g;
            float sv = scale_bf16 ? mlx_bf16(scale_bf[si]) : mlx_group_scale(uint(scale_u[si]), gs);
            w_dq = DQ_FP4_E2M1[nib] * sv;
        } else {
            uint si = sc_off + j * n_groups + g;
            float sv = scale_bf16 ? mlx_bf16(scale_bf[si]) : mlx_group_scale(uint(scale_u[si]), gs);
            w_dq = dq_fp8_e4m3(wqe[j * k + p]) * sv;
        }
        acc += x[p] * w_dq;
    }
    smem[tid] = acc;
    threadgroup_barrier(mem_flags::mem_threadgroup);
    for (uint s = tpg >> 1u; s > 0u; s >>= 1u) {
        if (tid < s) smem[tid] += smem[tid + s];
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }
    if (tid == 0u) out[j] = smem[0];
}

// Prefill: one threadgroup per (col, row_tile); threads split K and stage
// an X tile in threadgroup memory (TM=8 rows × threads_per_threadgroup).
// Threadgroup id is linearized: tg = col * n_row_tiles + row_tile.
kernel void dequant_matmul_mlx_gemm(
    device const float* x      [[buffer(0)]],
    device const uchar* wq     [[buffer(1)]],
    device const float* scales [[buffer(2)]],
    device const float* biases [[buffer(3)]],
    device float* out          [[buffer(4)]],
    constant uint& m           [[buffer(5)]],
    constant uint& k           [[buffer(6)]],
    constant uint& n           [[buffer(7)]],
    constant uint& kind        [[buffer(8)]],
    constant uint& bits        [[buffer(9)]],
    constant uint& group_size  [[buffer(10)]],
    uint tid [[thread_index_in_threadgroup]],
    uint tg [[threadgroup_position_in_grid]],
    uint tpg [[threads_per_threadgroup]]
) {
    const uint TM = 8u;
    uint n_row_tiles = (m + TM - 1u) / TM;
    uint col = tg / n_row_tiles;
    uint row0 = (tg - col * n_row_tiles) * TM;
    if (col >= n) return;
    uint gs = group_size;
    uint n_groups = k / gs;
    device const uchar* scale_u = (device const uchar*)scales;
    // Only the final cross-thread reduction needs threadgroup memory. X used to
    // be staged here too (`xs[t*tpg+tid]`), but every thread wrote and read back
    // its OWN slot — a no-op round-trip costing 8 KB of occupancy-limiting
    // threadgroup memory plus a barrier per K-chunk. It lives in registers now.
    //
    // X stays f32 there. Staging it as `half` looks safe per-term — its partner
    // is a 3.3-mantissa-bit MXFP4 weight, so f16's 11 bits are far more than one
    // product can resolve — but this is a DOT product, and per-term rounding is
    // amplified by Σ|terms| / |result| wherever the sum cancels. Measured, that
    // pushed CPU-parity error to 2.9e-4 … 6.5e-4 (f16 eps is 4.9e-4) against the
    // 1e-4 bound the mlx parity tests hold this kernel to. The ACCUMULATOR stays
    // f32 for the same reason — k runs to 1536 terms and would lose the tail.
    threadgroup float smem[8 * 256];
    float acc[8];
    for (uint t = 0u; t < TM; ++t) acc[t] = 0.0f;
    for (uint p0 = 0u; p0 < k; p0 += tpg) {
        uint p = p0 + tid;
        float xv[8];
        for (uint t = 0u; t < TM; ++t) {
            uint row = row0 + t;
            xv[t] = (row < m && p < k) ? x[row * k + p] : 0.0f;
        }
        if (p < k) {
            uint g = p / gs;
            float w_dq;
            if (kind == 0u) {
                uint pf = mlx_pack_factor(bits);
                uint bpp = mlx_bpp(bits);
                uint packs_in_group = gs / pf;
                uint local = p % gs;
                uint row_base = col * n_groups * packs_in_group * bpp + g * packs_in_group * bpp;
                uint code = 0u;
                if (bits == 2u || bits == 4u || bits == 8u) {
                    uint pack_idx = local / pf;
                    uint in_pack = local % pf;
                    uchar byte = wq[row_base + pack_idx];
                    uint mask = (1u << bits) - 1u;
                    code = (uint(byte) >> (in_pack * bits)) & mask;
                } else if (bits == 3u) {
                    uint pack_idx = local / 8u;
                    uint in_pack = local % 8u;
                    uint bo = row_base + pack_idx * 3u;
                    uchar b0 = wq[bo], b1 = wq[bo + 1], b2 = wq[bo + 2];
                    uint codes[8] = {
                        uint(b0) & 0x7u, (uint(b0) & 0x38u) >> 3,
                        ((uint(b0) & 0xc0u) >> 6) + ((uint(b1) & 0x1u) << 2),
                        (uint(b1) & 0xeu) >> 1, (uint(b1) & 0x70u) >> 4,
                        ((uint(b1) & 0x80u) >> 7) + ((uint(b2) & 0x3u) << 1),
                        (uint(b2) & 0x1cu) >> 2, (uint(b2) & 0xe0u) >> 5
                    };
                    code = codes[in_pack];
                } else if (bits == 5u) {
                    uint pack_idx = local / 8u;
                    uint in_pack = local % 8u;
                    uint bo = row_base + pack_idx * 5u;
                    uchar b0 = wq[bo], b1 = wq[bo+1], b2 = wq[bo+2], b3 = wq[bo+3], b4 = wq[bo+4];
                    uint codes[8] = {
                        uint(b0) & 0x1fu,
                        ((uint(b0) & 0xe0u) >> 5) + ((uint(b1) & 0x3u) << 3),
                        (uint(b1) & 0x7cu) >> 2,
                        ((uint(b1) & 0x80u) >> 7) + ((uint(b2) & 0xfu) << 1),
                        ((uint(b2) & 0xf0u) >> 4) + ((uint(b3) & 0x1u) << 4),
                        (uint(b3) & 0x3eu) >> 1,
                        ((uint(b3) & 0xc0u) >> 6) + ((uint(b4) & 0x7u) << 2),
                        (uint(b4) & 0xf8u) >> 3
                    };
                    code = codes[in_pack];
                } else {
                    uint pack_idx = local / 4u;
                    uint in_pack = local % 4u;
                    uint bo = row_base + pack_idx * 3u;
                    uchar b0 = wq[bo], b1 = wq[bo+1], b2 = wq[bo+2];
                    uint codes[4] = {
                        uint(b0) & 0x3fu,
                        ((uint(b0) >> 6) & 0x03u) + ((uint(b1) & 0x0fu) << 2),
                        ((uint(b1) >> 4) & 0x0fu) + ((uint(b2) & 0x03u) << 4),
                        (uint(b2) >> 2) & 0x3fu
                    };
                    code = codes[in_pack];
                }
                w_dq = scales[col * n_groups + g] * float(code) + biases[col * n_groups + g];
            } else if (kind == 1u) {
                uint bidx = col * (k / 2u) + (p / 2u);
                uchar byte = wq[bidx];
                uint nib = ((p & 1u) == 0u) ? (uint(byte) & 0x0Fu) : (uint(byte) >> 4);
                w_dq = DQ_FP4_E2M1[nib] * mlx_group_scale(uint(scale_u[col * n_groups + g]), gs);
            } else {
                w_dq = dq_fp8_e4m3(wq[col * k + p]) * mlx_group_scale(uint(scale_u[col * n_groups + g]), gs);
            }
            for (uint t = 0u; t < TM; ++t) {
                acc[t] += float(xv[t]) * w_dq;
            }
        }
    }
    for (uint t = 0u; t < TM; ++t) smem[t * tpg + tid] = acc[t];
    threadgroup_barrier(mem_flags::mem_threadgroup);
    for (uint s = tpg >> 1u; s > 0u; s >>= 1u) {
        if (tid < s) {
            for (uint t = 0u; t < TM; ++t) {
                smem[t * tpg + tid] += smem[t * tpg + tid + s];
            }
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }
    if (tid == 0u) {
        for (uint t = 0u; t < TM; ++t) {
            uint row = row0 + t;
            if (row < m) out[row * n + col] = smem[t * tpg];
        }
    }
}

// Dequant one weight element w[col, p] from expert slab `wqe` (+ scale base
// `sc_off`) for the grouped MoE GEMM — mirrors the GEMV dequant math (all bit
// widths + scale_bf16). `col` is the output/weight-row index.
inline float mlx_grouped_w(
    device const uchar* wqe,
    device const float* scales,
    device const float* biases,
    uint sc_off, uint col, uint p, uint g,
    uint n_groups, uint gs, uint kind, uint bits, uint scale_bf16, uint k
) {
    device const uchar* scale_u = (device const uchar*)scales;
    device const ushort* scale_bf = (device const ushort*)scales;
    device const ushort* bias_bf = (device const ushort*)biases;
    if (kind == 0u) {
        uint pf = mlx_pack_factor(bits);
        uint bpp = mlx_bpp(bits);
        uint packs_in_group = gs / pf;
        uint local = p % gs;
        uint row_base = col * n_groups * packs_in_group * bpp + g * packs_in_group * bpp;
        uint code = 0u;
        if (bits == 2u || bits == 4u || bits == 8u) {
            uint pack_idx = local / pf;
            uint in_pack = local % pf;
            uchar byte = wqe[row_base + pack_idx];
            uint mask = (1u << bits) - 1u;
            code = (uint(byte) >> (in_pack * bits)) & mask;
        } else if (bits == 3u) {
            uint pack_idx = local / 8u; uint in_pack = local % 8u;
            uint bo = row_base + pack_idx * 3u;
            uchar b0 = wqe[bo], b1 = wqe[bo+1], b2 = wqe[bo+2];
            uint codes[8] = {
                uint(b0) & 0x7u, (uint(b0) & 0x38u) >> 3,
                ((uint(b0) & 0xc0u) >> 6) + ((uint(b1) & 0x1u) << 2),
                (uint(b1) & 0xeu) >> 1, (uint(b1) & 0x70u) >> 4,
                ((uint(b1) & 0x80u) >> 7) + ((uint(b2) & 0x3u) << 1),
                (uint(b2) & 0x1cu) >> 2, (uint(b2) & 0xe0u) >> 5
            };
            code = codes[in_pack];
        } else if (bits == 5u) {
            uint pack_idx = local / 8u; uint in_pack = local % 8u;
            uint bo = row_base + pack_idx * 5u;
            uchar b0 = wqe[bo], b1 = wqe[bo+1], b2 = wqe[bo+2], b3 = wqe[bo+3], b4 = wqe[bo+4];
            uint codes[8] = {
                uint(b0) & 0x1fu,
                ((uint(b0) & 0xe0u) >> 5) + ((uint(b1) & 0x3u) << 3),
                (uint(b1) & 0x7cu) >> 2,
                ((uint(b1) & 0x80u) >> 7) + ((uint(b2) & 0xfu) << 1),
                ((uint(b2) & 0xf0u) >> 4) + ((uint(b3) & 0x1u) << 4),
                (uint(b3) & 0x3eu) >> 1,
                ((uint(b3) & 0xc0u) >> 6) + ((uint(b4) & 0x7u) << 2),
                (uint(b4) & 0xf8u) >> 3
            };
            code = codes[in_pack];
        } else {
            uint pack_idx = local / 4u; uint in_pack = local % 4u;
            uint bo = row_base + pack_idx * 3u;
            uchar b0 = wqe[bo], b1 = wqe[bo+1], b2 = wqe[bo+2];
            uint codes[4] = {
                uint(b0) & 0x3fu,
                ((uint(b0) >> 6) & 0x03u) + ((uint(b1) & 0x0fu) << 2),
                ((uint(b1) >> 4) & 0x0fu) + ((uint(b2) & 0x03u) << 4),
                (uint(b2) >> 2) & 0x3fu
            };
            code = codes[in_pack];
        }
        uint si = sc_off + col * n_groups + g;
        float sv = scale_bf16 ? mlx_bf16(scale_bf[si]) : scales[si];
        float bv = scale_bf16 ? mlx_bf16(bias_bf[si]) : biases[si];
        return sv * float(code) + bv;
    } else if (kind == 1u) {
        uint bidx = col * (k / 2u) + (p / 2u);
        uchar byte = wqe[bidx];
        uint nib = ((p & 1u) == 0u) ? (uint(byte) & 0x0Fu) : (uint(byte) >> 4);
        uint si = sc_off + col * n_groups + g;
        float sv = scale_bf16 ? mlx_bf16(scale_bf[si]) : mlx_group_scale(uint(scale_u[si]), gs);
        return DQ_FP4_E2M1[nib] * sv;
    } else {
        uint si = sc_off + col * n_groups + g;
        float sv = scale_bf16 ? mlx_bf16(scale_bf[si]) : mlx_group_scale(uint(scale_u[si]), gs);
        return dq_fp8_e4m3(wqe[col * k + p]) * sv;
    }
}

// Grouped prefill GEMM — the native MoE expert kernel for m>1. Each row's expert
// e = e_idx[row] offsets into the stacked slab; since rows in a TM-tile can route
// to different experts, w is dequanted per row (no column-share). One threadgroup
// per (col, row_tile); threads split K and stage an X tile in threadgroup memory.
kernel void grouped_dequant_matmul_mlx_gemm(
    device const float* x      [[buffer(0)]],
    device const uchar* wq     [[buffer(1)]],
    device const float* scales [[buffer(2)]],
    device const float* biases [[buffer(3)]],
    device float* out          [[buffer(4)]],
    device const float* e_idx  [[buffer(5)]],
    constant uint& m           [[buffer(6)]],
    constant uint& k           [[buffer(7)]],
    constant uint& n           [[buffer(8)]],
    constant uint& kind        [[buffer(9)]],
    constant uint& bits        [[buffer(10)]],
    constant uint& group_size  [[buffer(11)]],
    constant uint& slab_bytes  [[buffer(12)]],
    constant uint& scale_bf16  [[buffer(13)]],
    uint tid [[thread_index_in_threadgroup]],
    uint tg [[threadgroup_position_in_grid]],
    uint tpg [[threads_per_threadgroup]]
) {
    const uint TM = 8u;
    uint n_row_tiles = (m + TM - 1u) / TM;
    uint col = tg / n_row_tiles;
    uint row0 = (tg - col * n_row_tiles) * TM;
    if (col >= n) return;
    uint gs = group_size;
    uint n_groups = k / gs;
    // Only the final cross-thread reduction needs threadgroup memory. X used to
    // be staged here too (`xs[t*tpg+tid]`), but every thread wrote and read back
    // its OWN slot — a no-op round-trip costing 8 KB of occupancy-limiting
    // threadgroup memory plus a barrier per K-chunk. It lives in registers now.
    //
    // X stays f32 there. Staging it as `half` looks safe per-term — its partner
    // is a 3.3-mantissa-bit MXFP4 weight, so f16's 11 bits are far more than one
    // product can resolve — but this is a DOT product, and per-term rounding is
    // amplified by Σ|terms| / |result| wherever the sum cancels. Measured, that
    // pushed CPU-parity error to 2.9e-4 … 6.5e-4 (f16 eps is 4.9e-4) against the
    // 1e-4 bound the mlx parity tests hold this kernel to. The ACCUMULATOR stays
    // f32 for the same reason — k runs to 1536 terms and would lose the tail.
    threadgroup float smem[8 * 256];
    float acc[8];
    for (uint t = 0u; t < TM; ++t) acc[t] = 0.0f;
    for (uint p0 = 0u; p0 < k; p0 += tpg) {
        uint p = p0 + tid;
        float xv[8];
        for (uint t = 0u; t < TM; ++t) {
            uint row = row0 + t;
            xv[t] = (row < m && p < k) ? x[row * k + p] : 0.0f;
        }
        if (p < k) {
            uint g = p / gs;
            for (uint t = 0u; t < TM; ++t) {
                uint row = row0 + t;
                if (row < m) {
                    uint e = uint(e_idx[row] + 0.5f);
                    device const uchar* wqe = wq + e * slab_bytes;
                    uint sc_off = e * n * n_groups;
                    float w_dq = mlx_grouped_w(wqe, scales, biases, sc_off, col, p, g, n_groups, gs, kind, bits, scale_bf16, k);
                    acc[t] += float(xv[t]) * w_dq;
                }
            }
        }
    }
    for (uint t = 0u; t < TM; ++t) smem[t * tpg + tid] = acc[t];
    threadgroup_barrier(mem_flags::mem_threadgroup);
    for (uint s = tpg >> 1u; s > 0u; s >>= 1u) {
        if (tid < s) {
            for (uint t = 0u; t < TM; ++t) {
                smem[t * tpg + tid] += smem[t * tpg + tid + s];
            }
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }
    if (tid == 0u) {
        for (uint t = 0u; t < TM; ++t) {
            uint row = row0 + t;
            if (row < m) out[row * n + col] = smem[t * tpg];
        }
    }
}

// in-place SiLU: x * sigmoid(x)
kernel void silu_inplace(
    device float* data [[buffer(0)]],
    constant uint& len [[buffer(1)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= len) return;
    float x = data[gid];
    data[gid] = x / (1.0 + exp(-x));
}

// Fused SwiGLU: input is concat'd [outer, 2N] (per-row up || gate).
// Output: [outer, N] where out[r,i] = up[r,i] * silu(gate[r,i]).
// One thread per output element. Each thread reads exactly two source
// values from the same row (up + gate) and writes one — no inter-thread
// communication, no shared memory, no reductions.
//
// Grid: total output elements (outer * N). The thread maps to (row, col)
// via the n_half stride. Up and gate live at offsets [row*2N + col] and
// [row*2N + N + col] respectively.
kernel void fused_swiglu(
    device const float* x  [[buffer(0)]],   // [outer, 2*n_half]
    device float* out      [[buffer(1)]],   // [outer, n_half]
    constant uint& n_half  [[buffer(2)]],
    constant uint& total   [[buffer(3)]],   // outer * n_half
    constant uint& gate_first [[buffer(4)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= total) return;
    uint row = gid / n_half;
    uint col = gid % n_half;
    uint base = row * (2u * n_half);
    float up;
    float gate;
    if (gate_first != 0u) {
        gate = x[base + col];
        up   = x[base + n_half + col];
    } else {
        up   = x[base + col];
        gate = x[base + n_half + col];
    }
    out[gid] = up * (gate / (1.0f + exp(-gate)));
}

// Half-precision variant: f16 in/out. Computation in f32 (silu's exp can
// underflow at half precision). Same dispatch as fused_swiglu.
kernel void fused_swiglu_h(
    device const half* x   [[buffer(0)]],
    device half* out       [[buffer(1)]],
    constant uint& n_half  [[buffer(2)]],
    constant uint& total   [[buffer(3)]],
    constant uint& gate_first [[buffer(4)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= total) return;
    uint row = gid / n_half;
    uint col = gid % n_half;
    uint base = row * (2u * n_half);
    float up;
    float gate;
    if (gate_first != 0u) {
        gate = float(x[base + col]);
        up   = float(x[base + n_half + col]);
    } else {
        up   = float(x[base + col]);
        gate = float(x[base + n_half + col]);
    }
    out[gid] = half(up * (gate / (1.0f + exp(-gate))));
}

// SwiGLU + cast: f32 input, f16 output. Saves a separate cast pass when
// the next consumer wants half precision. Reserved for paths where the
// AutoMixedPrecision boundary lands right after SwiGLU.
kernel void fused_swiglu_cast_f32_to_f16(
    device const float* x  [[buffer(0)]],
    device half* out       [[buffer(1)]],
    constant uint& n_half  [[buffer(2)]],
    constant uint& total   [[buffer(3)]],
    constant uint& gate_first [[buffer(4)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= total) return;
    uint row = gid / n_half;
    uint col = gid % n_half;
    uint base = row * (2u * n_half);
    float up;
    float gate;
    if (gate_first != 0u) {
        gate = x[base + col];
        up   = x[base + n_half + col];
    } else {
        up   = x[base + col];
        gate = x[base + n_half + col];
    }
    out[gid] = half(up * (gate / (1.0f + exp(-gate))));
}

// SwiGLU + cast: f16 input, f32 output. Symmetric to the above.
kernel void fused_swiglu_cast_f16_to_f32(
    device const half* x   [[buffer(0)]],
    device float* out      [[buffer(1)]],
    constant uint& n_half  [[buffer(2)]],
    constant uint& total   [[buffer(3)]],
    constant uint& gate_first [[buffer(4)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= total) return;
    uint row = gid / n_half;
    uint col = gid % n_half;
    uint base = row * (2u * n_half);
    float up;
    float gate;
    if (gate_first != 0u) {
        gate = float(x[base + col]);
        up   = float(x[base + n_half + col]);
    } else {
        up   = float(x[base + col]);
        gate = float(x[base + n_half + col]);
    }
    out[gid] = up * (gate / (1.0f + exp(-gate)));
}

// LayerNorm: out = (x - mean) * inv_std * gamma + beta, per row
// One threadgroup per row; reductions via threadgroup memory.
kernel void layer_norm(
    device const float* input [[buffer(0)]],
    device const float* gamma [[buffer(1)]],
    device const float* beta  [[buffer(2)]],
    device float* output      [[buffer(3)]],
    constant uint& h          [[buffer(4)]],
    constant float& eps       [[buffer(5)]],
    uint row [[threadgroup_position_in_grid]],
    uint tid [[thread_position_in_threadgroup]],
    uint tsize [[threads_per_threadgroup]]
) {
    threadgroup float partial_sum[256];
    threadgroup float partial_dev[256];

    // TWO-PASS: var = mean((x−mean)²). The one-pass E[x²]−mean² catastrophically
    // cancels in f32 on rows with a large DC offset (pre-norm ViT/DINOv2) — this
    // matches the CPU oracle.
    // Pass 1: mean.
    float local_sum = 0.0;
    for (uint i = tid; i < h; i += tsize) {
        local_sum += input[row * h + i];
    }
    partial_sum[tid] = local_sum;
    threadgroup_barrier(mem_flags::mem_threadgroup);
    for (uint stride = tsize / 2; stride > 0; stride /= 2) {
        if (tid < stride) {
            partial_sum[tid] += partial_sum[tid + stride];
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }
    float mean = partial_sum[0] / float(h);

    // Pass 2: mean of squared deviation (separate array → no extra barrier).
    float local_dev = 0.0;
    for (uint i = tid; i < h; i += tsize) {
        float d = input[row * h + i] - mean;
        local_dev += d * d;
    }
    partial_dev[tid] = local_dev;
    threadgroup_barrier(mem_flags::mem_threadgroup);
    for (uint stride = tsize / 2; stride > 0; stride /= 2) {
        if (tid < stride) {
            partial_dev[tid] += partial_dev[tid + stride];
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }
    float var = fmax(0.0f, partial_dev[0] / float(h));
    float inv_std = rsqrt(var + eps);

    // Pass 3: normalize.
    for (uint i = tid; i < h; i += tsize) {
        float v = input[row * h + i];
        output[row * h + i] = (v - mean) * inv_std * gamma[i] + beta[i];
    }
}

// RMSNorm: out = (x / sqrt(mean(x^2) + eps)) * gamma + beta. No mean
// subtraction. Same dispatch shape as layer_norm (one threadgroup per row,
// power-of-2 reduction within the group).
kernel void rms_norm(
    device const char* arena [[buffer(0)]],
    constant ulong& in_off [[buffer(1)]],
    constant ulong& g_off [[buffer(2)]],
    constant ulong& b_off [[buffer(3)]],
    constant ulong& out_off [[buffer(4)]],
    constant uint& h [[buffer(5)]],
    constant float& eps [[buffer(6)]],
    uint row [[threadgroup_position_in_grid]],
    uint tid [[thread_position_in_threadgroup]],
    uint tsize [[threads_per_threadgroup]]
) {
    device const float* input = (device const float*)(arena + in_off);
    device const float* gamma = (device const float*)(arena + g_off);
    device const float* beta = (device const float*)(arena + b_off);
    device float* output = (device float*)(arena + out_off);
    threadgroup float partial[256];
    // Two-pass: mean(x²) = mean((x−mean)²) + mean². Subtract the row mean first
    // so the deviation sum stays well-conditioned under a large DC offset (the
    // canonical form CPU + all backends match).
    // Pass 1: row mean.
    float local_sum = 0.0f;
    for (uint i = tid; i < h; i += tsize) {
        local_sum += input[row * h + i];
    }
    partial[tid] = local_sum;
    threadgroup_barrier(mem_flags::mem_threadgroup);
    for (uint stride = tsize / 2; stride > 0; stride /= 2) {
        if (tid < stride) {
            partial[tid] += partial[tid + stride];
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }
    float mean = partial[0] / float(h);
    threadgroup_barrier(mem_flags::mem_threadgroup);
    // Pass 2: mean of squared deviation.
    float local_sumsq = 0.0f;
    for (uint i = tid; i < h; i += tsize) {
        float d = input[row * h + i] - mean;
        local_sumsq += d * d;
    }
    partial[tid] = local_sumsq;
    threadgroup_barrier(mem_flags::mem_threadgroup);
    for (uint stride = tsize / 2; stride > 0; stride /= 2) {
        if (tid < stride) {
            partial[tid] += partial[tid + stride];
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }
    float inv_rms = rsqrt(partial[0] / float(h) + mean * mean + eps);
    for (uint i = tid; i < h; i += tsize) {
        output[row * h + i] = input[row * h + i] * inv_rms * gamma[i] + beta[i];
    }
}

// GDN gated norm: out = rms_norm(x) * silu(z). Same dispatch as rms_norm.
kernel void rms_norm_mul_silu(
    device const char* arena [[buffer(0)]],
    constant ulong& in_off [[buffer(1)]],
    constant ulong& g_off [[buffer(2)]],
    constant ulong& b_off [[buffer(3)]],
    constant ulong& z_off [[buffer(4)]],
    constant ulong& out_off [[buffer(5)]],
    constant uint& h [[buffer(6)]],
    constant float& eps [[buffer(7)]],
    uint row [[threadgroup_position_in_grid]],
    uint tid [[thread_position_in_threadgroup]],
    uint tsize [[threads_per_threadgroup]]
) {
    device const float* input = (device const float*)(arena + in_off);
    device const float* gamma = (device const float*)(arena + g_off);
    device const float* beta = (device const float*)(arena + b_off);
    device const float* z = (device const float*)(arena + z_off);
    device float* output = (device float*)(arena + out_off);
    threadgroup float partial_sumsq[256];
    float local_sumsq = 0.0f;
    for (uint i = tid; i < h; i += tsize) {
        float v = input[row * h + i];
        local_sumsq += v * v;
    }
    partial_sumsq[tid] = local_sumsq;
    threadgroup_barrier(mem_flags::mem_threadgroup);
    for (uint stride = tsize / 2; stride > 0; stride /= 2) {
        if (tid < stride) {
            partial_sumsq[tid] += partial_sumsq[tid + stride];
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }
    float inv_rms = rsqrt(partial_sumsq[0] / float(h) + eps);
    for (uint i = tid; i < h; i += tsize) {
        float zv = z[row * h + i];
        float silu = zv / (1.0f + exp(-zv));
        output[row * h + i] =
            (input[row * h + i] * inv_rms * gamma[i] + beta[i]) * silu;
    }
}

// f16 RMSNorm: half I/O, float accumulation.
kernel void rms_norm_h(
    device const char* arena [[buffer(0)]],
    constant ulong& in_off [[buffer(1)]],
    constant ulong& g_off [[buffer(2)]],
    constant ulong& b_off [[buffer(3)]],
    constant ulong& out_off [[buffer(4)]],
    constant uint& h [[buffer(5)]],
    constant float& eps [[buffer(6)]],
    uint row [[threadgroup_position_in_grid]],
    uint tid [[thread_position_in_threadgroup]],
    uint tsize [[threads_per_threadgroup]]
) {
    device const half* input = (device const half*)(arena + in_off);
    device const half* gamma = (device const half*)(arena + g_off);
    device const half* beta = (device const half*)(arena + b_off);
    device half* output = (device half*)(arena + out_off);
    threadgroup float partial_sumsq[256];
    float local_sumsq = 0.0f;
    for (uint i = tid; i < h; i += tsize) {
        float v = float(input[row * h + i]);
        local_sumsq += v * v;
    }
    partial_sumsq[tid] = local_sumsq;
    threadgroup_barrier(mem_flags::mem_threadgroup);
    for (uint stride = tsize / 2; stride > 0; stride /= 2) {
        if (tid < stride) {
            partial_sumsq[tid] += partial_sumsq[tid + stride];
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }
    float inv_rms = rsqrt(partial_sumsq[0] / float(h) + eps);
    for (uint i = tid; i < h; i += tsize) {
        float v = float(input[row * h + i]);
        output[row * h + i] = half(v * inv_rms * float(gamma[i]) + float(beta[i]));
    }
}

// f16 standalone softmax along the last axis. Half I/O, float accumulation
// for max + exp-sum (matters: f16 sum overflows above ~65k summands and
// exp() loses precision for moderate negatives).
kernel void softmax_lastax_h(
    device half* data     [[buffer(0)]],
    constant uint& cols   [[buffer(1)]],
    uint row [[threadgroup_position_in_grid]],
    uint tid [[thread_position_in_threadgroup]],
    uint tsize [[threads_per_threadgroup]]
) {
    threadgroup float partial[256];
    uint base = row * cols;

    float local_max = -INFINITY;
    for (uint i = tid; i < cols; i += tsize) {
        local_max = max(local_max, float(data[base + i]));
    }
    partial[tid] = local_max;
    threadgroup_barrier(mem_flags::mem_threadgroup);
    for (uint stride = tsize / 2; stride > 0; stride /= 2) {
        if (tid < stride) {
            partial[tid] = max(partial[tid], partial[tid + stride]);
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }
    float row_max = partial[0];

    float local_sum = 0.0f;
    for (uint i = tid; i < cols; i += tsize) {
        float e = exp(float(data[base + i]) - row_max);
        data[base + i] = half(e);
        local_sum += e;
    }
    partial[tid] = local_sum;
    threadgroup_barrier(mem_flags::mem_threadgroup);
    for (uint stride = tsize / 2; stride > 0; stride /= 2) {
        if (tid < stride) {
            partial[tid] += partial[tid + stride];
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }
    float inv_sum = 1.0f / partial[0];

    for (uint i = tid; i < cols; i += tsize) {
        data[base + i] = half(float(data[base + i]) * inv_sum);
    }
}

// f16 multi-axis reduce. Same op_kind encoding as reduce_axes; accumulate
// in float so 1e-2 .. 1e+4 f16 values don't lose precision summing across
// the reduced axis.
kernel void reduce_axes_h(
    device const half* src  [[buffer(0)]],
    device half* dst        [[buffer(1)]],
    constant uint& reduced  [[buffer(2)]],
    constant uint& inner    [[buffer(3)]],
    constant uint& op_kind  [[buffer(4)]],
    uint2 gid [[thread_position_in_grid]]
) {
    uint i = gid.x;
    uint o = gid.y;
    if (i >= inner) return;
    float acc;
    if      (op_kind == 2) acc = -INFINITY;
    else if (op_kind == 3) acc =  INFINITY;
    else if (op_kind == 4) acc =  1.0f;
    else                   acc =  0.0f;

    uint base = o * reduced * inner + i;
    for (uint r = 0; r < reduced; ++r) {
        float v = float(src[base + r * inner]);
        if      (op_kind == 0 || op_kind == 1) acc += v;
        else if (op_kind == 2) acc = max(acc, v);
        else if (op_kind == 3) acc = min(acc, v);
        else                   acc *= v;
    }
    if (op_kind == 1) acc /= float(reduced);
    dst[o * inner + i] = half(acc);
}

// PLAN L2 — interpreted N-ary element-wise chain kernel.
// One thread per output element. Walks the chain encoding (4 u32s
// per step: op_kind, op_sub, lhs_enc, rhs_enc) into a private
// scratch register array. Operand encoding: bit 31 = src kind
// (0=Input, 1=Step), bits 0..30 = index. Caps: 32 steps, 16 inputs.
inline uint region_input_row_resize2x_nchw(
    uint gid,
    uint out_n,
    uint out_c,
    uint out_h,
    uint out_w
) {
    uint plane = out_c * out_h * out_w;
    uint local = gid % plane;
    uint batch = gid / plane;
    uint w_pos = local % out_w;
    uint tmp = local / out_w;
    uint h_pos = tmp % out_h;
    uint c_pos = tmp / out_h;
    uint in_w = out_w / 2u;
    uint in_h = out_h / 2u;
    uint in_plane = out_c * in_h * in_w;
    return batch * in_plane + c_pos * in_h * in_w + (h_pos / 2u) * in_w + (w_pos / 2u);
}

inline uint region_resolve_row(
    uint gid,
    uint kind,
    uint idx,
    uint prologue_row0,
    uint has_prologue_row0,
    uint prologue_input,
    uint scalar_input_mask,
    device const uint* input_modulus
) {
    if (kind != 0u) { return 0u; }
    if (has_prologue_row0 != 0u && idx == prologue_input) {
        return prologue_row0;
    }
    if ((scalar_input_mask & (1u << idx)) != 0u) { return 0u; }
    if (input_modulus[idx] != 0u) { return gid % input_modulus[idx]; }
    return gid;
}

kernel void elementwise_region(
    device float* arena              [[buffer(0)]],
    constant uint& len               [[buffer(1)]],
    constant uint& num_inputs        [[buffer(2)]],
    constant uint& num_steps         [[buffer(3)]],
    constant uint& dst_off           [[buffer(4)]],
    device const uint* input_offs    [[buffer(5)]],   // 16 entries
    device const uint* chain         [[buffer(6)]],   // 128 entries (32 steps * 4)
    constant uint& scalar_input_mask [[buffer(7)]],
    device const uint* input_modulus [[buffer(8)]],   // 16 entries
    constant uint& prologue          [[buffer(9)]],
    constant uint& out_n             [[buffer(10)]],
    constant uint& out_c             [[buffer(11)]],
    constant uint& out_h             [[buffer(12)]],
    constant uint& out_w             [[buffer(13)]],
    constant uint& prologue_input    [[buffer(14)]],
    uint3 gpos [[thread_position_in_grid]]
) {
    uint gid;
    if (prologue == 1u) {
        uint nc = gpos.z;
        uint ho = gpos.y;
        uint wo = gpos.x;
        if (nc >= out_n * out_c || ho >= out_h || wo >= out_w) { return; }
        gid = nc * out_h * out_w + ho * out_w + wo;
    } else {
        gid = gpos.x;
        if (gid >= len) { return; }
    }
    uint prologue_row0 = 0u;
    uint has_prologue_row0 = 0u;
    if (prologue == 1u) {
        prologue_row0 = region_input_row_resize2x_nchw(gid, out_n, out_c, out_h, out_w);
        has_prologue_row0 = 1u;
    }
    float scratch[32];
    uint last_idx = 0;
    for (uint k = 0; k < num_steps; ++k) {
        uint base    = k * 4;
        uint op_kind = chain[base + 0];
        uint op_sub  = chain[base + 1];
        uint lhs_enc = chain[base + 2];
        uint rhs_enc = chain[base + 3];

        // resolve_operand inline. Scalar-broadcast inputs read element
        // 0 regardless of gid (fast path); trailing-shape broadcast
        // reads `gid % input_modulus[idx]`. `input_modulus[idx]==0`
        // means "no broadcast" and the kernel reads gid directly.
        float lhs;
        {
            uint kind = lhs_enc >> 31;
            uint idx  = lhs_enc & 0x7FFFFFFFu;
            uint row = region_resolve_row(
                gid, kind, idx, prologue_row0, has_prologue_row0, prologue_input,
                scalar_input_mask, input_modulus);
            lhs = (kind == 0u) ? arena[input_offs[idx] + row] : scratch[idx];
        }
        float result;
        if (op_kind == 4u) {
            // Where (3-operand select). op_sub carries cond_enc; lhs_enc
            // / rhs_enc carry on_true / on_false. lhs already resolved
            // above is on_true; resolve cond from op_sub and on_false
            // from rhs_enc here.
            float cond;
            {
                uint kind = op_sub >> 31;
                uint idx  = op_sub & 0x7FFFFFFFu;
                uint row = region_resolve_row(
                    gid, kind, idx, prologue_row0, has_prologue_row0, prologue_input,
                    scalar_input_mask, input_modulus);
                cond = (kind == 0u) ? arena[input_offs[idx] + row] : scratch[idx];
            }
            float on_false;
            {
                uint kind = rhs_enc >> 31;
                uint idx  = rhs_enc & 0x7FFFFFFFu;
                uint row = region_resolve_row(
                    gid, kind, idx, prologue_row0, has_prologue_row0, prologue_input,
                    scalar_input_mask, input_modulus);
                on_false = (kind == 0u) ? arena[input_offs[idx] + row] : scratch[idx];
            }
            result = (cond != 0.0f) ? lhs : on_false;
        } else if (op_kind == 0u) {
            // Activation
            if      (op_sub == 3u) result = max(lhs, 0.0f);                // Relu
            else if (op_sub == 0u || op_sub == 1u) {
                float c = 0.7978845608f;
                float inner = clamp(c * (lhs + 0.044715f * lhs * lhs * lhs), -15.0f, 15.0f);
                result = 0.5f * lhs * (1.0f + tanh(inner));                // Gelu
            }
            else if (op_sub == 2u) result = lhs / (1.0f + exp(-lhs));      // Silu
            else if (op_sub == 4u) result = 1.0f / (1.0f + exp(-lhs));     // Sigmoid
            else if (op_sub == 5u) result = tanh(clamp(lhs, -15.0f, 15.0f));
            else if (op_sub == 6u) result = exp(lhs);
            else if (op_sub == 7u) result = log(lhs);
            else if (op_sub == 8u) result = sqrt(lhs);
            else if (op_sub == 9u) result = 1.0f / sqrt(lhs);
            else if (op_sub == 10u) result = -lhs;
            else if (op_sub == 11u) result = fabs(lhs);
            else if (op_sub == 12u) result = round(lhs);
            else if (op_sub == 13u) result = sin(lhs);
            else if (op_sub == 14u) result = cos(lhs);
            else if (op_sub == 15u) result = tan(lhs);
            else if (op_sub == 16u) result = atan(lhs);
            else                    result = lhs;
        } else if (op_kind == 1u) {
            // Cast at f32-arena layer is identity
            result = lhs;
        } else {
            float rhs;
            {
                uint kind = rhs_enc >> 31;
                uint idx  = rhs_enc & 0x7FFFFFFFu;
                uint row = region_resolve_row(
                    gid, kind, idx, prologue_row0, has_prologue_row0, prologue_input,
                    scalar_input_mask, input_modulus);
                rhs = (kind == 0u) ? arena[input_offs[idx] + row] : scratch[idx];
            }
            if (op_kind == 2u) {
                if      (op_sub == 0u) result = lhs + rhs;
                else if (op_sub == 1u) result = lhs - rhs;
                else if (op_sub == 2u) result = lhs * rhs;
                else if (op_sub == 3u) result = lhs / rhs;
                else if (op_sub == 4u) result = max(lhs, rhs);
                else if (op_sub == 5u) result = min(lhs, rhs);
                else                   result = rlx_pow_scalar(lhs, rhs);
            } else {
                bool b;
                if      (op_sub == 0u) b = (lhs == rhs);
                else if (op_sub == 1u) b = (lhs != rhs);
                else if (op_sub == 2u) b = (lhs <  rhs);
                else if (op_sub == 3u) b = (lhs <= rhs);
                else if (op_sub == 4u) b = (lhs >  rhs);
                else                   b = (lhs >= rhs);
                result = b ? 1.0f : 0.0f;
            }
        }
        scratch[k] = result;
        last_idx = k;
    }
    arena[dst_off + gid] = scratch[last_idx];
}

inline uint batch_region_resolve_row(
    uint gid,
    uint kind,
    uint idx,
    uint scalar_input_mask,
    constant uint* input_modulus
) {
    if (kind != 0u) { return 0u; }
    if ((scalar_input_mask & (1u << idx)) != 0u) { return 0u; }
    if (input_modulus[idx] != 0u) { return gid % input_modulus[idx]; }
    return gid;
}

// FKL batch horizontal fusion: one dispatch, thread_position_in_grid.z = slice index.
// Requires prologue == 0 (no resize prologue on batch slices).
kernel void batch_elementwise_region(
    device float* arena              [[buffer(0)]],
    constant uint& slice_len         [[buffer(1)]],
    constant uint& num_batch         [[buffer(2)]],
    constant uint& num_steps         [[buffer(3)]],
    constant uint& base_dst_off      [[buffer(4)]],
    constant uint& slice_elems       [[buffer(5)]],
    constant uint* batch_input_offs  [[buffer(6)]],   // 64 entries
    constant uint* chain             [[buffer(7)]],   // 128 entries
    constant uint& scalar_input_mask [[buffer(8)]],
    constant uint* input_modulus     [[buffer(9)]],   // 16 entries
    uint3 gpos [[thread_position_in_grid]]
) {
    uint batch_idx = gpos.z;
    if (batch_idx >= num_batch) { return; }
    uint i = gpos.x;
    if (i >= slice_len) { return; }

    uint input_offs[16];
    for (uint k = 0; k < 16u; ++k) { input_offs[k] = 0u; }
    input_offs[0] = batch_input_offs[batch_idx];
    uint dst_off = base_dst_off + batch_idx * slice_elems;

    float scratch[32];
    uint last_idx = 0;
    for (uint k = 0; k < num_steps; ++k) {
        uint base    = k * 4;
        uint op_kind = chain[base + 0];
        uint op_sub  = chain[base + 1];
        uint lhs_enc = chain[base + 2];
        uint rhs_enc = chain[base + 3];

        float lhs;
        {
            uint kind = lhs_enc >> 31;
            uint idx  = lhs_enc & 0x7FFFFFFFu;
            uint row = batch_region_resolve_row(
                i, kind, idx, scalar_input_mask, input_modulus);
            lhs = (kind == 0u) ? arena[input_offs[idx] + row] : scratch[idx];
        }
        float result;
        if (op_kind == 4u) {
            float cond;
            {
                uint kind = op_sub >> 31;
                uint idx  = op_sub & 0x7FFFFFFFu;
                uint row = batch_region_resolve_row(
                    i, kind, idx, scalar_input_mask, input_modulus);
                cond = (kind == 0u) ? arena[input_offs[idx] + row] : scratch[idx];
            }
            float on_false;
            {
                uint kind = rhs_enc >> 31;
                uint idx  = rhs_enc & 0x7FFFFFFFu;
                uint row = batch_region_resolve_row(
                    i, kind, idx, scalar_input_mask, input_modulus);
                on_false = (kind == 0u) ? arena[input_offs[idx] + row] : scratch[idx];
            }
            result = (cond != 0.0f) ? lhs : on_false;
        } else if (op_kind == 0u) {
            if      (op_sub == 3u) result = max(lhs, 0.0f);
            else if (op_sub == 0u || op_sub == 1u) {
                float c = 0.7978845608f;
                float inner = clamp(c * (lhs + 0.044715f * lhs * lhs * lhs), -15.0f, 15.0f);
                result = 0.5f * lhs * (1.0f + tanh(inner));
            }
            else if (op_sub == 2u) result = lhs / (1.0f + exp(-lhs));
            else if (op_sub == 4u) result = 1.0f / (1.0f + exp(-lhs));
            else if (op_sub == 5u) result = tanh(clamp(lhs, -15.0f, 15.0f));
            else if (op_sub == 6u) result = exp(lhs);
            else if (op_sub == 7u) result = log(lhs);
            else if (op_sub == 8u) result = sqrt(lhs);
            else if (op_sub == 9u) result = 1.0f / sqrt(lhs);
            else if (op_sub == 10u) result = -lhs;
            else if (op_sub == 11u) result = fabs(lhs);
            else if (op_sub == 12u) result = round(lhs);
            else if (op_sub == 13u) result = sin(lhs);
            else if (op_sub == 14u) result = cos(lhs);
            else if (op_sub == 15u) result = tan(lhs);
            else if (op_sub == 16u) result = atan(lhs);
            else                    result = lhs;
        } else if (op_kind == 1u) {
            result = lhs;
        } else {
            float rhs;
            {
                uint kind = rhs_enc >> 31;
                uint idx  = rhs_enc & 0x7FFFFFFFu;
                uint row = batch_region_resolve_row(
                    i, kind, idx, scalar_input_mask, input_modulus);
                rhs = (kind == 0u) ? arena[input_offs[idx] + row] : scratch[idx];
            }
            if (op_kind == 2u) {
                if      (op_sub == 0u) result = lhs + rhs;
                else if (op_sub == 1u) result = lhs - rhs;
                else if (op_sub == 2u) result = lhs * rhs;
                else if (op_sub == 3u) result = lhs / rhs;
                else if (op_sub == 4u) result = max(lhs, rhs);
                else if (op_sub == 5u) result = min(lhs, rhs);
                else                   result = rlx_pow_scalar(lhs, rhs);
            } else {
                bool b;
                if      (op_sub == 0u) b = (lhs == rhs);
                else if (op_sub == 1u) b = (lhs != rhs);
                else if (op_sub == 2u) b = (lhs <  rhs);
                else if (op_sub == 3u) b = (lhs <= rhs);
                else if (op_sub == 4u) b = (lhs >  rhs);
                else                   b = (lhs >= rhs);
                result = b ? 1.0f : 0.0f;
            }
        }
        scratch[k] = result;
        last_idx = k;
    }
    arena[dst_off + i] = scratch[last_idx];
}

// ── Gated DeltaNet scan (f32) ───────────────────────────────────────
// One thread per (batch, head) — same dispatch model as `selective_scan`.
// Each thread owns the full n×n state in device scratch (exclusive) and
// scans seq serially. Matches CPU `execute_gated_delta_net_f32`.
// Offsets are ulong float indices: Fara-sized arenas place ephemeral GDN
// scratch past 16 GiB, which overflows uint indexing.
#define GDN_MAX_N 128u

kernel void gated_delta_net(
    device float* arena        [[buffer(0)]],
    constant ulong& q_off      [[buffer(1)]],
    constant ulong& k_off      [[buffer(2)]],
    constant ulong& v_off      [[buffer(3)]],
    constant ulong& g_off      [[buffer(4)]],
    constant ulong& beta_off   [[buffer(5)]],
    constant ulong& state_off  [[buffer(6)]],
    constant ulong& dst_off    [[buffer(7)]],
    constant uint4& dims       [[buffer(8)]], // batch, seq, heads, n
    constant uint& use_carry   [[buffer(9)]],
    constant uint& gate_per_channel [[buffer(10)]],
    uint gid [[thread_position_in_grid]]
) {
    const uint b = dims.x, s = dims.y, h = dims.z, n = dims.w;
    if (n > GDN_MAX_N || n == 0u || gid >= b * h) return;

    const uint bi = gid / h;
    const uint hi = gid % h;
    const float scale = rsqrt(float(n));
    const uint hs_n = h * n;
    const ulong s_base = state_off + (ulong)((bi * h + hi) * n * n);
    device float* s_mat = arena + s_base;

    if (use_carry == 0u) {
        for (uint i = 0u; i < n * n; ++i) {
            s_mat[i] = 0.0f;
        }
    }

    float sk[GDN_MAX_N];

    for (uint ti = 0u; ti < s; ++ti) {
        const uint qkv_step = bi * s * hs_n + ti * hs_n + hi * n;
        const uint gb_step = bi * s * h + ti * h + hi;
        device const float* q_ptr = arena + q_off + qkv_step;
        device const float* k_ptr = arena + k_off + qkv_step;
        device const float* v_ptr = arena + v_off + qkv_step;
        const float g_exp = (gate_per_channel == 0u) ? exp(arena[g_off + gb_step]) : 0.0f;
        const float beta_t = arena[beta_off + gb_step];

        if (gate_per_channel != 0u) {
            // per-channel decay: S[i,j] *= exp(g[i]), g is [b,s,h,n]
            for (uint i = 0u; i < n; ++i) {
                const float a = exp(arena[g_off + qkv_step + i]);
                for (uint jj = 0u; jj < n; ++jj) {
                    s_mat[i * n + jj] *= a;
                }
            }
        } else {
            for (uint i = 0u; i < n * n; ++i) {
                s_mat[i] *= g_exp;
            }
        }
        for (uint j = 0u; j < n; ++j) {
            float acc = 0.0f;
            for (uint i = 0u; i < n; ++i) {
                acc += s_mat[i * n + j] * k_ptr[i];
            }
            sk[j] = (v_ptr[j] - acc) * beta_t;
        }
        device float* out = arena + dst_off + qkv_step;
        for (uint j = 0u; j < n; ++j) {
            const float d = sk[j];
            float y = 0.0f;
            for (uint i = 0u; i < n; ++i) {
                s_mat[i * n + j] += k_ptr[i] * d;
                y += s_mat[i * n + j] * q_ptr[i];
            }
            out[j] = y * scale;
        }
    }
}
// Simdgroup-cooperative GDN for n == 128 (Bonsai / Qwen3.5). llama.cpp-style:
// one simdgroup owns one state column; 32 threads × 4 floats keep that column
// in registers and use simd_sum — no threadgroup barriers inside the scan.
// Grid: (n/NSG, heads, batch) threadgroups, threads (32, NSG, 1).
// Opt-in via RLX_METAL_GDN_SG=1; column-private kernel above is the default.
constant uint GDN_SG_N = 128u;
constant uint GDN_SG_NSG = 4u;

kernel void gated_delta_net_sg(
    device float* arena        [[buffer(0)]],
    constant ulong& q_off      [[buffer(1)]],
    constant ulong& k_off      [[buffer(2)]],
    constant ulong& v_off      [[buffer(3)]],
    constant ulong& g_off      [[buffer(4)]],
    constant ulong& beta_off   [[buffer(5)]],
    constant ulong& state_off  [[buffer(6)]],
    constant ulong& dst_off    [[buffer(7)]],
    constant uint4& dims       [[buffer(8)]], // batch, seq, heads, n
    constant uint& use_carry   [[buffer(9)]],
    constant uint& gate_per_channel [[buffer(10)]],
    uint3 tgpig                [[threadgroup_position_in_grid]],
    uint3 tpitg                [[thread_position_in_threadgroup]]
) {
    const uint b = dims.x, s = dims.y, h = dims.z, n = dims.w;
    if (n != GDN_SG_N) return;

    const uint tx = tpitg.x; // 0..31 within simdgroup
    const uint ty = tpitg.y; // simdgroup index in threadgroup
    const uint bi = tgpig.z;
    const uint hi = tgpig.y;
    const uint col = tgpig.x * GDN_SG_NSG + ty; // state column j
    if (bi >= b || hi >= h || col >= n || tx >= 32u) return;

    const float scale = rsqrt(float(n));
    const ulong s_base = state_off + (ulong)((bi * h + hi) * n * n);
    device float* s_mat = arena + s_base;

    // Register tile of column `col`: S[is][col] for is = tx*4 .. tx*4+3.
    float ls[GDN_SG_NSG];
    #pragma unroll
    for (uint j = 0u; j < GDN_SG_NSG; ++j) {
        const uint is = tx * GDN_SG_NSG + j;
        ls[j] = (use_carry != 0u) ? s_mat[is * n + col] : 0.0f;
    }

    const uint hs_n = h * n;
    for (uint ti = 0u; ti < s; ++ti) {
        const uint qkv_step = bi * s * hs_n + ti * hs_n + hi * n;
        const uint gb_step  = bi * s * h + ti * h + hi;
        device const float* q_ptr = arena + q_off + qkv_step;
        device const float* k_ptr = arena + k_off + qkv_step;
        device const float* v_ptr = arena + v_off + qkv_step;
        const float g_exp = (gate_per_channel == 0u) ? exp(arena[g_off + gb_step]) : 0.0f;
        const float beta_t = arena[beta_off + gb_step];

        float s_k = 0.0f;
        #pragma unroll
        for (uint j = 0u; j < GDN_SG_NSG; ++j) {
            const uint is = tx * GDN_SG_NSG + j;
            // per-channel decay uses the row/key channel `is`; per-head uses g_exp.
            const float a = (gate_per_channel != 0u) ? exp(arena[g_off + qkv_step + is]) : g_exp;
            ls[j] *= a;
            s_k += ls[j] * k_ptr[is];
        }
        s_k = simd_sum(s_k);

        const float d = (v_ptr[col] - s_k) * beta_t;

        float y = 0.0f;
        #pragma unroll
        for (uint j = 0u; j < GDN_SG_NSG; ++j) {
            const uint is = tx * GDN_SG_NSG + j;
            ls[j] += k_ptr[is] * d;
            y += ls[j] * q_ptr[is];
        }
        y = simd_sum(y);

        if (tx == 0u) {
            arena[dst_off + qkv_step + col] = y * scale;
        }
    }

    // Write column back (carry / next step).
    #pragma unroll
    for (uint j = 0u; j < GDN_SG_NSG; ++j) {
        const uint is = tx * GDN_SG_NSG + j;
        s_mat[is * n + col] = ls[j];
    }
}

// ── Selective scan (Mamba SSM, f32) ─────────────────────────────────
// One thread per (batch, channel); each thread owns a private state
// vector of size n (n ≤ SSM_MAX_N) and scans sequentially over seq.
// Inputs (float indices): x, delta [b,s,h]; a [h,n]; b, c [b,s,n].
// Output [b,s,h]. Matches `execute_selective_scan_f32` on CPU:
//   h[t] = exp(Δ·A)·h[t-1] + Δ·B·x;   y[t] = Σ_n C·h[t]
#define SSM_MAX_N 128u
kernel void selective_scan(
    device float* arena       [[buffer(0)]],
    constant uint& x_off      [[buffer(1)]],
    constant uint& delta_off  [[buffer(2)]],
    constant uint& a_off      [[buffer(3)]],
    constant uint& b_off      [[buffer(4)]],
    constant uint& c_off      [[buffer(5)]],
    constant uint& dst_off    [[buffer(6)]],
    constant uint4& dims      [[buffer(7)]], // batch, seq, hidden, n
    uint gid [[thread_position_in_grid]]
) {
    uint b = dims.x, s = dims.y, h = dims.z, n = dims.w;
    if (n > SSM_MAX_N || gid >= b * h) return;

    uint bi = gid / h;
    uint ci = gid % h;

    float state[SSM_MAX_N];
    for (uint i = 0; i < n; ++i) {
        state[i] = 0.0f;
    }

    // a[ci, :] is constant across the sequence for this channel.
    uint a_base = a_off + ci * n;

    for (uint si = 0; si < s; ++si) {
        uint bsh = bi * s * h + si * h + ci;   // x/delta/out element offset
        uint bsn = (bi * s + si) * n;          // b/c row base
        float d = arena[delta_off + bsh];
        float xv = arena[x_off + bsh];
        float acc = 0.0f;
        for (uint ni = 0; ni < n; ++ni) {
            float da = exp(d * arena[a_base + ni]);
            float st = da * state[ni] + d * arena[b_off + bsn + ni] * xv;
            state[ni] = st;
            acc += arena[c_off + bsn + ni] * st;
        }
        arena[dst_off + bsh] = acc;
    }
}

// LSTM (gate order i, f, g, o; single merged bias). Dispatched once per (layer,
// direction) by `encode_lstm`, which loops layers×dirs and ping-pongs
// intermediate layer outputs through an in-arena scratch region (x_off/dst_off
// are absolute arena word offsets). One threadgroup per batch item; thread `k`
// owns hidden unit `k`, keeps `c[k]` in a register, shares `h_prev` in
// threadgroup memory. `h0_off`/`c0_off` (0 → 0) seed hidden/cell state; `more` =
// (h0_off, out_width, dir_off, reverse). Single-layer/unidir/no-carry reduces to
// the plain kernel. Matches `execute_lstm_f32` on CPU. hidden ≤ LSTM_MAX_H.
#define LSTM_MAX_H 1024u
kernel void lstm(
    device float* arena      [[buffer(0)]],
    constant uint& x_off     [[buffer(1)]],
    constant uint& wih_off   [[buffer(2)]],
    constant uint& whh_off   [[buffer(3)]],
    constant uint& bias_off  [[buffer(4)]],
    constant uint& dst_off   [[buffer(5)]],
    constant uint4& dims     [[buffer(6)]], // batch, seq, in_l, hidden
    constant uint4& more     [[buffer(7)]], // h0_off, out_width, dir_off, reverse
    constant uint& c0_off    [[buffer(8)]],
    uint gid [[threadgroup_position_in_grid]],
    uint tid [[thread_index_in_threadgroup]]
) {
    uint b = dims.x, s = dims.y, in_sz = dims.z, h = dims.w;
    if (h > LSTM_MAX_H || gid >= b || tid >= h) return;
    uint bi = gid;
    uint k = tid;
    uint h0_off = more.x, out_width = more.y, dir_off = more.z, reverse = more.w;

    threadgroup float h_sh[LSTM_MAX_H];
    h_sh[k] = (h0_off != 0u) ? arena[h0_off + bi * h + k] : 0.0f;
    float c_k = (c0_off != 0u) ? arena[c0_off + bi * h + k] : 0.0f;
    threadgroup_barrier(mem_flags::mem_threadgroup);

    for (uint step = 0; step < s; ++step) {
        uint t = (reverse != 0u) ? (s - 1u - step) : step;
        uint x_base = x_off + (bi * s + t) * in_sz;
        // Gate pre-activations for hidden unit k: rows i=k, f=h+k, g=2h+k, o=3h+k.
        float z[4];
        for (uint gate = 0u; gate < 4u; ++gate) {
            uint r = gate * h + k;
            float acc = arena[bias_off + r];
            uint wih_row = wih_off + r * in_sz;
            for (uint j = 0u; j < in_sz; ++j) {
                acc += arena[wih_row + j] * arena[x_base + j];
            }
            uint whh_row = whh_off + r * h;
            for (uint j = 0u; j < h; ++j) {
                acc += arena[whh_row + j] * h_sh[j];
            }
            z[gate] = acc;
        }
        float i_g = 1.0f / (1.0f + exp(-z[0]));
        float f_g = 1.0f / (1.0f + exp(-z[1]));
        float g_g = tanh(clamp(z[2], -15.0f, 15.0f));
        float o_g = 1.0f / (1.0f + exp(-z[3]));
        c_k = f_g * c_k + i_g * g_g;
        float h_k = o_g * tanh(c_k);
        // Finish reading h_prev across all threads before overwriting it.
        threadgroup_barrier(mem_flags::mem_threadgroup);
        h_sh[k] = h_k;
        threadgroup_barrier(mem_flags::mem_threadgroup);
        arena[dst_off + (bi * s + t) * out_width + dir_off + k] = h_k;
    }
}

// Single-layer unidirectional GRU (gate order r, z, n; linear_before_reset=1;
// separate b_ih/b_hh; h0 = 0). One threadgroup per batch item; thread `k` owns
// hidden unit `k`. Matches `execute_gru_f32` on CPU.
#define GRU_MAX_H 1024u
// GRU dispatched once per (layer, direction) — the caller (`encode_gru`) loops
// layers×dirs, ping-ponging intermediate layer outputs through an in-arena
// scratch region (x_off/dst_off are absolute arena word offsets, possibly into
// scratch). `dims.z` = in_l (input_size for layer 0, else dirs·hidden). `more`
// carries h0_off (0 → h0=0, else seed from arena[h0_off+bi·h+k]), out_width =
// dirs·hidden, dir_off = dir·hidden (this dir owns [dir_off,dir_off+h)), and
// reverse (walk the sequence backwards). Single-layer/unidir/no-carry reduces to
// the original kernel (h0_off=0, out_width=h, dir_off=0, reverse=0).
kernel void gru(
    device float* arena      [[buffer(0)]],
    constant uint& x_off     [[buffer(1)]],
    constant uint& wih_off   [[buffer(2)]],
    constant uint& whh_off   [[buffer(3)]],
    constant uint& bih_off   [[buffer(4)]],
    constant uint& bhh_off   [[buffer(5)]],
    constant uint& dst_off   [[buffer(6)]],
    constant uint4& dims     [[buffer(7)]], // batch, seq, in_l, hidden
    constant uint4& more     [[buffer(8)]], // h0_off, out_width, dir_off, reverse
    uint gid [[threadgroup_position_in_grid]],
    uint tid [[thread_index_in_threadgroup]]
) {
    uint b = dims.x, s = dims.y, in_sz = dims.z, h = dims.w;
    if (h > GRU_MAX_H || gid >= b || tid >= h) return;
    uint bi = gid, k = tid;
    uint h0_off = more.x, out_width = more.y, dir_off = more.z, reverse = more.w;

    threadgroup float h_sh[GRU_MAX_H];
    h_sh[k] = (h0_off != 0u) ? arena[h0_off + bi * h + k] : 0.0f;
    threadgroup_barrier(mem_flags::mem_threadgroup);

    for (uint step = 0; step < s; ++step) {
        uint t = (reverse != 0u) ? (s - 1u - step) : step;
        uint x_base = x_off + (bi * s + t) * in_sz;
        // Gate rows r=k, z=h+k, n=2h+k. Input and hidden parts kept separate
        // because the reset gate multiplies the hidden term after its bias.
        float xi[3], hi[3];
        for (uint g = 0u; g < 3u; ++g) {
            uint r = g * h + k;
            float ax = arena[bih_off + r];
            uint wih_row = wih_off + r * in_sz;
            for (uint j = 0u; j < in_sz; ++j) {
                ax += arena[wih_row + j] * arena[x_base + j];
            }
            float ah = arena[bhh_off + r];
            uint whh_row = whh_off + r * h;
            for (uint j = 0u; j < h; ++j) {
                ah += arena[whh_row + j] * h_sh[j];
            }
            xi[g] = ax;
            hi[g] = ah;
        }
        float rg = 1.0f / (1.0f + exp(-(xi[0] + hi[0])));
        float zg = 1.0f / (1.0f + exp(-(xi[1] + hi[1])));
        float ng = tanh(clamp(xi[2] + rg * hi[2], -15.0f, 15.0f));
        float h_k = (1.0f - zg) * ng + zg * h_sh[k];
        // Finish reading h_prev across all threads before overwriting.
        threadgroup_barrier(mem_flags::mem_threadgroup);
        h_sh[k] = h_k;
        threadgroup_barrier(mem_flags::mem_threadgroup);
        arena[dst_off + (bi * s + t) * out_width + dir_off + k] = h_k;
    }
}

// Elman RNN (`relu_flag` ? relu : tanh). Dispatched once per (layer, direction)
// by `encode_rnn` — same in-arena scratch ping-pong and `dims.z`=in_l / `more`
// convention as `gru` above. Single-layer/unidir/no-carry reduces to the
// original kernel. Matches `execute_rnn_f32` on CPU.
#define RNN_MAX_H 1024u
kernel void rnn(
    device float* arena       [[buffer(0)]],
    constant uint& x_off      [[buffer(1)]],
    constant uint& wih_off    [[buffer(2)]],
    constant uint& whh_off    [[buffer(3)]],
    constant uint& bias_off   [[buffer(4)]],
    constant uint& dst_off    [[buffer(5)]],
    constant uint4& dims      [[buffer(6)]], // batch, seq, in_l, hidden
    constant uint& relu_flag  [[buffer(7)]],
    constant uint4& more      [[buffer(8)]], // h0_off, out_width, dir_off, reverse
    uint gid [[threadgroup_position_in_grid]],
    uint tid [[thread_index_in_threadgroup]]
) {
    uint b = dims.x, s = dims.y, in_sz = dims.z, h = dims.w;
    if (h > RNN_MAX_H || gid >= b || tid >= h) return;
    uint bi = gid, k = tid;
    uint h0_off = more.x, out_width = more.y, dir_off = more.z, reverse = more.w;

    threadgroup float h_sh[RNN_MAX_H];
    h_sh[k] = (h0_off != 0u) ? arena[h0_off + bi * h + k] : 0.0f;
    threadgroup_barrier(mem_flags::mem_threadgroup);

    for (uint step = 0; step < s; ++step) {
        uint t = (reverse != 0u) ? (s - 1u - step) : step;
        uint x_base = x_off + (bi * s + t) * in_sz;
        float acc = arena[bias_off + k];
        uint wih_row = wih_off + k * in_sz;
        for (uint j = 0u; j < in_sz; ++j) {
            acc += arena[wih_row + j] * arena[x_base + j];
        }
        uint whh_row = whh_off + k * h;
        for (uint j = 0u; j < h; ++j) {
            acc += arena[whh_row + j] * h_sh[j];
        }
        float h_k = relu_flag != 0u ? fmax(acc, 0.0f) : tanh(acc);
        threadgroup_barrier(mem_flags::mem_threadgroup);
        h_sh[k] = h_k;
        threadgroup_barrier(mem_flags::mem_threadgroup);
        arena[dst_off + (bi * s + t) * out_width + dir_off + k] = h_k;
    }
}

// Mamba-2 / SSD scalar-decay scan. One thread per (batch, head, head_dim_pos);
// each owns a private N-state vector and scans the sequence. Matches
// `execute_mamba2_f32` on CPU. n ≤ MAMBA2_MAX_N.
#define MAMBA2_MAX_N 128u
kernel void mamba2(
    device float* arena      [[buffer(0)]],
    constant uint& x_off     [[buffer(1)]],
    constant uint& dt_off    [[buffer(2)]],
    constant uint& a_off     [[buffer(3)]],
    constant uint& b_off     [[buffer(4)]],
    constant uint& c_off     [[buffer(5)]],
    constant uint& dst_off   [[buffer(6)]],
    constant uint4& dims     [[buffer(7)]], // batch, seq, heads, (head_dim<<16 | state_size)
    uint gid [[thread_position_in_grid]]
) {
    uint bn = dims.x, s = dims.y, hh = dims.z;
    uint p = dims.w >> 16, n = dims.w & 0xffffu;
    if (n > MAMBA2_MAX_N || gid >= bn * hh * p) return;

    uint pi = gid % p;
    uint hi = (gid / p) % hh;
    uint bi = gid / (p * hh);

    float state[MAMBA2_MAX_N];
    for (uint i = 0u; i < n; ++i) {
        state[i] = 0.0f;
    }
    float ah = arena[a_off + hi];

    for (uint t = 0u; t < s; ++t) {
        uint bsh = (bi * s + t) * hh + hi;
        float dt_t = arena[dt_off + bsh];
        float da = exp(dt_t * ah);
        float dtx = dt_t * arena[x_off + bsh * p + pi];
        uint bc = bsh * n;
        float acc = 0.0f;
        for (uint ni = 0u; ni < n; ++ni) {
            float st = da * state[ni] + dtx * arena[b_off + bc + ni];
            state[ni] = st;
            acc += st * arena[c_off + bc + ni];
        }
        arena[dst_off + bsh * p + pi] = acc;
    }
}

// RMSNorm backward (wrt: 0=dx, 1=dgamma, 2=dbeta). One threadgroup per row.
kernel void rms_norm_bwd(
    device const float* x [[buffer(0)]],
    device const float* gamma [[buffer(1)]],
    device const float* beta [[buffer(2)]],
    device const float* dy [[buffer(3)]],
    device float* out [[buffer(4)]],
    constant uint& inner [[buffer(5)]],
    constant float& eps [[buffer(6)]],
    constant uint& wrt [[buffer(7)]],
    uint row [[threadgroup_position_in_grid]],
    uint tid [[thread_position_in_threadgroup]],
    uint tsize [[threads_per_threadgroup]]
) {
    if (wrt != 0u) return;
    threadgroup float partial[256];
    float local_dot = 0.0f;
    for (uint i = tid; i < inner; i += tsize) {
        float xv = x[row * inner + i];
        float gv = gamma[i];
        float dyv = dy[row * inner + i];
        local_dot += dyv * gv * xv;
    }
    partial[tid] = local_dot;
    threadgroup_barrier(mem_flags::mem_threadgroup);
    for (uint stride = tsize / 2; stride > 0; stride /= 2) {
        if (tid < stride) partial[tid] += partial[tid + stride];
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }
    float dot = partial[0] / float(inner);
    float local_ss = 0.0f;
    for (uint i = tid; i < inner; i += tsize) {
        float xv = x[row * inner + i];
        local_ss += xv * xv;
    }
    partial[tid] = local_ss;
    threadgroup_barrier(mem_flags::mem_threadgroup);
    for (uint stride = tsize / 2; stride > 0; stride /= 2) {
        if (tid < stride) partial[tid] += partial[tid + stride];
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }
    float inv_r = rsqrt(partial[0] / float(inner) + eps);
    // Cross term is inv_r³ (= inv_r2·inv_r below), NOT inv_r⁴: outer ·inv_r already supplies
    // one factor, so the inner term uses inv_r2. The prior inv_r3-then-·inv_r was a stray 1/r.
    float inv_r2 = inv_r * inv_r;
    for (uint i = tid; i < inner; i += tsize) {
        float xv = x[row * inner + i];
        float gv = gamma[i];
        float dyv = dy[row * inner + i];
        float term = gv * dyv - xv * dot * inv_r2;
        out[row * inner + i] = term * inv_r;
    }
}

kernel void rms_norm_bwd_param(
    device const float* x [[buffer(0)]],
    device const float* gamma [[buffer(1)]],
    device const float* beta [[buffer(2)]],
    device const float* dy [[buffer(3)]],
    device float* out [[buffer(4)]],
    constant uint& rows [[buffer(5)]],
    constant uint& inner [[buffer(6)]],
    constant float& eps [[buffer(7)]],
    constant uint& wrt [[buffer(8)]],
    uint tid [[thread_position_in_threadgroup]]
) {
    if (tid != 0u) return;
    for (uint i = 0; i < inner; ++i) out[i] = 0.0f;
    for (uint row = 0; row < rows; ++row) {
        float sumsq = 0.0f;
        for (uint i = 0; i < inner; ++i) {
            float xv = x[row * inner + i];
            sumsq += xv * xv;
        }
        float inv_r = rsqrt(sumsq / float(inner) + eps);
        if (wrt == 1u) {
            for (uint i = 0; i < inner; ++i) {
                out[i] += dy[row * inner + i] * x[row * inner + i] * inv_r;
            }
        } else {
            for (uint i = 0; i < inner; ++i) {
                out[i] += dy[row * inner + i];
            }
        }
    }
}

// Per-row RMS inverse scale — scratch for parallel dgamma/dbeta.
kernel void rms_norm_bwd_inv_r_f32(
    device const float* x [[buffer(0)]],
    device float* inv_r [[buffer(1)]],
    constant uint& inner [[buffer(2)]],
    constant float& eps [[buffer(3)]],
    uint row [[thread_position_in_grid]]
) {
    float sumsq = 0.0f;
    for (uint i = 0; i < inner; ++i) {
        float xv = x[row * inner + i];
        sumsq += xv * xv;
    }
    inv_r[row] = rsqrt(sumsq / float(inner) + eps);
}

// Reduce dy·x·inv_r (gamma) or dy (beta) across rows — one thread per param.
kernel void rms_norm_bwd_param_reduce_f32(
    device const float* x [[buffer(0)]],
    device const float* dy [[buffer(1)]],
    device const float* inv_r [[buffer(2)]],
    device float* out [[buffer(3)]],
    constant uint& rows [[buffer(4)]],
    constant uint& inner [[buffer(5)]],
    constant uint& wrt [[buffer(6)]],
    uint i [[thread_position_in_grid]]
) {
    if (i >= inner) return;
    float acc = 0.0f;
    for (uint row = 0; row < rows; ++row) {
        if (wrt == 1u) {
            acc += dy[row * inner + i] * x[row * inner + i] * inv_r[row];
        } else {
            acc += dy[row * inner + i];
        }
    }
    out[i] = acc;
}

// LayerNorm backward (last-axis rows). Matches CPU `LayerNormBackward*`.
// One threadgroup per row; power-of-2 TG reduction.
kernel void layer_norm_bwd(
    device const float* x [[buffer(0)]],
    device const float* gamma [[buffer(1)]],
    device const float* dy [[buffer(2)]],
    device float* dx [[buffer(3)]],
    constant uint& inner [[buffer(4)]],
    constant float& eps [[buffer(5)]],
    uint row [[threadgroup_position_in_grid]],
    uint tid [[thread_position_in_threadgroup]],
    uint tsize [[threads_per_threadgroup]]
) {
    threadgroup float partial_a[256];
    threadgroup float partial_b[256];
    float n_inv = 1.0f / float(inner);

    float local_sum = 0.0f;
    for (uint i = tid; i < inner; i += tsize) {
        local_sum += x[row * inner + i];
    }
    partial_a[tid] = local_sum;
    threadgroup_barrier(mem_flags::mem_threadgroup);
    for (uint stride = tsize / 2; stride > 0; stride /= 2) {
        if (tid < stride) partial_a[tid] += partial_a[tid + stride];
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }
    float mean = partial_a[0] * n_inv;

    float local_var = 0.0f;
    for (uint i = tid; i < inner; i += tsize) {
        float d = x[row * inner + i] - mean;
        local_var += d * d;
    }
    partial_a[tid] = local_var;
    threadgroup_barrier(mem_flags::mem_threadgroup);
    for (uint stride = tsize / 2; stride > 0; stride /= 2) {
        if (tid < stride) partial_a[tid] += partial_a[tid + stride];
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }
    float inv_std = rsqrt(partial_a[0] * n_inv + eps);

    float local_sy = 0.0f;
    float local_sxh = 0.0f;
    for (uint i = tid; i < inner; i += tsize) {
        float xh = (x[row * inner + i] - mean) * inv_std;
        float sy = dy[row * inner + i] * gamma[i];
        local_sy += sy;
        local_sxh += sy * xh;
    }
    partial_a[tid] = local_sy;
    partial_b[tid] = local_sxh;
    threadgroup_barrier(mem_flags::mem_threadgroup);
    for (uint stride = tsize / 2; stride > 0; stride /= 2) {
        if (tid < stride) {
            partial_a[tid] += partial_a[tid + stride];
            partial_b[tid] += partial_b[tid + stride];
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }
    float m_sy = partial_a[0] * n_inv;
    float m_sxh = partial_b[0] * n_inv;

    for (uint i = tid; i < inner; i += tsize) {
        float xh = (x[row * inner + i] - mean) * inv_std;
        float sy = dy[row * inner + i] * gamma[i];
        dx[row * inner + i] = inv_std * (sy - m_sy - xh * m_sxh);
    }
}

// Serial dgamma: one thread walks all rows (small-row / no-scratch path).
kernel void layer_norm_bwd_gamma(
    device const float* x [[buffer(0)]],
    device const float* dy [[buffer(1)]],
    device float* dgamma [[buffer(2)]],
    constant uint& rows [[buffer(3)]],
    constant uint& inner [[buffer(4)]],
    constant float& eps [[buffer(5)]],
    uint tid [[thread_position_in_threadgroup]]
) {
    if (tid != 0u) return;
    for (uint i = 0; i < inner; ++i) dgamma[i] = 0.0f;
    float n_inv = 1.0f / float(inner);
    for (uint row = 0; row < rows; ++row) {
        float sum = 0.0f;
        for (uint i = 0; i < inner; ++i) sum += x[row * inner + i];
        float mean = sum * n_inv;
        float var = 0.0f;
        for (uint i = 0; i < inner; ++i) {
            float d = x[row * inner + i] - mean;
            var += d * d;
        }
        float inv_std = rsqrt(var * n_inv + eps);
        for (uint i = 0; i < inner; ++i) {
            float xh = (x[row * inner + i] - mean) * inv_std;
            dgamma[i] += dy[row * inner + i] * xh;
        }
    }
}

// Per-row mean + inv_std into scratch[row*2 + {0,1}] for parallel dgamma.
kernel void layer_norm_bwd_stats_f32(
    device const float* x [[buffer(0)]],
    device float* stats [[buffer(1)]],
    constant uint& inner [[buffer(2)]],
    constant float& eps [[buffer(3)]],
    uint row [[thread_position_in_grid]]
) {
    float n_inv = 1.0f / float(inner);
    float sum = 0.0f;
    for (uint i = 0; i < inner; ++i) sum += x[row * inner + i];
    float mean = sum * n_inv;
    float var = 0.0f;
    for (uint i = 0; i < inner; ++i) {
        float d = x[row * inner + i] - mean;
        var += d * d;
    }
    stats[row * 2u + 0u] = mean;
    stats[row * 2u + 1u] = rsqrt(var * n_inv + eps);
}

// One thread per last-axis index: sum_r dy[r,i] * x_hat[r,i].
kernel void layer_norm_bwd_gamma_reduce_f32(
    device const float* x [[buffer(0)]],
    device const float* dy [[buffer(1)]],
    device const float* stats [[buffer(2)]],
    device float* dgamma [[buffer(3)]],
    constant uint& rows [[buffer(4)]],
    constant uint& inner [[buffer(5)]],
    uint i [[thread_position_in_grid]]
) {
    if (i >= inner) return;
    float acc = 0.0f;
    for (uint row = 0; row < rows; ++row) {
        float mean = stats[row * 2u + 0u];
        float inv_std = stats[row * 2u + 1u];
        float xh = (x[row * inner + i] - mean) * inv_std;
        acc += dy[row * inner + i] * xh;
    }
    dgamma[i] = acc;
}

// SIMD-parallel variant: one 32-wide threadgroup (== 1 SIMD group) per column
// `i`; the 32 lanes split the `rows` reduction and combine with simd_sum. The
// scalar kernel above dispatches ONE thread per column, each serially summing
// all rows (batch·seq ≈ 1024) — low occupancy + a fully serial reduction. This
// adds 32× row parallelism (same win as `sdpa_simd`'s softmax).
kernel void layer_norm_bwd_gamma_reduce_simd(
    device const float* x [[buffer(0)]],
    device const float* dy [[buffer(1)]],
    device const float* stats [[buffer(2)]],
    device float* dgamma [[buffer(3)]],
    constant uint& rows [[buffer(4)]],
    constant uint& inner [[buffer(5)]],
    uint i [[threadgroup_position_in_grid]],
    uint tid [[thread_position_in_threadgroup]],
    uint tsize [[threads_per_threadgroup]]
) {
    if (i >= inner) return;
    float local = 0.0f;
    for (uint row = tid; row < rows; row += tsize) {
        float mean = stats[row * 2u + 0u];
        float inv_std = stats[row * 2u + 1u];
        float xh = (x[row * inner + i] - mean) * inv_std;
        local += dy[row * inner + i] * xh;
    }
    float total = simd_sum(local);
    if (tid == 0) {
        dgamma[i] = total;
    }
}

// GroupNorm (NCHW) backward — matches CPU `training_bwd::group_norm_backward_*`.
// One threadgroup per (batch, group); 256-wide reductions.
kernel void group_norm_bwd_input(
    device const float* x [[buffer(0)]],
    device const float* gamma [[buffer(1)]],
    device const float* dy [[buffer(2)]],
    device float* dx [[buffer(3)]],
    constant uint4& nchw [[buffer(4)]],
    constant uint& num_groups [[buffer(5)]],
    constant float& eps [[buffer(6)]],
    uint ng [[threadgroup_position_in_grid]],
    uint tid [[thread_position_in_threadgroup]],
    uint tsize [[threads_per_threadgroup]]
) {
    uint batch = nchw.x;
    uint c = nchw.y;
    uint h = nchw.z;
    uint w = nchw.w;
    if (ng >= batch * num_groups) return;
    uint n = ng / num_groups;
    uint g = ng % num_groups;
    uint cpg = c / num_groups;
    uint c0 = g * cpg;
    uint spatial = h * w;
    uint count = cpg * spatial;
    float n_inv = 1.0f / float(count);
    uint plane = c * spatial;
    uint b_base = n * plane;

    threadgroup float partial_a[256];
    threadgroup float partial_b[256];

    float local_sum = 0.0f;
    for (uint i = tid; i < count; i += tsize) {
        uint c_off = i / spatial;
        uint s = i % spatial;
        local_sum += x[b_base + (c0 + c_off) * spatial + s];
    }
    partial_a[tid] = local_sum;
    threadgroup_barrier(mem_flags::mem_threadgroup);
    for (uint stride = tsize / 2; stride > 0; stride /= 2) {
        if (tid < stride) partial_a[tid] += partial_a[tid + stride];
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }
    float mean = partial_a[0] * n_inv;

    float local_var = 0.0f;
    for (uint i = tid; i < count; i += tsize) {
        uint c_off = i / spatial;
        uint s = i % spatial;
        float d = x[b_base + (c0 + c_off) * spatial + s] - mean;
        local_var += d * d;
    }
    partial_a[tid] = local_var;
    threadgroup_barrier(mem_flags::mem_threadgroup);
    for (uint stride = tsize / 2; stride > 0; stride /= 2) {
        if (tid < stride) partial_a[tid] += partial_a[tid + stride];
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }
    float inv_std = rsqrt(partial_a[0] * n_inv + eps);

    float local_sy = 0.0f;
    float local_sxh = 0.0f;
    for (uint i = tid; i < count; i += tsize) {
        uint c_off = i / spatial;
        uint s = i % spatial;
        uint gi = c0 + c_off;
        float xh = (x[b_base + gi * spatial + s] - mean) * inv_std;
        float sy = dy[b_base + gi * spatial + s] * gamma[gi];
        local_sy += sy;
        local_sxh += sy * xh;
    }
    partial_a[tid] = local_sy;
    partial_b[tid] = local_sxh;
    threadgroup_barrier(mem_flags::mem_threadgroup);
    for (uint stride = tsize / 2; stride > 0; stride /= 2) {
        if (tid < stride) {
            partial_a[tid] += partial_a[tid + stride];
            partial_b[tid] += partial_b[tid + stride];
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }
    float m_sy = partial_a[0] * n_inv;
    float m_sxh = partial_b[0] * n_inv;

    for (uint i = tid; i < count; i += tsize) {
        uint c_off = i / spatial;
        uint s = i % spatial;
        uint gi = c0 + c_off;
        float xh = (x[b_base + gi * spatial + s] - mean) * inv_std;
        float sy = dy[b_base + gi * spatial + s] * gamma[gi];
        dx[b_base + gi * spatial + s] = inv_std * (sy - m_sy - xh * m_sxh);
    }
}

kernel void group_norm_bwd_gamma(
    device const float* x [[buffer(0)]],
    device const float* dy [[buffer(1)]],
    device float* dgamma [[buffer(2)]],
    constant uint4& nchw [[buffer(3)]],
    constant uint& num_groups [[buffer(4)]],
    constant float& eps [[buffer(5)]],
    uint tid [[thread_position_in_threadgroup]]
) {
    if (tid != 0u) return;
    uint n = nchw.x;
    uint c = nchw.y;
    uint h = nchw.z;
    uint w = nchw.w;
    uint spatial = h * w;
    uint plane = c * spatial;
    uint cpg = c / num_groups;
    float n_inv = 1.0f / float(cpg * spatial);
    for (uint ch = 0; ch < c; ++ch) dgamma[ch] = 0.0f;
    for (uint bn = 0; bn < n; ++bn) {
        uint b_base = bn * plane;
        for (uint g = 0; g < num_groups; ++g) {
            uint c0 = g * cpg;
            float mean = 0.0f;
            for (uint ci = 0; ci < cpg; ++ci) {
                uint base = b_base + (c0 + ci) * spatial;
                for (uint s = 0; s < spatial; ++s) mean += x[base + s];
            }
            mean *= n_inv;
            float var = 0.0f;
            for (uint ci = 0; ci < cpg; ++ci) {
                uint base = b_base + (c0 + ci) * spatial;
                for (uint s = 0; s < spatial; ++s) {
                    float d = x[base + s] - mean;
                    var += d * d;
                }
            }
            float inv_std = rsqrt(var * n_inv + eps);
            for (uint ci = 0; ci < cpg; ++ci) {
                uint gi = c0 + ci;
                uint x_base = b_base + gi * spatial;
                uint dy_base = b_base + gi * spatial;
                float acc = dgamma[gi];
                for (uint s = 0; s < spatial; ++s) {
                    float xh = (x[x_base + s] - mean) * inv_std;
                    acc += dy[dy_base + s] * xh;
                }
                dgamma[gi] = acc;
            }
        }
    }
}

kernel void group_norm_bwd_beta(
    device const float* dy [[buffer(0)]],
    device float* dbeta [[buffer(1)]],
    constant uint4& nchw [[buffer(2)]],
    uint tid [[thread_position_in_threadgroup]]
) {
    if (tid != 0u) return;
    uint n = nchw.x;
    uint c = nchw.y;
    uint h = nchw.z;
    uint w = nchw.w;
    uint spatial = h * w;
    uint plane = c * spatial;
    for (uint ch = 0; ch < c; ++ch) dbeta[ch] = 0.0f;
    for (uint bn = 0; bn < n; ++bn) {
        uint b_base = bn * plane;
        for (uint ch = 0; ch < c; ++ch) {
            uint dy_base = b_base + ch * spatial;
            float acc = dbeta[ch];
            for (uint s = 0; s < spatial; ++s) acc += dy[dy_base + s];
            dbeta[ch] = acc;
        }
    }
}

kernel void rope_bwd(
    device const float* dy [[buffer(0)]],
    device const float* cos [[buffer(1)]],
    device const float* sin [[buffer(2)]],
    device float* dx [[buffer(3)]],
    constant uint& batch [[buffer(4)]],
    constant uint& seq [[buffer(5)]],
    constant uint& hidden [[buffer(6)]],
    constant uint& head_dim [[buffer(7)]],
    constant uint& n_rot [[buffer(8)]],
    constant uint& cos_len [[buffer(9)]],
    uint3 gid [[thread_position_in_grid]]
) {
    uint d = gid.x;
    uint hi = gid.y;
    uint bs = gid.z;
    if (d >= head_dim) return;
    uint nh = hidden / head_dim;
    if (hi >= nh) return;
    if (bs >= batch * seq) return;
    uint bi = bs / seq;
    uint si = bs % seq;
    uint rot_half = n_rot / 2u;
    // The cos/sin table stores exactly the rotation angles — `n_rot/2` per
    // token, NOT head_dim/2. For PARTIAL rope (n_rot < head_dim) a head_dim/2
    // row stride overruns into the next token's angles from position 1 onward,
    // which is why this indexes by `rot_half`. Full rope has n_rot == head_dim,
    // so rot_half == head_dim/2 and nothing changes there.
    uint tab_off = (si * rot_half) % max(cos_len, 1u);
    uint dy_base = bi * seq * hidden + si * hidden + hi * head_dim;
    uint dx_base = dy_base;
    if (d < rot_half) {
        float y1 = dy[dy_base + d];
        float y2 = dy[dy_base + rot_half + d];
        float c = cos[tab_off + d];
        float s = sin[tab_off + d];
        dx[dx_base + d] = y1 * c + y2 * s;
        dx[dx_base + rot_half + d] = -y1 * s + y2 * c;
    } else if (d >= n_rot) {
        dx[dx_base + d] = dy[dy_base + d];
    }
}

kernel void cumsum_fwd(
    device const float* src [[buffer(0)]],
    device float* dst [[buffer(1)]],
    constant uint& inner [[buffer(2)]],
    constant uint& exclusive [[buffer(3)]],
    uint row [[threadgroup_position_in_grid]]
) {
    float acc = 0.0f;
    for (uint i = 0; i < inner; ++i) {
        if (exclusive != 0u) {
            dst[row * inner + i] = acc;
            acc += src[row * inner + i];
        } else {
            acc += src[row * inner + i];
            dst[row * inner + i] = acc;
        }
    }
}

// Cumulative product / maximum along the last axis (one threadgroup per row).
// is_max=1 runs a running max (identity -inf) else a running product (identity 1).
kernel void cum_scan(
    device const float* src [[buffer(0)]],
    device float* dst [[buffer(1)]],
    constant uint& inner [[buffer(2)]],
    constant uint& exclusive [[buffer(3)]],
    constant uint& is_max [[buffer(4)]],
    uint row [[threadgroup_position_in_grid]]
) {
    float acc = (is_max != 0u) ? (-INFINITY) : 1.0f;
    for (uint i = 0; i < inner; ++i) {
        float v = src[row * inner + i];
        if (exclusive != 0u) {
            dst[row * inner + i] = acc;
            acc = (is_max != 0u) ? fmax(acc, v) : (acc * v);
        } else {
            acc = (is_max != 0u) ? fmax(acc, v) : (acc * v);
            dst[row * inner + i] = acc;
        }
    }
}

kernel void cumsum_bwd(
    device const float* dy [[buffer(0)]],
    device float* dx [[buffer(1)]],
    constant uint& inner [[buffer(2)]],
    constant uint& exclusive [[buffer(3)]],
    uint row [[threadgroup_position_in_grid]]
) {
    float suffix = 0.0f;
    for (int i = int(inner) - 1; i >= 0; --i) {
        uint ui = uint(i);
        if (exclusive != 0u) {
            dx[row * inner + ui] = suffix;
            suffix += dy[row * inner + ui];
        } else {
            suffix += dy[row * inner + ui];
            dx[row * inner + ui] = suffix;
        }
    }
}

// Single im2col element for conv weight backward GEMM (B[k_idx, n_col]).
inline float conv_bwd_im2col_elem(
    device const float* x,
    uint k_idx,
    uint n_col,
    uint c_in,
    uint h,
    uint w_in,
    uint h_out,
    uint w_out,
    uint kh,
    uint kw,
    uint sh,
    uint sw,
    uint ph,
    uint pw,
    uint dh,
    uint dw_dil
) {
    uint ho = k_idx / w_out;
    uint wo = k_idx % w_out;
    uint rem = n_col;
    uint ci = rem / (kh * kw);
    rem = rem % (kh * kw);
    uint ki = rem / kw;
    uint kj = rem % kw;
    int hi = (int)(ho * sh + ki * dh) - (int)ph;
    int wi = (int)(wo * sw + kj * dw_dil) - (int)pw;
    if (hi < 0 || wi < 0 || hi >= (int)h || wi >= (int)w_in) {
        return 0.0f;
    }
    return x[(ci * h + (uint)hi) * w_in + (uint)wi];
}

// dw = dy @ im2col(x) — 8×8 simdgroup tiles, B generated on the fly (no scratch).
kernel void conv2d_bwd_weight_gemm(
    device const float* dy [[buffer(0)]],
    device const float* x [[buffer(1)]],
    device float* dw [[buffer(2)]],
    constant uint& M [[buffer(3)]],
    constant uint& K [[buffer(4)]],
    constant uint& N [[buffer(5)]],
    constant uint4& nchw [[buffer(6)]],
    constant uint4& out_dims [[buffer(7)]],
    constant uint4& kshape [[buffer(8)]],
    constant uint4& padd [[buffer(9)]],
    uint2 tgid [[threadgroup_position_in_grid]],
    uint slid [[thread_index_in_threadgroup]]
) {
    uint row_base = tgid.y * 8;
    uint col_base = tgid.x * 8;
    if (row_base >= M || col_base >= N) return;

    uint c_in = nchw.x;
    uint h = nchw.y;
    uint w_in = nchw.z;
    uint h_out = out_dims.y;
    uint w_out = out_dims.z;
    uint kh = kshape.x;
    uint kw = kshape.y;
    uint sh = kshape.z;
    uint sw = kshape.w;
    uint ph = padd.x;
    uint pw = padd.y;
    uint dh = padd.z;
    uint dw_dil = padd.w;

    threadgroup float B_tg[64];
    simdgroup_float8x8 a, b, c;
    c = simdgroup_float8x8(0.0f);

    for (uint k0 = 0; k0 < K; k0 += 8) {
        for (uint i = 0; i < 2; ++i) {
            uint idx = i * 32 + slid;
            if (idx < 64) {
                uint br = idx / 8;
                uint bc = idx % 8;
                uint k_idx = k0 + br;
                uint n_col = col_base + bc;
                B_tg[idx] = (k_idx < K && n_col < N)
                    ? conv_bwd_im2col_elem(
                          x, k_idx, n_col, c_in, h, w_in, h_out, w_out, kh, kw, sh, sw, ph, pw,
                          dh, dw_dil)
                    : 0.0f;
            }
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);
        simdgroup_load(a, dy + row_base * K + k0, K);
        simdgroup_load(b, B_tg, 8);
        simdgroup_multiply_accumulate(c, a, b, c);
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }
    simdgroup_store(c, dw + row_base * N + col_base, N);
}

// dw = dy @ im2col(x) — 32×32 threadgroup tiles (requires M,K,N % 32 == 0).
kernel void conv2d_bwd_weight_gemm_4x4(
    device const float* dy [[buffer(0)]],
    device const float* x [[buffer(1)]],
    device float* dw [[buffer(2)]],
    constant uint& M [[buffer(3)]],
    constant uint& K [[buffer(4)]],
    constant uint& N [[buffer(5)]],
    constant uint4& nchw [[buffer(6)]],
    constant uint4& out_dims [[buffer(7)]],
    constant uint4& kshape [[buffer(8)]],
    constant uint4& padd [[buffer(9)]],
    uint2 tgid [[threadgroup_position_in_grid]],
    uint sgid [[simdgroup_index_in_threadgroup]],
    uint slid [[thread_index_in_simdgroup]]
) {
    uint sg_row = sgid / 4;
    uint sg_col = sgid % 4;
    uint tg_row_base = tgid.y * 32;
    uint tg_col_base = tgid.x * 32;

    uint c_in = nchw.x;
    uint h = nchw.y;
    uint w_in = nchw.z;
    uint h_out = out_dims.y;
    uint w_out = out_dims.z;
    uint kh = kshape.x;
    uint kw = kshape.y;
    uint sh = kshape.z;
    uint sw = kshape.w;
    uint ph = padd.x;
    uint pw = padd.y;
    uint dh = padd.z;
    uint dw_dil = padd.w;

    threadgroup float A_tg[32 * 32];
    threadgroup float B_tg[32 * 32];
    simdgroup_float8x8 a, b, c;
    c = simdgroup_float8x8(0.0f);

    for (uint kk = 0; kk < K; kk += 32) {
        uint linear = sgid * 32 + slid;
        for (uint i = 0; i < 2; ++i) {
            uint idx = i * 512 + linear;
            uint ar = idx / 32;
            uint ac = idx % 32;
            A_tg[idx] = dy[(tg_row_base + ar) * K + (kk + ac)];
        }
        for (uint i = 0; i < 2; ++i) {
            uint idx = i * 512 + linear;
            uint br = idx / 32;
            uint bc = idx % 32;
            uint k_idx = kk + br;
            uint n_col = tg_col_base + bc;
            B_tg[idx] = conv_bwd_im2col_elem(
                x, k_idx, n_col, c_in, h, w_in, h_out, w_out, kh, kw, sh, sw, ph, pw, dh,
                dw_dil);
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);

        for (uint k_inner = 0; k_inner < 32; k_inner += 8) {
            simdgroup_load(a, &A_tg[sg_row * 8 * 32 + k_inner], 32);
            simdgroup_load(b, &B_tg[k_inner * 32 + sg_col * 8], 32);
            simdgroup_multiply_accumulate(c, a, b, c);
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }

    uint out_row = tg_row_base + sg_row * 8;
    uint out_col = tg_col_base + sg_col * 8;
    simdgroup_store(c, &dw[out_row * N + out_col], N);
}

// Fast im2col when W_in = W_out = 1 (Voxtral codec `[1,C,T,1]` slices).
kernel void im2col_group_w1(
    device const float* x [[buffer(0)]],
    device float* col [[buffer(1)]],
    constant uint4& nchw [[buffer(2)]],     // [C_in/g, H, 1, unused]
    constant uint4& out_dims [[buffer(3)]], // [unused, H_out, 1, unused]
    constant uint4& kshape [[buffer(4)]],
    constant uint4& padd [[buffer(5)]],
    uint gid [[thread_position_in_grid]]
) {
    uint h_out = out_dims.y;
    uint c_in = nchw.x;
    uint h = nchw.y;
    uint kh = kshape.x;
    uint kw = kshape.y;
    uint sh = kshape.z;
    uint ph = padd.x;
    uint dh = padd.z;
    uint n_dim = c_in * kh * kw;
    uint k_dim = h_out;
    uint idx = gid;
    if (idx >= n_dim * k_dim) return;
    uint row = idx / k_dim;
    uint ho = idx % k_dim;
    uint rem = row;
    uint ci = rem / (kh * kw);
    rem = rem % (kh * kw);
    uint ki = rem / kw;
    int hi = (int)(ho * sh + ki * dh) - (int)ph;
    col[ho * n_dim + row] = (hi < 0 || hi >= (int)h)
        ? 0.0f
        : x[ci * h + (uint)hi];
}

// im2col for one (batch, group) slice — layout matches `rlx_cpu::conv_bwd` /
// `[n_dim, k_dim]` row-major with `n_dim = C_in/g · kH · kW`, `k_dim = H_out · W_out`.
kernel void im2col_group(
    device const float* x [[buffer(0)]],
    device float* col [[buffer(1)]],
    constant uint4& nchw [[buffer(2)]],     // [C_in/g, H, W, unused] (group slice)
    constant uint4& out_dims [[buffer(3)]], // [unused, H_out, W_out, unused]
    constant uint4& kshape [[buffer(4)]],
    constant uint4& padd [[buffer(5)]],
    uint2 gid [[thread_position_in_grid]]
) {
    uint h_out = out_dims.y;
    uint w_out = out_dims.z;
    uint c_in = nchw.x;
    uint h = nchw.y;
    uint w = nchw.z;
    uint kh = kshape.x;
    uint kw = kshape.y;
    uint sh = kshape.z;
    uint sw = kshape.w;
    uint ph = padd.x;
    uint pw = padd.y;
    uint dh = padd.z;
    uint dw_dil = padd.w;
    uint n_dim = c_in * kh * kw;
    uint k_dim = h_out * w_out;
    uint idx = gid.x;
    if (idx >= n_dim * k_dim) return;
    uint row = idx / k_dim;
    uint k_idx = idx % k_dim;
    uint ho = k_idx / w_out;
    uint wo = k_idx % w_out;
    uint rem = row;
    uint ci = rem / (kh * kw);
    rem = rem % (kh * kw);
    uint ki = rem / kw;
    uint kj = rem % kw;
    int hi = (int)(ho * sh + ki * dh) - (int)ph;
    int wi = (int)(wo * sw + kj * dw_dil) - (int)pw;
    col[k_idx * n_dim + row] = (hi < 0 || wi < 0 || hi >= (int)h || wi >= (int)w)
        ? 0.0f
        : x[(ci * h + (uint)hi) * w + (uint)wi];
}

// ── Attention backward (recompute scores + softmax) ─────────────────────

kernel void attn_bwd_scores_f32(
    device const float* q [[buffer(0)]],
    device const float* k [[buffer(1)]],
    device float* scores [[buffer(2)]],
    constant uint& sq [[buffer(3)]],
    constant uint& sk [[buffer(4)]],
    constant uint& hs [[buffer(5)]],
    constant uint& head_dim [[buffer(6)]],
    constant float& scale [[buffer(7)]],
    constant uint& mask_kind [[buffer(8)]],
    constant uint& window [[buffer(9)]],
    uint2 gid [[thread_position_in_grid]]
) {
    uint qi = gid.y;
    uint ki = gid.x;
    if (qi >= sq || ki >= sk) return;
    float dot = 0.0f;
    for (uint d = 0; d < head_dim; ++d) {
        dot += q[qi * hs + d] * k[ki * hs + d];
    }
    float s = dot * scale;
    if (mask_kind == 1u) {
        if (ki > qi) s = -1e4f;
    } else if (mask_kind == 3u) {
        uint lo = qi > window ? qi - window : 0u;
        if (ki < lo || ki > qi) s = -1e4f;
    }
    scores[qi * sk + ki] = s;
}

kernel void attn_bwd_dp_f32(
    device const float* dy [[buffer(0)]],
    device const float* v [[buffer(1)]],
    device float* dp [[buffer(2)]],
    constant uint& sq [[buffer(3)]],
    constant uint& sk [[buffer(4)]],
    constant uint& hs [[buffer(5)]],
    constant uint& head_dim [[buffer(6)]],
    uint2 gid [[thread_position_in_grid]]
) {
    uint qi = gid.y;
    uint ki = gid.x;
    if (qi >= sq || ki >= sk) return;
    float acc = 0.0f;
    for (uint d = 0; d < head_dim; ++d) {
        acc += dy[qi * hs + d] * v[ki * hs + d];
    }
    dp[qi * sk + ki] = acc;
}

kernel void attn_bwd_ds_f32(
    device const float* scores [[buffer(0)]],
    device const float* dp [[buffer(1)]],
    device float* ds [[buffer(2)]],
    constant uint& sq [[buffer(3)]],
    constant uint& sk [[buffer(4)]],
    constant float& scale [[buffer(5)]],
    uint row [[threadgroup_position_in_grid]],
    uint tid [[thread_position_in_threadgroup]],
    uint tsize [[threads_per_threadgroup]]
) {
    if (row >= sq) return;
    float row_sum = 0.0f;
    for (uint ki = tid; ki < sk; ki += tsize) {
        row_sum += scores[row * sk + ki] * dp[row * sk + ki];
    }
    threadgroup float partial[256];
    partial[tid] = row_sum;
    threadgroup_barrier(mem_flags::mem_threadgroup);
    for (uint stride = tsize / 2; stride > 0; stride /= 2) {
        if (tid < stride) {
            partial[tid] += partial[tid + stride];
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }
    float sum = partial[0];
    for (uint ki = tid; ki < sk; ki += tsize) {
        uint idx = row * sk + ki;
        float p = scores[idx];
        ds[idx] = p * (dp[idx] - sum) * scale;
    }
}

kernel void attn_bwd_dv_f32(
    device const float* scores [[buffer(0)]],
    device const float* dy [[buffer(1)]],
    device float* out [[buffer(2)]],
    constant uint& sq [[buffer(3)]],
    constant uint& sk [[buffer(4)]],
    constant uint& hs [[buffer(5)]],
    constant uint& head_dim [[buffer(6)]],
    uint2 gid [[thread_position_in_grid]]
) {
    uint ki = gid.y;
    uint d = gid.x;
    if (ki >= sk || d >= head_dim) return;
    float acc = 0.0f;
    for (uint qi = 0; qi < sq; ++qi) {
        acc += scores[qi * sk + ki] * dy[qi * hs + d];
    }
    out[ki * hs + d] = acc;
}

kernel void attn_bwd_dq_f32(
    device const float* ds [[buffer(0)]],
    device const float* k [[buffer(1)]],
    device float* out [[buffer(2)]],
    constant uint& sq [[buffer(3)]],
    constant uint& sk [[buffer(4)]],
    constant uint& hs [[buffer(5)]],
    constant uint& head_dim [[buffer(6)]],
    uint2 gid [[thread_position_in_grid]]
) {
    uint qi = gid.y;
    uint d = gid.x;
    if (qi >= sq || d >= head_dim) return;
    float acc = 0.0f;
    for (uint ki = 0; ki < sk; ++ki) {
        acc += ds[qi * sk + ki] * k[ki * hs + d];
    }
    out[qi * hs + d] = acc;
}

kernel void attn_bwd_dk_f32(
    device const float* ds [[buffer(0)]],
    device const float* q [[buffer(1)]],
    device float* out [[buffer(2)]],
    constant uint& sq [[buffer(3)]],
    constant uint& sk [[buffer(4)]],
    constant uint& hs [[buffer(5)]],
    constant uint& head_dim [[buffer(6)]],
    uint2 gid [[thread_position_in_grid]]
) {
    uint ki = gid.y;
    uint d = gid.x;
    if (ki >= sk || d >= head_dim) return;
    float acc = 0.0f;
    for (uint qi = 0; qi < sq; ++qi) {
        acc += ds[qi * sk + ki] * q[qi * hs + d];
    }
    out[ki * hs + d] = acc;
}

// ── Batched attention-backward kernels ────────────────────────────────────
// One 3D dispatch (grid.z = head-slot) processes MANY (batch,head) pairs in
// parallel instead of the per-(b,h) host loop, which serialized on the shared
// scratch (Metal hazard-tracks at buffer granularity). Per-slot pointers are
// computed in-kernel from `tile_base + slot`; scratch is laid out as
// `[tile][sq][sk]` (scores/dp/ds each a `tile*ss` region). `hs = heads*head_dim`.
kernel void attn_bwd_scores_batched_f32(
    device const float* q [[buffer(0)]],
    device const float* k [[buffer(1)]],
    device float* scores [[buffer(2)]],
    constant uint& sq [[buffer(3)]],
    constant uint& sk [[buffer(4)]],
    constant uint& heads [[buffer(5)]],
    constant uint& head_dim [[buffer(6)]],
    constant float& scale [[buffer(7)]],
    constant uint& mask_kind [[buffer(8)]],
    constant uint& window [[buffer(9)]],
    constant uint& tile_base [[buffer(10)]],
    uint3 gid [[thread_position_in_grid]]
) {
    uint ki = gid.x, qi = gid.y, slot = gid.z;
    if (qi >= sq || ki >= sk) return;
    // Causal / sliding-window: masked pairs become 0 after softmax
    // (exp(-1e4)=0 in f32), so write -1e4 and SKIP the dot — ~half the work.
    bool masked = (mask_kind == 1u && ki > qi)
        || (mask_kind == 3u && (ki > qi || (qi > window && ki < qi - window)));
    if (masked) {
        scores[slot * sq * sk + qi * sk + ki] = -1e4f;
        return;
    }
    uint hs = heads * head_dim;
    uint gp = tile_base + slot;
    uint bi = gp / heads, hi = gp % heads;
    uint qbase = bi * sq * hs + hi * head_dim;
    uint kbase = bi * sk * hs + hi * head_dim;
    float dot = 0.0f;
    for (uint d = 0; d < head_dim; ++d) {
        dot += q[qbase + qi * hs + d] * k[kbase + ki * hs + d];
    }
    scores[slot * sq * sk + qi * sk + ki] = dot * scale;
}

kernel void attn_bwd_dp_batched_f32(
    device const float* dy [[buffer(0)]],
    device const float* v [[buffer(1)]],
    device float* dp [[buffer(2)]],
    constant uint& sq [[buffer(3)]],
    constant uint& sk [[buffer(4)]],
    constant uint& heads [[buffer(5)]],
    constant uint& head_dim [[buffer(6)]],
    constant uint& tile_base [[buffer(7)]],
    constant uint& mask_kind [[buffer(8)]],
    constant uint& window [[buffer(9)]],
    uint3 gid [[thread_position_in_grid]]
) {
    uint ki = gid.x, qi = gid.y, slot = gid.z;
    if (qi >= sq || ki >= sk) return;
    // Masked pairs contribute 0 to ds (scores=0); write 0, skip the dot.
    bool masked = (mask_kind == 1u && ki > qi)
        || (mask_kind == 3u && (ki > qi || (qi > window && ki < qi - window)));
    if (masked) {
        dp[slot * sq * sk + qi * sk + ki] = 0.0f;
        return;
    }
    uint hs = heads * head_dim;
    uint gp = tile_base + slot;
    uint bi = gp / heads, hi = gp % heads;
    uint qbase = bi * sq * hs + hi * head_dim;
    uint kbase = bi * sk * hs + hi * head_dim;
    float acc = 0.0f;
    for (uint d = 0; d < head_dim; ++d) {
        acc += dy[qbase + qi * hs + d] * v[kbase + ki * hs + d];
    }
    dp[slot * sq * sk + qi * sk + ki] = acc;
}

// Per-row softmax-jacobian; one threadgroup per (slot,row). `row = slot*sq + lr`.
kernel void attn_bwd_ds_batched_f32(
    device const float* scores [[buffer(0)]],
    device const float* dp [[buffer(1)]],
    device float* ds [[buffer(2)]],
    constant uint& sq [[buffer(3)]],
    constant uint& sk [[buffer(4)]],
    constant float& scale [[buffer(5)]],
    constant uint& mask_kind [[buffer(6)]],
    constant uint& window [[buffer(7)]],
    uint row [[threadgroup_position_in_grid]],
    uint tid [[thread_position_in_threadgroup]],
    uint tsize [[threads_per_threadgroup]]
) {
    uint slot = row / sq;
    uint lr = row % sq;
    uint base = slot * sq * sk + lr * sk;
    // Only the causal/window band contributes (masked scores are 0); bound the
    // reduction AND the write to `[ki_lo, ki_hi]`. dq/dk are bounded the same
    // way, so they never read the un-written masked entries.
    uint ki_lo = 0u;
    uint ki_hi = sk - 1u;
    if (mask_kind == 1u) {
        ki_hi = min(lr, sk - 1u);
    } else if (mask_kind == 3u) {
        ki_hi = min(lr, sk - 1u);
        ki_lo = lr > window ? lr - window : 0u;
    }
    float row_sum = 0.0f;
    for (uint ki = ki_lo + tid; ki <= ki_hi; ki += tsize) {
        row_sum += scores[base + ki] * dp[base + ki];
    }
    threadgroup float partial[256];
    partial[tid] = row_sum;
    threadgroup_barrier(mem_flags::mem_threadgroup);
    for (uint stride = tsize / 2; stride > 0; stride /= 2) {
        if (tid < stride) {
            partial[tid] += partial[tid + stride];
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }
    float sum = partial[0];
    for (uint ki = ki_lo + tid; ki <= ki_hi; ki += tsize) {
        uint idx = base + ki;
        ds[idx] = scores[idx] * (dp[idx] - sum) * scale;
    }
}

kernel void attn_bwd_dv_batched_f32(
    device const float* scores [[buffer(0)]],
    device const float* dy [[buffer(1)]],
    device float* out [[buffer(2)]],
    constant uint& sq [[buffer(3)]],
    constant uint& sk [[buffer(4)]],
    constant uint& heads [[buffer(5)]],
    constant uint& head_dim [[buffer(6)]],
    constant uint& tile_base [[buffer(7)]],
    constant uint& mask_kind [[buffer(8)]],
    constant uint& window [[buffer(9)]],
    uint3 gid [[thread_position_in_grid]]
) {
    uint d = gid.x, ki = gid.y, slot = gid.z;
    if (ki >= sk || d >= head_dim) return;
    // scores[qi,ki] is 0 unless ki is in qi's causal/window band ⇒ qi ≥ ki
    // (and qi ≤ ki+window for sliding). Bound the qi accumulation to that band.
    uint qi_lo = 0u;
    uint qi_hi = sq - 1u;
    if (mask_kind == 1u) {
        qi_lo = ki;
    } else if (mask_kind == 3u) {
        qi_lo = ki;
        qi_hi = min(sq - 1u, ki + window);
    }
    uint hs = heads * head_dim;
    uint gp = tile_base + slot;
    uint bi = gp / heads, hi = gp % heads;
    uint qbase = bi * sq * hs + hi * head_dim;
    uint kbase = bi * sk * hs + hi * head_dim;
    float acc = 0.0f;
    for (uint qi = qi_lo; qi <= qi_hi; ++qi) {
        acc += scores[slot * sq * sk + qi * sk + ki] * dy[qbase + qi * hs + d];
    }
    out[kbase + ki * hs + d] = acc;
}

kernel void attn_bwd_dq_batched_f32(
    device const float* ds [[buffer(0)]],
    device const float* k [[buffer(1)]],
    device float* out [[buffer(2)]],
    constant uint& sq [[buffer(3)]],
    constant uint& sk [[buffer(4)]],
    constant uint& heads [[buffer(5)]],
    constant uint& head_dim [[buffer(6)]],
    constant uint& tile_base [[buffer(7)]],
    constant uint& mask_kind [[buffer(8)]],
    constant uint& window [[buffer(9)]],
    uint3 gid [[thread_position_in_grid]]
) {
    uint d = gid.x, qi = gid.y, slot = gid.z;
    if (qi >= sq || d >= head_dim) return;
    // ds[qi,ki] is 0 outside qi's causal/window band ⇒ ki ≤ qi (and ki ≥
    // qi-window for sliding). Bound the ki accumulation to that band.
    uint ki_lo = 0u;
    uint ki_hi = sk - 1u;
    if (mask_kind == 1u) {
        ki_hi = min(qi, sk - 1u);
    } else if (mask_kind == 3u) {
        ki_hi = min(qi, sk - 1u);
        ki_lo = qi > window ? qi - window : 0u;
    }
    uint hs = heads * head_dim;
    uint gp = tile_base + slot;
    uint bi = gp / heads, hi = gp % heads;
    uint qbase = bi * sq * hs + hi * head_dim;
    uint kbase = bi * sk * hs + hi * head_dim;
    float acc = 0.0f;
    for (uint ki = ki_lo; ki <= ki_hi; ++ki) {
        acc += ds[slot * sq * sk + qi * sk + ki] * k[kbase + ki * hs + d];
    }
    out[qbase + qi * hs + d] = acc;
}

kernel void attn_bwd_dk_batched_f32(
    device const float* ds [[buffer(0)]],
    device const float* q [[buffer(1)]],
    device float* out [[buffer(2)]],
    constant uint& sq [[buffer(3)]],
    constant uint& sk [[buffer(4)]],
    constant uint& heads [[buffer(5)]],
    constant uint& head_dim [[buffer(6)]],
    constant uint& tile_base [[buffer(7)]],
    constant uint& mask_kind [[buffer(8)]],
    constant uint& window [[buffer(9)]],
    uint3 gid [[thread_position_in_grid]]
) {
    uint d = gid.x, ki = gid.y, slot = gid.z;
    if (ki >= sk || d >= head_dim) return;
    // ds[qi,ki] is 0 unless ki is in qi's causal/window band ⇒ qi ≥ ki (and
    // qi ≤ ki+window for sliding). Bound the qi accumulation to that band.
    uint qi_lo = 0u;
    uint qi_hi = sq - 1u;
    if (mask_kind == 1u) {
        qi_lo = ki;
    } else if (mask_kind == 3u) {
        qi_lo = ki;
        qi_hi = min(sq - 1u, ki + window);
    }
    uint hs = heads * head_dim;
    uint gp = tile_base + slot;
    uint bi = gp / heads, hi = gp % heads;
    uint qbase = bi * sq * hs + hi * head_dim;
    uint kbase = bi * sk * hs + hi * head_dim;
    float acc = 0.0f;
    for (uint qi = qi_lo; qi <= qi_hi; ++qi) {
        acc += ds[slot * sq * sk + qi * sk + ki] * q[qbase + qi * hs + d];
    }
    out[kbase + ki * hs + d] = acc;
}

// ── Fused flash-attention backward (single kernel) ──────────────────────────
// One threadgroup per (query-tile of Br rows, head, batch). Recomputes S=QKᵀ·scale
// and the row softmax P entirely in THREADGROUP memory (never written to device —
// no scores/dp/ds S² scratch buffers), then:
//   dQ[qi] = Σ_ki dS[qi,ki]·K[ki]          — written directly (each query once)
//   dV[ki] += Σ_qi P[qi,ki]·dO[qi]          — atomic-add across query-tiles
//   dK[ki] += Σ_qi dS[qi,ki]·Q[qi]          — atomic-add across query-tiles
// where dS = P∘(dP - rowsum(P∘dP))·scale, dP = dO·Vᵀ. dK/dV MUST be pre-zeroed.
// Same math as the 6-pass attn_bwd_* kernels, fused. Causal (mask_kind==1) bounds
// ki to [0, qi_abs]. Requires sk ≤ MAX_SK and head_dim ≤ MAX_DH (threadgroup tiles).
// mask_kind ∈ {0,1} only (padding/bias fall back to the 6-pass path).
kernel void attn_bwd_fused_f32(
    device const float* q    [[buffer(0)]],
    device const float* k    [[buffer(1)]],
    device const float* v    [[buffer(2)]],
    device const float* dy   [[buffer(3)]],
    device float*        dq  [[buffer(4)]],
    device atomic_float* dk  [[buffer(5)]],
    device atomic_float* dv  [[buffer(6)]],
    constant uint&  sq        [[buffer(7)]],
    constant uint&  sk        [[buffer(8)]],
    constant uint&  heads     [[buffer(9)]],
    constant uint&  head_dim  [[buffer(10)]],
    constant float& scale     [[buffer(11)]],
    constant uint&  mask_kind [[buffer(12)]],
    uint3 tgid [[threadgroup_position_in_grid]],
    uint tid   [[thread_index_in_threadgroup]]
) {
    constexpr uint Br = 8u;
    constexpr uint MAX_SK = 256u;
    constexpr uint MAX_DH = 64u;
    constexpr uint THREADS = 64u;

    threadgroup float Ptile[Br * MAX_SK];    // S, then softmax P
    threadgroup float dStile[Br * MAX_SK];   // dP, then dS
    threadgroup float Qtile[Br * MAX_DH];
    threadgroup float dOtile[Br * MAX_DH];
    threadgroup float Drow[Br];

    uint q_tile = tgid.x, hi = tgid.y, bi = tgid.z;
    uint qi0 = q_tile * Br;
    uint hs = heads * head_dim;
    uint qbase = bi * sq * hs + hi * head_dim;
    uint kbase = bi * sk * hs + hi * head_dim;
    bool causal = (mask_kind == 1u);

    // Load Q, dO tiles (Br × head_dim), zero-pad rows past sq.
    for (uint i = tid; i < Br * head_dim; i += THREADS) {
        uint qi = i / head_dim, d = i % head_dim;
        uint pos = qi0 + qi;
        Qtile[qi * MAX_DH + d]  = (pos < sq) ? q[qbase + pos * hs + d]  : 0.0f;
        dOtile[qi * MAX_DH + d] = (pos < sq) ? dy[qbase + pos * hs + d] : 0.0f;
    }
    threadgroup_barrier(mem_flags::mem_threadgroup);

    // Pass 1: S[qi,ki] = scale·(Q·K), causal-masked to -INF. Store in Ptile.
    for (uint c = tid; c < Br * sk; c += THREADS) {
        uint qi = c / sk, ki = c % sk;
        uint pos = qi0 + qi;
        float s = -INFINITY;
        if (pos < sq && !(causal && ki > pos)) {
            float acc = 0.0f;
            for (uint d = 0; d < head_dim; ++d)
                acc = fma(Qtile[qi * MAX_DH + d], k[kbase + ki * hs + d], acc);
            s = acc * scale;
        }
        Ptile[qi * MAX_SK + ki] = s;
    }
    threadgroup_barrier(mem_flags::mem_threadgroup);

    // Softmax each row over its causal band [0, khi]; Br threads.
    if (tid < Br) {
        uint qi = tid;
        uint pos = qi0 + qi;
        if (pos < sq) {
            uint khi = causal ? min(pos, sk - 1u) : (sk - 1u);
            float m = -INFINITY;
            for (uint ki = 0; ki <= khi; ++ki) m = max(m, Ptile[qi * MAX_SK + ki]);
            float l = 0.0f;
            for (uint ki = 0; ki <= khi; ++ki) {
                float e = exp(Ptile[qi * MAX_SK + ki] - m);
                Ptile[qi * MAX_SK + ki] = e;
                l += e;
            }
            float inv = (l > 0.0f) ? (1.0f / l) : 0.0f;
            for (uint ki = 0; ki <= khi; ++ki) Ptile[qi * MAX_SK + ki] *= inv;
        }
    }
    threadgroup_barrier(mem_flags::mem_threadgroup);

    // Pass 2: dP[qi,ki] = dO[qi]·V[ki] over the band (else 0). Store in dStile.
    for (uint c = tid; c < Br * sk; c += THREADS) {
        uint qi = c / sk, ki = c % sk;
        uint pos = qi0 + qi;
        float acc = 0.0f;
        if (pos < sq && !(causal && ki > pos)) {
            for (uint d = 0; d < head_dim; ++d)
                acc = fma(dOtile[qi * MAX_DH + d], v[kbase + ki * hs + d], acc);
        }
        dStile[qi * MAX_SK + ki] = acc;
    }
    threadgroup_barrier(mem_flags::mem_threadgroup);

    // Drow[qi] = Σ_ki P·dP over the band; Br threads.
    if (tid < Br) {
        uint qi = tid;
        uint pos = qi0 + qi;
        float D = 0.0f;
        if (pos < sq) {
            uint khi = causal ? min(pos, sk - 1u) : (sk - 1u);
            for (uint ki = 0; ki <= khi; ++ki)
                D += Ptile[qi * MAX_SK + ki] * dStile[qi * MAX_SK + ki];
        }
        Drow[qi] = D;
    }
    threadgroup_barrier(mem_flags::mem_threadgroup);

    // dS[qi,ki] = P·(dP - D)·scale, in place in dStile (0 outside band).
    for (uint c = tid; c < Br * sk; c += THREADS) {
        uint qi = c / sk, ki = c % sk;
        uint pos = qi0 + qi;
        float ds = 0.0f;
        if (pos < sq && !(causal && ki > pos))
            ds = Ptile[qi * MAX_SK + ki] * (dStile[qi * MAX_SK + ki] - Drow[qi]) * scale;
        dStile[qi * MAX_SK + ki] = ds;
    }
    threadgroup_barrier(mem_flags::mem_threadgroup);

    // dQ[qi,d] = Σ_ki dS·K — direct write (each query row owned by this tile).
    for (uint c = tid; c < Br * head_dim; c += THREADS) {
        uint qi = c / head_dim, d = c % head_dim;
        uint pos = qi0 + qi;
        if (pos < sq) {
            uint khi = causal ? min(pos, sk - 1u) : (sk - 1u);
            float acc = 0.0f;
            for (uint ki = 0; ki <= khi; ++ki)
                acc = fma(dStile[qi * MAX_SK + ki], k[kbase + ki * hs + d], acc);
            dq[qbase + pos * hs + d] = acc;
        }
    }

    // dV[ki,d] += Σ_qi P·dO ; dK[ki,d] += Σ_qi dS·Q — atomic across query-tiles.
    for (uint c = tid; c < sk * head_dim; c += THREADS) {
        uint ki = c / head_dim, d = c % head_dim;
        float dvacc = 0.0f, dkacc = 0.0f;
        for (uint qi = 0; qi < Br; ++qi) {
            uint pos = qi0 + qi;
            if (pos < sq && !(causal && ki > pos)) {
                dvacc = fma(Ptile[qi * MAX_SK + ki], dOtile[qi * MAX_DH + d], dvacc);
                dkacc = fma(dStile[qi * MAX_SK + ki], Qtile[qi * MAX_DH + d], dkacc);
            }
        }
        if (dvacc != 0.0f)
            atomic_fetch_add_explicit(&dv[kbase + ki * hs + d], dvacc, memory_order_relaxed);
        if (dkacc != 0.0f)
            atomic_fetch_add_explicit(&dk[kbase + ki * hs + d], dkacc, memory_order_relaxed);
    }
}

kernel void gather_bwd_zero(
    device float* dst [[buffer(0)]],
    constant uint& n [[buffer(1)]],
    uint i [[thread_position_in_grid]]
) {
    if (i < n) dst[i] = 0.0f;
}

kernel void gather_bwd_acc(
    device const float* dy [[buffer(0)]],
    device const float* idx [[buffer(1)]],
    device float* dst [[buffer(2)]],
    constant uint& outer [[buffer(3)]],
    constant uint& axis_dim [[buffer(4)]],
    constant uint& num_idx [[buffer(5)]],
    constant uint& trailing [[buffer(6)]],
    uint o [[threadgroup_position_in_grid]]
) {
    if (o >= outer) return;
    for (uint k = 0; k < num_idx; ++k) {
        uint row = uint(idx[k]);
        if (row >= axis_dim) continue;
        for (uint j = 0; j < trailing; ++j) {
            float v = dy[(o * num_idx + k) * trailing + j];
            dst[(o * axis_dim + row) * trailing + j] += v;
        }
    }
}

// ── On-device Philox4x32-10 RNG ──────────────────────────────────────────
// Bit-matched to `rlx_ir::Philox4x32` / the shared `rng_philox.cu` (CUDA/ROCm):
// normal sample i reads Philox block i/2 lanes {0,1}|{2,3}; uniform i reads
// block i/4 lane i%4; `u32_to_unit=(bits>>8)/2^24`. The arena buffer is bound
// at the destination byte offset, so each entry writes `out[i]`.
static inline void rlx_philox_round(thread uint* s, uint k0, uint k1) {
    const ulong M0 = 0xD2561A75UL;
    const ulong M1 = 0xCD9E8D57UL;
    ulong p0 = (ulong)s[0] * M0;
    ulong p1 = (ulong)s[2] * M1;
    uint hi0 = (uint)(p0 >> 32); uint lo0 = (uint)p0;
    uint hi1 = (uint)(p1 >> 32); uint lo1 = (uint)p1;
    s[0] = hi1 ^ s[1] ^ k0;
    s[1] = lo1;
    s[2] = hi0 ^ s[3] ^ k1;
    s[3] = lo0;
}
static inline void rlx_philox_10(uint blk, uint seed_lo, uint seed_hi, thread uint* out) {
    // Counter for block `blk` (blk < 2^31 for u32 len → high words zero).
    uint s[4] = { blk, 0u, 0u, 0u };
    uint k0 = seed_lo, k1 = seed_hi;
    for (int i = 0; i < 10; ++i) {
        rlx_philox_round(s, k0, k1);
        k0 += 0x9E3779B9u;
        k1 += 0xBB67AE85u;
    }
    out[0] = s[0]; out[1] = s[1]; out[2] = s[2]; out[3] = s[3];
}
static inline float rlx_u32_to_unit(uint bits) {
    return (float)(bits >> 8) / (float)(1u << 24);
}
kernel void rng_normal_philox(
    device float* out [[buffer(0)]],
    constant uint& len [[buffer(1)]],
    constant float& mean [[buffer(2)]],
    constant float& scale [[buffer(3)]],
    constant uint& seed_lo [[buffer(4)]],
    constant uint& seed_hi [[buffer(5)]],
    uint i [[thread_position_in_grid]]
) {
    if (i >= len) return;
    uint lane0 = (i & 1u) ? 2u : 0u;
    uint buf[4];
    rlx_philox_10(i / 2u, seed_lo, seed_hi, buf);
    float u1 = rlx_u32_to_unit(buf[lane0]);
    float u2 = rlx_u32_to_unit(buf[lane0 + 1u]);
    if (u1 < 1.17549435e-38f) u1 = 1.17549435e-38f; // f32::MIN_POSITIVE
    float r = sqrt(-2.0f * log(u1));
    float theta = 6.283185307179586f * u2;
    out[i] = mean + scale * (r * cos(theta));
}
kernel void rng_uniform_philox(
    device float* out [[buffer(0)]],
    constant uint& len [[buffer(1)]],
    constant float& low [[buffer(2)]],
    constant float& high [[buffer(3)]],
    constant uint& seed_lo [[buffer(4)]],
    constant uint& seed_hi [[buffer(5)]],
    uint i [[thread_position_in_grid]]
) {
    if (i >= len) return;
    uint buf[4];
    rlx_philox_10(i / 4u, seed_lo, seed_hi, buf);
    float u = rlx_u32_to_unit(buf[i & 3u]);
    out[i] = low + u * (high - low);
}
kernel void rng_fill_zero(
    device float* out [[buffer(0)]],
    constant uint& len [[buffer(1)]],
    uint i [[thread_position_in_grid]]
) {
    if (i >= len) return;
    out[i] = 0.0f;
}
"#;

const RLX_KERNELS_MSL_DEQUANT: &str = include_str!("dequant_gguf.msl");
const RLX_KERNELS_MSL_FFT_GPU: &str = include_str!("fft_gpu.msl");
const RLX_KERNELS_MSL_SPLAT: &str = include_str!("splat.msl");
const RLX_KERNELS_MSL_SPLAT_CONIC: &str = include_str!("splat_conic_bin.msl");

// ── Register-blocked simdgroup GEMM tile configs (macro-generated) ──────────
// One MSL template per structure (plain / split-K), parameterized by tile:
//   {NAME}  kernel name          {NACC}  8×8-col accumulators / simdgroup
//   {TROWS} rows per threadgroup  {TCOLS} cols per threadgroup (= NACC*8)
// A config is TROWS×TCOLS output, (TROWS/8) simdgroups × NACC accumulators.
// `sgemm_tile_variant!(name, trows, nacc)` stamps a config; add a tile size by
// adding one call in `msl_source`. Keeps the kernel families DRY + tunable.
const SGEMM_TILE_TMPL: &str = r#"
kernel void {NAME}(
    device const float* A [[buffer(0)]],
    device const float* B [[buffer(1)]],
    device float* C       [[buffer(2)]],
    constant uint& M [[buffer(3)]], constant uint& K [[buffer(4)]], constant uint& N [[buffer(5)]],
    uint2 tgid [[threadgroup_position_in_grid]], uint sgid [[simdgroup_index_in_threadgroup]]
) {
    uint row0 = tgid.y * {TROWS} + sgid * 8;
    uint col0 = tgid.x * {TCOLS};
    simdgroup_float8x8 acc[{NACC}];
    for (int j = 0; j < {NACC}; j++) acc[j] = make_filled_simdgroup_matrix<float, 8, 8>(0.0);
    for (uint kk = 0; kk < K; kk += 8) {
        simdgroup_float8x8 a, b[{NACC}];
        simdgroup_load(a, A + row0 * K + kk, K);
        for (int j = 0; j < {NACC}; j++) simdgroup_load(b[j], B + kk * N + col0 + j * 8, N);
        for (int j = 0; j < {NACC}; j++) simdgroup_multiply_accumulate(acc[j], a, b[j], acc[j]);
    }
    for (int j = 0; j < {NACC}; j++) simdgroup_store(acc[j], C + row0 * N + col0 + j * 8, N);
}
"#;

// Split-K variant: grid gains a Ksplits z-axis; each z-slice sums a K-chunk into
// its tile, then hardware-atomic-adds into a pre-zeroed C. For fat-K / small-MN.
const SGEMM_SPLITK_TMPL: &str = r#"
kernel void {NAME}(
    device const float* A [[buffer(0)]],
    device const float* B [[buffer(1)]],
    device atomic_float* C [[buffer(2)]],
    constant uint& M [[buffer(3)]], constant uint& K [[buffer(4)]], constant uint& N [[buffer(5)]],
    constant uint& Ksplits [[buffer(6)]],
    uint3 tgid [[threadgroup_position_in_grid]],
    uint sgid  [[simdgroup_index_in_threadgroup]],
    uint slid  [[thread_index_in_simdgroup]]
) {
    uint row0 = tgid.y * {TROWS} + sgid * 8;
    uint col0 = tgid.x * {TCOLS};
    uint kchunk = K / Ksplits;
    uint kstart = tgid.z * kchunk;
    simdgroup_float8x8 acc[{NACC}];
    for (int j = 0; j < {NACC}; j++) acc[j] = make_filled_simdgroup_matrix<float, 8, 8>(0.0);
    for (uint kk = kstart; kk < kstart + kchunk; kk += 8) {
        simdgroup_float8x8 a, b[{NACC}];
        simdgroup_load(a, A + row0 * K + kk, K);
        for (int j = 0; j < {NACC}; j++) simdgroup_load(b[j], B + kk * N + col0 + j * 8, N);
        for (int j = 0; j < {NACC}; j++) simdgroup_multiply_accumulate(acc[j], a, b[j], acc[j]);
    }
    threadgroup float scratch[{TROWS} * {TCOLS}];
    for (int j = 0; j < {NACC}; j++) simdgroup_store(acc[j], scratch + (sgid * 8) * {TCOLS} + j * 8, {TCOLS});
    threadgroup_barrier(mem_flags::mem_threadgroup);
    uint tile_elems = {TROWS} * {TCOLS};
    uint threads = ({TROWS} / 8) * 32;
    for (uint idx = sgid * 32 + slid; idx < tile_elems; idx += threads) {
        uint r = idx / {TCOLS}, c = idx % {TCOLS};
        atomic_fetch_add_explicit(&C[(tgid.y * {TROWS} + r) * N + col0 + c], scratch[idx],
                                  memory_order_relaxed);
    }
}
"#;

macro_rules! sgemm_tile_variant {
    ($name:literal, $trows:expr, $nacc:expr) => {
        SGEMM_TILE_TMPL
            .replace("{NAME}", $name)
            .replace("{NACC}", &($nacc).to_string())
            .replace("{TROWS}", &($trows).to_string())
            .replace("{TCOLS}", &(($nacc) * 8).to_string())
    };
}
macro_rules! sgemm_splitk_variant {
    ($name:literal, $trows:expr, $nacc:expr) => {
        SGEMM_SPLITK_TMPL
            .replace("{NAME}", $name)
            .replace("{NACC}", &($nacc).to_string())
            .replace("{TROWS}", &($trows).to_string())
            .replace("{TCOLS}", &(($nacc) * 8).to_string())
    };
}

/// Single source of truth for Metal's scalar activation kernels. Each entry
/// `(Variant, "name", "msl_expr")` generates an f32 + f16 in-place MSL kernel
/// (the expr sees `float x`) and its pipeline pair. Adding an activation is one
/// line here — no struct field, no dispatch arm, no separate kernel edit.
// The macro owns the (Activation → kernel-name) binding once; the scalar MATH
// comes from the shared `rlxsl` manifest (a single definition across all
// backends; the A&S erf polynomial now lives once in rlxsl rather than a
// hand-written `rlx_erf` helper — MSL has no erf builtin). Metal compiles MSL
// at runtime, so the source is assembled as a `String`; no build.rs needed.
macro_rules! scalar_activation_kernels {
    ( $( $variant:ident, $name:literal );+ $(;)? ) => {
        /// Build the standalone scalar-activation MSL kernels (f32 + f16) from
        /// the rlxsl manifest.
        fn scalar_act_msl() -> String {
            let mut src = String::new();
            $(
                {
                    let (stmts, expr) = rlxsl::emit_activation(
                        rlx_ir::op::Activation::$variant, rlxsl::Lang::Msl);
                    let body = stmts.join(" ");
                    src.push_str(&format!(
                        "kernel void {n}_sa(device float* d [[buffer(0)]], constant uint& n [[buffer(1)]], uint g [[thread_position_in_grid]]) {{ if (g>=n) return; float x = d[g]; {b} d[g] = {e}; }}\n",
                        n = $name, b = body, e = expr));
                    src.push_str(&format!(
                        "kernel void {n}_sa_h(device half* d [[buffer(0)]], constant uint& n [[buffer(1)]], uint g [[thread_position_in_grid]]) {{ if (g>=n) return; float x = float(d[g]); {b} d[g] = half({e}); }}\n",
                        n = $name, b = body, e = expr));
                }
            )+
            src
        }

        /// Build the (f32, f16) pipeline pair for each scalar activation.
        fn build_scalar_act_kernels(
            pipeline: &dyn Fn(&str) -> ComputePipelineState,
        ) -> std::collections::HashMap<rlx_ir::op::Activation, (ComputePipelineState, ComputePipelineState)>
        {
            let mut m = std::collections::HashMap::new();
            $(
                m.insert(
                    rlx_ir::op::Activation::$variant,
                    (pipeline(concat!($name, "_sa")), pipeline(concat!($name, "_sa_h"))),
                );
            )+
            m
        }
    };
}

scalar_activation_kernels! {
    Floor,       "floor";
    Ceil,        "ceil";
    Sign,        "sign";
    Softplus,    "softplus";
    Elu,         "elu";
    Erf,         "erf";
    HardSwish,   "hardswish";
    HardSigmoid, "hardsigmoid";
    Mish,        "mish";
    Softsign,    "softsign";
    LogSigmoid,  "logsigmoid";
}

/// MSL template for the split-K codebook weight-synthesis kernel (decode). `{S}`
/// = name suffix, `{T}` = x/dst element type (`float` or `half`); accumulation
/// is always f32 and the codebook stays f32 (only x/dst change precision).
const SYNTH_SPLITK_TMPL: &str = r#"
kernel void synth_matmul_codebook{S}(
    device float* arena          [[buffer(0)]],
    constant ulong& x_off        [[buffer(1)]],
    constant ulong& idx_off      [[buffer(2)]],
    constant ulong& cb_off       [[buffer(3)]],
    constant ulong& dst_off      [[buffer(4)]],
    constant uint& k_dim         [[buffer(5)]],
    constant uint& n_dim         [[buffer(6)]],
    constant uint& entry_dim     [[buffer(7)]],
    constant uint& num_entries   [[buffer(8)]],
    constant uint& m_dim         [[buffer(9)]],
    uint3 gid                    [[thread_position_in_grid]]
) {
    uint split = gid.x; uint j = gid.y; uint r = gid.z;
    if (j >= n_dim || r >= m_dim) return;
    (void)num_entries;
    uint nb = k_dim / entry_dim;
    device const {T}* xr    = (device const {T}*)((device const char*)arena + x_off) + (ulong)r * k_dim;
    device const uchar* idx = (device const uchar*)arena + idx_off + (ulong)j * nb;
    device const float* cb  = (device const float*)((device const char*)arena + cb_off);
    device {T}*         dst  = (device {T}*)((device char*)arena + dst_off);
    float acc = 0.0f;
    for (uint b = split; b < nb; b += 32u) {
        uint code = uint(idx[b]);
        device const float* c = cb + (ulong)code * entry_dim;
        uint base = b * entry_dim;
        for (uint t = 0u; t < entry_dim; ++t) acc += float(xr[base + t]) * c[t];
    }
    acc = simd_sum(acc);
    if (split == 0u) dst[(ulong)r * n_dim + j] = ({T})acc;
}
"#;

/// MSL template for the one-thread-per-output prefill kernel. NOTE: register-
/// tiling (reuse centroids across a row tile) was measured SLOWER here — the 4 KB
/// codebook is L1-resident so there's nothing to amortize, and tiling hurts x
/// coalescing + occupancy. A fused GPU kernel can't beat MPS for prefill anyway
/// (reconstruction is cache-cheap; MPS is a world-class tiled GEMM); the fast
/// prefill path is reconstruct→f32→MPS/AMX (see the CPU m>1 path). `{S}`=suffix,
/// `{T}`=x/dst element type.
const SYNTH_MM_TMPL: &str = r#"
kernel void synth_matmul_codebook_mm{S}(
    device float* arena          [[buffer(0)]],
    constant ulong& x_off        [[buffer(1)]],
    constant ulong& idx_off      [[buffer(2)]],
    constant ulong& cb_off       [[buffer(3)]],
    constant ulong& dst_off      [[buffer(4)]],
    constant uint& k_dim         [[buffer(5)]],
    constant uint& n_dim         [[buffer(6)]],
    constant uint& entry_dim     [[buffer(7)]],
    constant uint& num_entries   [[buffer(8)]],
    constant uint& m_dim         [[buffer(9)]],
    uint2 gid                    [[thread_position_in_grid]]
) {
    uint j = gid.x; uint r = gid.y;
    if (j >= n_dim || r >= m_dim) return;
    (void)num_entries;
    uint nb = k_dim / entry_dim;
    device const {T}* xr    = (device const {T}*)((device const char*)arena + x_off) + (ulong)r * k_dim;
    device const uchar* idx = (device const uchar*)arena + idx_off + (ulong)j * nb;
    device const float* cb  = (device const float*)((device const char*)arena + cb_off);
    device {T}*         dst  = (device {T}*)((device char*)arena + dst_off);
    float acc = 0.0f;
    for (uint b = 0u; b < nb; ++b) {
        uint code = uint(idx[b]);
        device const float* c = cb + (ulong)code * entry_dim;
        uint base = b * entry_dim;
        for (uint t = 0u; t < entry_dim; ++t) acc += float(xr[base + t]) * c[t];
    }
    dst[(ulong)r * n_dim + j] = ({T})acc;
}
"#;

/// Generate the codebook weight-synthesis MSL kernels for each
/// `(suffix => element-type)` pair from ONE template, so the f32 and f16
/// variants can't drift (mirrors `scalar_activation_kernels!`).
/// MSL kernel that reconstructs the dense f32 weight Wᵀ [n, k] (row-major,
/// contiguous) from u8 codebook indices — the weight-only half used by the m>8
/// prefill path (reconstruct → MPS sgemm, which beats any fused kernel). One
/// thread per (kb block, column j) writes entry_dim contiguous weights:
/// W[j, kb·entry_dim + t] = codebook[indices[j,kb], t]. Not dtype-templated
/// (MPS consumes f32); paired with `encode_mps_sgemm_bt` (B stored [n,k]).
const SYNTH_RECON_MSL: &str = r#"
kernel void synth_reconstruct(
    device float* arena          [[buffer(0)]],
    constant ulong& idx_off      [[buffer(1)]],
    constant ulong& cb_off       [[buffer(2)]],
    constant ulong& w_off        [[buffer(3)]],
    constant uint& k_dim         [[buffer(4)]],
    constant uint& n_dim         [[buffer(5)]],
    constant uint& entry_dim     [[buffer(6)]],
    uint2 gid                    [[thread_position_in_grid]]
) {
    uint nb = k_dim / entry_dim;
    uint kb = gid.x;
    uint j = gid.y;
    if (kb >= nb || j >= n_dim) return;
    device const uchar* idx = (device const uchar*)arena + idx_off + (ulong)j * nb;
    device const float* cb  = (device const float*)((device const char*)arena + cb_off);
    device float* w = (device float*)((device char*)arena + w_off)
                    + (ulong)j * k_dim + (ulong)kb * entry_dim;
    uint code = uint(idx[kb]);
    device const float* c = cb + (ulong)code * entry_dim;
    for (uint t = 0u; t < entry_dim; ++t) w[t] = c[t];
}
"#;

/// f16 reconstruct for the RLX_METAL_SYNTH_RECON_F16 prefill path: writes the dense
/// weight W[k,n] (row-major, stride n — the NON-transposed layout MPS `hgemm` reads)
/// as `half` from u8 indices + f32 codebook. Half the scratch bytes of the f32
/// reconstruct; paired with `encode_mps_hgemm` after casting x→f16. `W[b·d+t, j] =
/// codebook[indices[j,b], t]`.
const SYNTH_RECON_H_MSL: &str = r#"
kernel void synth_reconstruct_h(
    device float* arena          [[buffer(0)]],
    constant ulong& idx_off      [[buffer(1)]],
    constant ulong& cb_off       [[buffer(2)]],
    constant ulong& w_off        [[buffer(3)]],
    constant uint& k_dim         [[buffer(4)]],
    constant uint& n_dim         [[buffer(5)]],
    constant uint& entry_dim     [[buffer(6)]],
    uint2 gid                    [[thread_position_in_grid]]
) {
    uint nb = k_dim / entry_dim;
    uint kb = gid.x, j = gid.y;
    if (kb >= nb || j >= n_dim) return;
    device const uchar* idx = (device const uchar*)arena + idx_off + (ulong)j * nb;
    device const float* cb  = (device const float*)((device const char*)arena + cb_off);
    device half* w = (device half*)((device char*)arena + w_off);
    uint code = uint(idx[kb]);
    device const float* c = cb + (ulong)code * entry_dim;
    for (uint t = 0u; t < entry_dim; ++t) w[(ulong)(kb * entry_dim + t) * n_dim + j] = (half)c[t];
}
"#;

/// Threadgroup-tiled FUSED codebook matmul via `simdgroup_float8x8` MMAs. A 4×4
/// grid of 16 simdgroups (512 threads) computes a 32×32 output tile. Each K-step
/// stages the x tile (A) into threadgroup memory AND reconstructs the weight tile
/// (Bᵀ) from u8 indices + the L1-resident codebook directly into threadgroup memory
/// — the dense weight is NEVER materialized to DRAM. Bounds-checked on all three
/// dims (zero-fill loads, staged bounds-safe store), so it handles M/N/K not
/// multiples of 32. f32 only. Pairs with `encode_synth_matmul_tiled`.
///
/// MEASURED (M4 Pro, `synth_m_sweep_bench`): register blocking (this 64×64/2×2
/// version) cut large-M time ~15–20% vs the first 32×32/1×1 tiling (M=256: 1.5→1.27
/// ms) — but it STILL does NOT beat recon→MPS (M=256: 1.27 vs 0.71 ms; ~1.8× off
/// MPS's tuned GEMM). Two honest kernel iterations confirm the physics: at large M
/// you can't out-GEMM MPS by hand and the ceiling (dense-MPS) is only ~20% under
/// recon→MPS; at small/medium M the theoretical win is bigger (16× fewer weight
/// bytes) but a big register-blocked tile wastes rows + starves occupancy there — it
/// wants a different small-M split-K GEMM. Diminishing returns, so recon→MPS stays
/// the DEFAULT for m>8. Kept opt-in (`RLX_METAL_SYNTH_TILED`) for its non-speed wins:
/// zero scratch DRAM (recon→MPS needs a k·n·4 scratch — 16 MB at 2048²) and a single
/// capture-friendly pure-MSL dispatch (no MPS call). Next lever if ever needed:
/// double-buffer the K-panel (blocked here by the 32 KB threadgroup budget) +
/// a small-M split-K variant.
const SYNTH_TILED_MSL: &str = r#"
kernel void synth_matmul_codebook_tiled(
    device float* arena          [[buffer(0)]],
    constant ulong& x_off        [[buffer(1)]],
    constant ulong& idx_off      [[buffer(2)]],
    constant ulong& cb_off       [[buffer(3)]],
    constant ulong& dst_off      [[buffer(4)]],
    constant uint& k_dim         [[buffer(5)]],
    constant uint& n_dim         [[buffer(6)]],
    constant uint& entry_dim     [[buffer(7)]],
    constant uint& num_entries   [[buffer(8)]],
    constant uint& m_dim         [[buffer(9)]],
    uint2 tgid [[threadgroup_position_in_grid]],
    uint sgid  [[simdgroup_index_in_threadgroup]],
    uint slid  [[thread_index_in_simdgroup]]
) {
    (void)num_entries;
    // 64×64 output tile per threadgroup, K-panel 32. 16 simdgroups (4×4); each owns
    // a 16×16 sub-tile = a 2×2 grid of 8×8 accumulators, so every A/B tile load feeds
    // FOUR MMAs (4× the arithmetic intensity of one 8×8 per load).
    uint sg_row = sgid / 4;            // 0..3
    uint sg_col = sgid % 4;            // 0..3
    uint tg_row_base = tgid.y * 64;    // output rows = m
    uint tg_col_base = tgid.x * 64;    // output cols = n
    uint nb = k_dim / entry_dim;

    threadgroup float A_tg[64 * 32];   // [m64][k32]
    threadgroup float B_tg[32 * 64];   // [k32][n64]

    simdgroup_float8x8 c00 = simdgroup_float8x8(0.0f);
    simdgroup_float8x8 c01 = simdgroup_float8x8(0.0f);
    simdgroup_float8x8 c10 = simdgroup_float8x8(0.0f);
    simdgroup_float8x8 c11 = simdgroup_float8x8(0.0f);

    device const float* xbase   = (device const float*)((device const char*)arena + x_off);
    device const uchar* idxbase = (device const uchar*)arena + idx_off;
    device const float* cb      = (device const float*)((device const char*)arena + cb_off);

    uint linear = sgid * 32 + slid;    // 0..511

    for (uint kk = 0; kk < k_dim; kk += 32) {
        // A tile (x): 64×32 = 2048 elems, 4 per thread.
        for (uint i = 0; i < 4; ++i) {
            uint idx = i * 512 + linear;
            uint ar = idx / 32, ac = idx % 32;
            uint r = tg_row_base + ar;
            uint kcol = kk + ac;
            A_tg[idx] = (r < m_dim && kcol < k_dim) ? xbase[(ulong)r * k_dim + kcol] : 0.0f;
        }
        // B tile (Wᵀ) reconstructed on-chip: B[k,j] = codebook[indices[j,k/D]*D + k%D].
        for (uint i = 0; i < 4; ++i) {
            uint idx = i * 512 + linear;
            uint br = idx / 64, bc = idx % 64;
            uint krow = kk + br;
            uint j = tg_col_base + bc;
            float w = 0.0f;
            if (j < n_dim && krow < k_dim) {
                uint blk = krow / entry_dim;
                uint t   = krow % entry_dim;
                uint code = uint(idxbase[(ulong)j * nb + blk]);
                w = cb[(ulong)code * entry_dim + t];
            }
            B_tg[idx] = w;
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);

        for (uint kin = 0; kin < 32; kin += 8) {
            simdgroup_float8x8 a0, a1, b0, b1;
            simdgroup_load(a0, &A_tg[(sg_row * 16 + 0) * 32 + kin], 32);
            simdgroup_load(a1, &A_tg[(sg_row * 16 + 8) * 32 + kin], 32);
            simdgroup_load(b0, &B_tg[kin * 64 + sg_col * 16 + 0], 64);
            simdgroup_load(b1, &B_tg[kin * 64 + sg_col * 16 + 8], 64);
            simdgroup_multiply_accumulate(c00, a0, b0, c00);
            simdgroup_multiply_accumulate(c01, a0, b1, c01);
            simdgroup_multiply_accumulate(c10, a1, b0, c10);
            simdgroup_multiply_accumulate(c11, a1, b1, c11);
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }

    // Bounds-safe store of the four 8×8 blocks via a reused staging buffer.
    threadgroup float stage[16 * 64];
    device float* dst = (device float*)((device char*)arena + dst_off);
#define SYNTH_STORE_BLOCK(CC, DI, DJ)                                              \
    simdgroup_store(CC, &stage[sgid * 64], 8);                                     \
    threadgroup_barrier(mem_flags::mem_threadgroup);                               \
    {                                                                             \
        uint row_base = tg_row_base + sg_row * 16 + (DI) * 8;                      \
        uint col_base = tg_col_base + sg_col * 16 + (DJ) * 8;                      \
        for (uint e = slid; e < 64; e += 32) {                                    \
            uint rr = e / 8, cc2 = e % 8;                                         \
            uint r = row_base + rr, col = col_base + cc2;                          \
            if (r < m_dim && col < n_dim)                                          \
                dst[(ulong)r * n_dim + col] = stage[sgid * 64 + rr * 8 + cc2];     \
        }                                                                         \
    }                                                                             \
    threadgroup_barrier(mem_flags::mem_threadgroup);
    SYNTH_STORE_BLOCK(c00, 0, 0)
    SYNTH_STORE_BLOCK(c01, 0, 1)
    SYNTH_STORE_BLOCK(c10, 1, 0)
    SYNTH_STORE_BLOCK(c11, 1, 1)
#undef SYNTH_STORE_BLOCK
}
"#;

/// f16 ("relaxed precision") variant of the tiled kernel: identical 64×64/2×2
/// structure, but the A/B threadgroup tiles are `half` and the MMAs use
/// `simdgroup_half8x8` inputs with an f32 accumulator — the fast path Apple's
/// matrix units + MPS/`matmul2d` actually take (`dv_f16_dv_f16_dv_f32`). External
/// I/O stays f32 (x/codebook cast to half on load; f32 accumulate → f32 dst), so it
/// drops into the same dispatch/bench harness. Half-width tiles also HALVE the
/// threadgroup-memory footprint (8 KB vs 16 KB), the headroom double-buffering needs.
/// Opt-in via `RLX_METAL_SYNTH_TILED` + `RLX_METAL_SYNTH_TILED_F16`.
///
/// MEASURED: correct (rel err ~1e-4 vs f32 CPU) but ~0 speedup over the f32 tiled
/// kernel (M=256: 1.24 vs 1.22 ms). That's the tell: this kernel is bound by the
/// SYNCHRONOUS-load + barrier stall (see the note on `simdgroup_async_copy`), NOT
/// matrix-ALU throughput — so switching to the faster f16 MMA path moves nothing.
/// It still loses to recon→MPS. The freed SRAM would let double-buffering hide some
/// of that stall (the one remaining lever short of the async-copy intrinsic).
const SYNTH_TILED_H_MSL: &str = r#"
kernel void synth_matmul_codebook_tiled_h(
    device float* arena          [[buffer(0)]],
    constant ulong& x_off        [[buffer(1)]],
    constant ulong& idx_off      [[buffer(2)]],
    constant ulong& cb_off       [[buffer(3)]],
    constant ulong& dst_off      [[buffer(4)]],
    constant uint& k_dim         [[buffer(5)]],
    constant uint& n_dim         [[buffer(6)]],
    constant uint& entry_dim     [[buffer(7)]],
    constant uint& num_entries   [[buffer(8)]],
    constant uint& m_dim         [[buffer(9)]],
    uint2 tgid [[threadgroup_position_in_grid]],
    uint sgid  [[simdgroup_index_in_threadgroup]],
    uint slid  [[thread_index_in_simdgroup]]
) {
    (void)num_entries;
    uint sg_row = sgid / 4;
    uint sg_col = sgid % 4;
    uint tg_row_base = tgid.y * 64;
    uint tg_col_base = tgid.x * 64;
    uint nb = k_dim / entry_dim;

    threadgroup half A_tg[64 * 32];    // f16 tiles (half the SRAM of the f32 kernel)
    threadgroup half B_tg[32 * 64];

    simdgroup_float8x8 c00 = simdgroup_float8x8(0.0f);
    simdgroup_float8x8 c01 = simdgroup_float8x8(0.0f);
    simdgroup_float8x8 c10 = simdgroup_float8x8(0.0f);
    simdgroup_float8x8 c11 = simdgroup_float8x8(0.0f);

    device const float* xbase   = (device const float*)((device const char*)arena + x_off);
    device const uchar* idxbase = (device const uchar*)arena + idx_off;
    device const float* cb      = (device const float*)((device const char*)arena + cb_off);

    uint linear = sgid * 32 + slid;

    for (uint kk = 0; kk < k_dim; kk += 32) {
        for (uint i = 0; i < 4; ++i) {
            uint idx = i * 512 + linear;
            uint ar = idx / 32, ac = idx % 32;
            uint r = tg_row_base + ar;
            uint kcol = kk + ac;
            A_tg[idx] = (r < m_dim && kcol < k_dim) ? (half)xbase[(ulong)r * k_dim + kcol] : (half)0.0h;
        }
        for (uint i = 0; i < 4; ++i) {
            uint idx = i * 512 + linear;
            uint br = idx / 64, bc = idx % 64;
            uint krow = kk + br;
            uint j = tg_col_base + bc;
            half w = (half)0.0h;
            if (j < n_dim && krow < k_dim) {
                uint blk = krow / entry_dim;
                uint t   = krow % entry_dim;
                uint code = uint(idxbase[(ulong)j * nb + blk]);
                w = (half)cb[(ulong)code * entry_dim + t];
            }
            B_tg[idx] = w;
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);

        for (uint kin = 0; kin < 32; kin += 8) {
            simdgroup_half8x8 a0, a1, b0, b1;
            simdgroup_load(a0, &A_tg[(sg_row * 16 + 0) * 32 + kin], 32);
            simdgroup_load(a1, &A_tg[(sg_row * 16 + 8) * 32 + kin], 32);
            simdgroup_load(b0, &B_tg[kin * 64 + sg_col * 16 + 0], 64);
            simdgroup_load(b1, &B_tg[kin * 64 + sg_col * 16 + 8], 64);
            simdgroup_multiply_accumulate(c00, a0, b0, c00);
            simdgroup_multiply_accumulate(c01, a0, b1, c01);
            simdgroup_multiply_accumulate(c10, a1, b0, c10);
            simdgroup_multiply_accumulate(c11, a1, b1, c11);
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }

    threadgroup float stage[16 * 64];
    device float* dst = (device float*)((device char*)arena + dst_off);
#define SYNTH_STORE_BLOCK_H(CC, DI, DJ)                                            \
    simdgroup_store(CC, &stage[sgid * 64], 8);                                     \
    threadgroup_barrier(mem_flags::mem_threadgroup);                               \
    {                                                                             \
        uint row_base = tg_row_base + sg_row * 16 + (DI) * 8;                      \
        uint col_base = tg_col_base + sg_col * 16 + (DJ) * 8;                      \
        for (uint e = slid; e < 64; e += 32) {                                    \
            uint rr = e / 8, cc2 = e % 8;                                         \
            uint r = row_base + rr, col = col_base + cc2;                          \
            if (r < m_dim && col < n_dim)                                          \
                dst[(ulong)r * n_dim + col] = stage[sgid * 64 + rr * 8 + cc2];     \
        }                                                                         \
    }                                                                             \
    threadgroup_barrier(mem_flags::mem_threadgroup);
    SYNTH_STORE_BLOCK_H(c00, 0, 0)
    SYNTH_STORE_BLOCK_H(c01, 0, 1)
    SYNTH_STORE_BLOCK_H(c10, 1, 0)
    SYNTH_STORE_BLOCK_H(c11, 1, 1)
#undef SYNTH_STORE_BLOCK_H
}
"#;

/// Fused backward of `Op::SynthMatMul` (`SynthKind::Codebook`) — the two
/// gradients as single dispatches each, replacing the ~11-op decomposition.
/// Arena-buffer + byte-offset convention (matches `synth_reconstruct`); f32.
const SYNTH_BWD_MSL: &str = r#"
// dx[m,k] = upstream[m,n] · Ŵᵀ[n,k], where Ŵᵀ[j, kb·d+t] = codebook[idx[j·nb+kb]][t].
// One thread per output element (i,p); the codebook entry is reconstructed in the
// inner loop over n — no materialized weight, no separate gather.
kernel void synth_bwd_dx(
    device float* arena     [[buffer(0)]],
    constant ulong& up_off  [[buffer(1)]],   // upstream [m,n] f32
    constant ulong& idx_off [[buffer(2)]],   // indices  [n,nb] u8
    constant ulong& cb_off  [[buffer(3)]],   // codebook [ne,d] f32
    constant ulong& dst_off [[buffer(4)]],   // dx       [m,k] f32
    constant uint& m [[buffer(5)]],
    constant uint& n [[buffer(6)]],
    constant uint& k [[buffer(7)]],
    constant uint& d [[buffer(8)]],
    uint2 gid [[thread_position_in_grid]]
) {
    uint p = gid.x;
    uint i = gid.y;
    if (i >= m || p >= k) return;
    device const float* up  = (device const float*)((device const char*)arena + up_off);
    device const uchar* idx = (device const uchar*)arena + idx_off;
    device const float* cb  = (device const float*)((device const char*)arena + cb_off);
    device float* dx        = (device float*)((device char*)arena + dst_off);
    uint nb = k / d;
    uint kb = p / d;
    uint t  = p - kb * d;
    float acc = 0.0f;
    for (uint j = 0; j < n; ++j) {
        uint code = uint(idx[j * nb + kb]);
        acc += up[i * n + j] * cb[code * d + t];
    }
    dx[i * k + p] = acc;
}

// d_codebook[ne,d]: each entry gets the summed weight-gradient of the blocks that
// index it. grad_W = upstreamᵀ·x; block bi=j·nb+kb contributes
// grad_W_block[bi][t] = Σ_i upstream[i,j]·x[i, kb·d+t] to codebook[idx[bi]][t].
// One thread per output (e,t) scans all blocks — no atomics, no pre-zero.
kernel void synth_bwd_codebook(
    device float* arena     [[buffer(0)]],
    constant ulong& up_off  [[buffer(1)]],   // upstream [m,n] f32
    constant ulong& idx_off [[buffer(2)]],   // indices  [n,nb] u8
    constant ulong& x_off   [[buffer(3)]],   // x        [m,k] f32
    constant ulong& dst_off [[buffer(4)]],   // d_codebook [ne,d] f32
    constant uint& m  [[buffer(5)]],
    constant uint& n  [[buffer(6)]],
    constant uint& k  [[buffer(7)]],
    constant uint& d  [[buffer(8)]],
    constant uint& ne [[buffer(9)]],
    uint2 gid [[thread_position_in_grid]]
) {
    uint t = gid.x;
    uint e = gid.y;
    if (e >= ne || t >= d) return;
    device const float* up  = (device const float*)((device const char*)arena + up_off);
    device const uchar* idx = (device const uchar*)arena + idx_off;
    device const float* x   = (device const float*)((device const char*)arena + x_off);
    device float* dcb       = (device float*)((device char*)arena + dst_off);
    uint nb = k / d;
    float acc = 0.0f;
    for (uint j = 0; j < n; ++j) {
        for (uint kb = 0; kb < nb; ++kb) {
            if (uint(idx[j * nb + kb]) != e) continue;
            uint col = kb * d + t;
            float g = 0.0f;
            for (uint i = 0; i < m; ++i) {
                g += up[i * n + j] * x[i * k + col];
            }
            acc += g;
        }
    }
    dcb[e * d + t] = acc;
}
"#;

/// Register-blocked simdgroup GEMM with a bias + activation epilogue folded into
/// the store (one dispatch, no separate epilogue pass). 64×64 tile, 4×4
/// simdgroups × 2×2 accumulators — MPS-class arithmetic intensity (measured).
/// Arena-buffer + byte-offset convention; f32; m,n multiple of 64, k of 16.
/// `act`: 0 = none, 1 = ReLU. Beats MPS+separate-epilogue on small/aligned shapes
/// (2.14× on 192²) — the win on kernels MPS can't fuse. Opt-in via encode gate.
const RB_GEMM_BIAS_MSL: &str = r#"
kernel void gemm_rb_bias(
    device const float* A    [[buffer(0)]],   // A [M,K] f32 (arena)
    device const float* B    [[buffer(1)]],   // B [K,N] f32 (arena or weight buf)
    device const float* bias [[buffer(2)]],   // bias [N] f32
    device float* C          [[buffer(3)]],   // C [M,N] f32 (arena)
    constant uint& M [[buffer(4)]],
    constant uint& K [[buffer(5)]],
    constant uint& N [[buffer(6)]],
    constant uint& act [[buffer(7)]],
    uint2 tgid [[threadgroup_position_in_grid]],
    uint sgid  [[simdgroup_index_in_threadgroup]],
    uint slid  [[thread_index_in_simdgroup]]
) {
    const uint KT = 16;
    uint sg_row = sgid / 4, sg_col = sgid % 4;
    uint rbase = tgid.y * 64, cbase = tgid.x * 64;
    threadgroup float A_tg[64 * 16];
    threadgroup float B_tg[16 * 64];
    threadgroup float C_tg[64 * 64];
    simdgroup_float8x8 c00 = simdgroup_float8x8(0.0f), c01 = c00, c10 = c00, c11 = c00;
    uint lin = sgid * 32 + slid;
    for (uint kk = 0; kk < K; kk += KT) {
        for (uint i = 0; i < 2; ++i) {
            uint idx = i * 512 + lin;
            uint ar = idx / KT, ac = idx % KT;
            A_tg[idx] = A[(rbase + ar) * K + (kk + ac)];
        }
        for (uint i = 0; i < 2; ++i) {
            uint idx = i * 512 + lin;
            uint br = idx / 64, bc = idx % 64;
            B_tg[br * 64 + bc] = B[(kk + br) * N + (cbase + bc)];
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);
        for (uint ki = 0; ki < KT; ki += 8) {
            simdgroup_float8x8 a0, a1, b0, b1;
            simdgroup_load(a0, &A_tg[(sg_row * 16 + 0) * KT + ki], KT);
            simdgroup_load(a1, &A_tg[(sg_row * 16 + 8) * KT + ki], KT);
            simdgroup_load(b0, &B_tg[ki * 64 + sg_col * 16 + 0], 64);
            simdgroup_load(b1, &B_tg[ki * 64 + sg_col * 16 + 8], 64);
            simdgroup_multiply_accumulate(c00, a0, b0, c00);
            simdgroup_multiply_accumulate(c01, a0, b1, c01);
            simdgroup_multiply_accumulate(c10, a1, b0, c10);
            simdgroup_multiply_accumulate(c11, a1, b1, c11);
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }
    uint cr = sg_row * 16, cc = sg_col * 16;
    simdgroup_store(c00, &C_tg[(cr + 0) * 64 + cc + 0], 64);
    simdgroup_store(c01, &C_tg[(cr + 0) * 64 + cc + 8], 64);
    simdgroup_store(c10, &C_tg[(cr + 8) * 64 + cc + 0], 64);
    simdgroup_store(c11, &C_tg[(cr + 8) * 64 + cc + 8], 64);
    threadgroup_barrier(mem_flags::mem_threadgroup);
    for (uint i = 0; i < 8; ++i) {
        uint idx = i * 512 + lin;
        uint r = idx / 64, cn = idx % 64;
        uint gc = cbase + cn;
        float v = C_tg[idx] + bias[gc];
        if (act == 1u) v = max(v, 0.0f);
        C[(rbase + r) * N + gc] = v;
    }
}
"#;

/// Fused reconstruct of the dense weight `W[k,n]` directly (coalesced write over
/// n) from u8 indices + codebook — one dispatch replacing cast+gather+reshape+
/// transpose. `W[kb·d+t, j] = codebook[idx[j·nb+kb], t]`, `nb=k/d`.
// Fused reconstruct of the codebook weight in the BACKWARD-friendly `w_bt[n,k]`
// layout (cast+gather+reshape in one dispatch, no transpose). The model emits
// `W[k,n] = Transpose(this)`, so the forward transpose is the only conversion and
// the backward `dx = dy·w_bt` reuses this buffer for free (AD cancels `Transposeᵀ`).
// Coalesced write over the fast `k` axis.
const SYNTH_RECON_NK_MSL: &str = r#"
kernel void synth_reconstruct_nk(
    device float* arena     [[buffer(0)]],
    constant ulong& idx_off [[buffer(1)]],
    constant ulong& cb_off  [[buffer(2)]],
    constant ulong& w_off   [[buffer(3)]],
    constant uint& n [[buffer(4)]],
    constant uint& k [[buffer(5)]],
    constant uint& d [[buffer(6)]],
    uint2 gid [[thread_position_in_grid]]
) {
    uint j = gid.x;   // k (fast → coalesced write)
    uint i = gid.y;   // n
    if (j >= k || i >= n) return;
    uint nb = k / d;
    device const uchar* idx = (device const uchar*)arena + idx_off;
    device const float* cb  = (device const float*)((device const char*)arena + cb_off);
    device float* w         = (device float*)((device char*)arena + w_off);
    uint kb = j / d, t = j - kb * d;
    uint code = uint(idx[i * nb + kb]);
    w[i * k + j] = cb[code * d + t];
}
"#;

macro_rules! synth_matmul_kernels {
    ( $( $suffix:literal => $elem:literal );+ $(;)? ) => {
        fn synth_matmul_msl() -> String {
            let mut src = String::new();
            $(
                src.push_str(&SYNTH_SPLITK_TMPL.replace("{S}", $suffix).replace("{T}", $elem));
                src.push_str(&SYNTH_MM_TMPL.replace("{S}", $suffix).replace("{T}", $elem));
            )+
            src.push_str(SYNTH_RECON_MSL);
            src.push_str(SYNTH_RECON_H_MSL);
            src.push_str(SYNTH_TILED_MSL);
            src.push_str(SYNTH_TILED_H_MSL);
            src.push_str(SYNTH_BWD_MSL);
            src.push_str(RB_GEMM_BIAS_MSL);
            src.push_str(SYNTH_RECON_NK_MSL);
            src
        }
    };
}

synth_matmul_kernels! {
    ""   => "float";
    "_h" => "half";
}

/// The core scalar activations paired with their Metal kernel base name — the
/// single list driving the generated inline functions and the f16/f32 kernels.
const CORE_ACTS: &[(rlx_ir::op::Activation, &str)] = {
    use rlx_ir::op::Activation as A;
    &[
        (A::Gelu, "gelu"),
        (A::GeluApprox, "gelu_approx"),
        (A::Silu, "silu"),
        (A::Relu, "relu"),
        (A::Sigmoid, "sigmoid"),
        (A::Tanh, "tanh"),
        (A::Exp, "exp"),
        (A::Log, "log"),
        (A::Sqrt, "sqrt"),
        (A::Rsqrt, "rsqrt"),
        (A::Recip, "rec"),
        (A::Neg, "neg"),
        (A::Abs, "abs"),
        (A::Sin, "sin"),
        (A::Cos, "cos"),
        (A::Tan, "tan"),
        (A::Atan, "atan"),
        (A::Round, "round"),
    ]
};

/// One `inline float rlx_<name>_scalar(float x)` per core activation, generated
/// from the shared rlxsl manifest — the single on-device definition of each
/// activation's scalar math. The f16/f32/vec4 kernels below all call these, so
/// the A&S erf polynomial (etc.) is no longer re-inlined per kernel. Metal
/// inlines these, so routing through them costs no performance.
fn scalar_act_fns_msl() -> String {
    let mut src = String::from("// @generated from rlxsl — scalar activation device functions.\n");
    for (act, name) in CORE_ACTS {
        let (stmts, expr) = rlxsl::emit_activation(*act, rlxsl::Lang::Msl);
        let body = stmts.join(" ");
        src.push_str(&format!(
            "inline float rlx_{name}_scalar(float x) {{ {body} return {expr}; }}\n"
        ));
    }
    src
}

/// The core scalar-activation **f16** in-place kernels (`{name}_inplace_h`),
/// each just calling the shared `rlx_<name>_scalar`. Replaces the hand-written
/// MSL that re-inlined the A&S erf polynomial (and had drifted: unclamped erf
/// arg, ties-away `round`). Signature matches the dispatch: `buffer(0)=half*
/// data`, `buffer(1)=len`.
fn core_act_inplace_h_msl() -> String {
    let mut src =
        String::from("// @generated from rlxsl — core scalar-activation f16 in-place kernels.\n");
    for (_act, name) in CORE_ACTS {
        src.push_str(&format!(
            "kernel void {name}_inplace_h(device half* data [[buffer(0)]], constant uint& len [[buffer(1)]], uint gid [[thread_position_in_grid]]) {{ if (gid >= len) return; data[gid] = half(rlx_{name}_scalar(float(data[gid]))); }}\n"
        ));
    }
    src
}

/// The f32 `gelu`/`gelu_approx` in-place kernels (char-arena form), each calling
/// the shared `rlx_<name>_scalar`. These were the f32 scalar `_inplace` kernels
/// that re-inlined non-trivial math; the trivial ones (relu/exp/…) stay
/// hand-written.
fn gelu_inplace_f32_msl() -> String {
    let mut src = String::from("// @generated from rlxsl — f32 gelu/gelu_approx in-place.\n");
    for name in ["gelu", "gelu_approx"] {
        src.push_str(&format!(
            "kernel void {name}_inplace(device char* arena [[buffer(0)]], constant ulong& data_byte_off [[buffer(1)]], constant uint& len [[buffer(2)]], uint gid [[thread_position_in_grid]]) {{ if (gid >= len) return; device float* data = (device float*)(arena + data_byte_off); data[gid] = rlx_{name}_scalar(data[gid]); }}\n"
        ));
    }
    src
}

/// `inline float rlx_pow_scalar(float a, float b)` — the Rust-`powf`-matching
/// pow generated from rlxsl, for the tuned broadcast kernels (whose bare MSL
/// `pow` NaN'd on a negative base). Metal inlines it → no perf cost.
fn pow_scalar_fn_msl() -> String {
    let (stmts, expr) = rlxsl::binary::emit_binary(rlx_ir::op::BinaryOp::Pow, rlxsl::Lang::Msl);
    format!(
        "// @generated from rlxsl — Rust-powf-matching scalar pow (+ a vec4 overload\n\
         // for the packed_float4 broadcast kernels).\n\
         inline float rlx_pow_scalar(float a, float b) {{ {} return {expr}; }}\n\
         inline packed_float4 rlx_pow_scalar(packed_float4 a, packed_float4 b) {{ \
         packed_float4 r; for (uint i = 0u; i < 4u; ++i) {{ r[i] = rlx_pow_scalar(a[i], b[i]); }} return r; }}\n",
        stmts.join(" ")
    )
}

/// f32 + f16-KV decode SDPA generated from ONE template (below): the K/V
/// buffers are `__KV__` (`float` or `half`), while Q, the online-softmax
/// accumulation, and the output stay f32 — so `sdpa_decode_m1_f16kv` reads a
/// half-sized KV cache with no accumulation-precision change. `float()` /
/// `float4()` are no-ops on the f32 variant.
const SDPA_DECODE_M1_TEMPLATE: &str = r####"kernel void __NAME__(
    device const float* arena_q   [[buffer(0)]],
    device const __KV__* arena_k   [[buffer(1)]],
    device const __KV__* arena_v   [[buffer(2)]],
    device const float* arena_m   [[buffer(3)]],
    device float*       arena_o   [[buffer(4)]],
    constant uint& batch       [[buffer(5)]],
    constant uint& heads       [[buffer(6)]],
    constant uint& head_dim    [[buffer(7)]],
    constant uint& q_stride    [[buffer(8)]],
    constant uint& mask_kind   [[buffer(9)]],
    constant uint& seq_k       [[buffer(10)]],
    constant uint& k_stride    [[buffer(11)]],
    constant uint& bhsd        [[buffer(12)]],
    constant uint& window      [[buffer(13)]],
    constant float& score_scale  [[buffer(14)]],
    constant float& attn_softcap [[buffer(15)]],
    constant SdpaOffsets& byte_offs [[buffer(16)]],
    uint tid  [[thread_position_in_threadgroup]],
    uint tgid [[threadgroup_position_in_grid]],
    uint tsize [[threads_per_threadgroup]]
) {
    device const float* Q = (device const float*)((device const char*)arena_q + byte_offs.q);
    device const __KV__* K = (device const __KV__*)((device const char*)arena_k + byte_offs.k);
    device const __KV__* V = (device const __KV__*)((device const char*)arena_v + byte_offs.v);
    device const float* M = (device const float*)((device const char*)arena_m + byte_offs.m);
    device float* OUT     = (device float*)((device char*)arena_o + byte_offs.o);

    // TG=32 × dh≤128 → 16 KiB for partial O; fits Apple7/8 32 KiB tg limit.
    constexpr uint MAX_DH = 128u;
    constexpr uint TG = 32u;
    threadgroup float tg_m[TG];
    threadgroup float tg_l[TG];
    threadgroup float tg_o[TG * MAX_DH];

    uint bi = tgid / heads;
    uint hi = tgid % heads;
    if (bi >= batch) return;

    float scale = (score_scale > 0.0f) ? score_scale : rsqrt(float(head_dim));
    float softcap_inv = (attn_softcap > 0.0f) ? (1.0f / attn_softcap) : 0.0f;
    uint q_offset = seq_k - 1u;
    uint q_base = qkv_q_offset(bi, hi, 0u, heads, 1u, head_dim, q_stride, bhsd);
    // V/output per-head width (asymmetric MLA; == head_dim for symmetric).
    uint vdh = (byte_offs.v_head_dim == 0u) ? head_dim : byte_offs.v_head_dim;

    // Large head_dim OR v_head_dim: one thread walks all of K (rare for LLM
    // decode; Gemma-4, and MLA where qk=192 exceeds MAX_DH).
    if (head_dim > MAX_DH || vdh > MAX_DH || tsize < 2u) {
        if (tid != 0u) return;
        float q_reg[512];
        for (uint d = 0; d < head_dim; ++d) q_reg[d] = Q[q_base + d];
        float m_acc = -1e30f;
        float l_acc = 0.0f;
        float o_acc[512];
        for (uint d = 0; d < vdh; ++d) o_acc[d] = 0.0f;
        for (uint ki = 0; ki < seq_k; ++ki) {
            uint k_base = qkv_kv_offset(bi, hi, ki, heads, byte_offs.kv_heads, seq_k, head_dim, k_stride, bhsd);
            float dot = 0.0f;
            for (uint d = 0; d < head_dim; ++d) dot += q_reg[d] * float(K[k_base + d]);
            float s = dot * scale;
            if (softcap_inv > 0.0f) s = precise::tanh(s * softcap_inv) * attn_softcap;
            if (mask_kind == 1u) {
                if (ki > q_offset) s = -1e9f;
            } else if (mask_kind == 2u) {
                if (M[bi * k_stride + ki] < 0.5f) s = -1e9f;
            } else if (mask_kind == 4u) {
                uint lo = q_offset > window ? q_offset - window : 0u;
                if (ki < lo || ki > q_offset) s = -1e9f;
            }
            // Masked positions contribute 0; skip them so a masked V slot holding
            // inf (uninitialized f16 KV padding) can't poison the accumulator via
            // 0*inf = NaN. Bit-identical for finite (f32) padding.
            if (s <= -1.0e9f) continue;
            float m_new = max(m_acc, s);
            float e_old = exp(m_acc - m_new);
            float e_cur = exp(s - m_new);
            l_acc = e_old * l_acc + e_cur;
            uint v_base = qkv_v_offset(bi, hi, ki, heads, byte_offs.kv_heads, seq_k, vdh, k_stride, bhsd);
            for (uint d = 0; d < vdh; ++d) {
                o_acc[d] = e_old * o_acc[d] + e_cur * float(V[v_base + d]);
            }
            m_acc = m_new;
        }
        float inv_l = 1.0f / l_acc;
        uint o_base = qkv_out_offset(bi, hi, 0u, heads, 1u, vdh, q_stride, bhsd);
        for (uint d = 0; d < vdh; ++d) OUT[o_base + d] = o_acc[d] * inv_l;
        return;
    }

    float q_reg[MAX_DH];
    for (uint d = tid; d < head_dim; d += tsize) tg_o[d] = Q[q_base + d];
    threadgroup_barrier(mem_flags::mem_threadgroup);
    for (uint d = 0; d < head_dim; ++d) q_reg[d] = tg_o[d];
    threadgroup_barrier(mem_flags::mem_threadgroup);

    float m_acc = -1e30f;
    float l_acc = 0.0f;
    float o_acc[MAX_DH];
    for (uint d = 0; d < vdh; ++d) o_acc[d] = 0.0f;

    for (uint ki = tid; ki < seq_k; ki += tsize) {
        uint k_base = qkv_kv_offset(bi, hi, ki, heads, byte_offs.kv_heads, seq_k, head_dim, k_stride, bhsd);
        float dot = 0.0f;
        for (uint d = 0; d < head_dim; d += 4u) {
            if (d + 3u < head_dim) {
                float4 qv = float4(q_reg[d], q_reg[d + 1u], q_reg[d + 2u], q_reg[d + 3u]);
                float4 kv = float4(*(device const __KV__4*)(K + k_base + d));
                dot += qv.x * kv.x + qv.y * kv.y + qv.z * kv.z + qv.w * kv.w;
            } else {
                for (uint dd = d; dd < head_dim; ++dd) dot += q_reg[dd] * float(K[k_base + dd]);
            }
        }
        float s = dot * scale;
        if (softcap_inv > 0.0f) s = precise::tanh(s * softcap_inv) * attn_softcap;
        if (mask_kind == 1u) {
            if (ki > q_offset) s = -1e9f;
        } else if (mask_kind == 2u) {
            if (M[bi * k_stride + ki] < 0.5f) s = -1e9f;
        } else if (mask_kind == 4u) {
            uint lo = q_offset > window ? q_offset - window : 0u;
            if (ki < lo || ki > q_offset) s = -1e9f;
        }
        // Skip masked positions (contribute 0) so an inf in a masked V slot
        // (uninitialized f16 KV padding) can't poison the accumulator via
        // 0*inf = NaN. Bit-identical for finite (f32) padding.
        if (s <= -1.0e9f) continue;
        float m_new = max(m_acc, s);
        float e_old = exp(m_acc - m_new);
        float e_cur = exp(s - m_new);
        l_acc = e_old * l_acc + e_cur;
        uint v_base = qkv_v_offset(bi, hi, ki, heads, byte_offs.kv_heads, seq_k, vdh, k_stride, bhsd);
        for (uint d = 0; d < vdh; ++d) {
            o_acc[d] = e_old * o_acc[d] + e_cur * float(V[v_base + d]);
        }
        m_acc = m_new;
    }

    // Idle lanes beyond TG keep identity merge state (already set if tid>=TG).
    if (tid < TG) {
        tg_m[tid] = m_acc;
        tg_l[tid] = l_acc;
        for (uint d = 0; d < vdh; ++d) tg_o[tid * MAX_DH + d] = o_acc[d];
    }
    threadgroup_barrier(mem_flags::mem_threadgroup);

    for (uint stride = TG / 2u; stride > 0u; stride /= 2u) {
        if (tid < stride) {
            float m1 = tg_m[tid];
            float m2 = tg_m[tid + stride];
            float m_new = max(m1, m2);
            float e1 = exp(m1 - m_new);
            float e2 = exp(m2 - m_new);
            tg_l[tid] = e1 * tg_l[tid] + e2 * tg_l[tid + stride];
            tg_m[tid] = m_new;
            for (uint d = 0; d < vdh; ++d) {
                tg_o[tid * MAX_DH + d] =
                    e1 * tg_o[tid * MAX_DH + d] + e2 * tg_o[(tid + stride) * MAX_DH + d];
            }
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }

    if (tid == 0u) {
        float inv_l = 1.0f / tg_l[0];
        uint o_base = qkv_out_offset(bi, hi, 0u, heads, 1u, vdh, q_stride, bhsd);
        for (uint d = 0; d < vdh; ++d) OUT[o_base + d] = tg_o[d] * inv_l;
    }
}"####;

macro_rules! sdpa_decode_m1_variant {
    ($name:expr, $kv:expr) => {
        SDPA_DECODE_M1_TEMPLATE
            .replace("__NAME__", $name)
            .replace("__KV__", $kv)
    };
}

// ── Flash-decoding (split-KV) for m=1 decode ────────────────────────────────
// The base `sdpa_decode_m1` launches exactly `batch*heads` threadgroups (one
// per head), each 32 threads walking ALL of K. At batch=1 that's ~16 tgs —
// far too few to fill the GPU, so decode attention is occupancy-starved (~37%
// of decode GPU time for trivial FLOPs). Flash-decoding adds a KV-partition
// axis: `batch*heads*P` threadgroups each attend one KV slice and write a
// partial online-softmax state {m, l, o[vdh]} to `scratch`; a tiny combine
// kernel then merges the P partials per head. Raises tg count 16→16*P.
//
// Scratch layout: float[(bi*heads+hi)*n_part + part][SLOT], SLOT = 2 + MAX_DH
// (m at +0, l at +1, un-normalized o at +2+d). Partial writes o WITHOUT the
// 1/l divide; combine does the cross-partition rescale then normalizes.
const SDPA_DECODE_M1_PARTIAL_TEMPLATE: &str = r####"kernel void __NAME__(
    device const float* arena_q   [[buffer(0)]],
    device const __KV__* arena_k   [[buffer(1)]],
    device const __KV__* arena_v   [[buffer(2)]],
    device const float* arena_m   [[buffer(3)]],
    device float*       arena_o   [[buffer(4)]],
    constant uint& batch       [[buffer(5)]],
    constant uint& heads       [[buffer(6)]],
    constant uint& head_dim    [[buffer(7)]],
    constant uint& q_stride    [[buffer(8)]],
    constant uint& mask_kind   [[buffer(9)]],
    constant uint& seq_k       [[buffer(10)]],
    constant uint& k_stride    [[buffer(11)]],
    constant uint& bhsd        [[buffer(12)]],
    constant uint& window      [[buffer(13)]],
    constant float& score_scale  [[buffer(14)]],
    constant float& attn_softcap [[buffer(15)]],
    constant SdpaOffsets& byte_offs [[buffer(16)]],
    device float* scratch      [[buffer(17)]],
    constant uint& n_part      [[buffer(18)]],
    uint tid  [[thread_position_in_threadgroup]],
    uint tgid [[threadgroup_position_in_grid]],
    uint tsize [[threads_per_threadgroup]]
) {
    device const float* Q = (device const float*)((device const char*)arena_q + byte_offs.q);
    device const __KV__* K = (device const __KV__*)((device const char*)arena_k + byte_offs.k);
    device const __KV__* V = (device const __KV__*)((device const char*)arena_v + byte_offs.v);
    device const float* M = (device const float*)((device const char*)arena_m + byte_offs.m);

    constexpr uint MAX_DH = 128u;
    constexpr uint SLOT = 2u + MAX_DH;
    // No threadgroup memory: the threadgroup is exactly one simdgroup (32
    // threads), so the online-softmax reduction below uses simd_max/simd_sum
    // (warp-level) instead of a threadgroup-memory tree. This frees the old
    // 16 KB tg_o[32*128] scratch — the real occupancy limiter — so many more
    // decode-attention threadgroups stay resident per core, and it drops the
    // reduction barriers. Numerically identical to the tree merge.

    uint part = tgid % n_part;
    uint t    = tgid / n_part;
    uint hi   = t % heads;
    uint bi   = t / heads;
    if (bi >= batch) return;

    uint vdh = (byte_offs.v_head_dim == 0u) ? head_dim : byte_offs.v_head_dim;
    uint slot = (bi * heads + hi) * n_part + part;

    uint chunk = (seq_k + n_part - 1u) / n_part;
    uint start = part * chunk;
    uint end   = min(start + chunk, seq_k);
    if (start >= seq_k) {
        if (tid == 0u) {
            scratch[slot * SLOT + 0u] = -1e30f;
            scratch[slot * SLOT + 1u] = 0.0f;
            for (uint d = 0; d < vdh; ++d) scratch[slot * SLOT + 2u + d] = 0.0f;
        }
        return;
    }

    float scale = (score_scale > 0.0f) ? score_scale : rsqrt(float(head_dim));
    float softcap_inv = (attn_softcap > 0.0f) ? (1.0f / attn_softcap) : 0.0f;
    uint q_offset = seq_k - 1u;
    uint q_base = qkv_q_offset(bi, hi, 0u, heads, 1u, head_dim, q_stride, bhsd);

    // Each lane loads the full query row directly (≤128 floats, L1-cached — the
    // 32× redundancy is far cheaper than tg-memory staging + two barriers).
    float q_reg[MAX_DH];
    for (uint d = 0; d < head_dim; ++d) q_reg[d] = Q[q_base + d];

    float m_acc = -1e30f;
    float l_acc = 0.0f;
    float o_acc[MAX_DH];
    for (uint d = 0; d < vdh; ++d) o_acc[d] = 0.0f;

    for (uint ki = start + tid; ki < end; ki += tsize) {
        uint k_base = qkv_kv_offset(bi, hi, ki, heads, byte_offs.kv_heads, seq_k, head_dim, k_stride, bhsd);
        float dot = 0.0f;
        for (uint d = 0; d < head_dim; d += 4u) {
            if (d + 3u < head_dim) {
                float4 qv = float4(q_reg[d], q_reg[d + 1u], q_reg[d + 2u], q_reg[d + 3u]);
                float4 kv = float4(*(device const __KV__4*)(K + k_base + d));
                dot += qv.x * kv.x + qv.y * kv.y + qv.z * kv.z + qv.w * kv.w;
            } else {
                for (uint dd = d; dd < head_dim; ++dd) dot += q_reg[dd] * float(K[k_base + dd]);
            }
        }
        float s = dot * scale;
        if (softcap_inv > 0.0f) s = precise::tanh(s * softcap_inv) * attn_softcap;
        if (mask_kind == 1u) {
            if (ki > q_offset) s = -1e9f;
        } else if (mask_kind == 2u) {
            if (M[bi * k_stride + ki] < 0.5f) s = -1e9f;
        } else if (mask_kind == 4u) {
            uint lo = q_offset > window ? q_offset - window : 0u;
            if (ki < lo || ki > q_offset) s = -1e9f;
        }
        if (s <= -1.0e9f) continue;
        float m_new = max(m_acc, s);
        float e_old = exp(m_acc - m_new);
        float e_cur = exp(s - m_new);
        l_acc = e_old * l_acc + e_cur;
        uint v_base = qkv_v_offset(bi, hi, ki, heads, byte_offs.kv_heads, seq_k, vdh, k_stride, bhsd);
        for (uint d = 0; d < vdh; ++d) o_acc[d] = e_old * o_acc[d] + e_cur * float(V[v_base + d]);
        m_acc = m_new;
    }

    // Warp-level online-softmax reduction across the 32 lanes: global max, then
    // rescale each lane by exp(m_lane - m_global) and sum l + each o dim.
    // Identical to the tree merge; no threadgroup memory, no barriers.
    float m_g = simd_max(m_acc);
    float resc = exp(m_acc - m_g);          // lanes with no keys (m_acc=-1e30) → 0
    float l_g = simd_sum(l_acc * resc);
    if (tid == 0u) {
        scratch[slot * SLOT + 0u] = m_g;
        scratch[slot * SLOT + 1u] = l_g;
    }
    for (uint d = 0; d < vdh; ++d) {
        float og = simd_sum(o_acc[d] * resc);
        if (tid == 0u) scratch[slot * SLOT + 2u + d] = og;
    }
}"####;

macro_rules! sdpa_decode_m1_partial_variant {
    ($name:expr, $kv:expr) => {
        SDPA_DECODE_M1_PARTIAL_TEMPLATE
            .replace("__NAME__", $name)
            .replace("__KV__", $kv)
    };
}

// ── Head-dim-split flash-decode partial (register-pressure fix) ─────────────
// The KV-split partial above has every lane hold the FULL q_reg[head_dim] +
// o_acc[vdh] (256 regs/thread at D=128) → occupancy is register-capped. This
// variant splits head_dim ACROSS the 32 lanes instead: each lane owns
// `head_dim/32` dims (4 at D=128), all lanes process each key in lockstep, a
// `simd_sum` forms the full q·k score (broadcast to every lane), and each lane
// accumulates only its o-slice — so q_reg[4]+o_acc[4] = 8 regs/thread and the
// final o needs NO cross-lane reduction (m/l are identical on every lane; each
// lane writes its disjoint o dims). K/V reads are coalesced (consecutive lanes →
// consecutive addresses). Requires head_dim % 32 == 0 and vdh % 32 == 0 (caller
// guards; else the KV-split kernel above is used). Port of the llama.cpp
// `flash_attn_ext_vec` thread mapping. `MAX_DPL` = 128/32.
const SDPA_DECODE_M1_PARTIAL_HD_TEMPLATE: &str = r####"kernel void __NAME__(
    device const float* arena_q   [[buffer(0)]],
    device const __KV__* arena_k   [[buffer(1)]],
    device const __KV__* arena_v   [[buffer(2)]],
    device const float* arena_m   [[buffer(3)]],
    device float*       arena_o   [[buffer(4)]],
    constant uint& batch       [[buffer(5)]],
    constant uint& heads       [[buffer(6)]],
    constant uint& head_dim    [[buffer(7)]],
    constant uint& q_stride    [[buffer(8)]],
    constant uint& mask_kind   [[buffer(9)]],
    constant uint& seq_k       [[buffer(10)]],
    constant uint& k_stride    [[buffer(11)]],
    constant uint& bhsd        [[buffer(12)]],
    constant uint& window      [[buffer(13)]],
    constant float& score_scale  [[buffer(14)]],
    constant float& attn_softcap [[buffer(15)]],
    constant SdpaOffsets& byte_offs [[buffer(16)]],
    device float* scratch      [[buffer(17)]],
    constant uint& n_part      [[buffer(18)]],
    uint tid  [[thread_position_in_threadgroup]],
    uint tgid [[threadgroup_position_in_grid]],
    uint tsize [[threads_per_threadgroup]]
) {
    device const float* Q = (device const float*)((device const char*)arena_q + byte_offs.q);
    device const __KV__* K = (device const __KV__*)((device const char*)arena_k + byte_offs.k);
    device const __KV__* V = (device const __KV__*)((device const char*)arena_v + byte_offs.v);
    device const float* M = (device const float*)((device const char*)arena_m + byte_offs.m);

    constexpr uint MAX_DPL = __MAX_DPL__u;
    constexpr uint SLOT = 2u + __MAX_DH__u;

    uint part = tgid % n_part;
    uint t    = tgid / n_part;
    uint hi   = t % heads;
    uint bi   = t / heads;
    if (bi >= batch) return;

    uint vdh = (byte_offs.v_head_dim == 0u) ? head_dim : byte_offs.v_head_dim;
    uint slot = (bi * heads + hi) * n_part + part;

    uint chunk = (seq_k + n_part - 1u) / n_part;
    uint start = part * chunk;
    uint end   = min(start + chunk, seq_k);
    if (start >= seq_k) {
        if (tid == 0u) {
            scratch[slot * SLOT + 0u] = -1e30f;
            scratch[slot * SLOT + 1u] = 0.0f;
            for (uint d = 0; d < vdh; ++d) scratch[slot * SLOT + 2u + d] = 0.0f;
        }
        return;
    }

    float scale = (score_scale > 0.0f) ? score_scale : rsqrt(float(head_dim));
    float softcap_inv = (attn_softcap > 0.0f) ? (1.0f / attn_softcap) : 0.0f;
    uint q_offset = seq_k - 1u;
    uint q_base = qkv_q_offset(bi, hi, 0u, heads, 1u, head_dim, q_stride, bhsd);

    // This lane owns dims [d0, d0+dpl) of q/k and [vd0, vd0+vdpl) of v/o.
    uint dpl  = head_dim / 32u;   // head_dim % 32 == 0 guaranteed by the caller
    uint d0   = tid * dpl;
    uint vdpl = vdh / 32u;
    uint vd0  = tid * vdpl;

    float q_reg[MAX_DPL];
    for (uint j = 0; j < dpl; ++j) q_reg[j] = Q[q_base + d0 + j];

    float o_acc[MAX_DPL];
    for (uint j = 0; j < vdpl; ++j) o_acc[j] = 0.0f;
    float m_acc = -1e30f;
    float l_acc = 0.0f;

    // All 32 lanes walk the SAME key range in lockstep; the score reduction is a
    // per-key simd_sum (barrier-free warp op), so m/l stay identical on every lane.
    for (uint ki = start; ki < end; ++ki) {
        uint k_base = qkv_kv_offset(bi, hi, ki, heads, byte_offs.kv_heads, seq_k, head_dim, k_stride, bhsd);
        float pd = 0.0f;
        for (uint j = 0; j < dpl; ++j) pd += q_reg[j] * float(K[k_base + d0 + j]);
        float s = simd_sum(pd) * scale;
        if (softcap_inv > 0.0f) s = precise::tanh(s * softcap_inv) * attn_softcap;
        if (mask_kind == 1u) {
            if (ki > q_offset) s = -1e9f;
        } else if (mask_kind == 2u) {
            if (M[bi * k_stride + ki] < 0.5f) s = -1e9f;
        } else if (mask_kind == 4u) {
            uint lo = q_offset > window ? q_offset - window : 0u;
            if (ki < lo || ki > q_offset) s = -1e9f;
        }
        if (s <= -1.0e9f) continue;
        float m_new = max(m_acc, s);
        float e_old = exp(m_acc - m_new);
        float e_cur = exp(s - m_new);
        l_acc = e_old * l_acc + e_cur;
        uint v_base = qkv_v_offset(bi, hi, ki, heads, byte_offs.kv_heads, seq_k, vdh, k_stride, bhsd);
        for (uint j = 0; j < vdpl; ++j) o_acc[j] = e_old * o_acc[j] + e_cur * float(V[v_base + vd0 + j]);
        m_acc = m_new;
    }

    if (tid == 0u) {
        scratch[slot * SLOT + 0u] = m_acc;
        scratch[slot * SLOT + 1u] = l_acc;
    }
    for (uint j = 0; j < vdpl; ++j) scratch[slot * SLOT + 2u + vd0 + j] = o_acc[j];
}"####;

macro_rules! sdpa_decode_m1_partial_hd_variant {
    ($name:expr, $kv:expr, $dpl:expr, $dh:expr) => {
        SDPA_DECODE_M1_PARTIAL_HD_TEMPLATE
            .replace("__NAME__", $name)
            .replace("__KV__", $kv)
            .replace("__MAX_DPL__", $dpl)
            .replace("__MAX_DH__", $dh)
    };
}

// Combine the P partial online-softmax states per (batch, head) → final OUT.
// One threadgroup per (bi,hi); threads parallelize over vdh. m_g/l_g are cheap
// (loop over n_part ≤ ~16) so each thread recomputes them rather than sharing.
const SDPA_DECODE_M1_COMBINE_TEMPLATE: &str = r####"kernel void __NAME__(
    device const float* scratch [[buffer(0)]],
    device float*       arena_o [[buffer(1)]],
    constant uint& batch     [[buffer(2)]],
    constant uint& heads     [[buffer(3)]],
    constant uint& n_part    [[buffer(4)]],
    constant uint& head_dim  [[buffer(5)]],
    constant uint& q_stride  [[buffer(6)]],
    constant uint& bhsd      [[buffer(7)]],
    constant SdpaOffsets& byte_offs [[buffer(8)]],
    uint tid  [[thread_position_in_threadgroup]],
    uint tgid [[threadgroup_position_in_grid]],
    uint tsize [[threads_per_threadgroup]]
) {
    constexpr uint MAX_DH = __MAX_DH__u;
    constexpr uint SLOT = 2u + MAX_DH;
    device float* OUT = (device float*)((device char*)arena_o + byte_offs.o);
    uint hi = tgid % heads;
    uint bi = tgid / heads;
    if (bi >= batch) return;
    uint vdh = (byte_offs.v_head_dim == 0u) ? head_dim : byte_offs.v_head_dim;
    uint base = (bi * heads + hi) * n_part;

    float m_g = -1e30f;
    for (uint p = 0; p < n_part; ++p) m_g = max(m_g, scratch[(base + p) * SLOT + 0u]);
    float l_g = 0.0f;
    for (uint p = 0; p < n_part; ++p) {
        float mp = scratch[(base + p) * SLOT + 0u];
        float lp = scratch[(base + p) * SLOT + 1u];
        l_g += exp(mp - m_g) * lp;
    }
    float inv_l = (l_g > 0.0f) ? (1.0f / l_g) : 0.0f;
    uint o_base = qkv_out_offset(bi, hi, 0u, heads, 1u, vdh, q_stride, bhsd);
    for (uint d = tid; d < vdh; d += tsize) {
        float acc = 0.0f;
        for (uint p = 0; p < n_part; ++p) {
            float mp = scratch[(base + p) * SLOT + 0u];
            float op = scratch[(base + p) * SLOT + 2u + d];
            acc += exp(mp - m_g) * op;
        }
        OUT[o_base + d] = acc * inv_l;
    }
}"####;

macro_rules! sdpa_decode_m1_combine_variant {
    ($name:expr, $dh:expr) => {
        SDPA_DECODE_M1_COMBINE_TEMPLATE
            .replace("__NAME__", $name)
            .replace("__MAX_DH__", $dh)
    };
}

// ── Prefill flash attention, head-dim-split for head_dim=256 (qwen3.5) ───────
// The tiled prefill FA + splitk kernels cap at head_dim<=128 (per-lane q_reg),
// so head_dim=256 prefill fell to `sdpa_long` at 1 thread/threadgroup (single-
// threaded per head → ~1.3s/layer @4K, 55% of TTFT). This kernel is the decode
// flash-256 head-dim-split applied per query row: one threadgroup per (b,h,qi),
// 32 lanes each own head_dim/32 dims; the Q·K dot is a per-key simd_sum
// (barrier-free), online-softmax accumulates each lane's own output dims. Causal
// breaks past the query position (halves the walk). No scores matrix, O(D/32)
// registers/lane. Token-identical to sdpa_long.
const SDPA_PREFILL_HD256: &str = r####"kernel void sdpa_prefill_hd256(
    device const float* arena_q   [[buffer(0)]],
    device const float* arena_k   [[buffer(1)]],
    device const float* arena_v   [[buffer(2)]],
    device const float* arena_m   [[buffer(3)]],
    device float*       arena_o   [[buffer(4)]],
    constant uint& batch       [[buffer(5)]],
    constant uint& seq_q       [[buffer(6)]],
    constant uint& heads       [[buffer(7)]],
    constant uint& head_dim    [[buffer(8)]],
    constant uint& q_stride    [[buffer(9)]],
    constant uint& mask_kind   [[buffer(10)]],
    constant uint& seq_k       [[buffer(11)]],
    constant uint& k_stride    [[buffer(12)]],
    constant uint& bhsd        [[buffer(13)]],
    constant uint& window      [[buffer(14)]],
    constant float& score_scale  [[buffer(15)]],
    constant float& attn_softcap [[buffer(16)]],
    constant SdpaOffsets& byte_offs [[buffer(17)]],
    uint tgid_x [[threadgroup_position_in_grid]],
    uint lane   [[thread_index_in_threadgroup]]
) {
    device const float* Q = (device const float*)((device const char*)arena_q + byte_offs.q);
    device const float* K = (device const float*)((device const char*)arena_k + byte_offs.k);
    device const float* V = (device const float*)((device const char*)arena_v + byte_offs.v);
    device const float* M = (device const float*)((device const char*)arena_m + byte_offs.m);
    device float* OUT     = (device float*)((device char*)arena_o + byte_offs.o);

    constexpr uint MAX_DPL = 8u; // head_dim=256 / 32 lanes

    uint total_rows = batch * heads * seq_q;
    if (tgid_x >= total_rows) return;
    uint qi = tgid_x % seq_q;
    uint bh = tgid_x / seq_q;
    uint hi = bh % heads;
    uint bi = bh / heads;

    float scale = (score_scale > 0.0f) ? score_scale : rsqrt(float(head_dim));
    float softcap_inv = (attn_softcap > 0.0f) ? (1.0f / attn_softcap) : 0.0f;
    uint vdh = (byte_offs.v_head_dim == 0u) ? head_dim : byte_offs.v_head_dim;
    uint q_offset = seq_k - seq_q;
    uint abs_q = q_offset + qi;

    // This lane owns dims [d0, d0+dpl) of q/k and [vd0, vd0+vdpl) of v/o.
    uint dpl  = head_dim / 32u;   // head_dim % 32 == 0 guaranteed by the caller
    uint d0   = lane * dpl;
    uint vdpl = vdh / 32u;
    uint vd0  = lane * vdpl;

    float q_reg[MAX_DPL];
    uint q_base = qkv_q_offset(bi, hi, qi, heads, seq_q, head_dim, q_stride, bhsd);
    for (uint j = 0; j < dpl; ++j) q_reg[j] = Q[q_base + d0 + j];

    float o_acc[MAX_DPL];
    for (uint j = 0; j < vdpl; ++j) o_acc[j] = 0.0f;
    float m_acc = -1e30f;
    float l_acc = 0.0f;
    uint bias_row_base = ((bi * heads + hi) * seq_q + qi) * seq_k;

    // Sliding window (mask_kind==4): START the key scan at the window lower
    // bound and (with causal) break at `abs_q`, so each query iterates only
    // ~`window` keys instead of all `seq_k` → prefill attention is O(seq·window)
    // (LINEAR in seq) instead of O(seq²). The prior code masked with `continue`
    // but still iterated every key (quadratic). Other mask kinds start at 0.
    uint ki_start = (mask_kind == 4u && abs_q > window) ? (abs_q - window) : 0u;
    // Attention sinks (StreamingLLM): the first few tokens anchor the softmax, so
    // keep attending them even after the sliding window scrolls past. Only for
    // SWA (mask_kind==4), and only the sinks that fall before the window start.
    // All 32 lanes share `ki` (cooperative simd_sum), so this control flow is
    // uniform — no divergence.
    const uint SINKS = 4u;
    uint sink_end = (mask_kind == 4u) ? min(SINKS, ki_start) : 0u;
    #define HD256_KV_STEP(KI) { \
        uint k_base = qkv_kv_offset(bi, hi, (KI), heads, byte_offs.kv_heads, seq_k, head_dim, k_stride, bhsd); \
        float pd = 0.0f; \
        for (uint j = 0; j < dpl; ++j) pd += q_reg[j] * K[k_base + d0 + j]; \
        float s = simd_sum(pd) * scale; \
        if (softcap_inv > 0.0f) s = precise::tanh(s * softcap_inv) * attn_softcap; \
        if (mask_kind == 3u) s += M[bias_row_base + (KI)]; \
        float m_new = max(m_acc, s); \
        float e_old = precise::exp(m_acc - m_new); \
        float e_cur = precise::exp(s - m_new); \
        l_acc = e_old * l_acc + e_cur; \
        uint v_base = qkv_v_offset(bi, hi, (KI), heads, byte_offs.kv_heads, seq_k, vdh, k_stride, bhsd); \
        for (uint j = 0; j < vdpl; ++j) o_acc[j] = e_old * o_acc[j] + e_cur * V[v_base + vd0 + j]; \
        m_acc = m_new; \
    }
    // Sink pass (SWA only): [0, sink_end) — the anchor tokens.
    for (uint ki = 0; ki < sink_end; ++ki) HD256_KV_STEP(ki)
    // Window / causal pass: [ki_start, abs_q].
    for (uint ki = ki_start; ki < seq_k; ++ki) {
        if ((mask_kind == 1u || mask_kind == 4u) && ki > abs_q) break;
        if (mask_kind == 2u && M[bi * k_stride + ki] < 0.5f) continue;
        HD256_KV_STEP(ki)
    }
    #undef HD256_KV_STEP

    uint o_base = qkv_out_offset(bi, hi, qi, heads, seq_q, vdh, q_stride, bhsd);
    float inv_l = (l_acc > 0.0f) ? (1.0f / l_acc) : 0.0f;
    for (uint j = 0; j < vdpl; ++j) OUT[o_base + vd0 + j] = o_acc[j] * inv_l;
}"####;

// ── W8A8 decode attention (int8 Q·K integer dot + int8 V) ───────────────────
// Quantize one KV row to int8 (contiguous kv-major) + a per-row f32 absmax
// scale, for the W8A8 flash-decode path. One thread per row;
// gid = (bi*kv_heads + hkv)*seq + ki. The arena K/V may be f32 or f16 (__KV__);
// the int8 output layout is always contiguous [row][dh] so the partial reads it
// independently of the arena's BSNH/BHSD layout. `src_off` is a BYTE offset.
const KV_QUANT_I8_TEMPLATE: &str = r####"kernel void __NAME__(
    device const void*  arena_raw [[buffer(0)]],
    device char*        i8out     [[buffer(1)]],
    device float*       scout     [[buffer(2)]],
    constant ulong& src_off       [[buffer(3)]],
    constant uint&  nrows         [[buffer(4)]],
    constant uint&  dh            [[buffer(5)]],
    constant uint&  bhsd          [[buffer(6)]],
    constant uint&  kv_heads      [[buffer(7)]],
    constant uint&  seq           [[buffer(8)]],
    constant uint&  kstride        [[buffer(9)]],
    constant uint&  blk           [[buffer(10)]],
    uint gid [[thread_position_in_grid]]
) {
    if (gid >= nrows) return;
    uint ki  = gid % seq;
    uint bh  = gid / seq;
    uint hkv = bh % kv_heads;
    uint bi  = bh / kv_heads;
    device const __KV__* src = (device const __KV__*)((device const char*)arena_raw + src_off);
    uint aoff;
    if (bhsd != 0u) aoff = (bi * kv_heads + hkv) * seq * dh + ki * dh;
    else            aoff = bi * kstride * kv_heads * dh + ki * kv_heads * dh + hkv * dh;
    // blk=0: one absmax scale per `dh` row. blk=1: one scale per 32-element
    // sub-block (Q8_0-style) → ~4-8× lower rounding error at 4 scales/row.
    uint nb = (blk != 0u) ? (dh / 32u) : 1u;
    uint bs = (blk != 0u) ? 32u : dh;
    device char* dst = i8out + (ulong)gid * dh;
    for (uint b = 0; b < nb; ++b) {
        float amax = 0.0f;
        for (uint j = 0; j < bs; ++j) amax = max(amax, fabs(float(src[aoff + b * bs + j])));
        float s = amax * (1.0f / 127.0f);
        scout[(ulong)gid * nb + b] = s;
        float inv = (s > 1e-20f) ? (1.0f / s) : 0.0f;
        for (uint j = 0; j < bs; ++j)
            dst[b * bs + j] = char(clamp(round(float(src[aoff + b * bs + j]) * inv), -127.0f, 127.0f));
    }
}"####;

macro_rules! kv_quant_i8_variant {
    ($name:expr, $kv:expr) => {
        KV_QUANT_I8_TEMPLATE
            .replace("__NAME__", $name)
            .replace("__KV__", $kv)
    };
}

// W8A8 flash-decode partial: identical online-softmax / warp-reduction / scratch
// layout to `sdpa_decode_m1_partial` (so the SAME combine kernel merges it), but
// K/V come from the int8 scratch (contiguous kv-major) + per-row scales, and the
// query row is quantized to int8 in-kernel so the Q·K score is an INTEGER dot
// (int8×int8→int32 on the int ALU — ~1.6× the f32 dot on M-series). Score =
// int_dot * qscale * kscale * scale; V is dequantized per element (int8 * vscale).
// Gated by RLX_METAL_W8A8_ATTN; f32 KV only (arena K/V pre-quantized by kv_quant_i8).
const SDPA_DECODE_M1_PARTIAL_W8A8: &str = r####"kernel void sdpa_decode_m1_partial_w8a8(
    device const float* arena_q [[buffer(0)]],
    device const char*  i8k     [[buffer(1)]],
    device const char*  i8v     [[buffer(2)]],
    device const float* arena_m [[buffer(3)]],
    device const float* ksc     [[buffer(4)]],
    device const float* vsc     [[buffer(5)]],
    constant uint& batch        [[buffer(6)]],
    constant uint& heads        [[buffer(7)]],
    constant uint& head_dim     [[buffer(8)]],
    constant uint& q_stride     [[buffer(9)]],
    constant uint& mask_kind    [[buffer(10)]],
    constant uint& seq_k        [[buffer(11)]],
    constant uint& k_stride     [[buffer(12)]],
    constant uint& bhsd         [[buffer(13)]],
    constant uint& window       [[buffer(14)]],
    constant float& score_scale  [[buffer(15)]],
    constant float& attn_softcap [[buffer(16)]],
    constant SdpaOffsets& byte_offs [[buffer(17)]],
    device float* scratch       [[buffer(18)]],
    constant uint& packed       [[buffer(19)]],
    uint tid  [[thread_position_in_threadgroup]],
    uint tgid [[threadgroup_position_in_grid]],
    uint tsize [[threads_per_threadgroup]]
) {
    // n_part + mode flags packed into one slot: Metal's arg-table aliases
    // individual set_bytes past index 16, so separate flag slots (20/21/22)
    // clobber each other. Unpack: n_part = low 16b; q_i8/v_i8/blk = bits 16/17/18.
    uint n_part = packed & 0xFFFFu;
    uint q_i8   = (packed >> 16) & 1u;
    uint v_i8   = (packed >> 17) & 1u;
    uint blk    = (packed >> 18) & 1u;
    uint k_i8   = (packed >> 19) & 1u;
    device const float* Q = (device const float*)((device const char*)arena_q + byte_offs.q);
    device const float* M = (device const float*)((device const char*)arena_m + byte_offs.m);
    // {k,v}_i8==0: read K/V straight from the f32 arena (exact) instead of the int8
    // scratch — isolates each operand's quant error. arena_q is the arena base.
    device const float* Vf = (device const float*)((device const char*)arena_q + byte_offs.v);
    device const float* Kf = (device const float*)((device const char*)arena_q + byte_offs.k);
    constexpr uint MAX_DH = 128u;
    constexpr uint SLOT = 2u + MAX_DH;
    uint part = tgid % n_part;
    uint t    = tgid / n_part;
    uint hi   = t % heads;
    uint bi   = t / heads;
    if (bi >= batch) return;
    uint vdh = (byte_offs.v_head_dim == 0u) ? head_dim : byte_offs.v_head_dim;
    uint slot = (bi * heads + hi) * n_part + part;
    uint chunk = (seq_k + n_part - 1u) / n_part;
    uint start = part * chunk;
    uint end   = min(start + chunk, seq_k);
    if (start >= seq_k) {
        if (tid == 0u) {
            scratch[slot * SLOT + 0u] = -1e30f;
            scratch[slot * SLOT + 1u] = 0.0f;
            for (uint d = 0; d < vdh; ++d) scratch[slot * SLOT + 2u + d] = 0.0f;
        }
        return;
    }
    float scale = (score_scale > 0.0f) ? score_scale : rsqrt(float(head_dim));
    float softcap_inv = (attn_softcap > 0.0f) ? (1.0f / attn_softcap) : 0.0f;
    uint q_offset = seq_k - 1u;
    uint q_base = qkv_q_offset(bi, hi, 0u, heads, 1u, head_dim, q_stride, bhsd);

    // Sub-block layout (blk=1 → 32-elem blocks, one scale each; blk=0 → whole row).
    uint nb = (blk != 0u) ? (head_dim / 32u) : 1u;
    uint bs = (blk != 0u) ? 32u : head_dim;
    uint vnb = (blk != 0u) ? (vdh / 32u) : 1u;

    // Load the query row; quantize to int8 per-block when q_i8 (else f32 Q is kept
    // and dotted against dequantized int8 K — isolates whether int8-Q matters).
    float q_reg[MAX_DH];
    for (uint d = 0; d < head_dim; ++d) q_reg[d] = Q[q_base + d];
    float qs_b[4];
    char qi[MAX_DH];
    if (q_i8 != 0u) {
        for (uint b = 0; b < nb; ++b) {
            float am = 0.0f;
            for (uint j = 0; j < bs; ++j) am = max(am, fabs(q_reg[b * bs + j]));
            qs_b[b] = am * (1.0f / 127.0f);
            float inv = (qs_b[b] > 1e-20f) ? (1.0f / qs_b[b]) : 0.0f;
            for (uint j = 0; j < bs; ++j)
                qi[b * bs + j] = char(clamp(round(q_reg[b * bs + j] * inv), -127.0f, 127.0f));
        }
    }

    // GQA kv head + contiguous int8 row base (matches kv_quant_i8's gid layout).
    uint nkv   = (byte_offs.kv_heads == 0u) ? heads : byte_offs.kv_heads;
    uint group = heads / nkv;
    uint hkv   = (group > 1u) ? (hi / group) : hi;
    uint rb    = (bi * nkv + hkv) * seq_k;

    float m_acc = -1e30f;
    float l_acc = 0.0f;
    float o_acc[MAX_DH];
    for (uint d = 0; d < vdh; ++d) o_acc[d] = 0.0f;

    for (uint ki = start + tid; ki < end; ki += tsize) {
        device const char* kr = i8k + (ulong)(rb + ki) * head_dim;
        ulong kscrow = (ulong)(rb + ki) * nb;
        uint k_base = (k_i8 != 0u)
            ? 0u
            : qkv_kv_offset(bi, hi, ki, heads, byte_offs.kv_heads, seq_k, head_dim, k_stride, bhsd);
        float s = 0.0f;
        // Per-block score. Fast path (int8 Q & int8 K): integer dot × block scales.
        // Otherwise an f32 accumulate so any exact operand (q_i8/k_i8==0) is used
        // directly — lets the all-exact case validate the kernel vs CPU (~1e-5).
        for (uint b = 0; b < nb; ++b) {
            if (q_i8 != 0u && k_i8 != 0u) {
                int idot = 0;
                for (uint j = 0; j < bs; ++j) idot += int(qi[b * bs + j]) * int(kr[b * bs + j]);
                s += float(idot) * qs_b[b] * ksc[kscrow + b];
            } else {
                float bd = 0.0f;
                for (uint j = 0; j < bs; ++j) {
                    uint d = b * bs + j;
                    float qv = (q_i8 != 0u) ? (float(qi[d]) * qs_b[b]) : q_reg[d];
                    float kv = (k_i8 != 0u) ? (float(kr[d]) * ksc[kscrow + b]) : Kf[k_base + d];
                    bd += qv * kv;
                }
                s += bd;
            }
        }
        s *= scale;
        if (softcap_inv > 0.0f) s = precise::tanh(s * softcap_inv) * attn_softcap;
        if (mask_kind == 1u) {
            if (ki > q_offset) s = -1e9f;
        } else if (mask_kind == 2u) {
            if (M[bi * k_stride + ki] < 0.5f) s = -1e9f;
        } else if (mask_kind == 4u) {
            uint lo = q_offset > window ? q_offset - window : 0u;
            if (ki < lo || ki > q_offset) s = -1e9f;
        }
        if (s <= -1.0e9f) continue;
        float m_new = max(m_acc, s);
        float e_old = exp(m_acc - m_new);
        float e_cur = exp(s - m_new);
        l_acc = e_old * l_acc + e_cur;
        // V: int8 (per-block or per-row scale) from scratch, or exact f32 from arena.
        device const char* vr = i8v + (ulong)(rb + ki) * vdh;
        ulong vscrow = (ulong)(rb + ki) * vnb;
        uint v_base = (v_i8 != 0u)
            ? 0u
            : qkv_v_offset(bi, hi, ki, heads, byte_offs.kv_heads, seq_k, vdh, k_stride, bhsd);
        for (uint d = 0; d < vdh; ++d) {
            float vval;
            if (v_i8 != 0u) {
                uint b = (blk != 0u) ? (d >> 5) : 0u;
                vval = float(vr[d]) * vsc[vscrow + b];
            } else {
                vval = Vf[v_base + d];
            }
            o_acc[d] = e_old * o_acc[d] + e_cur * vval;
        }
        m_acc = m_new;
    }

    float m_g  = simd_max(m_acc);
    float resc = exp(m_acc - m_g);
    float l_g  = simd_sum(l_acc * resc);
    if (tid == 0u) {
        scratch[slot * SLOT + 0u] = m_g;
        scratch[slot * SLOT + 1u] = l_g;
    }
    for (uint d = 0; d < vdh; ++d) {
        float og = simd_sum(o_acc[d] * resc);
        if (tid == 0u) scratch[slot * SLOT + 2u + d] = og;
    }
}"####;

// Flash-attention-2 PREFILL kernel (seq_q>1) supporting head_dim ≤ 128 + GQA.
// Fixes the O(seq²) DRAM bottleneck of the scalar `sdpa_long` at qwen3's
// head_dim=128 (which no existing tiled FA kernel handles: sdpa_fa_f32 caps at
// MAX_DH=32, sdpa_fa2 at 64). Each threadgroup owns Br=8 query rows of one
// (batch, head) and streams the K/V sequence in Bc=16 tiles staged to
// threadgroup memory — so each K/V element is read once per 8-query tile, not
// once per query (≈Br× less DRAM traffic). Online softmax, F32 accumulate.
// Uses the sdpa_long/FA arg layout (buffers 0-17, SdpaOffsets @17 → byte
// offsets + kv_heads for GQA + v_head_dim). tg mem @ Br=8,Bc=16,MAX_DH=128:
// Q 4KB + K 8KB + V 8KB + o 4KB + S 0.5KB ≈ 24.5KB < 32KB. Dispatch 128 threads,
// grid (ceil(seq_q/8), heads, batch).
// Shared skeleton for the flash-attention prefill kernels. The staging, online
// softmax, causal early-exit and emit are identical across variants; only the
// score (Q@Kᵀ) and P@V matmuls differ (scalar FMA vs simdgroup-matrix MMA), and
// the tile sizes (Br/Bc/MAX_DH) are parameters — so both kernels are generated
// from this one template via `sdpa_prefill_fa_variant!` instead of being
// hand-duplicated. Placeholders: __NAME__, __SG_DECL__ (extra sig params),
// __EXTRA_TG__ (extra threadgroup buffers), __BR__/__BC__/__MDH__ (tile dims),
// __SCORE_IMPL__ (fills masked/scaled S_tg), __PV_IMPL__ (updates o_row from P).
const SDPA_PREFILL_FA_SKELETON: &str = r####"kernel void __NAME__(
    device const float* arena_q   [[buffer(0)]],
    device const float* arena_k   [[buffer(1)]],
    device const float* arena_v   [[buffer(2)]],
    device const float* arena_m   [[buffer(3)]],
    device float*       arena_o   [[buffer(4)]],
    constant uint& batch       [[buffer(5)]],
    constant uint& seq_q       [[buffer(6)]],
    constant uint& heads       [[buffer(7)]],
    constant uint& head_dim    [[buffer(8)]],
    constant uint& q_stride    [[buffer(9)]],
    constant uint& mask_kind   [[buffer(10)]],
    constant uint& seq_k       [[buffer(11)]],
    constant uint& k_stride    [[buffer(12)]],
    constant uint& bhsd        [[buffer(13)]],
    constant uint& window      [[buffer(14)]],
    constant float& score_scale  [[buffer(15)]],
    constant float& attn_softcap [[buffer(16)]],
    constant SdpaOffsets& byte_offs [[buffer(17)]],
    uint3 tgid  [[threadgroup_position_in_grid]],
    uint3 tid3  [[thread_position_in_threadgroup]],
    uint3 tsz   [[threads_per_threadgroup]]__SG_DECL__
) {
    uint tid = tid3.x;
    uint tsize = tsz.x;
    device const float* Q = (device const float*)((device const char*)arena_q + byte_offs.q);
    device const float* K = (device const float*)((device const char*)arena_k + byte_offs.k);
    device const float* V = (device const float*)((device const char*)arena_v + byte_offs.v);
    device const float* M = (device const float*)((device const char*)arena_m + byte_offs.m);
    device float* OUT     = (device float*)((device char*)arena_o + byte_offs.o);

    constexpr uint Br = __BR__u;
    constexpr uint Bc = __BC__u;
    constexpr uint MAX_DH = __MDH__u;
    threadgroup float Q_tg[Br * MAX_DH];
    threadgroup float K_tg[Bc * MAX_DH];
    threadgroup float V_tg[Bc * MAX_DH];
    threadgroup float S_tg[Br * Bc];__EXTRA_TG__
    threadgroup float m_row[Br];
    threadgroup float l_row[Br];
    threadgroup float o_row[Br * MAX_DH];
    threadgroup float m_new_tg[Br];
    threadgroup float e_old_tg[Br];

    uint q_tile = tgid.x;
    uint hi     = tgid.y;
    uint bi     = tgid.z;
    if (bi >= batch) return;
    uint q_start = q_tile * Br;

    uint vdh = (byte_offs.v_head_dim == 0u) ? head_dim : byte_offs.v_head_dim;
    float scale = (score_scale > 0.0f) ? score_scale : rsqrt(float(head_dim));
    float softcap_inv = (attn_softcap > 0.0f) ? (1.0f / attn_softcap) : 0.0f;
    uint q_off = seq_k - seq_q;

    for (uint i = tid; i < Br * head_dim; i += tsize) {
        uint qi = i / head_dim, di = i % head_dim;
        uint pos = q_start + qi;
        Q_tg[qi * MAX_DH + di] = (pos < seq_q)
            ? Q[qkv_q_offset(bi, hi, pos, heads, seq_q, head_dim, q_stride, bhsd) + di]
            : 0.0f;
    }
    if (tid < Br) { m_row[tid] = -1e30f; l_row[tid] = 0.0f; }
    for (uint i = tid; i < Br * vdh; i += tsize) o_row[(i / vdh) * MAX_DH + (i % vdh)] = 0.0f;
    threadgroup_barrier(mem_flags::mem_threadgroup);

    uint kt_end = seq_k;
    if (mask_kind == 1u) kt_end = min(seq_k, q_off + q_start + Br);

    for (uint kt = 0; kt < kt_end; kt += Bc) {
        for (uint i = tid; i < Bc * head_dim; i += tsize) {
            uint ki = i / head_dim, di = i % head_dim;
            uint pos = kt + ki;
            bool ok = pos < seq_k;
            uint koff = ok ? qkv_kv_offset(bi, hi, pos, heads, byte_offs.kv_heads, seq_k, head_dim, k_stride, bhsd) : 0u;
            K_tg[ki * MAX_DH + di] = ok ? K[koff + di] : 0.0f;
        }
        for (uint i = tid; i < Bc * vdh; i += tsize) {
            uint ki = i / vdh, di = i % vdh;
            uint pos = kt + ki;
            bool ok = pos < seq_k;
            uint voff = ok ? qkv_v_offset(bi, hi, pos, heads, byte_offs.kv_heads, seq_k, vdh, k_stride, bhsd) : 0u;
            V_tg[ki * MAX_DH + di] = ok ? V[voff + di] : 0.0f;
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);

__SCORE_IMPL__

        // Online softmax: (A) running max, (B) P=exp(S-m), (C) row-sum l.
        if (tid < Br) {
            uint qi = tid;
            float m_prev = m_row[qi];
            float m_new = m_prev;
            for (uint ki = 0; ki < Bc; ++ki) m_new = max(m_new, S_tg[qi * Bc + ki]);
            m_new_tg[qi] = m_new;
            e_old_tg[qi] = exp(m_prev - m_new);
            m_row[qi] = m_new;
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);
        for (uint c = tid; c < Br * Bc; c += tsize) {
            uint qi = c / Bc;
            S_tg[c] = exp(S_tg[c] - m_new_tg[qi]);
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);
        if (tid < Br) {
            uint qi = tid;
            float lsum = 0.0f;
            for (uint ki = 0; ki < Bc; ++ki) lsum += S_tg[qi * Bc + ki];
            l_row[qi] = e_old_tg[qi] * l_row[qi] + lsum;
        }

__PV_IMPL__
    }

    for (uint i = tid; i < Br * vdh; i += tsize) {
        uint qi = i / vdh, di = i % vdh;
        uint pos = q_start + qi;
        if (pos < seq_q) {
            float l = l_row[qi];
            float o = (l > 0.0f) ? (o_row[qi * MAX_DH + di] / l) : 0.0f;
            OUT[qkv_out_offset(bi, hi, pos, heads, seq_q, vdh, q_stride, bhsd) + di] = o;
        }
    }
}"####;

// Scalar score: fused Q·K dot + scale + mask → S_tg.
const FA_SCORE_SCALAR: &str = r####"        for (uint c = tid; c < Br * Bc; c += tsize) {
            uint qi = c / Bc, ki = c % Bc;
            uint qpos = q_start + qi, kpos = kt + ki;
            float s;
            if (qpos < seq_q && kpos < seq_k) {
                float dot = 0.0f;
                for (uint di = 0; di < head_dim; ++di) dot += Q_tg[qi * MAX_DH + di] * K_tg[ki * MAX_DH + di];
                s = dot * scale;
                if (softcap_inv > 0.0f) s = precise::tanh(s * softcap_inv) * attn_softcap;
                if (mask_kind == 1u) { if (kpos > q_off + qpos) s = -1e9f; }
                else if (mask_kind == 2u) { if (M[bi * k_stride + kpos] < 0.5f) s = -1e9f; }
                else if (mask_kind == 4u) { uint hp = q_off + qpos; uint lo = hp > window ? hp - window : 0u; if (kpos < lo || kpos > hp) s = -1e9f; }
            } else { s = -1e9f; }
            S_tg[c] = s;
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);"####;

// MMA score: simdgroups 0..Bc/8 compute raw Q@Kᵀ via tensor units → S_tg, then
// scalar scale+mask. Bc/8 n-tiles; one simdgroup each.
const FA_SCORE_MMA: &str = r####"        if (sgid < (Bc / 8u)) {
            simdgroup_float8x8 sacc = make_filled_simdgroup_matrix<float, 8, 8>(0.0f);
            for (uint kd = 0; kd < head_dim; kd += 8u) {
                simdgroup_float8x8 qa, kb;
                simdgroup_load(qa, Q_tg + kd, MAX_DH);
                simdgroup_load(kb, K_tg + sgid * 8u * MAX_DH + kd, MAX_DH, ulong2(0, 0), true);
                simdgroup_multiply_accumulate(sacc, qa, kb, sacc);
            }
            simdgroup_store(sacc, S_tg + sgid * 8u, Bc);
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);
        for (uint c = tid; c < Br * Bc; c += tsize) {
            uint qi = c / Bc, ki = c % Bc;
            uint qpos = q_start + qi, kpos = kt + ki;
            float s = S_tg[c] * scale;
            if (softcap_inv > 0.0f) s = precise::tanh(s * softcap_inv) * attn_softcap;
            if (!(qpos < seq_q && kpos < seq_k)) { s = -1e9f; }
            else if (mask_kind == 1u) { if (kpos > q_off + qpos) s = -1e9f; }
            else if (mask_kind == 2u) { if (M[bi * k_stride + kpos] < 0.5f) s = -1e9f; }
            else if (mask_kind == 4u) { uint hp = q_off + qpos; uint lo = hp > window ? hp - window : 0u; if (kpos < lo || kpos > hp) s = -1e9f; }
            S_tg[c] = s;
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);"####;

// Scalar P@V: each thread accumulates its o_row dims (rescaled by e_old) over Bc.
const FA_PV_SCALAR: &str = r####"        for (uint i = tid; i < Br * vdh; i += tsize) {
            uint qi = i / vdh, di = i % vdh;
            float o = o_row[qi * MAX_DH + di] * e_old_tg[qi];
            for (uint ki = 0; ki < Bc; ++ki) o += S_tg[qi * Bc + ki] * V_tg[ki * MAX_DH + di];
            o_row[qi * MAX_DH + di] = o;
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);"####;

// MMA P@V: 16 n-tiles (MAX_DH/8) striped across the 4 simdgroups → PV_tg, then
// scalar per-row rescale o = o·e_old + PV.
const FA_PV_MMA: &str = r####"        threadgroup_barrier(mem_flags::mem_threadgroup);
        for (uint mm = 0; mm < (MAX_DH / 32u); ++mm) {
            uint n = (sgid + mm * 4u) * 8u;
            if (n < vdh) {
                simdgroup_float8x8 pacc = make_filled_simdgroup_matrix<float, 8, 8>(0.0f);
                for (uint kk = 0; kk < Bc; kk += 8u) {
                    simdgroup_float8x8 pa, vb;
                    simdgroup_load(pa, S_tg + kk, Bc);
                    simdgroup_load(vb, V_tg + kk * MAX_DH + n, MAX_DH);
                    simdgroup_multiply_accumulate(pacc, pa, vb, pacc);
                }
                simdgroup_store(pacc, PV_tg + n, MAX_DH);
            }
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);
        for (uint i = tid; i < Br * vdh; i += tsize) {
            uint qi = i / vdh, di = i % vdh;
            o_row[qi * MAX_DH + di] = o_row[qi * MAX_DH + di] * e_old_tg[qi] + PV_tg[qi * MAX_DH + di];
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);"####;

/// Generate one prefill flash-attention kernel from the shared skeleton with
/// the given name, extra signature/threadgroup decls, tile sizes, and score/PV
/// matmul implementations.
macro_rules! sdpa_prefill_fa_variant {
    ($name:expr, $sg_decl:expr, $extra_tg:expr, $br:expr, $bc:expr, $mdh:expr, $score:expr, $pv:expr) => {
        SDPA_PREFILL_FA_SKELETON
            .replace("__SCORE_IMPL__", $score)
            .replace("__PV_IMPL__", $pv)
            .replace("__NAME__", $name)
            .replace("__SG_DECL__", $sg_decl)
            .replace("__EXTRA_TG__", $extra_tg)
            .replace("__BR__", $br)
            .replace("__BC__", $bc)
            .replace("__MDH__", $mdh)
    };
}

/// Mid-axis concat segment, parameterized by input (`__IN__`) and output
/// (`__OUT__`) element type. The write casts `(__OUT__)(src)`, so a mismatched
/// pair converts precision (f32↔f16) instead of reinterpreting raw bytes.
const CONCAT_MIDAXIS_SEG_TMPL: &str = r####"kernel void __NAME__(
    device char* arena       [[buffer(0)]],
    constant ulong& dst_byte [[buffer(1)]],
    constant ulong& src_byte [[buffer(2)]],
    constant uint& outer     [[buffer(3)]],
    constant uint& dst_axis  [[buffer(4)]],
    constant uint& src_axis  [[buffer(5)]],
    constant uint& inner     [[buffer(6)]],
    constant uint& dst_col   [[buffer(7)]],
    uint gid [[thread_position_in_grid]]
) {
    uint total = outer * src_axis * inner;
    if (gid >= total) return;
    uint ii = gid % inner;
    uint tmp = gid / inner;
    uint a = tmp % src_axis;
    uint o = tmp / src_axis;
    device const __IN__* src = (device const __IN__*)(arena + src_byte);
    device __OUT__* dst      = (device __OUT__*)(arena + dst_byte);
    dst[(o * dst_axis + dst_col + a) * inner + ii] = (__OUT__)(src[(o * src_axis + a) * inner + ii]);
}
"####;

/// Generate one concat-midaxis kernel `$name` reading `$in`, writing `$out`.
macro_rules! concat_midaxis_variant {
    ($name:expr, $in:expr, $out:expr) => {
        CONCAT_MIDAXIS_SEG_TMPL
            .replace("__NAME__", $name)
            .replace("__IN__", $in)
            .replace("__OUT__", $out)
    };
}

pub(crate) fn msl_source() -> String {
    // Substitute the generated dispatch/kernels at their markers. The backward
    // derivative dispatch must be defined before the `activation_backward` kernel
    // that calls it; the in-place activation kernels are standalone entry points.
    let core = RLX_KERNELS_MSL
        .replace("// @@RLX_SCALAR_ACT_FNS@@", &scalar_act_fns_msl())
        .replace("// @@RLX_POW_SCALAR_FN@@", &pow_scalar_fn_msl())
        .replace(
            "// @@RLX_ACTIVATION_BACKWARD@@",
            &rlxsl::msl_activation_backward_module(),
        )
        .replace("// @@RLX_ACT_INPLACE_H@@", &core_act_inplace_h_msl())
        .replace("// @@RLX_GELU_INPLACE_F32@@", &gelu_inplace_f32_msl())
        .replace("// @@RLX_BINARY_FN@@", &rlxsl::binary::msl_binary_module())
        .replace(
            "// @@RLX_COMPARE_FN@@",
            &rlxsl::compare::msl_compare_module(),
        )
        .replace(
            "// @@RLX_SDPA_DECODE_M1@@",
            &format!(
                "{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}",
                SDPA_PREFILL_HD256,
                sdpa_decode_m1_variant!("sdpa_decode_m1", "float"),
                sdpa_decode_m1_variant!("sdpa_decode_m1_f16kv", "half"),
                sdpa_decode_m1_partial_variant!("sdpa_decode_m1_partial", "float"),
                sdpa_decode_m1_partial_variant!("sdpa_decode_m1_partial_f16kv", "half"),
                sdpa_decode_m1_partial_hd_variant!(
                    "sdpa_decode_m1_partial_hd",
                    "float",
                    "4",
                    "128"
                ),
                sdpa_decode_m1_partial_hd_variant!(
                    "sdpa_decode_m1_partial_hd_f16kv",
                    "half",
                    "4",
                    "128"
                ),
                sdpa_decode_m1_partial_hd_variant!(
                    "sdpa_decode_m1_partial_hd_256",
                    "float",
                    "8",
                    "256"
                ),
                sdpa_decode_m1_partial_hd_variant!(
                    "sdpa_decode_m1_partial_hd_256_f16kv",
                    "half",
                    "8",
                    "256"
                ),
                sdpa_decode_m1_combine_variant!("sdpa_decode_m1_combine", "128"),
                sdpa_decode_m1_combine_variant!("sdpa_decode_m1_combine_256", "256"),
                sdpa_prefill_fa_variant!(
                    "sdpa_prefill_fa",
                    "",
                    "",
                    "8",
                    "16",
                    "128",
                    FA_SCORE_SCALAR,
                    FA_PV_SCALAR
                ),
                sdpa_prefill_fa_variant!(
                    "sdpa_prefill_fa_mma",
                    ",\n    uint sgid   [[simdgroup_index_in_threadgroup]]",
                    "\n    threadgroup float PV_tg[Br * MAX_DH];",
                    "8",
                    "16",
                    "128",
                    FA_SCORE_MMA,
                    FA_PV_MMA
                ),
                kv_quant_i8_variant!("kv_quant_i8", "float"),
                kv_quant_i8_variant!("kv_quant_i8_f16", "half"),
                SDPA_DECODE_M1_PARTIAL_W8A8,
            ),
        )
        .replace(
            "// @@RLX_CONCAT_MIDAXIS@@",
            &format!(
                "{}\n{}\n{}\n{}",
                concat_midaxis_variant!("concat_midaxis_seg", "float", "float"),
                concat_midaxis_variant!("concat_midaxis_seg_h", "half", "half"),
                concat_midaxis_variant!("concat_midaxis_seg_f32_to_f16", "float", "half"),
                concat_midaxis_variant!("concat_midaxis_seg_f16_to_f32", "half", "float"),
            ),
        )
        .replace(
            "// @@RLX_SGEMM_TILES@@",
            // 64×64 tile config (TROWS=64 → 8 simdgroups, NACC=8 → TCOLS=64).
            // Add a config (e.g. 64×128: trows=64, nacc=16) in one line + a pipeline.
            &format!(
                "{}\n{}",
                sgemm_tile_variant!("sgemm_simd64", 64, 8),
                sgemm_splitk_variant!("sgemm_simd64_splitk", 64, 8),
            ),
        );
    format!(
        "{core}\n{RLX_KERNELS_MSL_DEQUANT}\n{RLX_KERNELS_MSL_FFT_GPU}\n{RLX_KERNELS_MSL_SPLAT}\n{RLX_KERNELS_MSL_SPLAT_CONIC}\n{}\n{}",
        scalar_act_msl(),
        synth_matmul_msl()
    )
}

pub struct Kernels {
    pub library: Library,
    pub sgemm: ComputePipelineState,
    pub sgemm_f16a: ComputePipelineState,
    pub sgemm_f16w: ComputePipelineState,
    pub sgemm_f16w_small_m: ComputePipelineState,
    pub sgemm_simd: ComputePipelineState,
    pub sgemm_simd_bias: ComputePipelineState,
    pub sgemm_simd_4x4: ComputePipelineState,
    pub sgemm_simd64: ComputePipelineState,
    pub sgemm_simd64_splitk: ComputePipelineState,
    pub zero_f32: ComputePipelineState,
    pub sgemm_simd_4x4_bias: ComputePipelineState,
    pub hgemm_simd_4x4: ComputePipelineState,
    pub hgemm_simd_4x4_bias: ComputePipelineState,
    pub bias_add_h: ComputePipelineState,
    pub gelu_inplace_h: ComputePipelineState,
    pub gelu_approx_inplace_h: ComputePipelineState,
    pub silu_inplace_h: ComputePipelineState,
    pub relu_inplace_h: ComputePipelineState,
    pub sigmoid_inplace_h: ComputePipelineState,
    pub tanh_inplace_h: ComputePipelineState,
    pub exp_inplace_h: ComputePipelineState,
    pub log_inplace_h: ComputePipelineState,
    pub sqrt_inplace_h: ComputePipelineState,
    pub rsqrt_inplace_h: ComputePipelineState,
    pub rec_inplace_h: ComputePipelineState,
    pub neg_inplace_h: ComputePipelineState,
    pub abs_inplace_h: ComputePipelineState,
    pub sin_inplace_h: ComputePipelineState,
    pub cos_inplace_h: ComputePipelineState,
    pub tan_inplace_h: ComputePipelineState,
    pub atan_inplace_h: ComputePipelineState,
    pub round_inplace_h: ComputePipelineState,
    pub layer_norm_h: ComputePipelineState,
    pub fused_residual_ln_h: ComputePipelineState,
    pub fused_residual_rms_norm_h: ComputePipelineState,
    pub rms_norm_h: ComputePipelineState,
    pub softmax_lastax_h: ComputePipelineState,
    pub reduce_axes_h: ComputePipelineState,
    pub elem_add_h: ComputePipelineState,
    pub elem_mul_h: ComputePipelineState,
    pub elem_sub_h: ComputePipelineState,
    pub elem_div_h: ComputePipelineState,
    pub elem_max_h: ComputePipelineState,
    pub elem_min_h: ComputePipelineState,
    pub elem_pow_h: ComputePipelineState,
    pub gather_axis0_h: ComputePipelineState,
    pub narrow_lastax_h: ComputePipelineState,
    pub sdpa_h: ComputePipelineState,
    /// Native f32 fused-attention core for `Op::FusedAttentionBlock`.
    pub fused_attn_block: ComputePipelineState,
    pub rope_h: ComputePipelineState,
    pub cast_f32_to_f16: ComputePipelineState,
    pub cast_f16_to_f32: ComputePipelineState,
    pub copy_f32: ComputePipelineState,
    pub copy4: ComputePipelineState,
    pub sgemm_simd_padded: ComputePipelineState,
    pub sgemm_simd_padded_residual: ComputePipelineState,
    pub sgemm_simd_padded_f16w: ComputePipelineState,
    pub sgemm_wide8x64_f16w: ComputePipelineState,
    pub gemv_f16w_splitk: ComputePipelineState,
    pub gemv_f16w_kpart: ComputePipelineState,
    pub gemv_zero_f32: ComputePipelineState,
    pub sgemm_simd_padded_bias: ComputePipelineState,
    pub sgemm_tiled: ComputePipelineState,
    pub bias_add: ComputePipelineState,
    pub gelu_inplace: ComputePipelineState,
    pub gelu_inplace4: ComputePipelineState,
    pub gelu_approx_inplace: ComputePipelineState,
    pub gelu_approx_inplace4: ComputePipelineState,
    pub gelu_approx_out4: ComputePipelineState,
    pub silu_inplace: ComputePipelineState,
    pub silu_inplace4: ComputePipelineState,
    pub silu_out4: ComputePipelineState,
    pub binary_broadcast_rhs_col_f32: ComputePipelineState,
    pub binary_broadcast_rhs_col4: ComputePipelineState,
    pub binary_broadcast_rhs_row_f32: ComputePipelineState,
    pub binary_broadcast_rhs_row4: ComputePipelineState,
    pub binary_broadcast_rhs_scalar_f32: ComputePipelineState,
    pub binary_broadcast_rhs_scalar4: ComputePipelineState,
    pub binary_broadcast_1ax_f32: ComputePipelineState,
    pub binary_broadcast_1ax4: ComputePipelineState,
    pub fused_binary_activation_f32: ComputePipelineState,
    pub fused_binary_activation4: ComputePipelineState,
    pub fused_ternary_activation_f32: ComputePipelineState,
    pub fused_ternary_activation4: ComputePipelineState,
    pub layer_norm: ComputePipelineState,
    pub rms_norm: ComputePipelineState,
    pub rms_norm_mul_silu: ComputePipelineState,
    pub elem_add: ComputePipelineState,
    pub elem_add4: ComputePipelineState,
    pub elem_sub4: ComputePipelineState,
    pub binary_broadcast_f32: ComputePipelineState,
    pub binary_broadcast_rank2_f32: ComputePipelineState,
    pub binary_broadcast_rank24: ComputePipelineState,
    pub elem_mul: ComputePipelineState,
    pub elem_mul4: ComputePipelineState,
    pub elem_div4: ComputePipelineState,
    pub gather_axis0: ComputePipelineState,
    pub narrow_lastax: ComputePipelineState,
    pub narrow_lastax4: ComputePipelineState,
    pub split_lastax: ComputePipelineState,
    pub split_lastax4: ComputePipelineState,
    pub fused_residual_ln: ComputePipelineState,
    pub fused_residual_rms_norm: ComputePipelineState,
    pub ada_layer_norm: ComputePipelineState,
    pub ada_layer_norm_h: ComputePipelineState,
    pub gated_residual: ComputePipelineState,
    pub gated_residual_h: ComputePipelineState,
    pub ada_layer_norm_backward: ComputePipelineState,
    pub ada_layer_norm_backward_h: ComputePipelineState,
    pub gated_residual_backward: ComputePipelineState,
    pub gated_residual_backward_h: ComputePipelineState,
    pub sdpa: ComputePipelineState,
    pub sdpa_simd: ComputePipelineState,
    pub sdpa_simd_h16: ComputePipelineState,
    pub sdpa_long: ComputePipelineState,
    pub sdpa_long_occpad: ComputePipelineState,
    pub sdpa_decode_m1: ComputePipelineState,
    pub sdpa_decode_m1_f16kv: ComputePipelineState,
    pub sdpa_decode_m1_partial: ComputePipelineState,
    pub sdpa_decode_m1_partial_f16kv: ComputePipelineState,
    /// Head-dim-split flash-decode partials (`head_dim % 32 == 0`): split D
    /// across the 32 lanes → 8 regs/thread vs 256, no cross-lane o reduction,
    /// coalesced K/V. Off-switch RLX_METAL_SDPA_HDSPLIT=0.
    pub sdpa_decode_m1_partial_hd: ComputePipelineState,
    pub sdpa_decode_m1_partial_hd_f16kv: ComputePipelineState,
    pub sdpa_decode_m1_partial_hd_256: ComputePipelineState,
    pub sdpa_decode_m1_partial_hd_256_f16kv: ComputePipelineState,
    pub sdpa_decode_m1_combine: ComputePipelineState,
    pub sdpa_decode_m1_combine_256: ComputePipelineState,
    pub sdpa_prefill_hd256: ComputePipelineState,
    pub sdpa_decode_m1_partial_w8a8: ComputePipelineState,
    pub kv_quant_i8: ComputePipelineState,
    pub kv_quant_i8_f16: ComputePipelineState,
    pub sdpa_prefill_fa: ComputePipelineState,
    pub sdpa_prefill_fa_mma: ComputePipelineState,
    pub sdpa_fa_f32: ComputePipelineState,
    pub sdpa_splitk: ComputePipelineState,
    pub sdpa_fa2: ComputePipelineState,
    pub sdpa_mma: ComputePipelineState,
    pub argreduce: ComputePipelineState,
    /// Cooperative last-axis ArgMax/ArgMin (one threadgroup per row) — used
    /// for `inner == 1` (decode logits) instead of the serial `argreduce`.
    pub argreduce_lastaxis: ComputePipelineState,
    /// Fused nearest-codebook assignment (`Op::Custom("rlx.vq_assign")`).
    pub vq_assign: ComputePipelineState,
    /// On-GPU temperature/top-k/top-p/Philox logit sampler (one threadgroup
    /// per batch row). Replaces the unified-memory host fallback.
    pub sample_logits: ComputePipelineState,
    pub rng_normal_philox: ComputePipelineState,
    pub rng_uniform_philox: ComputePipelineState,
    pub rng_fill_zero: ComputePipelineState,
    pub dequant_matmul_int8: ComputePipelineState,
    pub dequant_matmul_int4: ComputePipelineState,
    pub dequant_matmul_fp8: ComputePipelineState,
    pub dequant_matmul_nvfp4: ComputePipelineState,
    pub dequant_matmul_mxfp4x2: ComputePipelineState,
    pub dequant_matmul_mlx_gemv: ComputePipelineState,
    pub dequant_matmul_mlx_gemm: ComputePipelineState,
    pub grouped_dequant_matmul_mlx_gemv: ComputePipelineState,
    pub grouped_dequant_matmul_mlx_gemm: ComputePipelineState,
    pub rope: ComputePipelineState,
    pub fused_swiglu: ComputePipelineState,
    pub fused_swiglu_h: ComputePipelineState,
    /// PLAN L2 — interpreted N-ary element-wise region kernel.
    pub elementwise_region: ComputePipelineState,
    /// FKL batch horizontal fusion (one launch; no prologue).
    pub batch_elementwise_region: ComputePipelineState,
    pub fused_swiglu_cast_f32_to_f16: ComputePipelineState,
    pub fused_swiglu_cast_f16_to_f32: ComputePipelineState,
    pub concat_segment_lastax: ComputePipelineState,
    pub concat_segment_lastax4: ComputePipelineState,
    pub concat_lastax_multi: ComputePipelineState,
    pub concat_lastax_multi4: ComputePipelineState,
    pub concat_segment_lastax_h: ComputePipelineState,
    pub concat_midaxis_seg: ComputePipelineState,
    pub concat_midaxis_seg_h: ComputePipelineState,
    pub concat_midaxis_seg_f32_to_f16: ComputePipelineState,
    pub concat_midaxis_seg_f16_to_f32: ComputePipelineState,
    pub elem_sub: ComputePipelineState,
    pub elem_div: ComputePipelineState,
    pub elem_max: ComputePipelineState,
    pub elem_min: ComputePipelineState,
    pub elem_pow: ComputePipelineState,
    pub elem_binop: ComputePipelineState,
    pub elem_binop_h: ComputePipelineState,
    pub elem_compare: ComputePipelineState,
    pub elem_compare_bcast: ComputePipelineState,
    pub elem_where: ComputePipelineState,
    pub elem_where_bcast: ComputePipelineState,
    pub elem_fma: ComputePipelineState,
    pub relu_backward: ComputePipelineState,
    pub activation_backward: ComputePipelineState,
    pub complex_norm_sq: ComputePipelineState,
    pub complex_norm_sq_backward: ComputePipelineState,
    pub conjugate_c64: ComputePipelineState,
    pub fft_butterfly_stage: ComputePipelineState,
    pub fake_quantize_fixed: ComputePipelineState,
    pub fake_quantize_perbatch: ComputePipelineState,
    pub reduce_axes: ComputePipelineState,
    pub reduce_axes_sum_simd: ComputePipelineState,
    /// Double-single (2× f32 ≈ f64) full-sum reduction — high-precision
    /// `Op::Reduce{Sum}` on Metal (opt-in via `RLX_METAL_DW_SUM`).
    pub dw_sum_arena: ComputePipelineState,
    pub topk_lastax: ComputePipelineState,
    pub grouped_matmul: ComputePipelineState,
    pub grouped_gemv_splitk: ComputePipelineState,
    pub gemv_f32_splitk: ComputePipelineState,
    pub scatter_add_zero: ComputePipelineState,
    pub scatter_add_accumulate: ComputePipelineState,
    pub transpose_nd: ComputePipelineState,
    pub transpose_nd_h: ComputePipelineState,
    pub transpose_2d_f32: ComputePipelineState,
    pub transpose_2d_tiled_f32: ComputePipelineState,
    pub transpose_last2_batched_f32: ComputePipelineState,
    pub transpose_last2_batched_tiled_f32: ComputePipelineState,
    pub transpose_swap12_batched_trail_f32: ComputePipelineState,
    pub transpose_swap12_batched_trail_tiled_f32: ComputePipelineState,
    pub gather_axis: ComputePipelineState,
    pub pool2d: ComputePipelineState,
    pub maxpool2d_backward: ComputePipelineState,
    pub conv2d_backward_input: ComputePipelineState,
    pub conv2d_backward_weight: ComputePipelineState,
    pub conv2d_backward_weight_partial: ComputePipelineState,
    pub conv2d_backward_weight_reduce: ComputePipelineState,
    pub maxpool3d_backward: ComputePipelineState,
    pub conv3d_backward_input: ComputePipelineState,
    pub conv3d_backward_weight: ComputePipelineState,
    pub conv2d: ComputePipelineState,
    pub depthwise_conv1d_bsc: ComputePipelineState,
    pub conv2d_w1: ComputePipelineState,
    pub layer_norm2d: ComputePipelineState,
    pub group_norm: ComputePipelineState,
    pub resize_nearest_2x: ComputePipelineState,
    pub conv_transpose2d: ComputePipelineState,
    pub conv3d: ComputePipelineState,
    pub conv_transpose3d: ComputePipelineState,
    pub relu_inplace: ComputePipelineState,
    pub sigmoid_inplace: ComputePipelineState,
    pub tanh_inplace: ComputePipelineState,
    pub exp_inplace: ComputePipelineState,
    pub log_inplace: ComputePipelineState,
    pub sqrt_inplace: ComputePipelineState,
    pub rsqrt_inplace: ComputePipelineState,
    pub rec_inplace: ComputePipelineState,
    pub neg_inplace: ComputePipelineState,
    pub abs_inplace: ComputePipelineState,
    pub round_inplace: ComputePipelineState,
    pub sin_inplace: ComputePipelineState,
    pub cos_inplace: ComputePipelineState,
    pub tan_inplace: ComputePipelineState,
    pub atan_inplace: ComputePipelineState,
    pub softmax_lastax: ComputePipelineState,
    pub softmax_lastax_causal: ComputePipelineState,
    pub softmax_cross_entropy_dense: ComputePipelineState,
    pub softmax_cross_entropy_with_logits: ComputePipelineState,
    pub softmax_cross_entropy_backward: ComputePipelineState,
    pub fft_radix2_full_f32: ComputePipelineState,
    /// native-gpu-fft: single-kernel on-chip FFT for n in (1024, 4096].
    #[cfg(feature = "native-gpu-fft")]
    pub fft_radix2_full_big_f32: ComputePipelineState,
    /// native-gpu-fft: radix-4 in-place single-kernel FFT (pow-4 and 2·pow-4).
    #[cfg(feature = "native-gpu-fft")]
    pub fft_radix4_full_f32: ComputePipelineState,
    /// native-gpu-fft: radix-8 in-place single-kernel FFT for pow-8 sizes.
    #[cfg(feature = "native-gpu-fft")]
    pub fft_radix8_full_f32: ComputePipelineState,
    /// native-gpu-fft: radix-16 in-place single-kernel FFT for pow-16 sizes.
    #[cfg(feature = "native-gpu-fft")]
    pub fft_radix16_full_f32: ComputePipelineState,
    pub fft_bit_reverse_f32: ComputePipelineState,
    pub fft_inner_f32: ComputePipelineState,
    pub fft_outer_r4_f32: ComputePipelineState,
    pub fft_outer_r2_f32: ComputePipelineState,
    pub gated_delta_net: ComputePipelineState,
    pub gated_delta_net_sg: ComputePipelineState,
    pub selective_scan: ComputePipelineState,
    pub lstm: ComputePipelineState,
    pub gru: ComputePipelineState,
    pub rnn: ComputePipelineState,
    pub mamba2: ComputePipelineState,
    pub dequant_gguf: ComputePipelineState,
    /// Fused Q4_K_M GEMV — skips the f32 dequant scratch and produces
    /// `dst[n] = sum_k x[k] * dequant(w[n,k])` in a single pass. Used
    /// for `m == 1` (decode) GgufQ4K matmuls; m > 1 still goes through
    /// `dequant_gguf + encode_mps_sgemm_bt`.
    pub q4k_mv_f32: ComputePipelineState,
    /// Fused single-pass Q3_K decode GEMV (`m == 1`); mirrors `q4k_mv_f32`.
    pub q3k_mv_f32: ComputePipelineState,
    /// Fused single-pass Q6_K decode GEMV (`m == 1`) — the Q6_K LM head.
    pub q6k_mv_f32: ComputePipelineState,
    pub q4_0_mv_f32: ComputePipelineState,
    pub q4_1_mv_f32: ComputePipelineState,
    pub q8_0_mv_f32: ComputePipelineState,
    /// Fused Q1_0 (Bonsai-27B 1-bit) GEMV (decode) + GEMM (prefill): read the
    /// packed weight directly, skipping the dequant-to-f32 scratch + MPS sgemm
    /// path (whose shared scratch raced and zeroed large-n Q1_0 projections).
    pub q1_0_mv_f32: ComputePipelineState,
    /// Simdgroup-cooperative Q1_0 GEMV: 32 threads → 8 outputs via `simd_sum`
    /// (llama.cpp `kernel_mul_mv_q1_0_f32`). Used when `n_dim % 8 == 0`.
    pub q1_0_mv_f32_sg: ComputePipelineState,
    pub q1_0_dual_mv_f32_sg: ComputePipelineState,
    pub q1_0_mm_f32: ComputePipelineState,
    pub q2_0_mv_f32: ComputePipelineState,
    pub q2_0_mv_f32_sg: ComputePipelineState,
    pub q2_0_dual_mv_f32_sg: ComputePipelineState,
    pub q2_0_mm_f32: ComputePipelineState,
    pub iq4_nl_mv_f32: ComputePipelineState,
    pub iq2_xxs_mv_f32: ComputePipelineState,
    pub synth_matmul_codebook: ComputePipelineState,
    pub gemm_rb_bias: ComputePipelineState,
    pub synth_reconstruct_nk: ComputePipelineState,
    pub synth_bwd_dx: ComputePipelineState,
    pub synth_bwd_codebook: ComputePipelineState,
    pub synth_matmul_codebook_mm: ComputePipelineState,
    pub synth_matmul_codebook_h: ComputePipelineState,
    pub synth_matmul_codebook_mm_h: ComputePipelineState,
    pub synth_matmul_codebook_tiled: ComputePipelineState,
    pub synth_matmul_codebook_tiled_h: ComputePipelineState,
    pub synth_reconstruct: ComputePipelineState,
    pub synth_reconstruct_h: ComputePipelineState,
    pub spline_activation: ComputePipelineState,
    pub spline_activation_backward_x: ComputePipelineState,
    pub spline_activation_backward_coeff: ComputePipelineState,
    pub iq2_xs_mv_f32: ComputePipelineState,
    pub iq2_s_mv_f32: ComputePipelineState,
    pub iq3_xxs_mv_f32: ComputePipelineState,
    pub iq3_s_mv_f32: ComputePipelineState,
    /// Simdgroup-cooperative IQ3_XXS / IQ3_S GEMV — distributing the codebook
    /// LUT lookups across 32 lanes (the serial bottleneck in the 1-thread
    /// kernels) makes these ~2× Q3_K, second only to Q4_K.
    pub iq3_xxs_mv_f32_sg: ComputePipelineState,
    pub iq3_s_mv_f32_sg: ComputePipelineState,
    pub iq1_s_mv_f32: ComputePipelineState,
    pub iq1_m_mv_f32: ComputePipelineState,
    /// Simdgroup-cooperative Q4_K_M GEMV: 32 threads cooperate on 8
    /// output columns with `simd_sum`. Better x cache reuse than the
    /// single-thread-per-output `q4k_mv_f32`. Used when `n_dim % 8 == 0`.
    pub q4k_mv_f32_sg: ComputePipelineState,
    pub q5k_mv_f32: ComputePipelineState,
    pub q2k_mv_f32: ComputePipelineState,
    /// Simdgroup-cooperative Q6_K GEMV (32 threads reduce one output row via
    /// `simd_sum`; `Q6K_NSG` rows per threadgroup) and Q8_0 GEMV (32 threads →
    /// `Q8_0_NR0` rows). Replace the occupancy-starved one-thread-per-row
    /// `q6k_mv_f32` / `q8_0_mv_f32` on decode. Off: RLX_METAL_Q6K_SG_DISABLE /
    /// RLX_METAL_Q8_0_SG_DISABLE.
    pub q6k_mv_f32_sg: ComputePipelineState,
    pub q8_0_mv_f32_sg: ComputePipelineState,
    /// Simdgroup-cooperative Q4_0 / Q4_1 (32 threads → 4 rows) and Q3_K (one
    /// row per simdgroup) decode GEMVs — same simd_sum treatment as Q4_K/Q6_K,
    /// replacing the one-thread-per-row kernels. Off: RLX_METAL_Q40_SG_DISABLE /
    /// RLX_METAL_Q41_SG_DISABLE / RLX_METAL_Q3K_SG_DISABLE.
    pub q4_0_mv_f32_sg: ComputePipelineState,
    pub q4_1_mv_f32_sg: ComputePipelineState,
    pub q3k_mv_f32_sg: ComputePipelineState,
    /// Fused Q4_K / Q6_K GEMM (m > 1, prefill) — reads packed weight directly,
    /// dequants in-register and accumulates a row tile, replacing the
    /// `dequant_gguf` f32 scratch + MPS sgemm path for these two schemes.
    pub q4k_mm_f32: ComputePipelineState,
    pub q4k_mm_f32_xs: ComputePipelineState,
    pub q5k_mm_f32_xs: ComputePipelineState,
    pub q6k_mm_f32_xs: ComputePipelineState,
    pub q8_0_mm_f32_xs: ComputePipelineState,
    pub q6k_mm_f32: ComputePipelineState,
    /// Fused decode-layer MLP GEMVs (m == 1). Q4_K / Q5_0 gate+up + silu/gelu;
    /// Q4_K / Q5_0 / Q6_K down + residual. Produced by `fuse_decode_mlp*`
    /// (off-switch `RLX_METAL_FUSE_DECODE=0`).
    pub q4k_swiglu_mv_f32: ComputePipelineState,
    /// Simdgroup-cooperative Q4_K gate+up+SwiGLU (`_sg`): ~40% of peak vs the
    /// naive `q4k_swiglu_mv_f32`'s ~8% (one-thread-per-row).
    pub q4k_swiglu_mv_f32_sg: ComputePipelineState,
    pub q4k_gelu_mv_f32: ComputePipelineState,
    pub q5_0_swiglu_mv_f32: ComputePipelineState,
    pub q5_0_gelu_mv_f32: ComputePipelineState,
    pub q4k_mv_residual_f32: ComputePipelineState,
    /// Simdgroup-cooperative Q4_K down+residual (`_sg`), sibling of the above.
    pub q4k_mv_residual_f32_sg: ComputePipelineState,
    pub q6k_mv_residual_f32: ComputePipelineState,
    pub q5_0_mv_residual_f32: ComputePipelineState,
    /// Fused Q1_0 decode MLP (Bonsai): gate+up+SwiGLU and down+residual.
    pub q1_0_swiglu_mv_f32: ComputePipelineState,
    pub q1_0_swiglu_mv_f32_sg: ComputePipelineState,
    pub q1_0_mv_residual_f32: ComputePipelineState,
    pub q1_0_mv_residual_f32_sg: ComputePipelineState,
    pub q2_0_swiglu_mv_f32: ComputePipelineState,
    pub q2_0_swiglu_mv_f32_sg: ComputePipelineState,
    pub q2_0_mv_residual_f32: ComputePipelineState,
    pub q2_0_mv_residual_f32_sg: ComputePipelineState,
    /// Device buffer holding the concatenated IQ grid LUTs. Built once
    /// at Kernels init from `rlx_gguf::iq_grids::*`. Layout — see
    /// `dequant_gguf.msl` `IQ_GRID_OFF_*` constants.
    iq_grid_lut: Buffer,
    pub rms_norm_bwd: ComputePipelineState,
    pub rms_norm_bwd_param: ComputePipelineState,
    pub rms_norm_bwd_inv_r_f32: ComputePipelineState,
    pub rms_norm_bwd_param_reduce_f32: ComputePipelineState,
    pub layer_norm_bwd: ComputePipelineState,
    pub layer_norm_bwd_gamma: ComputePipelineState,
    pub layer_norm_bwd_stats_f32: ComputePipelineState,
    pub layer_norm_bwd_gamma_reduce_f32: ComputePipelineState,
    pub layer_norm_bwd_gamma_reduce_simd: ComputePipelineState,
    pub group_norm_bwd_input: ComputePipelineState,
    pub group_norm_bwd_gamma: ComputePipelineState,
    pub group_norm_bwd_beta: ComputePipelineState,
    pub rope_bwd: ComputePipelineState,
    pub cumsum_fwd: ComputePipelineState,
    pub cum_scan: ComputePipelineState,
    pub cumsum_bwd: ComputePipelineState,
    pub im2col_group: ComputePipelineState,
    pub im2col_group_w1: ComputePipelineState,
    pub conv2d_bwd_weight_gemm: ComputePipelineState,
    pub conv2d_bwd_weight_gemm_4x4: ComputePipelineState,
    pub attn_bwd_scores_f32: ComputePipelineState,
    pub attn_bwd_dp_f32: ComputePipelineState,
    pub attn_bwd_ds_f32: ComputePipelineState,
    pub attn_bwd_dv_f32: ComputePipelineState,
    pub attn_bwd_dq_f32: ComputePipelineState,
    pub attn_bwd_dk_f32: ComputePipelineState,
    pub attn_bwd_scores_batched_f32: ComputePipelineState,
    pub attn_bwd_dp_batched_f32: ComputePipelineState,
    pub attn_bwd_ds_batched_f32: ComputePipelineState,
    pub attn_bwd_dv_batched_f32: ComputePipelineState,
    pub attn_bwd_dq_batched_f32: ComputePipelineState,
    pub attn_bwd_dk_batched_f32: ComputePipelineState,
    pub attn_bwd_fused_f32: ComputePipelineState,
    pub gather_bwd_zero: ComputePipelineState,
    pub gather_bwd_acc: ComputePipelineState,
    /// Native Gaussian splat tile raster (see `splat.msl`).
    pub gaussian_splat_rasterize: ComputePipelineState,
    /// Training linear radiance raster (no display gamma).
    pub gaussian_splat_rasterize_linear: ComputePipelineState,
    pub gaussian_splat_rasterize_linear_traced: ComputePipelineState,
    pub gaussian_splat_rasterize_backward_linear: ComputePipelineState,
    pub gaussian_splat_adam_step: ComputePipelineState,
    pub gaussian_splat_mse_loss_grad: ComputePipelineState,
    pub gaussian_splat_ssim_stats: ComputePipelineState,
    pub gaussian_splat_blended_loss_grad: ComputePipelineState,
    pub gaussian_splat_project_training: ComputePipelineState,
    pub gaussian_splat_geometry_backward: ComputePipelineState,
    pub gaussian_splat_scene_grad_projection: ComputePipelineState,
    pub gaussian_splat_splat_color_backward: ComputePipelineState,
    pub gaussian_splat_emit_tile_keys: ComputePipelineState,
    pub gaussian_splat_project_screen_ellipse: ComputePipelineState,
    pub gaussian_splat_emit_tile_keys_conic: ComputePipelineState,
    pub gaussian_splat_bin_histogram: ComputePipelineState,
    pub gaussian_splat_bin_copy_counts: ComputePipelineState,
    pub gaussian_splat_bin_prefix_sum: ComputePipelineState,
    pub gaussian_splat_bin_scatter: ComputePipelineState,
    pub gaussian_splat_build_tile_ranges: ComputePipelineState,
    pub gaussian_splat_pack_grads: ComputePipelineState,
    /// Macro-generated scalar activation kernels (Floor/Ceil/Sign/Softplus/Elu):
    /// `Activation → (f32_pipeline, f16_pipeline)`. See `scalar_activation_kernels!`.
    pub scalar_acts: std::collections::HashMap<
        rlx_ir::op::Activation,
        (ComputePipelineState, ComputePipelineState),
    >,
}

unsafe impl Send for Kernels {}
unsafe impl Sync for Kernels {}

/// Double-single (2× f32 ≈ f64) sum reduction over the f32 arena — the
/// high-precision path for `Op::Reduce{Sum}` on Metal (no native f64). Prepended
/// with the `rlxsl::dw` prelude and compiled with PRECISE math (fast-math would
/// break the error-free transforms). See [`Kernels::new`].
const DW_SUM_ARENA_MSL: &str = r#"
// Buffers are bound at the src/dst byte offsets (mirrors encode_reduce_axes),
// so index from 0. Writes the correctly-rounded scalar sum to out[0].
kernel void dw_sum_arena(device const float* x [[buffer(0)]],
                         device float* out      [[buffer(1)]],
                         constant uint& n       [[buffer(2)]],
                         uint tid      [[thread_position_in_threadgroup]],
                         uint nthreads [[threads_per_threadgroup]]) {
    threadgroup DwF32 shared[256];
    DwF32 acc = DwF32{0.0f, 0.0f};
    for (uint i = tid; i < n; i += nthreads) { acc = dw_add(acc, DwF32{x[i], 0.0f}); }
    shared[tid] = acc;
    threadgroup_barrier(mem_flags::mem_threadgroup);
    for (uint s = nthreads >> 1; s > 0u; s >>= 1) {
        if (tid < s) { shared[tid] = dw_add(shared[tid], shared[tid + s]); }
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }
    if (tid == 0u) { out[0] = shared[0].hi + shared[0].lo; }
}
"#;

impl Kernels {
    fn new() -> Self {
        let dev = metal_device().expect("Metal device required");
        if let Some(path) = rlx_ir::env::var("RLX_METAL_DUMP_MSL") {
            let s = msl_source();
            let _ = std::fs::write(&path, &s);
            eprintln!("[msl-dump] wrote {} bytes of MSL source to {path}", s.len());
        }
        let library = crate::pipeline_cache::load_or_compile_library(&dev.device, &msl_source());
        let pipeline = |name: &str| -> ComputePipelineState {
            let f = library.get_function(name, None).expect(name);
            dev.device
                .new_compute_pipeline_state_with_function(&f)
                .unwrap_or_else(|_| panic!("pipeline {name}"))
        };
        // Separate PRECISE-math library for the double-single reduction.
        let dw_sum_arena = {
            let src = format!(
                "#include <metal_stdlib>\nusing namespace metal;\n{}\n{DW_SUM_ARENA_MSL}",
                rlxsl::dw::double_single_prelude(rlxsl::Lang::Msl)
            );
            let opts = crate::mtl::CompileOptions::new();
            opts.set_fast_math_enabled(false);
            let lib = dev
                .device
                .new_library_with_source(&src, &opts)
                .expect("rlx-metal: compile dw_sum_arena library");
            let f = lib
                .get_function("dw_sum_arena", None)
                .expect("dw_sum_arena");
            dev.device
                .new_compute_pipeline_state_with_function(&f)
                .expect("rlx-metal: dw_sum_arena pipeline")
        };
        Self {
            dw_sum_arena,
            sgemm: pipeline("sgemm"),
            sgemm_f16a: pipeline("sgemm_f16a"),
            sgemm_f16w: pipeline("sgemm_f16w"),
            sgemm_f16w_small_m: pipeline("sgemm_f16w_small_m"),
            sgemm_simd: pipeline("sgemm_simd"),
            sgemm_simd_bias: pipeline("sgemm_simd_bias"),
            sgemm_simd_4x4: pipeline("sgemm_simd_4x4"),
            sgemm_simd64: pipeline("sgemm_simd64"),
            sgemm_simd64_splitk: pipeline("sgemm_simd64_splitk"),
            zero_f32: pipeline("zero_f32"),
            sgemm_simd_4x4_bias: pipeline("sgemm_simd_4x4_bias"),
            hgemm_simd_4x4: pipeline("hgemm_simd_4x4"),
            hgemm_simd_4x4_bias: pipeline("hgemm_simd_4x4_bias"),
            bias_add_h: pipeline("bias_add_h"),
            gelu_inplace_h: pipeline("gelu_inplace_h"),
            gelu_approx_inplace_h: pipeline("gelu_approx_inplace_h"),
            silu_inplace_h: pipeline("silu_inplace_h"),
            relu_inplace_h: pipeline("relu_inplace_h"),
            sigmoid_inplace_h: pipeline("sigmoid_inplace_h"),
            tanh_inplace_h: pipeline("tanh_inplace_h"),
            exp_inplace_h: pipeline("exp_inplace_h"),
            log_inplace_h: pipeline("log_inplace_h"),
            sqrt_inplace_h: pipeline("sqrt_inplace_h"),
            rsqrt_inplace_h: pipeline("rsqrt_inplace_h"),
            rec_inplace_h: pipeline("rec_inplace_h"),
            neg_inplace_h: pipeline("neg_inplace_h"),
            abs_inplace_h: pipeline("abs_inplace_h"),
            sin_inplace_h: pipeline("sin_inplace_h"),
            cos_inplace_h: pipeline("cos_inplace_h"),
            tan_inplace_h: pipeline("tan_inplace_h"),
            atan_inplace_h: pipeline("atan_inplace_h"),
            round_inplace_h: pipeline("round_inplace_h"),
            layer_norm_h: pipeline("layer_norm_h"),
            fused_residual_ln_h: pipeline("fused_residual_ln_h"),
            fused_residual_rms_norm_h: pipeline("fused_residual_rms_norm_h"),
            rms_norm_h: pipeline("rms_norm_h"),
            softmax_lastax_h: pipeline("softmax_lastax_h"),
            reduce_axes_h: pipeline("reduce_axes_h"),
            elem_add_h: pipeline("elem_add_h"),
            elem_mul_h: pipeline("elem_mul_h"),
            elem_sub_h: pipeline("elem_sub_h"),
            elem_div_h: pipeline("elem_div_h"),
            elem_max_h: pipeline("elem_max_h"),
            elem_min_h: pipeline("elem_min_h"),
            elem_pow_h: pipeline("elem_pow_h"),
            gather_axis0_h: pipeline("gather_axis0_h"),
            narrow_lastax_h: pipeline("narrow_lastax_h"),
            sdpa_h: pipeline("sdpa_h"),
            fused_attn_block: pipeline("fused_attn_block"),
            rope_h: pipeline("rope_h"),
            cast_f32_to_f16: pipeline("cast_f32_to_f16"),
            cast_f16_to_f32: pipeline("cast_f16_to_f32"),
            copy_f32: pipeline("copy_f32"),
            copy4: pipeline("copy4"),
            sgemm_simd_padded: pipeline("sgemm_simd_padded"),
            sgemm_simd_padded_residual: pipeline("sgemm_simd_padded_residual"),
            sgemm_simd_padded_f16w: pipeline("sgemm_simd_padded_f16w"),
            sgemm_wide8x64_f16w: pipeline("sgemm_wide8x64_f16w"),
            gemv_f16w_splitk: pipeline("gemv_f16w_splitk"),
            gemv_f16w_kpart: pipeline("gemv_f16w_kpart"),
            gemv_zero_f32: pipeline("gemv_zero_f32"),
            sgemm_simd_padded_bias: pipeline("sgemm_simd_padded_bias"),
            sgemm_tiled: pipeline("sgemm_tiled"),
            bias_add: pipeline("bias_add"),
            gelu_inplace: pipeline("gelu_inplace"),
            gelu_inplace4: pipeline("gelu_inplace4"),
            gelu_approx_inplace: pipeline("gelu_approx_inplace"),
            gelu_approx_inplace4: pipeline("gelu_approx_inplace4"),
            gelu_approx_out4: pipeline("gelu_approx_out4"),
            silu_inplace: pipeline("silu_inplace"),
            silu_inplace4: pipeline("silu_inplace4"),
            silu_out4: pipeline("silu_out4"),
            binary_broadcast_rhs_col_f32: pipeline("binary_broadcast_rhs_col_f32"),
            binary_broadcast_rhs_col4: pipeline("binary_broadcast_rhs_col4"),
            binary_broadcast_rhs_row_f32: pipeline("binary_broadcast_rhs_row_f32"),
            binary_broadcast_rhs_row4: pipeline("binary_broadcast_rhs_row4"),
            binary_broadcast_rhs_scalar_f32: pipeline("binary_broadcast_rhs_scalar_f32"),
            binary_broadcast_rhs_scalar4: pipeline("binary_broadcast_rhs_scalar4"),
            binary_broadcast_1ax_f32: pipeline("binary_broadcast_1ax_f32"),
            binary_broadcast_1ax4: pipeline("binary_broadcast_1ax4"),
            fused_binary_activation_f32: pipeline("fused_binary_activation_f32"),
            fused_binary_activation4: pipeline("fused_binary_activation4"),
            fused_ternary_activation_f32: pipeline("fused_ternary_activation_f32"),
            fused_ternary_activation4: pipeline("fused_ternary_activation4"),
            layer_norm: pipeline("layer_norm"),
            rms_norm: pipeline("rms_norm"),
            rms_norm_mul_silu: pipeline("rms_norm_mul_silu"),
            elem_add: pipeline("elem_add"),
            elem_add4: pipeline("elem_add4"),
            elem_sub4: pipeline("elem_sub4"),
            binary_broadcast_f32: pipeline("binary_broadcast_f32"),
            binary_broadcast_rank2_f32: pipeline("binary_broadcast_rank2_f32"),
            binary_broadcast_rank24: pipeline("binary_broadcast_rank24"),
            elem_mul: pipeline("elem_mul"),
            elem_mul4: pipeline("elem_mul4"),
            elem_div4: pipeline("elem_div4"),
            gather_axis0: pipeline("gather_axis0"),
            narrow_lastax: pipeline("narrow_lastax"),
            narrow_lastax4: pipeline("narrow_lastax4"),
            split_lastax: pipeline("split_lastax"),
            split_lastax4: pipeline("split_lastax4"),
            fused_residual_ln: pipeline("fused_residual_ln"),
            fused_residual_rms_norm: pipeline("fused_residual_rms_norm"),
            ada_layer_norm: pipeline("ada_layer_norm"),
            ada_layer_norm_h: pipeline("ada_layer_norm_h"),
            gated_residual: pipeline("gated_residual"),
            gated_residual_h: pipeline("gated_residual_h"),
            ada_layer_norm_backward: pipeline("ada_layer_norm_backward"),
            ada_layer_norm_backward_h: pipeline("ada_layer_norm_backward_h"),
            gated_residual_backward: pipeline("gated_residual_backward"),
            gated_residual_backward_h: pipeline("gated_residual_backward_h"),
            sdpa: pipeline("sdpa"),
            sdpa_simd: pipeline("sdpa_simd"),
            sdpa_simd_h16: pipeline("sdpa_simd_h16"),
            sdpa_long: pipeline("sdpa_long"),
            sdpa_long_occpad: pipeline("sdpa_long_occpad"),
            sdpa_decode_m1: pipeline("sdpa_decode_m1"),
            sdpa_decode_m1_f16kv: pipeline("sdpa_decode_m1_f16kv"),
            sdpa_decode_m1_partial: pipeline("sdpa_decode_m1_partial"),
            sdpa_decode_m1_partial_f16kv: pipeline("sdpa_decode_m1_partial_f16kv"),
            sdpa_decode_m1_partial_hd: pipeline("sdpa_decode_m1_partial_hd"),
            sdpa_decode_m1_partial_hd_f16kv: pipeline("sdpa_decode_m1_partial_hd_f16kv"),
            sdpa_decode_m1_partial_hd_256: pipeline("sdpa_decode_m1_partial_hd_256"),
            sdpa_decode_m1_partial_hd_256_f16kv: pipeline("sdpa_decode_m1_partial_hd_256_f16kv"),
            sdpa_decode_m1_combine: pipeline("sdpa_decode_m1_combine"),
            sdpa_decode_m1_combine_256: pipeline("sdpa_decode_m1_combine_256"),
            sdpa_prefill_hd256: pipeline("sdpa_prefill_hd256"),
            sdpa_decode_m1_partial_w8a8: pipeline("sdpa_decode_m1_partial_w8a8"),
            kv_quant_i8: pipeline("kv_quant_i8"),
            kv_quant_i8_f16: pipeline("kv_quant_i8_f16"),
            sdpa_prefill_fa: pipeline("sdpa_prefill_fa"),
            sdpa_prefill_fa_mma: pipeline("sdpa_prefill_fa_mma"),
            sdpa_fa_f32: pipeline("sdpa_fa_f32"),
            sdpa_splitk: pipeline("sdpa_splitk"),
            sdpa_fa2: pipeline("sdpa_fa2"),
            sdpa_mma: pipeline("sdpa_mma"),
            argreduce: pipeline("argreduce"),
            argreduce_lastaxis: pipeline("argreduce_lastaxis"),
            vq_assign: pipeline("vq_assign"),
            sample_logits: pipeline("sample_logits"),
            rng_normal_philox: pipeline("rng_normal_philox"),
            rng_uniform_philox: pipeline("rng_uniform_philox"),
            rng_fill_zero: pipeline("rng_fill_zero"),
            dequant_matmul_int8: pipeline("dequant_matmul_int8"),
            dequant_matmul_int4: pipeline("dequant_matmul_int4"),
            dequant_matmul_fp8: pipeline("dequant_matmul_fp8"),
            dequant_matmul_nvfp4: pipeline("dequant_matmul_nvfp4"),
            dequant_matmul_mxfp4x2: pipeline("dequant_matmul_mxfp4x2"),
            dequant_matmul_mlx_gemv: pipeline("dequant_matmul_mlx_gemv"),
            dequant_matmul_mlx_gemm: pipeline("dequant_matmul_mlx_gemm"),
            grouped_dequant_matmul_mlx_gemv: pipeline("grouped_dequant_matmul_mlx_gemv"),
            grouped_dequant_matmul_mlx_gemm: pipeline("grouped_dequant_matmul_mlx_gemm"),
            rope: pipeline("rope"),
            fused_swiglu: pipeline("fused_swiglu"),
            fused_swiglu_h: pipeline("fused_swiglu_h"),
            elementwise_region: pipeline("elementwise_region"),
            batch_elementwise_region: pipeline("batch_elementwise_region"),
            fused_swiglu_cast_f32_to_f16: pipeline("fused_swiglu_cast_f32_to_f16"),
            fused_swiglu_cast_f16_to_f32: pipeline("fused_swiglu_cast_f16_to_f32"),
            concat_segment_lastax: pipeline("concat_segment_lastax"),
            concat_segment_lastax4: pipeline("concat_segment_lastax4"),
            concat_lastax_multi: pipeline("concat_lastax_multi"),
            concat_lastax_multi4: pipeline("concat_lastax_multi4"),
            concat_segment_lastax_h: pipeline("concat_segment_lastax_h"),
            concat_midaxis_seg: pipeline("concat_midaxis_seg"),
            concat_midaxis_seg_h: pipeline("concat_midaxis_seg_h"),
            concat_midaxis_seg_f32_to_f16: pipeline("concat_midaxis_seg_f32_to_f16"),
            concat_midaxis_seg_f16_to_f32: pipeline("concat_midaxis_seg_f16_to_f32"),
            elem_sub: pipeline("elem_sub"),
            elem_div: pipeline("elem_div"),
            elem_max: pipeline("elem_max"),
            elem_min: pipeline("elem_min"),
            elem_pow: pipeline("elem_pow"),
            elem_binop: pipeline("elem_binop"),
            elem_binop_h: pipeline("elem_binop_h"),
            elem_compare: pipeline("elem_compare"),
            elem_compare_bcast: pipeline("elem_compare_bcast"),
            elem_where: pipeline("elem_where"),
            elem_where_bcast: pipeline("elem_where_bcast"),
            elem_fma: pipeline("elem_fma"),
            relu_backward: pipeline("relu_backward"),
            activation_backward: pipeline("activation_backward"),
            complex_norm_sq: pipeline("complex_norm_sq"),
            complex_norm_sq_backward: pipeline("complex_norm_sq_backward"),
            conjugate_c64: pipeline("conjugate_c64"),
            fft_butterfly_stage: pipeline("fft_butterfly_stage"),
            fake_quantize_fixed: pipeline("fake_quantize_fixed"),
            fake_quantize_perbatch: pipeline("fake_quantize_perbatch"),
            reduce_axes: pipeline("reduce_axes"),
            reduce_axes_sum_simd: pipeline("reduce_axes_sum_simd"),
            topk_lastax: pipeline("topk_lastax"),
            grouped_matmul: pipeline("grouped_matmul"),
            grouped_gemv_splitk: pipeline("grouped_gemv_splitk"),
            gemv_f32_splitk: pipeline("gemv_f32_splitk"),
            scatter_add_zero: pipeline("scatter_add_zero"),
            scatter_add_accumulate: pipeline("scatter_add_accumulate"),
            transpose_nd: pipeline("transpose_nd"),
            transpose_nd_h: pipeline("transpose_nd_h"),
            transpose_2d_f32: pipeline("transpose_2d_f32"),
            transpose_2d_tiled_f32: pipeline("transpose_2d_tiled_f32"),
            transpose_last2_batched_f32: pipeline("transpose_last2_batched_f32"),
            transpose_last2_batched_tiled_f32: pipeline("transpose_last2_batched_tiled_f32"),
            transpose_swap12_batched_trail_f32: pipeline("transpose_swap12_batched_trail_f32"),
            transpose_swap12_batched_trail_tiled_f32: pipeline(
                "transpose_swap12_batched_trail_tiled_f32",
            ),
            gather_axis: pipeline("gather_axis"),
            pool2d: pipeline("pool2d"),
            maxpool2d_backward: pipeline("maxpool2d_backward"),
            conv2d_backward_input: pipeline("conv2d_backward_input"),
            conv2d_backward_weight: pipeline("conv2d_backward_weight"),
            conv2d_backward_weight_partial: pipeline("conv2d_backward_weight_partial"),
            conv2d_backward_weight_reduce: pipeline("conv2d_backward_weight_reduce"),
            maxpool3d_backward: pipeline("maxpool3d_backward"),
            conv3d_backward_input: pipeline("conv3d_backward_input"),
            conv3d_backward_weight: pipeline("conv3d_backward_weight"),
            conv2d: pipeline("conv2d"),
            depthwise_conv1d_bsc: pipeline("depthwise_conv1d_bsc"),
            conv2d_w1: pipeline("conv2d_w1"),
            layer_norm2d: pipeline("layer_norm2d"),
            group_norm: pipeline("group_norm"),
            resize_nearest_2x: pipeline("resize_nearest_2x"),
            conv_transpose2d: pipeline("conv_transpose2d"),
            conv3d: pipeline("conv3d"),
            conv_transpose3d: pipeline("conv_transpose3d"),
            relu_inplace: pipeline("relu_inplace"),
            sigmoid_inplace: pipeline("sigmoid_inplace"),
            tanh_inplace: pipeline("tanh_inplace"),
            exp_inplace: pipeline("exp_inplace"),
            log_inplace: pipeline("log_inplace"),
            sqrt_inplace: pipeline("sqrt_inplace"),
            rsqrt_inplace: pipeline("rsqrt_inplace"),
            rec_inplace: pipeline("rec_inplace"),
            neg_inplace: pipeline("neg_inplace"),
            abs_inplace: pipeline("abs_inplace"),
            round_inplace: pipeline("round_inplace"),
            sin_inplace: pipeline("sin_inplace"),
            cos_inplace: pipeline("cos_inplace"),
            tan_inplace: pipeline("tan_inplace"),
            atan_inplace: pipeline("atan_inplace"),
            softmax_lastax: pipeline("softmax_lastax"),
            softmax_lastax_causal: pipeline("softmax_lastax_causal"),
            softmax_cross_entropy_dense: pipeline("softmax_cross_entropy_dense"),
            softmax_cross_entropy_with_logits: pipeline("softmax_cross_entropy_with_logits"),
            softmax_cross_entropy_backward: pipeline("softmax_cross_entropy_backward"),
            fft_radix2_full_f32: pipeline("fft_radix2_full_f32"),
            #[cfg(feature = "native-gpu-fft")]
            fft_radix2_full_big_f32: pipeline("fft_radix2_full_big_f32"),
            #[cfg(feature = "native-gpu-fft")]
            fft_radix4_full_f32: pipeline("fft_radix4_full_f32"),
            #[cfg(feature = "native-gpu-fft")]
            fft_radix8_full_f32: pipeline("fft_radix8_full_f32"),
            #[cfg(feature = "native-gpu-fft")]
            fft_radix16_full_f32: pipeline("fft_radix16_full_f32"),
            fft_bit_reverse_f32: pipeline("fft_bit_reverse_f32"),
            fft_inner_f32: pipeline("fft_inner_f32"),
            fft_outer_r4_f32: pipeline("fft_outer_r4_f32"),
            fft_outer_r2_f32: pipeline("fft_outer_r2_f32"),
            gated_delta_net: pipeline("gated_delta_net"),
            gated_delta_net_sg: pipeline("gated_delta_net_sg"),
            selective_scan: pipeline("selective_scan"),
            lstm: pipeline("lstm"),
            gru: pipeline("gru"),
            rnn: pipeline("rnn"),
            mamba2: pipeline("mamba2"),
            dequant_gguf: pipeline("dequant_gguf"),
            q4k_mv_f32: pipeline("q4k_mv_f32"),
            q3k_mv_f32: pipeline("q3k_mv_f32"),
            q6k_mv_f32: pipeline("q6k_mv_f32"),
            q6k_mv_f32_sg: pipeline("q6k_mv_f32_sg"),
            q8_0_mv_f32_sg: pipeline("q8_0_mv_f32_sg"),
            q4_0_mv_f32_sg: pipeline("q4_0_mv_f32_sg"),
            q4_1_mv_f32_sg: pipeline("q4_1_mv_f32_sg"),
            q3k_mv_f32_sg: pipeline("q3k_mv_f32_sg"),
            q1_0_mv_f32: pipeline("q1_0_mv_f32"),
            q1_0_mv_f32_sg: pipeline("q1_0_mv_f32_sg"),
            q1_0_dual_mv_f32_sg: pipeline("q1_0_dual_mv_f32_sg"),
            q1_0_mm_f32: pipeline("q1_0_mm_f32"),
            q2_0_mv_f32: pipeline("q2_0_mv_f32"),
            q2_0_mv_f32_sg: pipeline("q2_0_mv_f32_sg"),
            q2_0_dual_mv_f32_sg: pipeline("q2_0_dual_mv_f32_sg"),
            q2_0_mm_f32: pipeline("q2_0_mm_f32"),
            q4_0_mv_f32: pipeline("q4_0_mv_f32"),
            q4_1_mv_f32: pipeline("q4_1_mv_f32"),
            q8_0_mv_f32: pipeline("q8_0_mv_f32"),
            iq4_nl_mv_f32: pipeline("iq4_nl_mv_f32"),
            iq2_xxs_mv_f32: pipeline("iq2_xxs_mv_f32"),
            synth_matmul_codebook: pipeline("synth_matmul_codebook"),
            gemm_rb_bias: pipeline("gemm_rb_bias"),
            synth_reconstruct_nk: pipeline("synth_reconstruct_nk"),
            synth_bwd_dx: pipeline("synth_bwd_dx"),
            synth_bwd_codebook: pipeline("synth_bwd_codebook"),
            synth_matmul_codebook_mm: pipeline("synth_matmul_codebook_mm"),
            synth_matmul_codebook_h: pipeline("synth_matmul_codebook_h"),
            synth_matmul_codebook_mm_h: pipeline("synth_matmul_codebook_mm_h"),
            synth_matmul_codebook_tiled: pipeline("synth_matmul_codebook_tiled"),
            synth_matmul_codebook_tiled_h: pipeline("synth_matmul_codebook_tiled_h"),
            synth_reconstruct: pipeline("synth_reconstruct"),
            synth_reconstruct_h: pipeline("synth_reconstruct_h"),
            spline_activation: pipeline("spline_activation"),
            spline_activation_backward_x: pipeline("spline_activation_backward_x"),
            spline_activation_backward_coeff: pipeline("spline_activation_backward_coeff"),
            iq2_xs_mv_f32: pipeline("iq2_xs_mv_f32"),
            iq2_s_mv_f32: pipeline("iq2_s_mv_f32"),
            iq3_xxs_mv_f32: pipeline("iq3_xxs_mv_f32"),
            iq3_s_mv_f32: pipeline("iq3_s_mv_f32"),
            iq3_xxs_mv_f32_sg: pipeline("iq3_xxs_mv_f32_sg"),
            iq3_s_mv_f32_sg: pipeline("iq3_s_mv_f32_sg"),
            iq1_s_mv_f32: pipeline("iq1_s_mv_f32"),
            iq1_m_mv_f32: pipeline("iq1_m_mv_f32"),
            q4k_mv_f32_sg: pipeline("q4k_mv_f32_sg"),
            q5k_mv_f32: pipeline("q5k_mv_f32"),
            q2k_mv_f32: pipeline("q2k_mv_f32"),
            q4k_mm_f32: pipeline("q4k_mm_f32"),
            q4k_mm_f32_xs: pipeline("q4k_mm_f32_xs"),
            q5k_mm_f32_xs: pipeline("q5k_mm_f32_xs"),
            q6k_mm_f32_xs: pipeline("q6k_mm_f32_xs"),
            q8_0_mm_f32_xs: pipeline("q8_0_mm_f32_xs"),
            q6k_mm_f32: pipeline("q6k_mm_f32"),
            q4k_swiglu_mv_f32: pipeline("q4k_swiglu_mv_f32"),
            q4k_swiglu_mv_f32_sg: pipeline("q4k_swiglu_mv_f32_sg"),
            q4k_gelu_mv_f32: pipeline("q4k_gelu_mv_f32"),
            q5_0_swiglu_mv_f32: pipeline("q5_0_swiglu_mv_f32"),
            q5_0_gelu_mv_f32: pipeline("q5_0_gelu_mv_f32"),
            q4k_mv_residual_f32: pipeline("q4k_mv_residual_f32"),
            q4k_mv_residual_f32_sg: pipeline("q4k_mv_residual_f32_sg"),
            q6k_mv_residual_f32: pipeline("q6k_mv_residual_f32"),
            q5_0_mv_residual_f32: pipeline("q5_0_mv_residual_f32"),
            q1_0_swiglu_mv_f32: pipeline("q1_0_swiglu_mv_f32"),
            q1_0_swiglu_mv_f32_sg: pipeline("q1_0_swiglu_mv_f32_sg"),
            q1_0_mv_residual_f32: pipeline("q1_0_mv_residual_f32"),
            q1_0_mv_residual_f32_sg: pipeline("q1_0_mv_residual_f32_sg"),
            q2_0_swiglu_mv_f32: pipeline("q2_0_swiglu_mv_f32"),
            q2_0_swiglu_mv_f32_sg: pipeline("q2_0_swiglu_mv_f32_sg"),
            q2_0_mv_residual_f32: pipeline("q2_0_mv_residual_f32"),
            q2_0_mv_residual_f32_sg: pipeline("q2_0_mv_residual_f32_sg"),
            rms_norm_bwd: pipeline("rms_norm_bwd"),
            rms_norm_bwd_param: pipeline("rms_norm_bwd_param"),
            rms_norm_bwd_inv_r_f32: pipeline("rms_norm_bwd_inv_r_f32"),
            rms_norm_bwd_param_reduce_f32: pipeline("rms_norm_bwd_param_reduce_f32"),
            layer_norm_bwd: pipeline("layer_norm_bwd"),
            layer_norm_bwd_gamma: pipeline("layer_norm_bwd_gamma"),
            layer_norm_bwd_stats_f32: pipeline("layer_norm_bwd_stats_f32"),
            layer_norm_bwd_gamma_reduce_f32: pipeline("layer_norm_bwd_gamma_reduce_f32"),
            layer_norm_bwd_gamma_reduce_simd: pipeline("layer_norm_bwd_gamma_reduce_simd"),
            group_norm_bwd_input: pipeline("group_norm_bwd_input"),
            group_norm_bwd_gamma: pipeline("group_norm_bwd_gamma"),
            group_norm_bwd_beta: pipeline("group_norm_bwd_beta"),
            rope_bwd: pipeline("rope_bwd"),
            cumsum_fwd: pipeline("cumsum_fwd"),
            cum_scan: pipeline("cum_scan"),
            cumsum_bwd: pipeline("cumsum_bwd"),
            im2col_group: pipeline("im2col_group"),
            im2col_group_w1: pipeline("im2col_group_w1"),
            conv2d_bwd_weight_gemm: pipeline("conv2d_bwd_weight_gemm"),
            conv2d_bwd_weight_gemm_4x4: pipeline("conv2d_bwd_weight_gemm_4x4"),
            attn_bwd_scores_f32: pipeline("attn_bwd_scores_f32"),
            attn_bwd_dp_f32: pipeline("attn_bwd_dp_f32"),
            attn_bwd_ds_f32: pipeline("attn_bwd_ds_f32"),
            attn_bwd_dv_f32: pipeline("attn_bwd_dv_f32"),
            attn_bwd_dq_f32: pipeline("attn_bwd_dq_f32"),
            attn_bwd_dk_f32: pipeline("attn_bwd_dk_f32"),
            attn_bwd_scores_batched_f32: pipeline("attn_bwd_scores_batched_f32"),
            attn_bwd_dp_batched_f32: pipeline("attn_bwd_dp_batched_f32"),
            attn_bwd_ds_batched_f32: pipeline("attn_bwd_ds_batched_f32"),
            attn_bwd_dv_batched_f32: pipeline("attn_bwd_dv_batched_f32"),
            attn_bwd_dq_batched_f32: pipeline("attn_bwd_dq_batched_f32"),
            attn_bwd_dk_batched_f32: pipeline("attn_bwd_dk_batched_f32"),
            attn_bwd_fused_f32: pipeline("attn_bwd_fused_f32"),
            gather_bwd_zero: pipeline("gather_bwd_zero"),
            gather_bwd_acc: pipeline("gather_bwd_acc"),
            gaussian_splat_rasterize: pipeline("gaussian_splat_rasterize"),
            gaussian_splat_rasterize_linear: pipeline("gaussian_splat_rasterize_linear"),
            gaussian_splat_rasterize_linear_traced: pipeline(
                "gaussian_splat_rasterize_linear_traced",
            ),
            gaussian_splat_rasterize_backward_linear: pipeline(
                "gaussian_splat_rasterize_backward_linear",
            ),
            gaussian_splat_adam_step: pipeline("gaussian_splat_adam_step"),
            gaussian_splat_mse_loss_grad: pipeline("gaussian_splat_mse_loss_grad"),
            gaussian_splat_ssim_stats: pipeline("gaussian_splat_ssim_stats"),
            gaussian_splat_blended_loss_grad: pipeline("gaussian_splat_blended_loss_grad"),
            gaussian_splat_project_training: pipeline("gaussian_splat_project_training"),
            gaussian_splat_geometry_backward: pipeline("gaussian_splat_geometry_backward"),
            gaussian_splat_scene_grad_projection: pipeline("gaussian_splat_scene_grad_projection"),
            gaussian_splat_splat_color_backward: pipeline("gaussian_splat_splat_color_backward"),
            gaussian_splat_emit_tile_keys: pipeline("gaussian_splat_emit_tile_keys"),
            gaussian_splat_project_screen_ellipse: pipeline(
                "gaussian_splat_project_screen_ellipse",
            ),
            gaussian_splat_emit_tile_keys_conic: pipeline("gaussian_splat_emit_tile_keys_conic"),
            gaussian_splat_bin_histogram: pipeline("gaussian_splat_bin_histogram"),
            gaussian_splat_bin_copy_counts: pipeline("gaussian_splat_bin_copy_counts"),
            gaussian_splat_bin_prefix_sum: pipeline("gaussian_splat_bin_prefix_sum"),
            gaussian_splat_bin_scatter: pipeline("gaussian_splat_bin_scatter"),
            gaussian_splat_build_tile_ranges: pipeline("gaussian_splat_build_tile_ranges"),
            gaussian_splat_pack_grads: pipeline("gaussian_splat_pack_grads"),
            scalar_acts: build_scalar_act_kernels(&pipeline),
            iq_grid_lut: build_iq_grid_lut(
                &metal_device()
                    .expect("rlx-metal: no Metal device for IQ grid LUT staging")
                    .device,
            ),
            library,
        }
    }

    /// Device buffer holding the IQ grid LUTs (see field docs).
    pub fn iq_grid_buffer(&self) -> &Buffer {
        &self.iq_grid_lut
    }

    /// Dump compiled-pipeline occupancy characteristics for the training-hot
    /// kernels (`RLX_METAL_KERNEL_STATS=1`). Reports, per kernel:
    ///   maxTG   = maxTotalThreadsPerThreadgroup — the register-limited ceiling
    ///             on threads/threadgroup (low ⇒ high register pressure).
    ///   simdW   = threadExecutionWidth (32 on Apple).
    ///   tgMem   = staticThreadgroupMemoryLength (bytes) — more ⇒ fewer
    ///             concurrent threadgroups per core (occupancy).
    ///   dispatch= threads/threadgroup we actually launch → occupancy vs maxTG.
    pub fn dump_stats(&self) {
        let rows: &[(&str, &ComputePipelineState, u64)] = &[
            // (name, pipeline, threads/threadgroup we dispatch)
            ("sdpa (serial softmax)", &self.sdpa, 32),
            ("sdpa_simd (parallel)", &self.sdpa_simd, 32),
            ("sdpa_simd_h16 (f16 scores)", &self.sdpa_simd_h16, 32),
            ("sdpa_long (baseline fwd, seq>64)", &self.sdpa_long, 64),
            (
                "sdpa_long_occpad (same work +20KB tg)",
                &self.sdpa_long_occpad,
                64,
            ),
            ("sdpa_splitk (V1 split-K)", &self.sdpa_splitk, 32),
            ("sdpa_fa2 (V2 flash-tile)", &self.sdpa_fa2, 64),
            ("sdpa_mma (V3 simdgroup)", &self.sdpa_mma, 32),
            (
                "attn_bwd_dv_batched (6-pass bwd)",
                &self.attn_bwd_dv_batched_f32,
                64,
            ),
            (
                "attn_bwd_dq_batched (6-pass bwd)",
                &self.attn_bwd_dq_batched_f32,
                64,
            ),
            (
                "attn_bwd_fused (Level 2 fused bwd)",
                &self.attn_bwd_fused_f32,
                64,
            ),
            ("sgemm_simd_4x4", &self.sgemm_simd_4x4, 64),
            ("sgemm_simd", &self.sgemm_simd, 64),
            ("sgemm_tiled", &self.sgemm_tiled, 256),
            ("layer_norm (fwd)", &self.layer_norm, 128),
            ("layer_norm_bwd (dx)", &self.layer_norm_bwd, 128),
            (
                "layer_norm_bwd_gamma_reduce_f32 (serial)",
                &self.layer_norm_bwd_gamma_reduce_f32,
                128,
            ),
            (
                "layer_norm_bwd_gamma_reduce_simd",
                &self.layer_norm_bwd_gamma_reduce_simd,
                32,
            ),
            ("reduce_axes (serial)", &self.reduce_axes, 256),
            ("reduce_axes_sum_simd", &self.reduce_axes_sum_simd, 32),
            (
                "attn_bwd_scores_batched",
                &self.attn_bwd_scores_batched_f32,
                64,
            ),
            ("attn_bwd_ds_batched", &self.attn_bwd_ds_batched_f32, 256),
            ("gather_axis0 (embed fwd)", &self.gather_axis0, 64),
            (
                "scatter_add_accumulate (embed bwd)",
                &self.scatter_add_accumulate,
                64,
            ),
            ("softmax_lastax (lm head)", &self.softmax_lastax, 256),
            (
                "softmax_cross_entropy_dense",
                &self.softmax_cross_entropy_dense,
                256,
            ),
        ];
        eprintln!("[kernel-stats] compiled-pipeline occupancy (Apple SIMD width 32):");
        eprintln!(
            "  {:42}  {:>6} {:>6} {:>7}  {:>8}  {:>6}",
            "kernel", "maxTG", "simdW", "tgMem", "dispatch", "occ%"
        );
        for (name, p, disp) in rows {
            let maxtg = p.max_total_threads_per_threadgroup();
            let simdw = p.thread_execution_width();
            let tgmem = p.static_threadgroup_memory_length();
            let occ = if maxtg > 0 {
                (*disp as f64) / (maxtg as f64) * 100.0
            } else {
                0.0
            };
            eprintln!(
                "  {:42}  {:>6} {:>6} {:>6}B  {:>8} {:>6.1}",
                name, maxtg, simdw, tgmem, disp, occ
            );
        }
    }
}

/// Concatenate every IQ LUT into one shared Metal buffer in the layout
/// declared at the top of `dequant_gguf.msl`. Offsets must match the
/// `IQ_GRID_OFF_*` constants.
fn build_iq_grid_lut(device: &crate::mtl::DeviceRef) -> Buffer {
    use rlx_gguf::iq_grids::{
        IQ1S_GRID, IQ2S_GRID, IQ2XS_GRID, IQ2XXS_GRID, IQ3S_GRID, IQ3XXS_GRID, KMASK_IQ2XS,
        KSIGNS_IQ2XS,
    };

    let mut bytes = Vec::with_capacity(33_944);
    // KMASK_IQ2XS (8) | KSIGNS_IQ2XS (128).
    bytes.extend_from_slice(&KMASK_IQ2XS);
    bytes.extend_from_slice(&KSIGNS_IQ2XS);
    // Each u64/u32 grid entry is stored little-endian — matches the
    // `to_le_bytes` cast used by the CPU kernel.
    for v in IQ2XXS_GRID.iter() {
        bytes.extend_from_slice(&v.to_le_bytes());
    }
    for v in IQ2XS_GRID.iter() {
        bytes.extend_from_slice(&v.to_le_bytes());
    }
    for v in IQ2S_GRID.iter() {
        bytes.extend_from_slice(&v.to_le_bytes());
    }
    for v in IQ3XXS_GRID.iter() {
        bytes.extend_from_slice(&v.to_le_bytes());
    }
    for v in IQ3S_GRID.iter() {
        bytes.extend_from_slice(&v.to_le_bytes());
    }
    for v in IQ1S_GRID.iter() {
        bytes.extend_from_slice(&v.to_le_bytes());
    }
    device.new_buffer_with_data(
        bytes.as_ptr() as *const _,
        bytes.len() as u64,
        MTLResourceOptions::StorageModeShared,
    )
}

/// Get or compile the global kernel library.
pub fn kernels() -> &'static Kernels {
    static K: OnceLock<Kernels> = OnceLock::new();
    let k = K.get_or_init(Kernels::new);
    if rlx_ir::env::flag("RLX_METAL_KERNEL_STATS") {
        static ONCE: OnceLock<()> = OnceLock::new();
        ONCE.get_or_init(|| k.dump_stats());
    }
    k
}

/// Force MSL/metallib + pipeline state init (call once at process load).
pub fn prewarm() -> &'static Kernels {
    kernels()
}

/// Buffer indices each kernel declares, parsed from the assembled MSL.
///
/// Feeds `mtl::bind_validate`, which cross-checks these against what an encoder
/// actually bound. Metal's own pipeline reflection would be the natural source,
/// but requesting it (`MTLPipelineOptionArgumentInfo`) perturbs pipeline
/// creation badly enough to abort unrelated encoders, so we read the signatures
/// straight from the source we compiled.
///
/// Limitation vs real reflection: this cannot tell whether an argument survived
/// optimisation. A declared-but-unused buffer would be reported active here and
/// flagged if unbound. In practice these kernels don't carry unused parameters,
/// and a false positive is loud and immediate rather than silent.
pub(crate) fn declared_buffer_indices(kernel: &str) -> Option<std::collections::BTreeSet<u64>> {
    use std::collections::{BTreeSet, HashMap};
    use std::sync::OnceLock;
    static TABLE: OnceLock<HashMap<String, BTreeSet<u64>>> = OnceLock::new();
    let table = TABLE.get_or_init(|| {
        let src = msl_source();
        let mut out: HashMap<String, BTreeSet<u64>> = HashMap::new();
        let mut rest = src.as_str();
        while let Some(pos) = rest.find("kernel void ") {
            rest = &rest[pos + "kernel void ".len()..];
            let Some(paren) = rest.find('(') else { break };
            let name = rest[..paren].trim().to_string();
            // Parameter list ends at the closing paren before the body.
            let body = rest.find(") {").unwrap_or(rest.len().min(4096));
            let params = &rest[paren..body.max(paren)];
            let mut idxs = BTreeSet::new();
            let mut scan = params;
            while let Some(b) = scan.find("[[buffer(") {
                scan = &scan[b + "[[buffer(".len()..];
                if let Some(close) = scan.find(')')
                    && let Ok(i) = scan[..close].trim().parse::<u64>()
                {
                    idxs.insert(i);
                }
            }
            if !name.is_empty() {
                out.insert(name, idxs);
            }
        }
        out
    });
    table.get(kernel).cloned()
}

#[cfg(test)]
mod msl_audit_tests {
    /// Every `kernel void NAME(` in the assembled source, in declaration order.
    fn kernel_names(src: &str) -> Vec<String> {
        src.lines()
            .filter_map(|l| l.trim().strip_prefix("kernel void "))
            .filter_map(|rest| rest.split('(').next())
            .map(|n| n.trim().to_string())
            .filter(|n| !n.is_empty())
            .collect()
    }

    /// The first `[[buffer(0)]]` parameter of `name`, as written.
    fn buffer0_decl(src: &str, name: &str) -> Option<String> {
        let start = src.find(&format!("kernel void {name}("))?;
        let tail = &src[start..];
        let end = tail.find(") {").unwrap_or(tail.len().min(2000));
        tail[..end]
            .lines()
            .find(|l| l.contains("[[buffer(0)]]"))
            .map(|l| l.trim().trim_end_matches(',').to_string())
    }

    /// The whole MSL blob must compile, and every kernel it declares must be
    /// retrievable by name.
    ///
    /// The source is assembled at *runtime* from string constants plus
    /// generated fragments, so nothing checks it until a pipeline is first
    /// built — a syntax error, or a kernel referencing a helper that moved,
    /// ships fine and fails on first use of that one op. Compiling the whole
    /// thing here moves that to the gate.
    #[test]
    fn msl_source_compiles_and_all_kernels_resolve() {
        let Some(dev) = crate::device::metal_device() else {
            eprintln!("no Metal device; skipping");
            return;
        };
        let src = super::msl_source();
        let opts = crate::mtl::CompileOptions::new();
        let lib = match dev.device.new_library_with_source(&src, &opts) {
            Ok(l) => l,
            Err(e) => panic!("assembled MSL failed to compile: {e}"),
        };

        let names = kernel_names(&src);
        assert!(
            names.len() > 100,
            "kernel scrape found only {} — parser is out of date, not the source",
            names.len()
        );
        let missing: Vec<&String> = names
            .iter()
            .filter(|n| lib.get_function(n, None).is_err())
            .collect();
        assert!(missing.is_empty(), "declared but unresolvable: {missing:?}");
    }

    /// The kernels `icb.rs` encodes must keep the buffer-0 ABI that encoder
    /// assumes.
    ///
    /// Two conventions coexist in this file: "arena-base" takes
    /// `device char* arena` plus explicit `ulong` byte offsets, and
    /// "offset-pointer" takes a typed pointer already offset to the data. They
    /// are indistinguishable by name, and `icb.rs` binds by *index*, so moving
    /// a kernel from one to the other silently shifts every later index — the
    /// unbound one reads 0, `len` comes back 0, and the dispatch writes
    /// nothing. That is a real bug this repo shipped. If this test fails,
    /// update the matching arm in `icb.rs` rather than the expectation here.
    #[test]
    fn icb_encoded_kernels_keep_their_buffer0_abi() {
        let src = super::msl_source();
        // (kernel, buffer-0 is the arena base rather than a typed data pointer)
        let expected: &[(&str, bool)] = &[
            ("elem_add", true),
            ("elem_mul", true),
            ("copy_f32", true),
            ("gelu_inplace", true),
            ("narrow_lastax", true),
            ("silu_inplace", false),
            ("bias_add", false),
            ("layer_norm", false),
            ("fused_residual_ln", false),
            ("rope", false),
        ];
        let mut wrong = Vec::new();
        for &(name, want_arena_base) in expected {
            let decl = buffer0_decl(&src, name)
                .unwrap_or_else(|| panic!("kernel `{name}` not found — icb.rs references it"));
            let is_arena_base = decl.contains("char*");
            if is_arena_base != want_arena_base {
                wrong.push(format!(
                    "{name}: expected arena_base={want_arena_base}, found `{decl}`"
                ));
            }
        }
        assert!(
            wrong.is_empty(),
            "ICB kernel ABI drift:\n  {}",
            wrong.join("\n  ")
        );
    }
}