alice-llm 2.0.0

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

use std::collections::HashMap;

// ─── Constants ──────────────────────────────────────────────────────────────

const GGUF_MAGIC: u32 = 0x4655_4747;
const GGUF_DEFAULT_ALIGNMENT: usize = 32;
const QK_K: usize = 256;
const QK8_0: usize = 32;

// ─── Half-precision conversion ──────────────────────────────────────────────

#[inline]
pub(crate) const fn f16_to_f32(h: u16) -> f32 {
    let sign = ((h >> 15) & 1) as u32;
    let exponent = ((h >> 10) & 0x1f) as u32;
    let mantissa = (h & 0x3ff) as u32;

    if exponent == 0 {
        if mantissa == 0 {
            f32::from_bits(sign << 31)
        } else {
            let mut e = 0u32;
            let mut m = mantissa;
            while (m & 0x400) == 0 {
                m <<= 1;
                e += 1;
            }
            let exp = 127 - 15 - e + 1;
            let man = (m & 0x3ff) << 13;
            f32::from_bits((sign << 31) | (exp << 23) | man)
        }
    } else if exponent == 31 {
        f32::from_bits((sign << 31) | (0xff << 23) | (mantissa << 13))
    } else {
        let exp = exponent + 127 - 15;
        let man = mantissa << 13;
        f32::from_bits((sign << 31) | (exp << 23) | man)
    }
}

// ─── Quantization types ────────────────────────────────────────────────────

/// GGML tensor quantization type.
///
/// `Q1_0`/`Q2_0` are the group-128 low-bit formats used by PrismML's
/// llama.cpp fork for Bonsai 27B (binary/ternary post-training conversion of
/// Qwen 3.6-27B). Type IDs 41/42 come from that fork; upstream llama.cpp
/// leaves those slots free.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[allow(non_camel_case_types)]
pub enum GgmlType {
    F32,
    F16,
    Q4_0,
    Q4_1,
    Q5_0,
    Q5_1,
    Q8_0,
    Q2_K,
    Q3_K,
    Q4_K,
    Q5_K,
    Q6_K,
    IQ4_XS,
    /// PrismML fork: 128-element binary ternary block, {−1, +1} × FP16 scale.
    /// 18 bytes/block (2 FP16 + 16 packed bits).
    Q1_0,
    /// PrismML fork: 128-element ternary block, {−1, 0, +1} × FP16 scale.
    /// 34 bytes/block (2 FP16 + 32 packed 2-bit slots).
    Q2_0,
    /// OCP Microscaling FP4 (MX v1.0, September 2023).
    ///
    /// 32-element block layout uses 1 byte E8M0 shared exponent (power-of-2
    /// scale) plus 16 bytes packed E2M1 elements = 17 bytes/block. Effective
    /// 4.25 bits per weight. Native quantization format for Kimi K3
    /// (Moonshot AI, 2026-07-27 open weight release), trained with MXFP4 QAT
    /// from SFT stage onward. Type ID matches upstream llama.cpp
    /// `GGML_TYPE_MXFP4 = 39`.
    Mxfp4,
    /// llama.cpp IQ1_S: 1.5625 bits per weight (256-element K-block).
    /// Layout: `d (2) + qs (QK_K/8 = 32) + qh (QK_K/32 * 2 = 16)` = 50 bytes.
    /// Real K3 GrEarl GGUF-IQ1_S variant uses this for expert cubes
    /// (`ffn_gate_exps` / `ffn_up_exps` / `ffn_down_exps`).
    ///
    /// Dequantization is NOT yet implemented in this crate — recognizing
    /// the type lets the K3 loader's shape derivation work (block-aligned
    /// per-expert slicing succeeds) but forward will panic at matvec
    /// time with an "IQ1_S dequant not implemented" message. Full
    /// dequant is scheduled for Phase X.4.b.7.
    IQ1_S,
    Other(u32),
}

impl GgmlType {
    const fn from_u32(v: u32) -> Self {
        match v {
            0 => Self::F32,
            1 => Self::F16,
            2 => Self::Q4_0,
            3 => Self::Q4_1,
            6 => Self::Q5_0,
            7 => Self::Q5_1,
            8 => Self::Q8_0,
            10 => Self::Q2_K,
            11 => Self::Q3_K,
            12 => Self::Q4_K,
            13 => Self::Q5_K,
            14 => Self::Q6_K,
            19 => Self::IQ1_S,
            23 => Self::IQ4_XS,
            39 => Self::Mxfp4,
            41 => Self::Q1_0,
            42 => Self::Q2_0,
            other => Self::Other(other),
        }
    }

    /// Byte size per block of quantized data.
    #[must_use]
    pub const fn block_bytes(&self) -> usize {
        match self {
            Self::F32 => 4,
            Self::F16 => 2,
            // Q4_0: d (2) + qs (16) = 18
            Self::Q4_0 => 18,
            // Q4_1: d (2) + m (2) + qs (16) = 20
            Self::Q4_1 => 20,
            // Q5_0: d (2) + qh (4) + qs (16) = 22
            Self::Q5_0 => 22,
            // Q5_1: d (2) + m (2) + qh (4) + qs (16) = 24
            Self::Q5_1 => 24,
            Self::Q8_0 => 34,
            Self::Q2_K => 84,
            Self::Q3_K => 110,
            Self::Q4_K => 144,
            Self::Q5_K => 176,
            Self::Q6_K => 210,
            // IQ4_XS: d (2) + scales_h (2) + scales_l (QK_K/64=4) + qs (QK_K/2=128) = 136
            Self::IQ4_XS => 136,
            // Q1_0 (PrismML g128): d (2) + qs (QK1_0/8 = 16) = 18
            Self::Q1_0 => 18,
            // Q2_0 (PrismML g128): d (2) + qs (QK2_0/4 = 32) = 34
            Self::Q2_0 => 34,
            // MXFP4 (OCP MX v1.0): E8M0 scale (1) + 32 × E2M1 packed (16) = 17
            Self::Mxfp4 => 17,
            // IQ1_S (K-block, 256 elements, ~1.5625 bits/elem):
            //   d (ggml_half = 2) + qs (QK_K/8 = 32) + qh (QK_K/32 × 2 = 16) = 50
            Self::IQ1_S => 50,
            Self::Other(_) => 0,
        }
    }

    /// Number of elements per block.
    #[must_use]
    pub const fn elements_per_block(&self) -> usize {
        match self {
            Self::F32 | Self::F16 => 1,
            Self::Q4_0 | Self::Q4_1 | Self::Q5_0 | Self::Q5_1 | Self::Q8_0 => QK8_0,
            Self::Q2_K
            | Self::Q3_K
            | Self::Q4_K
            | Self::Q5_K
            | Self::Q6_K
            | Self::IQ4_XS
            | Self::IQ1_S => QK_K,
            Self::Q1_0 | Self::Q2_0 => QK_G128,
            Self::Mxfp4 => QK_MXFP4,
            Self::Other(_) => 1,
        }
    }
}

/// PrismML fork block size for `Q1_0` / `Q2_0` (Bonsai 27B group-128 layout).
pub const QK_G128: usize = 128;

/// OCP Microscaling MXFP4 block size (spec v1.0, September 2023).
///
/// One block holds 32 E2M1 elements sharing a single E8M0 exponent.
pub const QK_MXFP4: usize = 32;

// ─── GGUF metadata values ──────────────────────────────────────────────────

/// A GGUF metadata value.
#[derive(Debug, Clone)]
pub enum MetaValue {
    U8(u8),
    I8(i8),
    U16(u16),
    I16(i16),
    U32(u32),
    I32(i32),
    F32(f32),
    U64(u64),
    I64(i64),
    F64(f64),
    Bool(bool),
    Str(String),
    Array(Vec<Self>),
}

impl MetaValue {
    /// Get as u32.
    pub const fn as_u32(&self) -> Option<u32> {
        match self {
            Self::U32(v) => Some(*v),
            Self::I32(v) => Some(*v as u32),
            Self::U64(v) => Some(*v as u32),
            _ => None,
        }
    }

    /// Get as f32.
    pub const fn as_f32(&self) -> Option<f32> {
        match self {
            Self::F32(v) => Some(*v),
            Self::F64(v) => Some(*v as f32),
            _ => None,
        }
    }

    /// Get as string.
    pub fn as_str(&self) -> Option<&str> {
        match self {
            Self::Str(s) => Some(s),
            _ => None,
        }
    }

    /// Get as bool. Accepts Bool, U8 (0/1), and integer types (nonzero = true).
    pub const fn as_bool(&self) -> Option<bool> {
        match self {
            Self::Bool(b) => Some(*b),
            Self::U8(v) => Some(*v != 0),
            Self::U32(v) => Some(*v != 0),
            Self::I32(v) => Some(*v != 0),
            _ => None,
        }
    }

    /// Get as string array.
    pub fn as_str_array(&self) -> Option<Vec<&str>> {
        match self {
            Self::Array(arr) => {
                let mut out = Vec::with_capacity(arr.len());
                for v in arr {
                    out.push(v.as_str()?);
                }
                Some(out)
            }
            _ => None,
        }
    }

    /// Get as u32 array (accepts arrays of U32/I32/U64 elements).
    pub fn as_u32_array(&self) -> Option<Vec<u32>> {
        match self {
            Self::Array(arr) => {
                let mut out = Vec::with_capacity(arr.len());
                for v in arr {
                    out.push(v.as_u32()?);
                }
                Some(out)
            }
            _ => None,
        }
    }
}

// ─── Tensor info ────────────────────────────────────────────────────────────

/// Information about a single tensor in the GGUF file.
#[derive(Debug, Clone)]
pub struct TensorInfo {
    pub name: String,
    pub n_dims: u32,
    pub dims: Vec<u64>,
    pub qtype: GgmlType,
    pub offset: u64,
}

impl TensorInfo {
    /// Total number of elements in this tensor.
    #[must_use]
    pub fn n_elements(&self) -> usize {
        self.dims.iter().product::<u64>() as usize
    }

    /// Size in bytes of the raw quantized data.
    #[must_use]
    pub fn data_size(&self) -> usize {
        let n = self.n_elements();
        let epb = self.qtype.elements_per_block();
        let n_blocks = n.div_ceil(epb);
        n_blocks * self.qtype.block_bytes()
    }
}

// ─── Binary reader ──────────────────────────────────────────────────────────

struct Reader<'a> {
    data: &'a [u8],
    pos: usize,
}

impl<'a> Reader<'a> {
    const fn new(data: &'a [u8]) -> Self {
        Self { data, pos: 0 }
    }

    fn read_bytes(&mut self, n: usize) -> Option<&'a [u8]> {
        if self.pos + n > self.data.len() {
            return None;
        }
        let slice = &self.data[self.pos..self.pos + n];
        self.pos += n;
        Some(slice)
    }

    fn read_u8(&mut self) -> Option<u8> {
        let b = self.read_bytes(1)?;
        Some(b[0])
    }

    fn read_i8(&mut self) -> Option<i8> {
        Some(self.read_u8()? as i8)
    }

    fn read_u16(&mut self) -> Option<u16> {
        let b = self.read_bytes(2)?;
        Some(u16::from_le_bytes([b[0], b[1]]))
    }

    fn read_i16(&mut self) -> Option<i16> {
        let b = self.read_bytes(2)?;
        Some(i16::from_le_bytes([b[0], b[1]]))
    }

    fn read_u32(&mut self) -> Option<u32> {
        let b = self.read_bytes(4)?;
        Some(u32::from_le_bytes([b[0], b[1], b[2], b[3]]))
    }

    fn read_i32(&mut self) -> Option<i32> {
        let b = self.read_bytes(4)?;
        Some(i32::from_le_bytes([b[0], b[1], b[2], b[3]]))
    }

    fn read_u64(&mut self) -> Option<u64> {
        let b = self.read_bytes(8)?;
        Some(u64::from_le_bytes([
            b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7],
        ]))
    }

    fn read_i64(&mut self) -> Option<i64> {
        let b = self.read_bytes(8)?;
        Some(i64::from_le_bytes([
            b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7],
        ]))
    }

    fn read_f32(&mut self) -> Option<f32> {
        let b = self.read_bytes(4)?;
        Some(f32::from_le_bytes([b[0], b[1], b[2], b[3]]))
    }

    fn read_f64(&mut self) -> Option<f64> {
        let b = self.read_bytes(8)?;
        Some(f64::from_le_bytes([
            b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7],
        ]))
    }

    fn read_string(&mut self) -> Option<String> {
        let len = self.read_u64()? as usize;
        let bytes = self.read_bytes(len)?;
        Some(String::from_utf8_lossy(bytes).into_owned())
    }

    fn read_bool(&mut self) -> Option<bool> {
        Some(self.read_u8()? != 0)
    }

    fn read_meta_value(&mut self, vtype: u32) -> Option<MetaValue> {
        match vtype {
            0 => Some(MetaValue::U8(self.read_u8()?)),
            1 => Some(MetaValue::I8(self.read_i8()?)),
            2 => Some(MetaValue::U16(self.read_u16()?)),
            3 => Some(MetaValue::I16(self.read_i16()?)),
            4 => Some(MetaValue::U32(self.read_u32()?)),
            5 => Some(MetaValue::I32(self.read_i32()?)),
            6 => Some(MetaValue::F32(self.read_f32()?)),
            7 => Some(MetaValue::Bool(self.read_bool()?)),
            8 => Some(MetaValue::Str(self.read_string()?)),
            9 => {
                let arr_type = self.read_u32()?;
                let arr_len = self.read_u64()? as usize;
                let mut items = Vec::with_capacity(arr_len.min(1_000_000));
                for _ in 0..arr_len {
                    items.push(self.read_meta_value(arr_type)?);
                }
                Some(MetaValue::Array(items))
            }
            10 => Some(MetaValue::U64(self.read_u64()?)),
            11 => Some(MetaValue::I64(self.read_i64()?)),
            12 => Some(MetaValue::F64(self.read_f64()?)),
            _ => None,
        }
    }

    const fn align(&mut self, alignment: usize) {
        let rem = self.pos % alignment;
        if rem != 0 {
            self.pos += alignment - rem;
        }
    }
}

// ─── GGUF file ──────────────────────────────────────────────────────────────

/// A parsed GGUF file. Holds metadata and raw data references for
/// zero-copy tensor access.
pub struct GgufFile<'a> {
    pub metadata: HashMap<String, MetaValue>,
    pub tensors: HashMap<String, TensorInfo>,
    tensor_data_start: usize,
    data: &'a [u8],
    _alignment: usize,
}

impl<'a> GgufFile<'a> {
    /// Parse a GGUF file from a byte slice (typically mmap'd).
    pub fn parse(data: &'a [u8]) -> Option<Self> {
        let mut r = Reader::new(data);

        let magic = r.read_u32()?;
        if magic != GGUF_MAGIC {
            return None;
        }

        let version = r.read_u32()?;
        if !(2..=3).contains(&version) {
            return None;
        }

        let tensor_count = r.read_u64()? as usize;
        let metadata_count = r.read_u64()? as usize;

        // Parse metadata
        let mut metadata = HashMap::with_capacity(metadata_count);
        for _ in 0..metadata_count {
            let key = r.read_string()?;
            let vtype = r.read_u32()?;
            let value = r.read_meta_value(vtype)?;
            metadata.insert(key, value);
        }

        // Parse tensor info
        let mut tensors = HashMap::with_capacity(tensor_count);
        for _ in 0..tensor_count {
            let name = r.read_string()?;
            let n_dims = r.read_u32()?;
            let mut dims = Vec::with_capacity(n_dims as usize);
            for _ in 0..n_dims {
                dims.push(r.read_u64()?);
            }
            let qtype = GgmlType::from_u32(r.read_u32()?);
            let offset = r.read_u64()?;
            tensors.insert(
                name.clone(),
                TensorInfo {
                    name,
                    n_dims,
                    dims,
                    qtype,
                    offset,
                },
            );
        }

        let alignment = metadata
            .get("general.alignment")
            .and_then(MetaValue::as_u32)
            .unwrap_or(GGUF_DEFAULT_ALIGNMENT as u32) as usize;

        // Align to data section
        r.align(alignment);
        let tensor_data_start = r.pos;

        Some(Self {
            metadata,
            tensors,
            tensor_data_start,
            data,
            _alignment: alignment,
        })
    }

    /// Get metadata value by key.
    #[must_use]
    pub fn meta(&self, key: &str) -> Option<&MetaValue> {
        self.metadata.get(key)
    }

    /// Get metadata string.
    #[must_use]
    pub fn meta_str(&self, key: &str) -> Option<&str> {
        self.meta(key)?.as_str()
    }

    /// Get metadata u32.
    #[must_use]
    pub fn meta_u32(&self, key: &str) -> Option<u32> {
        self.meta(key)?.as_u32()
    }

    /// Get metadata f32.
    #[must_use]
    pub fn meta_f32(&self, key: &str) -> Option<f32> {
        self.meta(key)?.as_f32()
    }

    /// Get metadata bool.
    #[must_use]
    pub fn meta_bool(&self, key: &str) -> Option<bool> {
        self.meta(key)?.as_bool()
    }

    /// Byte offset of a tensor's data section within the underlying file.
    ///
    /// Absolute offset = `tensor_data_start + info.offset`, matching what
    /// `tensor_data` would slice into. Exposed so the DeepSeek-V3 Phase 4
    /// streaming pool can locate expert slabs inside its own independent
    /// `Mmap` of the same file without re-borrowing this instance's slice.
    #[must_use]
    pub fn tensor_absolute_offset(&self, name: &str) -> Option<u64> {
        let info = self.tensors.get(name)?;
        Some(self.tensor_data_start as u64 + info.offset)
    }

    /// Byte length of a tensor's raw data (post-quant), matching what
    /// `tensor_data` would return. Convenient for streaming callers that
    /// need the size without also materialising a slice reference.
    #[must_use]
    pub fn tensor_byte_size(&self, name: &str) -> Option<usize> {
        Some(self.tensors.get(name)?.data_size())
    }

    /// Get raw quantized data for a tensor.
    #[must_use]
    pub fn tensor_data(&self, name: &str) -> Option<&'a [u8]> {
        let info = self.tensors.get(name)?;
        let start = self.tensor_data_start + info.offset as usize;
        let size = info.data_size();
        if start + size > self.data.len() {
            return None;
        }
        Some(&self.data[start..start + size])
    }

    /// Get tensor info by name.
    #[must_use]
    pub fn tensor_info(&self, name: &str) -> Option<&TensorInfo> {
        self.tensors.get(name)
    }

    /// Dequantize a tensor to f32 (allocates). Use fused matvec for inference.
    pub fn tensor_to_f32(&self, name: &str) -> Option<Vec<f32>> {
        let info = self.tensors.get(name)?;
        let data = self.tensor_data(name)?;
        let n_elements = info.n_elements();
        let mut out = vec![0.0f32; n_elements];

        match info.qtype {
            GgmlType::F32 => {
                for i in 0..n_elements {
                    let off = i * 4;
                    out[i] = f32::from_le_bytes([
                        data[off],
                        data[off + 1],
                        data[off + 2],
                        data[off + 3],
                    ]);
                }
            }
            GgmlType::F16 => {
                for i in 0..n_elements {
                    let off = i * 2;
                    out[i] = f16_to_f32(u16::from_le_bytes([data[off], data[off + 1]]));
                }
            }
            GgmlType::Q4_0 => dequantize_q4_0(data, &mut out),
            GgmlType::Q4_1 => dequantize_q4_1(data, &mut out),
            GgmlType::Q5_0 => dequantize_q5_0(data, &mut out),
            GgmlType::Q5_1 => dequantize_q5_1(data, &mut out),
            GgmlType::IQ4_XS => dequantize_iq4_xs(data, &mut out),
            GgmlType::Q8_0 => dequantize_q8_0(data, &mut out),
            GgmlType::Q2_K => dequantize_q2_k(data, &mut out),
            GgmlType::Q3_K => dequantize_q3_k(data, &mut out),
            GgmlType::Q4_K => dequantize_q4_k(data, &mut out),
            GgmlType::Q5_K => dequantize_q5_k(data, &mut out),
            GgmlType::Q6_K => dequantize_q6_k(data, &mut out),
            GgmlType::Q1_0 => dequantize_q1_0(data, &mut out),
            GgmlType::Q2_0 => dequantize_q2_0(data, &mut out),
            GgmlType::Mxfp4 => dequantize_row_mxfp4(data, &mut out),
            // Phase X.4.b.7: IQ1_S dequant via `iq1_s::dequantize_iq1_s`.
            GgmlType::IQ1_S => crate::iq1_s::dequantize_iq1_s(data, &mut out),
            GgmlType::Other(_) => return None,
        }

        Some(out)
    }
}

// ─── GGUF split multi-file support (Phase X.4.b.6) ─────────────────────────
//
// Real K3 GGUF is distributed as 94 shards (`Kimi-K3-IQ1_S-00001-of-00094.gguf`
// through `...-00094-of-00094.gguf`). Each shard is a self-contained mini
// GGUF: shard 0 has full model-level metadata + a subset of tensor infos,
// shards 1..N have minimal metadata (`split.count`, `split.no`,
// `split.tensors.count`) + their own subset of tensor infos + tensor data.
// The union of all shards' tensors = the full model.
//
// This wrapper virtualises across N `GgufFile`s so downstream loaders
// (`load_kimi_k3_model_weights` etc.) can query `tensor_data(name)` /
// `tensor_info(name)` / `meta_u32(key)` uniformly regardless of whether
// the model comes from a single file or 94 shards.

/// Multi-shard GGUF reader — wraps N `GgufFile`s + provides tensor-name
/// routing to the correct shard.
///
/// Model-level metadata is read from shard 0 (llama.cpp convention:
/// shard 0 has full metadata, shards 1..N have only `split.*` keys).
pub struct GgufMultiFile<'a> {
    shards: Vec<GgufFile<'a>>,
    /// `tensor_name` → index into `shards` of the shard that owns it.
    tensor_shard_idx: HashMap<String, usize>,
}

impl<'a> GgufMultiFile<'a> {
    /// Construct from `Vec<&[u8]>` of shard file bytes (typically mmap'd).
    /// Shards must be passed in order (`.gguf` split index 1, 2, ..., N).
    /// Returns `None` if any shard fails to parse.
    pub fn parse_shards(shard_bytes: Vec<&'a [u8]>) -> Option<Self> {
        if shard_bytes.is_empty() {
            return None;
        }
        let mut shards = Vec::with_capacity(shard_bytes.len());
        for bytes in shard_bytes {
            shards.push(GgufFile::parse(bytes)?);
        }
        let mut tensor_shard_idx: HashMap<String, usize> = HashMap::new();
        for (idx, shard) in shards.iter().enumerate() {
            for name in shard.tensors.keys() {
                // First shard wins on duplicate names (should not happen
                // in a well-formed split GGUF, but guards against corrupt
                // data). Duplicates are ignored silently.
                tensor_shard_idx.entry(name.clone()).or_insert(idx);
            }
        }
        Some(Self {
            shards,
            tensor_shard_idx,
        })
    }

    /// Number of shards this multi-file spans.
    #[must_use]
    pub fn shard_count(&self) -> usize {
        self.shards.len()
    }

    /// Total number of unique tensors across all shards.
    #[must_use]
    pub fn tensor_count(&self) -> usize {
        self.tensor_shard_idx.len()
    }

    // Model-level metadata delegates to shard 0.
    #[must_use]
    pub fn meta(&self, key: &str) -> Option<&MetaValue> {
        self.shards.first()?.meta(key)
    }
    #[must_use]
    pub fn meta_str(&self, key: &str) -> Option<&str> {
        self.meta(key)?.as_str()
    }
    #[must_use]
    pub fn meta_u32(&self, key: &str) -> Option<u32> {
        self.meta(key)?.as_u32()
    }
    #[must_use]
    pub fn meta_f32(&self, key: &str) -> Option<f32> {
        self.meta(key)?.as_f32()
    }
    #[must_use]
    pub fn meta_bool(&self, key: &str) -> Option<bool> {
        self.meta(key)?.as_bool()
    }

    /// Look up a tensor's raw byte data, routing to whichever shard owns it.
    #[must_use]
    pub fn tensor_data(&self, name: &str) -> Option<&'a [u8]> {
        let idx = *self.tensor_shard_idx.get(name)?;
        self.shards.get(idx)?.tensor_data(name)
    }

    /// Look up a tensor's info (shape / qtype / offset), routing to shard.
    #[must_use]
    pub fn tensor_info(&self, name: &str) -> Option<&TensorInfo> {
        let idx = *self.tensor_shard_idx.get(name)?;
        self.shards.get(idx)?.tensor_info(name)
    }

    /// Dequantize a tensor to f32 (allocates). Same semantics as
    /// [`GgufFile::tensor_to_f32`], just routed to the correct shard.
    pub fn tensor_to_f32(&self, name: &str) -> Option<Vec<f32>> {
        let idx = *self.tensor_shard_idx.get(name)?;
        self.shards.get(idx)?.tensor_to_f32(name)
    }
}

/// Small accessor trait so downstream loaders (K3 etc.) can work with
/// either `GgufFile` (single file) or `GgufMultiFile` (94 shards)
/// uniformly. Only the subset of methods actually used by the K3 loader.
pub trait GgufSource<'a> {
    fn meta(&self, key: &str) -> Option<&MetaValue>;
    fn meta_str(&self, key: &str) -> Option<&str> {
        self.meta(key)?.as_str()
    }
    fn meta_u32(&self, key: &str) -> Option<u32> {
        self.meta(key)?.as_u32()
    }
    fn meta_f32(&self, key: &str) -> Option<f32> {
        self.meta(key)?.as_f32()
    }
    fn meta_bool(&self, key: &str) -> Option<bool> {
        self.meta(key)?.as_bool()
    }
    fn tensor_data(&self, name: &str) -> Option<&'a [u8]>;
    fn tensor_info(&self, name: &str) -> Option<&TensorInfo>;
    fn tensor_to_f32(&self, name: &str) -> Option<Vec<f32>>;
}

impl<'a> GgufSource<'a> for GgufFile<'a> {
    fn meta(&self, key: &str) -> Option<&MetaValue> {
        self.metadata.get(key)
    }
    fn tensor_data(&self, name: &str) -> Option<&'a [u8]> {
        self.tensor_data(name)
    }
    fn tensor_info(&self, name: &str) -> Option<&TensorInfo> {
        self.tensor_info(name)
    }
    fn tensor_to_f32(&self, name: &str) -> Option<Vec<f32>> {
        self.tensor_to_f32(name)
    }
}

impl<'a> GgufSource<'a> for GgufMultiFile<'a> {
    fn meta(&self, key: &str) -> Option<&MetaValue> {
        self.meta(key)
    }
    fn tensor_data(&self, name: &str) -> Option<&'a [u8]> {
        self.tensor_data(name)
    }
    fn tensor_info(&self, name: &str) -> Option<&TensorInfo> {
        self.tensor_info(name)
    }
    fn tensor_to_f32(&self, name: &str) -> Option<Vec<f32>> {
        self.tensor_to_f32(name)
    }
}

#[cfg(test)]
mod split_tests {
    use super::*;

    #[test]
    fn multi_file_rejects_empty_shard_list() {
        assert!(GgufMultiFile::parse_shards(vec![]).is_none());
    }
}

// ─── Q4_K dequantization ────────────────────────────────────────────────────

#[inline]
fn get_scale_min_k4(j: usize, scales: &[u8]) -> (u8, u8) {
    if j < 4 {
        (scales[j] & 63, scales[j + 4] & 63)
    } else {
        let sc = (scales[j + 4] & 0xF) | ((scales[j - 4] >> 6) << 4);
        let m = (scales[j + 4] >> 4) | ((scales[j] >> 6) << 4);
        (sc, m)
    }
}

// ─── Q2_K dequantization ────────────────────────────────────────────────────
// Block layout (84 bytes per 256 elements):
//   scales[16] — 4-bit scale (low nibble) + 4-bit min (high nibble)
//   qs[64]     — 2-bit weights, 4 per byte
//   d (f16)    — super-block scale
//   dmin (f16) — super-block min

fn dequantize_q2_k(data: &[u8], out: &mut [f32]) {
    let block_bytes = 84;
    let n_blocks = data.len() / block_bytes;
    let mut out_idx = 0;

    for i in 0..n_blocks {
        let block = &data[i * block_bytes..(i + 1) * block_bytes];
        let scales = &block[0..16];
        let qs = &block[16..80];
        let d = f16_to_f32(u16::from_le_bytes([block[80], block[81]]));
        let dmin = f16_to_f32(u16::from_le_bytes([block[82], block[83]]));

        for group in 0..16 {
            let sc = (scales[group] & 0xF) as f32;
            let m = (scales[group] >> 4) as f32;
            for j in 0..16 {
                let flat = group * 16 + j;
                let byte_idx = flat / 4;
                let bit_shift = (flat % 4) * 2;
                let q = ((qs[byte_idx] >> bit_shift) & 3) as f32;
                out[out_idx + flat] = d * sc * q - dmin * m;
            }
        }
        out_idx += QK_K;
    }
}

// ─── Q3_K dequantization ────────────────────────────────────────────────────
// Block layout (110 bytes per 256 elements):
//   hmask[32]  — high bit per weight (bit n of hmask[l] → element at group n*32+l)
//   qs[64]     — low 2 bits packed (4 groups of 32 per 128-element half)
//   scales[12] — 16 six-bit scales packed (value - 32)
//   d (f16)    — super-block scale

/// Decode 16 six-bit scales from Q3_K's 12-byte packed format.
fn q3k_decode_scales(sc: &[u8]) -> [i32; 16] {
    let mut scales = [0i32; 16];
    for j in 0..4 {
        scales[j] = (sc[j] & 0xF) as i32;
        scales[j + 4] = (sc[j] >> 4) as i32;
        scales[j + 8] = (sc[j + 4] & 0xF) as i32;
        scales[j + 12] = (sc[j + 4] >> 4) as i32;
    }
    for j in 0..4 {
        let hi = sc[8 + j];
        scales[j] |= ((hi & 3) as i32) << 4;
        scales[j + 4] |= (((hi >> 2) & 3) as i32) << 4;
        scales[j + 8] |= (((hi >> 4) & 3) as i32) << 4;
        scales[j + 12] |= (((hi >> 6) & 3) as i32) << 4;
    }
    for s in &mut scales {
        *s -= 32;
    }
    scales
}

fn dequantize_q3_k(data: &[u8], out: &mut [f32]) {
    let block_bytes = 110;
    let n_blocks = data.len() / block_bytes;
    let mut out_idx = 0;

    for i in 0..n_blocks {
        let block = &data[i * block_bytes..(i + 1) * block_bytes];
        let hmask = &block[0..32];
        let qs = &block[32..96];
        let d = f16_to_f32(u16::from_le_bytes([block[108], block[109]]));
        let scales = q3k_decode_scales(&block[96..108]);

        // Unpack 3-bit weights: low 2 bits from qs, high bit from hmask
        // hmask[l] bit n → element at group_n * 32 + l
        // qs layout: 32 bytes per 128-element half, bits (shift*2)..(shift*2+1) for sub-group
        let mut m: u8 = 1;
        let mut q_off = 0usize;
        for j in (0..QK_K).step_by(128) {
            for shift in 0..4u8 {
                for l in 0..32usize {
                    let lo2 = (qs[q_off + l] >> (shift * 2)) & 3;
                    // hmask bit set → high bit present → value = lo2 (range 0..3)
                    // hmask bit clear → no high bit → value = lo2 - 4 (range -4..-1)
                    let hi = if hmask[l] & m != 0 { 0i8 } else { -4i8 };
                    let flat = j + shift as usize * 32 + l;
                    let is = flat / 16;
                    out[out_idx + flat] = d * scales[is] as f32 * (lo2 as i8 + hi) as f32;
                }
                m <<= 1;
            }
            q_off += 32;
        }
        out_idx += QK_K;
    }
}

fn dequantize_q4_k(data: &[u8], out: &mut [f32]) {
    let block_bytes = 144;
    let n_blocks = data.len() / block_bytes;
    let mut out_idx = 0;

    for i in 0..n_blocks {
        let block = &data[i * block_bytes..(i + 1) * block_bytes];
        let d = f16_to_f32(u16::from_le_bytes([block[0], block[1]]));
        let dmin = f16_to_f32(u16::from_le_bytes([block[2], block[3]]));
        let scales = &block[4..16];
        let qs = &block[16..144];

        let mut is = 0usize;
        let mut q_offset = 0usize;

        for _ in 0..4 {
            let (sc1, m1) = get_scale_min_k4(is, scales);
            let (sc2, m2) = get_scale_min_k4(is + 1, scales);
            let d1 = d * f32::from(sc1);
            let m1f = dmin * f32::from(m1);
            let d2 = d * f32::from(sc2);
            let m2f = dmin * f32::from(m2);

            for l in 0..32 {
                out[out_idx] = d1 * f32::from(qs[q_offset + l] & 0xF) - m1f;
                out_idx += 1;
            }
            for l in 0..32 {
                out[out_idx] = d2 * f32::from(qs[q_offset + l] >> 4) - m2f;
                out_idx += 1;
            }

            q_offset += 32;
            is += 2;
        }
    }
}

// ─── Q5_K dequantization ────────────────────────────────────────────────────
// Block layout (176 bytes per 256 elements):
//   d (f16)      — super-block scale
//   dmin (f16)   — super-block min
//   scales[12]   — same packing as Q4_K (8 scales + 8 mins, 6-bit each)
//   qh[32]       — high bits (bit 4 for each weight)
//   qs[128]      — low 4 bits

fn dequantize_q5_k(data: &[u8], out: &mut [f32]) {
    let block_bytes = 176;
    let n_blocks = data.len() / block_bytes;
    let mut out_idx = 0;

    for i in 0..n_blocks {
        let block = &data[i * block_bytes..(i + 1) * block_bytes];
        let d = f16_to_f32(u16::from_le_bytes([block[0], block[1]]));
        let dmin = f16_to_f32(u16::from_le_bytes([block[2], block[3]]));
        let scales_raw = &block[4..16];
        let qh = &block[16..48];
        let qs = &block[48..176];

        let mut is = 0usize;
        let mut q_offset = 0usize;

        for im in 0..4u8 {
            let (sc1, m1) = get_scale_min_k4(is, scales_raw);
            let (sc2, m2) = get_scale_min_k4(is + 1, scales_raw);
            let d1 = d * f32::from(sc1);
            let m1f = dmin * f32::from(m1);
            let d2 = d * f32::from(sc2);
            let m2f = dmin * f32::from(m2);

            for l in 0..32 {
                let hbit = (qh[l] >> (im * 2)) & 1;
                out[out_idx] = d1 * f32::from((qs[q_offset + l] & 0xF) | (hbit << 4)) - m1f;
                out_idx += 1;
            }
            for l in 0..32 {
                let hbit = (qh[l] >> (im * 2 + 1)) & 1;
                out[out_idx] = d2 * f32::from((qs[q_offset + l] >> 4) | (hbit << 4)) - m2f;
                out_idx += 1;
            }

            q_offset += 32;
            is += 2;
        }
    }
}

fn dequantize_q8_0(data: &[u8], out: &mut [f32]) {
    let block_bytes = 34;
    let n_blocks = data.len() / block_bytes;
    let mut out_idx = 0;

    for i in 0..n_blocks {
        let off = i * block_bytes;
        let d = f16_to_f32(u16::from_le_bytes([data[off], data[off + 1]]));

        for l in 0..QK8_0 {
            out[out_idx] = d * f32::from(data[off + 2 + l] as i8);
            out_idx += 1;
        }
    }
}

// ─── Q5_1 dequantization ────────────────────────────────────────────────────
//
// Block layout (24 bytes / 32 elements):
//   d:  f16 delta          (offset 0..2)
//   m:  f16 min            (offset 2..4)
//   qh: u32 high-bits mask (offset 4..8, little-endian)
//   qs: [u8; 16] nibbles   (offset 8..24)
//
// Dequant per llama.cpp `dequantize_row_q5_1`:
//   for j in 0..16:
//     xh_0 = ((qh >>  j     ) << 4) & 0x10   // high bit for element j
//     xh_1 = ((qh >> (j+12)))       & 0x10   // high bit for element j+16
//     x0   = (qs[j] & 0x0F) | xh_0
//     x1   = (qs[j] >>   4) | xh_1
//     y[j]      = x0 * d + m
//     y[j + 16] = x1 * d + m
pub(crate) fn dequantize_q5_1(data: &[u8], out: &mut [f32]) {
    let block_bytes = 24;
    let n_blocks = data.len() / block_bytes;
    let mut out_idx = 0;

    for i in 0..n_blocks {
        let off = i * block_bytes;
        let d = f16_to_f32(u16::from_le_bytes([data[off], data[off + 1]]));
        let m = f16_to_f32(u16::from_le_bytes([data[off + 2], data[off + 3]]));
        let qh = u32::from_le_bytes([data[off + 4], data[off + 5], data[off + 6], data[off + 7]]);
        for j in 0..16 {
            let qs = data[off + 8 + j];
            let xh_0 = ((qh >> j) << 4) & 0x10;
            let xh_1 = (qh >> (j + 12)) & 0x10;
            let x0 = ((qs & 0x0F) as u32) | xh_0;
            let x1 = ((qs >> 4) as u32) | xh_1;
            out[out_idx + j] = (x0 as f32) * d + m;
            out[out_idx + j + 16] = (x1 as f32) * d + m;
        }
        out_idx += QK8_0;
    }
}

// ─── Q4_0 dequantization ────────────────────────────────────────────────────
//
// Block layout (18 bytes / 32 elements):
//   d:  f16 delta          (offset 0..2)
//   qs: [u8; 16] nibbles   (offset 2..18)
//
// Dequant per llama.cpp `dequantize_row_q4_0`:
//   for j in 0..16:
//     x0 = (qs[j] & 0x0F) - 8   (signed 4-bit)
//     x1 = (qs[j] >>   4) - 8
//     y[j]      = d * x0
//     y[j + 16] = d * x1
pub(crate) fn dequantize_q4_0(data: &[u8], out: &mut [f32]) {
    let block_bytes = 18;
    let n_blocks = data.len() / block_bytes;
    let mut out_idx = 0;

    for i in 0..n_blocks {
        let off = i * block_bytes;
        let d = f16_to_f32(u16::from_le_bytes([data[off], data[off + 1]]));
        for j in 0..16 {
            let qs = data[off + 2 + j];
            let x0 = ((qs & 0x0F) as i32) - 8;
            let x1 = ((qs >> 4) as i32) - 8;
            out[out_idx + j] = (x0 as f32) * d;
            out[out_idx + j + 16] = (x1 as f32) * d;
        }
        out_idx += QK8_0;
    }
}

// ─── Q4_1 dequantization ────────────────────────────────────────────────────
//
// Block layout (20 bytes / 32 elements):
//   d:  f16 delta          (offset 0..2)
//   m:  f16 min            (offset 2..4)
//   qs: [u8; 16] nibbles   (offset 4..20)
//
// Dequant per llama.cpp `dequantize_row_q4_1`:
//   for j in 0..16:
//     y[j]      = d * (qs[j] & 0x0F) + m
//     y[j + 16] = d * (qs[j] >> 4)   + m
pub(crate) fn dequantize_q4_1(data: &[u8], out: &mut [f32]) {
    let block_bytes = 20;
    let n_blocks = data.len() / block_bytes;
    let mut out_idx = 0;

    for i in 0..n_blocks {
        let off = i * block_bytes;
        let d = f16_to_f32(u16::from_le_bytes([data[off], data[off + 1]]));
        let m = f16_to_f32(u16::from_le_bytes([data[off + 2], data[off + 3]]));
        for j in 0..16 {
            let qs = data[off + 4 + j];
            let x0 = (qs & 0x0F) as f32;
            let x1 = (qs >> 4) as f32;
            out[out_idx + j] = x0 * d + m;
            out[out_idx + j + 16] = x1 * d + m;
        }
        out_idx += QK8_0;
    }
}

// ─── Q5_0 dequantization ────────────────────────────────────────────────────
//
// Block layout (22 bytes / 32 elements):
//   d:  f16 delta          (offset 0..2)
//   qh: u32 high-bits mask (offset 2..6, little-endian)
//   qs: [u8; 16] nibbles   (offset 6..22)
//
// Dequant per llama.cpp `dequantize_row_q5_0`:
//   for j in 0..16:
//     xh_0 = ((qh >>  j     ) << 4) & 0x10
//     xh_1 = ((qh >> (j+12)))       & 0x10
//     x0   = ((qs[j] & 0x0F) | xh_0) - 16   (signed 5-bit)
//     x1   = ((qs[j] >>   4) | xh_1) - 16
//     y[j]      = d * x0
//     y[j + 16] = d * x1
pub(crate) fn dequantize_q5_0(data: &[u8], out: &mut [f32]) {
    let block_bytes = 22;
    let n_blocks = data.len() / block_bytes;
    let mut out_idx = 0;

    for i in 0..n_blocks {
        let off = i * block_bytes;
        let d = f16_to_f32(u16::from_le_bytes([data[off], data[off + 1]]));
        let qh = u32::from_le_bytes([data[off + 2], data[off + 3], data[off + 4], data[off + 5]]);
        for j in 0..16 {
            let qs = data[off + 6 + j];
            let xh_0 = ((qh >> j) << 4) & 0x10;
            let xh_1 = (qh >> (j + 12)) & 0x10;
            let x0 = (((qs & 0x0F) as u32) | xh_0) as i32 - 16;
            let x1 = (((qs >> 4) as u32) | xh_1) as i32 - 16;
            out[out_idx + j] = (x0 as f32) * d;
            out[out_idx + j + 16] = (x1 as f32) * d;
        }
        out_idx += QK8_0;
    }
}

// ─── IQ4_XS dequantization ──────────────────────────────────────────────────
//
// IQ4_XS is a 4-bit importance-weighted quantization with 6-bit sub-block
// scales, commonly produced by unsloth/bartowski for smaller-than-Q4_K models.
//
// Block layout (136 bytes / QK_K=256 elements = 8 sub-blocks × 32 elements):
//   d:        f16                (offset 0..2)
//   scales_h: u16 little-endian  (offset 2..4)
//   scales_l: [u8; 4]            (offset 4..8)
//   qs:       [u8; 128] nibbles  (offset 8..136)
//
// Dequant per llama.cpp `dequantize_row_iq4_xs`:
//   for ib in 0..8:
//     ls  = ((scales_l[ib/2] >> (4*(ib%2))) & 0xF) | (((scales_h >> (2*ib)) & 3) << 4)
//     dl  = d * (ls - 32)         (6-bit scale offset by -32)
//     for j in 0..16:
//       y[ib*32 + j]      = dl * KVALUES_IQ4NL[qs[ib*16 + j] & 0xF]
//       y[ib*32 + j + 16] = dl * KVALUES_IQ4NL[qs[ib*16 + j] >> 4]
const KVALUES_IQ4NL: [i8; 16] = [
    -127, -104, -83, -65, -49, -35, -22, -10, 1, 13, 25, 38, 53, 69, 89, 113,
];

pub(crate) fn dequantize_iq4_xs(data: &[u8], out: &mut [f32]) {
    let block_bytes = 136;
    let n_blocks = data.len() / block_bytes;
    let mut out_idx = 0;

    for i in 0..n_blocks {
        let off = i * block_bytes;
        let d = f16_to_f32(u16::from_le_bytes([data[off], data[off + 1]]));
        let scales_h = u16::from_le_bytes([data[off + 2], data[off + 3]]);
        let scales_l = [data[off + 4], data[off + 5], data[off + 6], data[off + 7]];
        let qs = &data[off + 8..off + 8 + 128];

        for ib in 0..8 {
            let ls_lo = (scales_l[ib / 2] >> (4 * (ib as u8 & 1))) & 0x0F;
            let ls_hi = ((scales_h >> (2 * ib)) & 0x3) as u8;
            let ls = ((ls_hi << 4) | ls_lo) as i32;
            let dl = d * ((ls - 32) as f32);
            let qs_base = ib * 16;
            let out_base = out_idx + ib * 32;
            for j in 0..16 {
                let byte = qs[qs_base + j];
                let n0 = (byte & 0x0F) as usize;
                let n1 = (byte >> 4) as usize;
                out[out_base + j] = dl * f32::from(KVALUES_IQ4NL[n0]);
                out[out_base + j + 16] = dl * f32::from(KVALUES_IQ4NL[n1]);
            }
        }
        out_idx += QK_K;
    }
}

// ─── Q6_K dequantization ────────────────────────────────────────────────────

/// Public wrapper for external slice-based callers (Gemma 4 per-layer input
/// embedding lookup).
pub(crate) fn dequantize_q6_k_public(data: &[u8], out: &mut [f32]) {
    dequantize_q6_k(data, out);
}

fn dequantize_q6_k(data: &[u8], out: &mut [f32]) {
    let block_bytes = 210;
    let n_blocks = data.len() / block_bytes;
    let mut out_idx = 0;

    for i in 0..n_blocks {
        let block = &data[i * block_bytes..(i + 1) * block_bytes];
        // Layout: ql[128] + qh[64] + scales[16] + d[2]
        let ql = &block[0..128];
        let qh = &block[128..192];
        let scales = &block[192..208];
        let d = f16_to_f32(u16::from_le_bytes([block[208], block[209]]));

        let mut ql_off = 0usize;
        let mut qh_off = 0usize;
        let mut sc_off = 0usize;

        for _n in (0..QK_K).step_by(128) {
            for l in 0..32 {
                let is = l / 16; // matches llama.cpp: is = l/16
                let q1 = ((ql[ql_off + l] & 0xF) | ((qh[qh_off + l] & 3) << 4)) as i8 - 32;
                let q2 =
                    ((ql[ql_off + l + 32] & 0xF) | (((qh[qh_off + l] >> 2) & 3) << 4)) as i8 - 32;
                let q3 = ((ql[ql_off + l] >> 4) | (((qh[qh_off + l] >> 4) & 3) << 4)) as i8 - 32;
                let q4 =
                    ((ql[ql_off + l + 32] >> 4) | (((qh[qh_off + l] >> 6) & 3) << 4)) as i8 - 32;

                out[out_idx + l] = d * f32::from(scales[sc_off + is] as i8) * f32::from(q1);
                out[out_idx + l + 32] =
                    d * f32::from(scales[sc_off + is + 2] as i8) * f32::from(q2);
                out[out_idx + l + 64] =
                    d * f32::from(scales[sc_off + is + 4] as i8) * f32::from(q3);
                out[out_idx + l + 96] =
                    d * f32::from(scales[sc_off + is + 6] as i8) * f32::from(q4);
            }
            out_idx += 128;
            ql_off += 64;
            qh_off += 32;
            sc_off += 8;
        }
    }
}

// ─── Q1_0 / Q2_0 dequantization (PrismML Bonsai 27B, group-128) ─────────────
//
// Q1_0: 18 bytes/block, 128 elements. Layout:
//   [0..2]   FP16 scale `d`
//   [2..18]  16 bytes of packed 1-bit ternary values (binary {−1, +1})
//            Bit `k` in byte `b` corresponds to element `b * 8 + k`
//            (LSB-first within each byte, matching llama.cpp's block_q1_0).
//   Value = ((bit == 1) ? +1 : −1) * d
//
// Q2_0: 34 bytes/block, 128 elements. Layout:
//   [0..2]   FP16 scale `d`
//   [2..34]  32 bytes of packed 2-bit ternary values ({−1, 0, +1})
//            Slot `k` in byte `b` (k=0..3) corresponds to element `b * 4 + k`
//            Standard ternary encoding: `((slot & 3) - 1) * d` yields
//            {−1, 0, +1, +2}, with the +2 code reserved / unused by the encoder.

fn dequantize_q1_0(data: &[u8], out: &mut [f32]) {
    let block_bytes = 18;
    let n_blocks = data.len() / block_bytes;
    let mut out_idx = 0;
    for i in 0..n_blocks {
        let block = &data[i * block_bytes..(i + 1) * block_bytes];
        let d = f16_to_f32(u16::from_le_bytes([block[0], block[1]]));
        for b in 0..16 {
            let byte = block[2 + b];
            for k in 0..8 {
                let bit = (byte >> k) & 1;
                let val = if bit == 1 { d } else { -d };
                out[out_idx] = val;
                out_idx += 1;
            }
        }
    }
}

pub(crate) fn dequantize_q2_0(data: &[u8], out: &mut [f32]) {
    let block_bytes = 34;
    let n_blocks = data.len() / block_bytes;
    let mut out_idx = 0;
    for i in 0..n_blocks {
        let block = &data[i * block_bytes..(i + 1) * block_bytes];
        let d = f16_to_f32(u16::from_le_bytes([block[0], block[1]]));
        for b in 0..32 {
            let byte = block[2 + b];
            for k in 0..4 {
                let slot = ((byte >> (k * 2)) & 0x3) as i8;
                let val = (slot - 1) as f32 * d;
                out[out_idx] = val;
                out_idx += 1;
            }
        }
    }
}

// ─── Q8_K quantization (matches llama.cpp) ──────────────────────────────────

/// Q8_K block: intermediate quantization format for input vectors.
/// Matches llama.cpp's `block_q8_K` exactly.
pub struct BlockQ8K {
    pub d: f32,           // scale factor (f32, not f16)
    pub qs: [i8; QK_K],   // 256 quantized values
    pub bsums: [i16; 16], // pre-computed sums of groups of 16
}

/// Round to nearest integer using the same algorithm as llama.cpp's `nearest_int`.
/// Uses IEEE 754 magic number trick for round-to-nearest-even.
#[inline]
fn nearest_int(fval: f32) -> i32 {
    let val = fval + 12_582_912.0f32; // 0x4B400000
    let i = val.to_bits() as i32;
    (i & 0x007f_ffff) - 0x0040_0000
}

/// Quantize a row of f32 values to Q8_K blocks.
/// Matches llama.cpp's `quantize_row_q8_K_ref`.
pub fn quantize_row_q8_k(input: &[f32]) -> Vec<BlockQ8K> {
    let nb = input.len() / QK_K;
    let mut blocks = Vec::with_capacity(nb);

    for i in 0..nb {
        let x = &input[i * QK_K..(i + 1) * QK_K];

        // Find max absolute value
        let mut amax = 0.0f32;
        let mut max_val = 0.0f32;
        for &v in x {
            let av = v.abs();
            if av > amax {
                amax = av;
                max_val = v;
            }
        }

        let mut block = BlockQ8K {
            d: 0.0,
            qs: [0i8; QK_K],
            bsums: [0i16; 16],
        };

        if amax == 0.0 {
            blocks.push(block);
            continue;
        }

        // Match llama.cpp: iscale = 128 / max (signed max, not absolute)
        let iscale = 128.0f32 / max_val;

        for j in 0..QK_K {
            let v = nearest_int(iscale * x[j]);
            block.qs[j] = v.max(-128).min(127) as i8;
        }

        // Pre-compute group-of-16 sums
        for j in 0..16 {
            let mut sum = 0i32;
            for ii in 0..16 {
                sum += block.qs[j * 16 + ii] as i32;
            }
            block.bsums[j] = sum as i16;
        }

        block.d = 1.0 / iscale;
        blocks.push(block);
    }

    blocks
}

// ─── Fused quantized matvec (llama.cpp compatible) ──────────────────────────

// ─── NEON SIMD dot products (aarch64 / Apple Silicon) ───────────────────────

#[cfg(target_arch = "aarch64")]
#[allow(dead_code)]
mod neon_dot {
    use super::*;
    use std::arch::aarch64::*;

    /// Horizontal sum of int32x4_t → i32 (stable intrinsics only).
    #[inline]
    #[target_feature(enable = "neon")]
    unsafe fn hsum_s32(v: int32x4_t) -> i32 {
        let low = vget_low_s32(v);
        let high = vget_high_s32(v);
        let sum2 = vadd_s32(low, high);
        let pair = vpadd_s32(sum2, sum2);
        vget_lane_s32::<0>(pair)
    }

    /// NEON dot product of 32 i8 values using vmull_s8 (stable).
    #[inline]
    #[target_feature(enable = "neon")]
    unsafe fn dot_32(a: *const i8, b: *const i8) -> i32 {
        let a0 = vld1q_s8(a);
        let a1 = vld1q_s8(a.add(16));
        let b0 = vld1q_s8(b);
        let b1 = vld1q_s8(b.add(16));

        // 4 widening multiplies: 8×i8×i8 → 8×i16 each
        let p0 = vmull_s8(vget_low_s8(a0), vget_low_s8(b0));
        let p1 = vmull_s8(vget_high_s8(a0), vget_high_s8(b0));
        let p2 = vmull_s8(vget_low_s8(a1), vget_low_s8(b1));
        let p3 = vmull_s8(vget_high_s8(a1), vget_high_s8(b1));

        // Pairwise add i16 → i32
        let s01 = vaddq_s32(vpaddlq_s16(p0), vpaddlq_s16(p1));
        let s23 = vaddq_s32(vpaddlq_s16(p2), vpaddlq_s16(p3));
        hsum_s32(vaddq_s32(s01, s23))
    }

    /// NEON dot product of 16 i8 values.
    #[inline]
    #[target_feature(enable = "neon")]
    unsafe fn dot_16(a: *const i8, b: *const i8) -> i32 {
        let a0 = vld1q_s8(a);
        let b0 = vld1q_s8(b);

        let p0 = vmull_s8(vget_low_s8(a0), vget_low_s8(b0));
        let p1 = vmull_s8(vget_high_s8(a0), vget_high_s8(b0));

        hsum_s32(vaddq_s32(vpaddlq_s16(p0), vpaddlq_s16(p1)))
    }

    /// Q4_K × Q8_K dot product using NEON.
    #[target_feature(enable = "neon")]
    pub unsafe fn q4k_q8k_dot(q4k_block: &[u8], q8k: &BlockQ8K) -> f32 {
        let d = f16_to_f32(u16::from_le_bytes([q4k_block[0], q4k_block[1]]));
        let dmin = f16_to_f32(u16::from_le_bytes([q4k_block[2], q4k_block[3]]));
        let q4 = &q4k_block[16..144];

        let (scales, mins) = unpack_scales_mins_q4k(&q4k_block[4..16]);

        // Mins correction using bsums
        let mut sumi = 0i32;
        for j in 0..16 {
            sumi += q8k.bsums[j] as i32 * mins[j / 2] as i32;
        }

        // Unpack nibbles and dot product with NEON
        let mask_lo = vdupq_n_u8(0x0F);
        let mut total = 0i32;
        let q8 = &q8k.qs;

        // Process 4 groups of 32 q4 bytes → 8 sub-blocks of 32 elements
        // But for NEON dot, we unpack to aux8 first, then use dot_32
        let mut aux8 = [0i8; QK_K];
        let mut a_off = 0usize;
        let mut q4_off = 0usize;

        // Unpack using NEON: extract low/high nibbles
        for _ in 0..4 {
            let q4_0 = vld1q_u8(q4.as_ptr().add(q4_off));
            let q4_1 = vld1q_u8(q4.as_ptr().add(q4_off + 16));

            let lo_0 = vreinterpretq_s8_u8(vandq_u8(q4_0, mask_lo));
            let lo_1 = vreinterpretq_s8_u8(vandq_u8(q4_1, mask_lo));
            let hi_0 = vreinterpretq_s8_u8(vshrq_n_u8::<4>(q4_0));
            let hi_1 = vreinterpretq_s8_u8(vshrq_n_u8::<4>(q4_1));

            vst1q_s8(aux8.as_mut_ptr().add(a_off), lo_0);
            vst1q_s8(aux8.as_mut_ptr().add(a_off + 16), lo_1);
            vst1q_s8(aux8.as_mut_ptr().add(a_off + 32), hi_0);
            vst1q_s8(aux8.as_mut_ptr().add(a_off + 48), hi_1);

            a_off += 64;
            q4_off += 32;
        }

        // 8 sub-blocks of 32 elements each
        for is in 0..8 {
            let off = is * 32;
            let dot = dot_32(aux8.as_ptr().add(off), q8.as_ptr().add(off));
            total += scales[is] as i32 * dot;
        }

        d * q8k.d * total as f32 - dmin * q8k.d * sumi as f32
    }

    /// Q6_K × Q8_K dot product using NEON.
    #[target_feature(enable = "neon")]
    pub unsafe fn q6k_q8k_dot(q6k_block: &[u8], q8k: &BlockQ8K) -> f32 {
        let ql = &q6k_block[0..128];
        let qh = &q6k_block[128..192];
        let scales = &q6k_block[192..208];
        let d = f16_to_f32(u16::from_le_bytes([q6k_block[208], q6k_block[209]]));

        // Reconstruct 6-bit signed quants using NEON.
        //
        // Q6_K packs 256 6-bit values into 128 ql bytes (4 low bits each,
        // two elements per byte) plus 64 qh bytes (2 high bits per element,
        // four elements per byte). Per iteration we unpack 128 elements from
        // 64 ql bytes + 32 qh bytes at a time, distributing into four output
        // quadrants of 32 elements each:
        //   Q0: aux8[  0.. 32] = (ql[ 0.. 32] & 0xF)   | ((qh << 4) & 0x30)
        //   Q1: aux8[ 32.. 64] = (ql[32.. 64] & 0xF)   | ((qh << 2) & 0x30)
        //   Q2: aux8[ 64.. 96] = (ql[ 0.. 32] >> 4)    | ( qh       & 0x30)
        //   Q3: aux8[ 96..128] = (ql[32.. 64] >> 4)    | ((qh >> 2) & 0x30)
        // Then subtract 32 to move the 6-bit unsigned value into the signed
        // range [-32, 31]. The shift-mask trick lets the same qh vector feed
        // all four quadrants with only add / and / or / shl / shr — no per-
        // element scalar packing. This replaces the previous scalar bit
        // packing loop (~14 µs of the old 89 µs NEON path on an arm64 laptop).
        let mut aux8 = [0i8; QK_K];
        let mask_nib = vdupq_n_u8(0x0F);
        let mask_hi = vdupq_n_u8(0x30);
        let bias = vdupq_n_s8(-32);
        let mut a_off = 0usize;
        let mut ql_off = 0usize;
        let mut qh_off = 0usize;
        for _ in 0..2 {
            let ql_a = vld1q_u8(ql.as_ptr().add(ql_off));
            let ql_b = vld1q_u8(ql.as_ptr().add(ql_off + 16));
            let ql_c = vld1q_u8(ql.as_ptr().add(ql_off + 32));
            let ql_d = vld1q_u8(ql.as_ptr().add(ql_off + 48));
            let qh_a = vld1q_u8(qh.as_ptr().add(qh_off));
            let qh_b = vld1q_u8(qh.as_ptr().add(qh_off + 16));

            // Quadrant 0: (ql[0..32] & 0xF) | ((qh << 4) & 0x30) - 32
            let hi_a_q0 = vandq_u8(vshlq_n_u8::<4>(qh_a), mask_hi);
            let hi_b_q0 = vandq_u8(vshlq_n_u8::<4>(qh_b), mask_hi);
            let out_0a = vaddq_s8(
                vreinterpretq_s8_u8(vorrq_u8(vandq_u8(ql_a, mask_nib), hi_a_q0)),
                bias,
            );
            let out_0b = vaddq_s8(
                vreinterpretq_s8_u8(vorrq_u8(vandq_u8(ql_b, mask_nib), hi_b_q0)),
                bias,
            );
            vst1q_s8(aux8.as_mut_ptr().add(a_off), out_0a);
            vst1q_s8(aux8.as_mut_ptr().add(a_off + 16), out_0b);

            // Quadrant 1: (ql[32..64] & 0xF) | ((qh << 2) & 0x30) - 32
            let hi_a_q1 = vandq_u8(vshlq_n_u8::<2>(qh_a), mask_hi);
            let hi_b_q1 = vandq_u8(vshlq_n_u8::<2>(qh_b), mask_hi);
            let out_1a = vaddq_s8(
                vreinterpretq_s8_u8(vorrq_u8(vandq_u8(ql_c, mask_nib), hi_a_q1)),
                bias,
            );
            let out_1b = vaddq_s8(
                vreinterpretq_s8_u8(vorrq_u8(vandq_u8(ql_d, mask_nib), hi_b_q1)),
                bias,
            );
            vst1q_s8(aux8.as_mut_ptr().add(a_off + 32), out_1a);
            vst1q_s8(aux8.as_mut_ptr().add(a_off + 48), out_1b);

            // Quadrant 2: (ql[0..32] >> 4) | (qh & 0x30) - 32
            let hi_a_q2 = vandq_u8(qh_a, mask_hi);
            let hi_b_q2 = vandq_u8(qh_b, mask_hi);
            let out_2a = vaddq_s8(
                vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8::<4>(ql_a), hi_a_q2)),
                bias,
            );
            let out_2b = vaddq_s8(
                vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8::<4>(ql_b), hi_b_q2)),
                bias,
            );
            vst1q_s8(aux8.as_mut_ptr().add(a_off + 64), out_2a);
            vst1q_s8(aux8.as_mut_ptr().add(a_off + 80), out_2b);

            // Quadrant 3: (ql[32..64] >> 4) | ((qh >> 2) & 0x30) - 32
            let hi_a_q3 = vandq_u8(vshrq_n_u8::<2>(qh_a), mask_hi);
            let hi_b_q3 = vandq_u8(vshrq_n_u8::<2>(qh_b), mask_hi);
            let out_3a = vaddq_s8(
                vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8::<4>(ql_c), hi_a_q3)),
                bias,
            );
            let out_3b = vaddq_s8(
                vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8::<4>(ql_d), hi_b_q3)),
                bias,
            );
            vst1q_s8(aux8.as_mut_ptr().add(a_off + 96), out_3a);
            vst1q_s8(aux8.as_mut_ptr().add(a_off + 112), out_3b);

            a_off += 128;
            ql_off += 64;
            qh_off += 32;
        }

        // NEON dot product: 16 sub-blocks of 16 elements each
        let q8 = &q8k.qs;
        let mut total = 0i32;
        for is in 0..16 {
            let scale = scales[is] as i8 as i32;
            let off = is * 16;
            let dot = dot_16(aux8.as_ptr().add(off), q8.as_ptr().add(off));
            total += scale * dot;
        }

        d * q8k.d * total as f32
    }

    /// Fused Q8_0 dequantize + f32 matvec for one row using NEON.
    ///
    /// Q8_0 stores 32 signed i8 weights per block plus a single f16 scale, and
    /// — unlike the K-series — the ALICE-LLM dispatch feeds it raw f32 inputs
    /// rather than a pre-quantised Q8_K block. That makes the SIMD path an f32
    /// FMA loop rather than an integer madd chain, mirroring the AVX2
    /// `avx2_dot::q8_0_matvec_row`.
    ///
    /// Per block of 32 elements we perform two 16-lane halves. Each half
    /// widens 16 i8 → i32 (two `vmovl` chains) → f32, multiplies by the block
    /// scale, and does four 4-lane FMAs against the input. The accumulator is
    /// horizontally summed at the end with `vaddvq_f32` (aarch64 baseline).
    #[target_feature(enable = "neon")]
    pub unsafe fn q8_0_matvec_row(input: &[f32], row_data: &[u8], cols: usize) -> f32 {
        let blocks_per_row = cols / QK8_0;
        let block_bytes = 34usize;
        let mut acc: float32x4_t = vdupq_n_f32(0.0);

        for bi in 0..blocks_per_row {
            let off = bi * block_bytes;
            let d = f16_to_f32(u16::from_le_bytes([row_data[off], row_data[off + 1]]));
            let d_bcast = vdupq_n_f32(d);
            let qs_ptr = row_data.as_ptr().add(off + 2).cast::<i8>();
            let col_base = bi * QK8_0;
            let in_ptr = input.as_ptr().add(col_base);

            // First half: bytes 0..15, inputs 0..15.
            let a0 = vld1q_s8(qs_ptr);
            let a0_lo = vmovl_s8(vget_low_s8(a0));
            let a0_hi = vmovl_s8(vget_high_s8(a0));
            let f0 = vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_low_s16(a0_lo))), d_bcast);
            let f1 = vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_high_s16(a0_lo))), d_bcast);
            let f2 = vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_low_s16(a0_hi))), d_bcast);
            let f3 = vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_high_s16(a0_hi))), d_bcast);
            acc = vfmaq_f32(acc, f0, vld1q_f32(in_ptr));
            acc = vfmaq_f32(acc, f1, vld1q_f32(in_ptr.add(4)));
            acc = vfmaq_f32(acc, f2, vld1q_f32(in_ptr.add(8)));
            acc = vfmaq_f32(acc, f3, vld1q_f32(in_ptr.add(12)));

            // Second half: bytes 16..31, inputs 16..31.
            let a1 = vld1q_s8(qs_ptr.add(16));
            let a1_lo = vmovl_s8(vget_low_s8(a1));
            let a1_hi = vmovl_s8(vget_high_s8(a1));
            let g0 = vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_low_s16(a1_lo))), d_bcast);
            let g1 = vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_high_s16(a1_lo))), d_bcast);
            let g2 = vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_low_s16(a1_hi))), d_bcast);
            let g3 = vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_high_s16(a1_hi))), d_bcast);
            acc = vfmaq_f32(acc, g0, vld1q_f32(in_ptr.add(16)));
            acc = vfmaq_f32(acc, g1, vld1q_f32(in_ptr.add(20)));
            acc = vfmaq_f32(acc, g2, vld1q_f32(in_ptr.add(24)));
            acc = vfmaq_f32(acc, g3, vld1q_f32(in_ptr.add(28)));
        }

        vaddvq_f32(acc)
    }

    /// Ternary bitmask × f32 input dot product for one row using NEON.
    ///
    /// Mirrors the AVX2 kernel `avx2_dot::ternary_dot_row_unscaled`. Per byte
    /// of `pos_mask` / `neg_mask` we broadcast the byte into a `u32x4` lane,
    /// AND it against the two 4-lane bit masks `[1, 2, 4, 8]` and
    /// `[16, 32, 64, 128]`, and `vceqq_u32` against the same bit mask. That
    /// yields all-ones per lane where the bit was set and 0 elsewhere; the
    /// bitwise AND with the reinterpreted f32 input lane produces `input[i]`
    /// or `0.0` — no branching, no per-lane multiplies.
    ///
    /// Returns the **unscaled** positive_sum − negative_sum; the caller
    /// multiplies by `row.scale` (bit-exact with the AVX2 / scalar callers).
    #[target_feature(enable = "neon")]
    pub unsafe fn ternary_dot_row_unscaled(
        pos_mask: &[u8],
        neg_mask: &[u8],
        input: &[f32],
        num_cols: usize,
    ) -> f32 {
        let bit_lanes_lo: uint32x4_t = vld1q_u32([0x01u32, 0x02, 0x04, 0x08].as_ptr());
        let bit_lanes_hi: uint32x4_t = vld1q_u32([0x10u32, 0x20, 0x40, 0x80].as_ptr());
        let mut pos_acc: float32x4_t = vdupq_n_f32(0.0);
        let mut neg_acc: float32x4_t = vdupq_n_f32(0.0);

        let full_bytes = num_cols / 8;
        for b in 0..full_bytes {
            let pm = u32::from(pos_mask[b]);
            let nm = u32::from(neg_mask[b]);
            if (pm | nm) == 0 {
                continue;
            }
            let in_ptr = input.as_ptr().add(b * 8);
            let in_lo = vld1q_f32(in_ptr);
            let in_hi = vld1q_f32(in_ptr.add(4));

            if pm != 0 {
                let bcast = vdupq_n_u32(pm);
                let is_set_lo = vceqq_u32(vandq_u32(bcast, bit_lanes_lo), bit_lanes_lo);
                let is_set_hi = vceqq_u32(vandq_u32(bcast, bit_lanes_hi), bit_lanes_hi);
                let contrib_lo = vandq_u32(is_set_lo, vreinterpretq_u32_f32(in_lo));
                let contrib_hi = vandq_u32(is_set_hi, vreinterpretq_u32_f32(in_hi));
                pos_acc = vaddq_f32(pos_acc, vreinterpretq_f32_u32(contrib_lo));
                pos_acc = vaddq_f32(pos_acc, vreinterpretq_f32_u32(contrib_hi));
            }
            if nm != 0 {
                let bcast = vdupq_n_u32(nm);
                let is_set_lo = vceqq_u32(vandq_u32(bcast, bit_lanes_lo), bit_lanes_lo);
                let is_set_hi = vceqq_u32(vandq_u32(bcast, bit_lanes_hi), bit_lanes_hi);
                let contrib_lo = vandq_u32(is_set_lo, vreinterpretq_u32_f32(in_lo));
                let contrib_hi = vandq_u32(is_set_hi, vreinterpretq_u32_f32(in_hi));
                neg_acc = vaddq_f32(neg_acc, vreinterpretq_f32_u32(contrib_lo));
                neg_acc = vaddq_f32(neg_acc, vreinterpretq_f32_u32(contrib_hi));
            }
        }

        let mut pos_sum = vaddvq_f32(pos_acc);
        let mut neg_sum = vaddvq_f32(neg_acc);

        // Tail: elements in the final byte that fall past `num_cols`.
        let tail_start = full_bytes * 8;
        if tail_start < num_cols {
            let last_b = full_bytes;
            let pm = pos_mask[last_b];
            let nm = neg_mask[last_b];
            let remaining = num_cols - tail_start;
            for bit in 0..remaining {
                let idx = tail_start + bit;
                let p = f32::from((pm >> bit) & 1);
                let n = f32::from((nm >> bit) & 1);
                pos_sum += p * input[idx];
                neg_sum += n * input[idx];
            }
        }

        pos_sum - neg_sum
    }

    #[inline]
    fn unpack_scales_mins_q4k(scale_bytes: &[u8]) -> ([u8; 8], [u8; 8]) {
        const KMASK1: u32 = 0x3f3f_3f3f;
        const KMASK2: u32 = 0x0f0f_0f0f;
        const KMASK3: u32 = 0x0303_0303;

        let mut utmp = [0u32; 4];
        utmp[0] = u32::from_le_bytes([
            scale_bytes[0],
            scale_bytes[1],
            scale_bytes[2],
            scale_bytes[3],
        ]);
        utmp[1] = u32::from_le_bytes([
            scale_bytes[4],
            scale_bytes[5],
            scale_bytes[6],
            scale_bytes[7],
        ]);
        utmp[2] = u32::from_le_bytes([
            scale_bytes[8],
            scale_bytes[9],
            scale_bytes[10],
            scale_bytes[11],
        ]);

        utmp[3] = ((utmp[2] >> 4) & KMASK2) | (((utmp[1] >> 6) & KMASK3) << 4);
        let uaux = utmp[1] & KMASK1;
        utmp[1] = (utmp[2] & KMASK2) | (((utmp[0] >> 6) & KMASK3) << 4);
        utmp[2] = uaux;
        utmp[0] &= KMASK1;

        let s0 = utmp[0].to_le_bytes();
        let s1 = utmp[1].to_le_bytes();
        let m0 = utmp[2].to_le_bytes();
        let m1 = utmp[3].to_le_bytes();
        (
            [s0[0], s0[1], s0[2], s0[3], s1[0], s1[1], s1[2], s1[3]],
            [m0[0], m0[1], m0[2], m0[3], m1[0], m1[1], m1[2], m1[3]],
        )
    }

    /// Bonsai 27B / PrismML `Q1_0` (binary g128) full-row matvec via NEON.
    ///
    /// Layout per block (18 bytes / 128 elements):
    ///   [0..2]  FP16 scale `d`
    ///   [2..18] 16 bytes of packed 1-bit values (LSB-first, 8 elements per byte)
    ///
    /// Value per element: `bit == 1 → +d`, `bit == 0 → −d`.
    ///
    /// Trick: reuse the Ternary bit-expansion pattern (broadcast + bit-lane
    /// AND + vceqq) to produce, for each 8-element group, a mask of "bit set"
    /// lanes and its complement "bit unset" lanes. The dot then decomposes
    /// into `d * (pos_sum − neg_sum)` where each side is a masked f32 add of
    /// the input.
    ///
    /// Returns the row's dot product (scale already applied per block).
    #[target_feature(enable = "neon")]
    pub unsafe fn q1_0_dot_row(row_data: &[u8], input: &[f32]) -> f32 {
        let bit_lanes_lo: uint32x4_t = vld1q_u32([0x01u32, 0x02, 0x04, 0x08].as_ptr());
        let bit_lanes_hi: uint32x4_t = vld1q_u32([0x10u32, 0x20, 0x40, 0x80].as_ptr());

        let block_bytes = 18usize;
        let n_blocks = row_data.len() / block_bytes;
        let mut total = 0.0f32;

        for i in 0..n_blocks {
            let block = &row_data[i * block_bytes..(i + 1) * block_bytes];
            let d = f16_to_f32(u16::from_le_bytes([block[0], block[1]]));
            let mut pos_acc: float32x4_t = vdupq_n_f32(0.0);
            let mut neg_acc: float32x4_t = vdupq_n_f32(0.0);
            let block_input = &input[i * 128..(i + 1) * 128];

            for b in 0..16 {
                let byte = block[2 + b];
                let pm = u32::from(byte);
                let nm = u32::from(!byte);
                let in_ptr = block_input.as_ptr().add(b * 8);
                let in_lo = vld1q_f32(in_ptr);
                let in_hi = vld1q_f32(in_ptr.add(4));

                // Positive contribution (bits set).
                let bcast_p = vdupq_n_u32(pm);
                let mask_p_lo = vceqq_u32(vandq_u32(bcast_p, bit_lanes_lo), bit_lanes_lo);
                let mask_p_hi = vceqq_u32(vandq_u32(bcast_p, bit_lanes_hi), bit_lanes_hi);
                let contrib_p_lo = vandq_u32(mask_p_lo, vreinterpretq_u32_f32(in_lo));
                let contrib_p_hi = vandq_u32(mask_p_hi, vreinterpretq_u32_f32(in_hi));
                pos_acc = vaddq_f32(pos_acc, vreinterpretq_f32_u32(contrib_p_lo));
                pos_acc = vaddq_f32(pos_acc, vreinterpretq_f32_u32(contrib_p_hi));

                // Negative contribution (bits unset).
                let bcast_n = vdupq_n_u32(nm);
                let mask_n_lo = vceqq_u32(vandq_u32(bcast_n, bit_lanes_lo), bit_lanes_lo);
                let mask_n_hi = vceqq_u32(vandq_u32(bcast_n, bit_lanes_hi), bit_lanes_hi);
                let contrib_n_lo = vandq_u32(mask_n_lo, vreinterpretq_u32_f32(in_lo));
                let contrib_n_hi = vandq_u32(mask_n_hi, vreinterpretq_u32_f32(in_hi));
                neg_acc = vaddq_f32(neg_acc, vreinterpretq_f32_u32(contrib_n_lo));
                neg_acc = vaddq_f32(neg_acc, vreinterpretq_f32_u32(contrib_n_hi));
            }

            let pos = vaddvq_f32(pos_acc);
            let neg = vaddvq_f32(neg_acc);
            total += d * (pos - neg);
        }

        total
    }

    /// Optimised sibling of [`q1_0_dot_row`] that exploits Q1_0's binary
    /// symmetry `value ∈ {−d, +d}`:
    ///
    /// ```text
    /// d * (pos_sum − neg_sum)
    ///   = d * (pos_sum − (block_input_sum − pos_sum))
    ///   = d * (2 * pos_sum − block_input_sum)
    /// ```
    ///
    /// The `block_input_sum` for each 128-element window depends only on
    /// the input vector, so the caller precomputes it once per matvec and
    /// passes the slice in via `input_block_sums`. The per-byte NEON work
    /// then only needs to expand the "bit set → +d" mask; the negative
    /// half is subsumed into the algebraic identity above. This roughly
    /// halves the SIMD instruction count per Q1_0 dot compared with
    /// [`q1_0_dot_row`].
    ///
    /// `input_block_sums.len()` must equal `row_data.len() / 18`.
    #[target_feature(enable = "neon")]
    pub unsafe fn q1_0_dot_row_pos_only(
        row_data: &[u8],
        input: &[f32],
        input_block_sums: &[f32],
    ) -> f32 {
        let bit_lanes_lo: uint32x4_t = vld1q_u32([0x01u32, 0x02, 0x04, 0x08].as_ptr());
        let bit_lanes_hi: uint32x4_t = vld1q_u32([0x10u32, 0x20, 0x40, 0x80].as_ptr());

        let block_bytes = 18usize;
        let n_blocks = row_data.len() / block_bytes;
        let mut total = 0.0f32;

        for i in 0..n_blocks {
            let block = &row_data[i * block_bytes..(i + 1) * block_bytes];
            let d = f16_to_f32(u16::from_le_bytes([block[0], block[1]]));
            let mut pos_acc: float32x4_t = vdupq_n_f32(0.0);
            let block_input = &input[i * 128..(i + 1) * 128];

            for b in 0..16 {
                let byte = block[2 + b];
                let pm = u32::from(byte);
                let in_ptr = block_input.as_ptr().add(b * 8);
                let in_lo = vld1q_f32(in_ptr);
                let in_hi = vld1q_f32(in_ptr.add(4));

                let bcast_p = vdupq_n_u32(pm);
                let mask_p_lo = vceqq_u32(vandq_u32(bcast_p, bit_lanes_lo), bit_lanes_lo);
                let mask_p_hi = vceqq_u32(vandq_u32(bcast_p, bit_lanes_hi), bit_lanes_hi);
                let contrib_p_lo = vandq_u32(mask_p_lo, vreinterpretq_u32_f32(in_lo));
                let contrib_p_hi = vandq_u32(mask_p_hi, vreinterpretq_u32_f32(in_hi));
                pos_acc = vaddq_f32(pos_acc, vreinterpretq_f32_u32(contrib_p_lo));
                pos_acc = vaddq_f32(pos_acc, vreinterpretq_f32_u32(contrib_p_hi));
            }

            let pos = vaddvq_f32(pos_acc);
            // d * (2 * pos − block_input_sum) — the negative half of the
            // dot is absorbed by the algebraic identity, so only one mask
            // expansion pass is needed per byte.
            total += d * (2.0 * pos - input_block_sums[i]);
        }

        total
    }

    /// Bonsai 27B / PrismML `Q2_0` (ternary g128) full-row matvec via NEON.
    ///
    /// Layout per block (34 bytes / 128 elements):
    ///   [0..2]  FP16 scale `d`
    ///   [2..34] 32 bytes of packed 2-bit values (4 slots per byte, LSB-first)
    ///
    /// Value per element: `((slot & 3) − 1) * d`, so `{-d, 0, +d, +2d}` for
    /// slots `{0b00, 0b01, 0b10, 0b11}` respectively.
    ///
    /// Per byte we broadcast the byte into a `u32x4` register, right-shift
    /// each lane by `[0, 2, 4, 6]` (via `vshlq_u32` with a signed negative
    /// shift vector), mask to 2 bits, subtract 1 to signed `{−1, 0, +1, +2}`,
    /// convert to f32, and multiply against the corresponding input lane.
    /// The scale `d` is multiplied once per block over the summed contribution.
    #[target_feature(enable = "neon")]
    pub unsafe fn q2_0_dot_row(row_data: &[u8], input: &[f32]) -> f32 {
        // vshlq_u32 with a negative shift is a right shift; the u32 lanes get
        // shifted by `[0, 2, 4, 6]` bits.
        let shifts: int32x4_t = vld1q_s32([0i32, -2, -4, -6].as_ptr());
        let two_bit_mask: uint32x4_t = vdupq_n_u32(0x3);
        let ones_s32: int32x4_t = vdupq_n_s32(1);

        let block_bytes = 34usize;
        let n_blocks = row_data.len() / block_bytes;
        let mut total = 0.0f32;

        for i in 0..n_blocks {
            let block = &row_data[i * block_bytes..(i + 1) * block_bytes];
            let d = f16_to_f32(u16::from_le_bytes([block[0], block[1]]));
            let mut acc: float32x4_t = vdupq_n_f32(0.0);
            let block_input = &input[i * 128..(i + 1) * 128];

            for b in 0..32 {
                let byte = u32::from(block[2 + b]);
                let bcast: uint32x4_t = vdupq_n_u32(byte);
                let shifted = vshlq_u32(bcast, shifts);
                let slots = vandq_u32(shifted, two_bit_mask);
                let vals_s32 = vsubq_s32(vreinterpretq_s32_u32(slots), ones_s32);
                let vals_f32 = vcvtq_f32_s32(vals_s32);

                let in_ptr = block_input.as_ptr().add(b * 4);
                let in_vec = vld1q_f32(in_ptr);
                acc = vfmaq_f32(acc, vals_f32, in_vec);
            }

            total += d * vaddvq_f32(acc);
        }

        total
    }

    /// NEON MXFP4 dot for one row (Phase X.4.f.2.a, 2026-07-28).
    ///
    /// Layout: 17 bytes per 32-element block (1 E8M0 shared scale +
    /// 16 packed E2M1 nibbles). Per block:
    ///
    /// 1. Scalar dequantize into a stack-resident `[f32; 32]` buffer
    ///    (E2M1 table lookup per nibble, then multiply by the E8M0
    ///    scale). The table has non-integer entries (0.5, 1.5, 3.0,
    ///    6.0) so a pure NEON table-lookup path is not straightforward;
    ///    scalar dequant is fast because the table + block fit in L1.
    /// 2. NEON matvec: 4-wide `vfmaq_f32` × 8 iterations = 32 elements
    ///    per block, then `vaddvq_f32` for the horizontal reduce and
    ///    accumulate into the row total.
    ///
    /// # Numerics
    ///
    /// The horizontal reduce order (`vaddvq_f32`) differs from the
    /// scalar kernel's strictly ascending element order, so parity
    /// with [`super::mxfp4_matvec_fused_scalar`] is tolerance-based
    /// (not bit-exact). Empirically `< 1e-4` relative error on
    /// random inputs.
    ///
    /// # Safety
    ///
    /// Caller must ensure `row_data.len() >= n_blocks * 17` and
    /// `input.len() >= n_blocks * QK_MXFP4`.
    #[target_feature(enable = "neon")]
    pub unsafe fn mxfp4_dot_row(row_data: &[u8], input: &[f32]) -> f32 {
        const BLOCK_BYTES: usize = 17;
        let n_blocks = row_data.len() / BLOCK_BYTES;
        let mut total = 0.0_f32;
        for i in 0..n_blocks {
            let block = &row_data[i * BLOCK_BYTES..(i + 1) * BLOCK_BYTES];
            let scale = decode_e8m0_scale(block[0]);
            // Scalar dequant into stack scratch (128 bytes = L1-resident).
            let mut dq = [0.0_f32; QK_MXFP4];
            for j in 0..16 {
                let byte = block[1 + j];
                dq[2 * j] = E2M1_DECODE_TABLE[usize::from(byte & 0x0F)] * scale;
                dq[2 * j + 1] = E2M1_DECODE_TABLE[usize::from(byte >> 4)] * scale;
            }
            // NEON matvec: 8 × 4-wide FMA.
            let block_input = &input[i * QK_MXFP4..(i + 1) * QK_MXFP4];
            let mut acc: float32x4_t = vdupq_n_f32(0.0);
            for k in 0..(QK_MXFP4 / 4) {
                // SAFETY: dq and block_input are both 32-element buffers,
                // k ranges 0..8 so k*4+4 <= 32; alignment is not required
                // for vld1q_f32 on aarch64.
                let dq_vec = vld1q_f32(dq.as_ptr().add(k * 4));
                let in_vec = vld1q_f32(block_input.as_ptr().add(k * 4));
                acc = vfmaq_f32(acc, dq_vec, in_vec);
            }
            total += vaddvq_f32(acc);
        }
        total
    }
}

// ─── x86_64 AVX2 SIMD dot products (Issue #13) ──────────────────────────────
//
// Mirrors the `neon_dot` module for x86_64 targets so Q4_K / Q6_K matvec
// gets SIMD acceleration on Ryzen / Xeon in addition to Apple Silicon.
// Runtime-dispatched through `is_x86_feature_detected!("avx2")` so binaries
// built without `-C target-cpu=native` still ship a scalar fallback for
// pre-AVX2 machines (Ivy Bridge and older).
//
// The core trick — reused from the JustVugg/colibri engine's `dot_i8i8` —
// is `_mm256_maddubs_epi16`, which multiplies **unsigned × signed** i8
// pairs. When both operands are signed (Q6_K case), pre-processing with
// `_mm256_sign_epi8(w, w)` yields `|w|` (unsigned) and `sign_epi8(x, w)`
// applies `sign(w)` to `x` — the product is then the original signed
// dot. Q4_K nibbles are natively unsigned `[0, 15]`, so the sign trick
// is unnecessary there.
//
// Reference: JustVugg/colibri `c/glm.c` (see PR body for background).

#[cfg(target_arch = "x86_64")]
#[allow(dead_code)]
mod avx2_dot {
    use super::*;
    use std::arch::x86_64::*;

    /// Horizontal sum of `__m256i` interpreted as 8 lanes of i32.
    #[inline]
    #[target_feature(enable = "avx2")]
    unsafe fn hsum_i32_avx2(v: __m256i) -> i32 {
        // SAFETY: caller guarantees AVX2 support.
        let lo = _mm256_castsi256_si128(v);
        let hi = _mm256_extracti128_si256(v, 1);
        let sum128 = _mm_add_epi32(lo, hi);
        let sh = _mm_shuffle_epi32(sum128, 0b1110);
        let sum64 = _mm_add_epi32(sum128, sh);
        let sh2 = _mm_shufflelo_epi16(sum64, 0b1110);
        let sum32 = _mm_add_epi32(sum64, sh2);
        _mm_cvtsi128_si32(sum32)
    }

    /// AVX2 signed dot product of 32 i8 values, seeded into an accumulator.
    ///
    /// Uses the `maddubs` sign trick: multiplies |w| (unsigned) by
    /// `sign(w) * x` (signed) to reuse the u8×i8 instruction for a
    /// signed×signed dot. Adds the resulting 8 lanes of i32 to the caller's
    /// running accumulator so multi-block loops can chain without spilling
    /// to memory.
    #[inline]
    #[target_feature(enable = "avx2")]
    unsafe fn dot_32_i8_signed(acc: __m256i, w: *const i8, x: *const i8) -> __m256i {
        let wv = _mm256_loadu_si256(w.cast());
        let xv = _mm256_loadu_si256(x.cast());
        // |w| (unsigned) × sign(w) * x (signed) via the u8×i8 madd.
        let w_abs = _mm256_sign_epi8(wv, wv);
        let x_signed = _mm256_sign_epi8(xv, wv);
        let prod16 = _mm256_maddubs_epi16(w_abs, x_signed);
        let ones = _mm256_set1_epi16(1);
        let prod32 = _mm256_madd_epi16(prod16, ones);
        _mm256_add_epi32(acc, prod32)
    }

    /// AVX2 dot product of 32 unsigned nibbles × 32 signed i8 values.
    ///
    /// Q4_K stores four-bit values in `[0, 15]`, so no sign trick is
    /// needed — `maddubs` natively consumes unsigned × signed. Adds into
    /// a running i32 accumulator so 32-element strides can be chained.
    #[inline]
    #[target_feature(enable = "avx2")]
    unsafe fn dot_32_u4_i8_from_nibbles(acc: __m256i, nib32: __m256i, x: *const i8) -> __m256i {
        let xv = _mm256_loadu_si256(x.cast());
        let prod16 = _mm256_maddubs_epi16(nib32, xv);
        let ones = _mm256_set1_epi16(1);
        let prod32 = _mm256_madd_epi16(prod16, ones);
        _mm256_add_epi32(acc, prod32)
    }

    /// Unpack 16 packed nibbles (`q4[0..16]`) into 32 unsigned bytes `[0, 15]`
    /// so `maddubs` can multiply against a matching 32-lane q8 slice.
    ///
    /// Low nibbles occupy the first 16 lanes, high nibbles the next 16 — the
    /// same order the scalar `q4k_q8k_dot_scalar` deposits into `aux8` when
    /// iterating each sub-block group.
    #[inline]
    #[target_feature(enable = "avx2")]
    unsafe fn unpack_16_q4_nibbles(q4: *const u8) -> __m256i {
        let bytes = _mm_loadu_si128(q4.cast());
        let mask = _mm_set1_epi8(0x0F);
        let lo = _mm_and_si128(bytes, mask);
        let hi = _mm_and_si128(_mm_srli_epi16(bytes, 4), mask);
        // Concatenate low nibbles (lanes 0..15) and high nibbles (16..31).
        _mm256_inserti128_si256(_mm256_castsi128_si256(lo), hi, 1)
    }

    /// Q4_K × Q8_K dot product using AVX2.
    ///
    /// Reproduces the arithmetic of `q4k_q8k_dot_scalar` (block layout
    /// documented on that function) using SIMD:
    /// 1. Reconstruct 8 sub-block scales + 8 mins from the 12-byte packed
    ///    scales/mins header (identical to the NEON path).
    /// 2. For each of 8 sub-blocks: unpack 32 nibbles, multiply against
    ///    32 q8 signed activations via `maddubs`, sum to i32, weight by
    ///    the sub-block's i8 scale.
    /// 3. Return `d * q8k.d * total - dmin * q8k.d * sumi` — the same
    ///    formula the scalar path emits.
    #[inline]
    #[target_feature(enable = "avx2")]
    pub(super) unsafe fn q4k_q8k_dot(q4k_block: &[u8], q8k: &BlockQ8K) -> f32 {
        const KMASK1: u32 = 0x3f3f_3f3f;
        const KMASK2: u32 = 0x0f0f_0f0f;
        const KMASK3: u32 = 0x0303_0303;

        let d = f16_to_f32(u16::from_le_bytes([q4k_block[0], q4k_block[1]]));
        let dmin = f16_to_f32(u16::from_le_bytes([q4k_block[2], q4k_block[3]]));
        let q4 = &q4k_block[16..144];

        let mut utmp = [0u32; 4];
        let sb = &q4k_block[4..16];
        utmp[0] = u32::from_le_bytes([sb[0], sb[1], sb[2], sb[3]]);
        utmp[1] = u32::from_le_bytes([sb[4], sb[5], sb[6], sb[7]]);
        utmp[2] = u32::from_le_bytes([sb[8], sb[9], sb[10], sb[11]]);
        utmp[3] = ((utmp[2] >> 4) & KMASK2) | (((utmp[1] >> 6) & KMASK3) << 4);
        let uaux = utmp[1] & KMASK1;
        utmp[1] = (utmp[2] & KMASK2) | (((utmp[0] >> 6) & KMASK3) << 4);
        utmp[2] = uaux;
        utmp[0] &= KMASK1;
        let s0 = utmp[0].to_le_bytes();
        let s1 = utmp[1].to_le_bytes();
        let m0 = utmp[2].to_le_bytes();
        let m1 = utmp[3].to_le_bytes();
        let scales = [s0[0], s0[1], s0[2], s0[3], s1[0], s1[1], s1[2], s1[3]];
        let mins = [m0[0], m0[1], m0[2], m0[3], m1[0], m1[1], m1[2], m1[3]];

        // Bias correction: sum over 16 half-sub-blocks of `bsum[j] * min[j/2]`.
        let mut sumi = 0i32;
        for j in 0..16 {
            sumi += q8k.bsums[j] as i32 * mins[j / 2] as i32;
        }

        // Per-sub-block SIMD dot × scale, accumulated as i32.
        //
        // Layout note: the scalar path packs `aux8` so that sub-block `2k`
        // is the **low nibbles** of `q4[k*32 : k*32+32]` and sub-block
        // `2k+1` is the **high nibbles** of the same 32 packed bytes.
        // Iterating on `q4[is*16 : is*16+16]` per sub-block reads the
        // wrong window entirely (initial implementation bug caught by CI),
        // so we stride by 32 bytes (64 nibbles) and produce two 32-lane
        // dots per iteration.
        let mut total: i32 = 0;
        for g in 0..4 {
            let q4_ptr = q4.as_ptr().add(g * 32);
            // Load 32 packed bytes = 64 nibbles.
            let bytes = _mm256_loadu_si256(q4_ptr.cast());
            let mask = _mm256_set1_epi8(0x0F);
            let lo_nibbles = _mm256_and_si256(bytes, mask);
            // `_mm256_srli_epi16` shifts each u16 lane by 4; after masking
            // with 0x0F the surviving bits are exactly the per-byte high
            // nibbles.
            let hi_nibbles = _mm256_and_si256(_mm256_srli_epi16(bytes, 4), mask);

            // Sub-block 2g: low nibbles × q8[g*64 .. g*64+32], scaled by scales[2g].
            let q8_ptr_a = q8k.qs.as_ptr().add(g * 64);
            let acc_a = _mm256_setzero_si256();
            let acc_a = dot_32_u4_i8_from_nibbles(acc_a, lo_nibbles, q8_ptr_a);
            let dot_a = hsum_i32_avx2(acc_a);
            total += scales[2 * g] as i32 * dot_a;

            // Sub-block 2g+1: high nibbles × q8[g*64+32 .. g*64+64].
            let q8_ptr_b = q8k.qs.as_ptr().add(g * 64 + 32);
            let acc_b = _mm256_setzero_si256();
            let acc_b = dot_32_u4_i8_from_nibbles(acc_b, hi_nibbles, q8_ptr_b);
            let dot_b = hsum_i32_avx2(acc_b);
            total += scales[2 * g + 1] as i32 * dot_b;
        }

        d * q8k.d * total as f32 - dmin * q8k.d * sumi as f32
    }

    /// AVX2 signed dot product of 16 i8 values via sign-extension.
    ///
    /// Widens each i8 to i16 first, then relies on `_mm256_madd_epi16`
    /// (which multiplies i16 pairs and sums adjacent lanes to i32 with
    /// **no saturation**). Slightly more expensive than the `maddubs`
    /// sign trick but immune to the i16 intermediate saturation risk
    /// for signed×signed dots, and its per-sub-block granularity matches
    /// the scalar Q6_K layout exactly.
    #[inline]
    #[target_feature(enable = "avx2")]
    unsafe fn dot_16_i8_widened(acc: __m256i, w: *const i8, x: *const i8) -> __m256i {
        let w16 = _mm256_cvtepi8_epi16(_mm_loadu_si128(w.cast()));
        let x16 = _mm256_cvtepi8_epi16(_mm_loadu_si128(x.cast()));
        let prod32 = _mm256_madd_epi16(w16, x16);
        _mm256_add_epi32(acc, prod32)
    }

    /// Q6_K × Q8_K dot product using AVX2.
    ///
    /// Reconstructs 6-bit values from the low 4 bits (`ql`) and top 2 bits
    /// (`qh`) via vectorised bit unpacking (mirrors the NEON kernel — see
    /// `neon_dot::q6k_q8k_dot`), then does 16-element sub-block dots with
    /// `dot_16_i8_widened`.
    ///
    /// Per outer iteration (2 iters cover the 256-element block) we load 64
    /// ql bytes as two 32-byte AVX2 vectors plus 32 qh bytes as one vector,
    /// then produce four 32-byte quadrants of aux8 via shift/mask/or:
    ///   Q0: (ql_lo & 0xF) | ((qh << 4) & 0x30) - 32
    ///   Q1: (ql_hi & 0xF) | ((qh << 2) & 0x30) - 32
    ///   Q2: (ql_lo >> 4)  | ( qh       & 0x30) - 32
    ///   Q3: (ql_hi >> 4)  | ((qh >> 2) & 0x30) - 32
    ///
    /// AVX2 lacks a native 8-bit shift, so we shift as u16 and rely on the
    /// `0x30` / `0x0F` byte mask to discard any bits that leaked across the
    /// byte boundary — those bits land outside the target byte positions
    /// (bits 4-5 for the high nibble, bits 0-3 for the low nibble) and are
    /// therefore killed by the mask.
    #[inline]
    #[target_feature(enable = "avx2")]
    pub(super) unsafe fn q6k_q8k_dot(q6k_block: &[u8], q8k: &BlockQ8K) -> f32 {
        let ql = &q6k_block[0..128];
        let qh = &q6k_block[128..192];
        let scales = &q6k_block[192..208];
        let d = f16_to_f32(u16::from_le_bytes([q6k_block[208], q6k_block[209]]));

        // Reconstruct 256 signed i8 values on the stack. The layout matches
        // `q6k_q8k_dot_scalar`: 16 sub-blocks of 16 elements each.
        let mut aux8 = [0i8; QK_K];
        let mask_nib = _mm256_set1_epi8(0x0F);
        let mask_hi = _mm256_set1_epi8(0x30);
        let bias = _mm256_set1_epi8(-32);
        let mut a_off = 0usize;
        let mut ql_off = 0usize;
        let mut qh_off = 0usize;
        for _ in 0..2 {
            let ql_lo = _mm256_loadu_si256(ql.as_ptr().add(ql_off).cast());
            let ql_hi = _mm256_loadu_si256(ql.as_ptr().add(ql_off + 32).cast());
            let qh_v = _mm256_loadu_si256(qh.as_ptr().add(qh_off).cast());

            // Quadrant 0: (ql_lo & 0xF) | ((qh << 4) & 0x30) - 32
            let hi0 = _mm256_and_si256(_mm256_slli_epi16(qh_v, 4), mask_hi);
            let q0 = _mm256_add_epi8(
                _mm256_or_si256(_mm256_and_si256(ql_lo, mask_nib), hi0),
                bias,
            );
            _mm256_storeu_si256(aux8.as_mut_ptr().add(a_off).cast(), q0);

            // Quadrant 1: (ql_hi & 0xF) | ((qh << 2) & 0x30) - 32
            let hi1 = _mm256_and_si256(_mm256_slli_epi16(qh_v, 2), mask_hi);
            let q1 = _mm256_add_epi8(
                _mm256_or_si256(_mm256_and_si256(ql_hi, mask_nib), hi1),
                bias,
            );
            _mm256_storeu_si256(aux8.as_mut_ptr().add(a_off + 32).cast(), q1);

            // Quadrant 2: (ql_lo >> 4) | (qh & 0x30) - 32
            let hi2 = _mm256_and_si256(qh_v, mask_hi);
            let q2 = _mm256_add_epi8(
                _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(ql_lo, 4), mask_nib), hi2),
                bias,
            );
            _mm256_storeu_si256(aux8.as_mut_ptr().add(a_off + 64).cast(), q2);

            // Quadrant 3: (ql_hi >> 4) | ((qh >> 2) & 0x30) - 32
            let hi3 = _mm256_and_si256(_mm256_srli_epi16(qh_v, 2), mask_hi);
            let q3 = _mm256_add_epi8(
                _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(ql_hi, 4), mask_nib), hi3),
                bias,
            );
            _mm256_storeu_si256(aux8.as_mut_ptr().add(a_off + 96).cast(), q3);

            a_off += 128;
            ql_off += 64;
            qh_off += 32;
        }

        // Q6_K has 16 sub-blocks of 16 elements — perfectly matched to a
        // 16-lane SIMD dot after i8→i16 sign extension.
        let mut total: i32 = 0;
        for is in 0..16 {
            let a_ptr = aux8.as_ptr().add(is * 16);
            let q8_ptr = q8k.qs.as_ptr().add(is * 16);
            let acc0 = _mm256_setzero_si256();
            let acc = dot_16_i8_widened(acc0, a_ptr, q8_ptr);
            let sub_dot = hsum_i32_avx2(acc);
            total += scales[is] as i8 as i32 * sub_dot;
        }

        d * q8k.d * total as f32
    }

    /// AVX2 dot of 32 unsigned u8 values × 32 signed i8 values.
    ///
    /// Sign-extends both operands to i16 first (u8 has a natural 0-padded
    /// promotion, so `_mm256_cvtepu8_epi16` is safe) and uses
    /// `_mm256_madd_epi16` to accumulate into i32 without saturation.
    #[inline]
    #[target_feature(enable = "avx2")]
    unsafe fn dot_32_u8_i8_widened(acc: __m256i, u: __m256i, x: *const i8) -> __m256i {
        let xv = _mm256_loadu_si256(x.cast());
        // Split each 32-lane u8 vector into two 128-bit halves, then
        // zero-extend to 16 x i16 per half so subsequent multiplies stay
        // in i16-safe range and the following `madd_epi16` produces
        // saturation-free i32 sums.
        let u_lo128 = _mm256_castsi256_si128(u);
        let u_hi128 = _mm256_extracti128_si256(u, 1);
        let x_lo128 = _mm256_castsi256_si128(xv);
        let x_hi128 = _mm256_extracti128_si256(xv, 1);
        let u_lo = _mm256_cvtepu8_epi16(u_lo128);
        let u_hi = _mm256_cvtepu8_epi16(u_hi128);
        let x_lo = _mm256_cvtepi8_epi16(x_lo128);
        let x_hi = _mm256_cvtepi8_epi16(x_hi128);
        let sum_lo = _mm256_madd_epi16(u_lo, x_lo);
        let sum_hi = _mm256_madd_epi16(u_hi, x_hi);
        _mm256_add_epi32(acc, _mm256_add_epi32(sum_lo, sum_hi))
    }

    /// Q5_K × Q8_K dot product using AVX2.
    ///
    /// Q5_K packs each 5-bit value as `(qs nibble) | (qh bit << 4)` so the
    /// per-element range is `[0, 31]` — still unsigned, so no sign trick
    /// is needed. Four groups (`im=0..4`), each processing 64 elements:
    /// low nibbles + qh bit `2*im` fill even sub-block, high nibbles +
    /// qh bit `2*im+1` fill odd sub-block. The single 32-byte qh load is
    /// reused across all four `im` iterations by shifting/masking to
    /// isolate the target bit.
    #[inline]
    #[target_feature(enable = "avx2")]
    pub(super) unsafe fn q5k_q8k_dot(q5k_block: &[u8], q8k: &BlockQ8K) -> f32 {
        let d = f16_to_f32(u16::from_le_bytes([q5k_block[0], q5k_block[1]]));
        let dmin = f16_to_f32(u16::from_le_bytes([q5k_block[2], q5k_block[3]]));
        let scales_raw = &q5k_block[4..16];
        let qh = &q5k_block[16..48];
        let qs = &q5k_block[48..176];

        // 32-byte qh loaded once and reused across all four `im` iterations —
        // each iteration selects a different bit position via `srli_epi16`
        // + `and` (the u16-lane shift is safe because the AND with 0x01
        // masks off any bits that leaked from the neighbouring byte).
        let qh_v = _mm256_loadu_si256(qh.as_ptr().cast());
        let mask_lo4 = _mm256_set1_epi8(0x0F);
        let mask_bit0 = _mm256_set1_epi8(1);

        let mut sumf = 0.0f32;
        for im in 0..4u8 {
            let (sc1, m1) = get_scale_min_k4((im as usize) * 2, scales_raw);
            let (sc2, m2) = get_scale_min_k4((im as usize) * 2 + 1, scales_raw);

            // 32 packed qs bytes = 64 nibbles for this `im` group.
            let qs_group = _mm256_loadu_si256(qs.as_ptr().add((im as usize) * 32).cast());
            let lo_nibbles = _mm256_and_si256(qs_group, mask_lo4);
            let hi_nibbles = _mm256_and_si256(_mm256_srli_epi16(qs_group, 4), mask_lo4);

            // Runtime-variable shifts (`_mm256_srli_epi16` requires a
            // compile-time immediate, so we use the sibling `_srl_` form
            // which takes a `__m128i` shift amount whose low 64 bits
            // control every lane).
            let shift_even = _mm_cvtsi32_si128(i32::from(im) * 2);
            let shift_odd = _mm_cvtsi32_si128(i32::from(im) * 2 + 1);

            // Even sub-block: qh bit `2*im` broadcasted into the top nibble.
            let hbits_even = _mm256_and_si256(_mm256_srl_epi16(qh_v, shift_even), mask_bit0);
            let hbits_even_hi = _mm256_slli_epi16(hbits_even, 4);
            let q_even = _mm256_or_si256(lo_nibbles, hbits_even_hi);

            // Odd sub-block: qh bit `2*im + 1`.
            let hbits_odd = _mm256_and_si256(_mm256_srl_epi16(qh_v, shift_odd), mask_bit0);
            let hbits_odd_hi = _mm256_slli_epi16(hbits_odd, 4);
            let q_odd = _mm256_or_si256(hi_nibbles, hbits_odd_hi);

            // Dot products against 64 q8 activations (32 for each sub-block).
            let q8_ptr_a = q8k.qs.as_ptr().add((im as usize) * 64);
            let acc_a = _mm256_setzero_si256();
            let acc_a = dot_32_u8_i8_widened(acc_a, q_even, q8_ptr_a);
            let dot_a = hsum_i32_avx2(acc_a);

            let q8_ptr_b = q8k.qs.as_ptr().add((im as usize) * 64 + 32);
            let acc_b = _mm256_setzero_si256();
            let acc_b = dot_32_u8_i8_widened(acc_b, q_odd, q8_ptr_b);
            let dot_b = hsum_i32_avx2(acc_b);

            // The bias-correction `sum_i = Σ q8[i]` for each 32-element
            // sub-block reuses the pre-computed `bsums[j]` (each covers
            // 16 q8 values, so two consecutive entries make a 32-lane sum).
            let sum1 =
                q8k.bsums[(im as usize) * 4] as i32 + q8k.bsums[(im as usize) * 4 + 1] as i32;
            let sum2 =
                q8k.bsums[(im as usize) * 4 + 2] as i32 + q8k.bsums[(im as usize) * 4 + 3] as i32;

            sumf += d * sc1 as f32 * dot_a as f32 - dmin * m1 as f32 * sum1 as f32;
            sumf += d * sc2 as f32 * dot_b as f32 - dmin * m2 as f32 * sum2 as f32;
        }
        sumf * q8k.d
    }

    /// Fused Q8_0 dequantize + f32 matvec for a whole row using AVX2.
    ///
    /// Q8_0 stores 32 signed i8 weights per block plus a single f16 scale,
    /// and — unlike the K-series — the ALICE-LLM dispatch feeds it raw f32
    /// inputs rather than a pre-quantised Q8_K block. That makes the SIMD
    /// path an f32 FMA loop (i8 → f32 dequant, multiply by the row's f32
    /// input, FMA into an accumulator) rather than an integer madd chain.
    ///
    /// Per block of 32 elements, four 8-lane iterations consume the whole
    /// block; the accumulator is horizontally summed at the end to yield
    /// the row's dot product.
    #[inline]
    #[target_feature(enable = "avx2,fma")]
    pub(super) unsafe fn q8_0_matvec_row(input: &[f32], row_data: &[u8], cols: usize) -> f32 {
        let blocks_per_row = cols / super::QK8_0;
        let block_bytes = 34usize;
        let mut acc = _mm256_setzero_ps();
        for bi in 0..blocks_per_row {
            let off = bi * block_bytes;
            let d = super::f16_to_f32(u16::from_le_bytes([row_data[off], row_data[off + 1]]));
            let d_bcast = _mm256_set1_ps(d);
            let qs_ptr = row_data.as_ptr().add(off + 2).cast::<i8>();
            let col_base = bi * super::QK8_0;
            // 4 × 8-lane iterations = 32 elements per block.
            for k in 0..4 {
                // `_mm_loadl_epi64` is an unaligned 8-byte load; the pointer
                // cast is only needed for the intrinsic's signature
                #[allow(clippy::cast_ptr_alignment)]
                let q8 = _mm_loadl_epi64(qs_ptr.add(k * 8).cast::<__m128i>());
                let q32 = _mm256_cvtepi8_epi32(q8);
                let qf = _mm256_cvtepi32_ps(q32);
                let scaled = _mm256_mul_ps(qf, d_bcast);
                let x = _mm256_loadu_ps(input.as_ptr().add(col_base + k * 8));
                acc = _mm256_fmadd_ps(scaled, x, acc);
            }
        }
        // Horizontal sum of 8 f32 lanes.
        let lo = _mm256_castps256_ps128(acc);
        let hi = _mm256_extractf128_ps(acc, 1);
        let s128 = _mm_add_ps(lo, hi);
        let sh = _mm_movehl_ps(s128, s128);
        let s64 = _mm_add_ps(s128, sh);
        let sh2 = _mm_shuffle_ps(s64, s64, 0x55);
        let s32 = _mm_add_ss(s64, sh2);
        _mm_cvtss_f32(s32)
    }

    /// Ternary bitmask × f32 input dot product for one row (AVX2).
    ///
    /// Each byte of `pos_mask` / `neg_mask` encodes 8 lanes. The 32-bit
    /// broadcast + per-lane AND with the `[0x01, 0x02, 0x04, 0x08, 0x10,
    /// 0x20, 0x40, 0x80]` pattern extracts one bit per lane; comparing
    /// against the same pattern yields an all-ones i32 (i.e. the f32
    /// mask `-NaN`-like bit pattern used as an AND-mask) where the bit
    /// was set. `_mm256_and_ps` with the input then produces `input[i]`
    /// or `0.0` — no branchy blending needed.
    ///
    /// The scalar dot the SIMD path reproduces:
    ///
    /// ```text
    /// sum = Σ_b Σ_{bit ∈ pos_mask[b]} input[b*8 + bit]
    ///     - Σ_b Σ_{bit ∈ neg_mask[b]} input[b*8 + bit]
    /// ```
    ///
    /// Returns the unscaled `pos_sum - neg_sum` — the caller applies the
    /// row's f32 scale afterwards so both dispatch arms stay identical.
    #[inline]
    #[target_feature(enable = "avx2")]
    pub(super) unsafe fn ternary_dot_row_unscaled(
        pos_mask: &[u8],
        neg_mask: &[u8],
        input: &[f32],
        num_cols: usize,
    ) -> f32 {
        // 8-lane bit-mask constants: lane i tests bit `1 << i` of the byte.
        let bit_lanes = _mm256_setr_epi32(0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80);
        let mut pos_acc = _mm256_setzero_ps();
        let mut neg_acc = _mm256_setzero_ps();

        // Cover the fully-populated bytes with 8 lanes each. Any tail that
        // spans an incomplete final byte is finished by the scalar loop
        // below (edge case: `num_cols % 8 != 0`).
        let full_bytes = num_cols / 8;
        for b in 0..full_bytes {
            let pm = u32::from(pos_mask[b]);
            let nm = u32::from(neg_mask[b]);
            if (pm | nm) == 0 {
                continue;
            }
            let input_v = _mm256_loadu_ps(input.as_ptr().add(b * 8));

            // Positive contributions.
            if pm != 0 {
                let bcast = _mm256_set1_epi32(pm as i32);
                let masked = _mm256_and_si256(bcast, bit_lanes);
                let is_set = _mm256_cmpeq_epi32(masked, bit_lanes);
                let contrib = _mm256_and_ps(_mm256_castsi256_ps(is_set), input_v);
                pos_acc = _mm256_add_ps(pos_acc, contrib);
            }
            // Negative contributions.
            if nm != 0 {
                let bcast = _mm256_set1_epi32(nm as i32);
                let masked = _mm256_and_si256(bcast, bit_lanes);
                let is_set = _mm256_cmpeq_epi32(masked, bit_lanes);
                let contrib = _mm256_and_ps(_mm256_castsi256_ps(is_set), input_v);
                neg_acc = _mm256_add_ps(neg_acc, contrib);
            }
        }

        // Horizontal sums for the SIMD accumulators.
        let hsum = |v: __m256| -> f32 {
            let lo = _mm256_castps256_ps128(v);
            let hi = _mm256_extractf128_ps(v, 1);
            let s = _mm_add_ps(lo, hi);
            let sh = _mm_movehl_ps(s, s);
            let s2 = _mm_add_ps(s, sh);
            let sh2 = _mm_shuffle_ps(s2, s2, 0x55);
            let s3 = _mm_add_ss(s2, sh2);
            _mm_cvtss_f32(s3)
        };
        let mut pos_sum = hsum(pos_acc);
        let mut neg_sum = hsum(neg_acc);

        // Tail: elements in the final byte that fall past `num_cols`.
        let tail_start = full_bytes * 8;
        if tail_start < num_cols {
            let last_b = full_bytes;
            let pm = pos_mask[last_b];
            let nm = neg_mask[last_b];
            let remaining = num_cols - tail_start;
            for bit in 0..remaining {
                let idx = tail_start + bit;
                let p = f32::from((pm >> bit) & 1);
                let n = f32::from((nm >> bit) & 1);
                pos_sum += p * input[idx];
                neg_sum += n * input[idx];
            }
        }

        pos_sum - neg_sum
    }

    /// AVX2 MXFP4 dot for one row (Phase X.4.f.2.b, 2026-07-28).
    ///
    /// Mirrors the aarch64 NEON kernel design: per block, scalar
    /// dequantize into a stack-resident `[f32; 32]` scratch, then
    /// AVX2 8-wide FMA × 4 iterations = 32 elements. Horizontal
    /// reduction via the standard 128-bit halve-and-sum pattern
    /// already used by `q1_0_dot_row` above.
    ///
    /// # Numerics
    ///
    /// The horizontal reduction order (halve-and-sum) differs from
    /// the scalar kernel's strictly ascending element order, so
    /// parity with `mxfp4_matvec_fused_scalar` is tolerance-based
    /// (~1e-4 relative), not bit-exact — same as the NEON kernel.
    ///
    /// # Safety
    ///
    /// Caller must have verified AVX2 support via
    /// `is_x86_feature_detected!("avx2")` and must ensure
    /// `row_data.len() >= n_blocks * 17` and
    /// `input.len() >= n_blocks * QK_MXFP4`.
    #[target_feature(enable = "avx2,fma")]
    pub unsafe fn mxfp4_dot_row(row_data: &[u8], input: &[f32]) -> f32 {
        const BLOCK_BYTES: usize = 17;
        let n_blocks = row_data.len() / BLOCK_BYTES;
        let mut total = 0.0_f32;
        for i in 0..n_blocks {
            let block = &row_data[i * BLOCK_BYTES..(i + 1) * BLOCK_BYTES];
            let scale = decode_e8m0_scale(block[0]);
            // Scalar dequant into stack scratch (128 bytes = L1-resident).
            let mut dq = [0.0_f32; QK_MXFP4];
            for j in 0..16 {
                let byte = block[1 + j];
                dq[2 * j] = E2M1_DECODE_TABLE[usize::from(byte & 0x0F)] * scale;
                dq[2 * j + 1] = E2M1_DECODE_TABLE[usize::from(byte >> 4)] * scale;
            }
            // AVX2 matvec: 4 × 8-wide FMA.
            let block_input = &input[i * QK_MXFP4..(i + 1) * QK_MXFP4];
            let mut acc: __m256 = _mm256_setzero_ps();
            for k in 0..(QK_MXFP4 / 8) {
                // SAFETY: dq and block_input are both 32-element buffers,
                // k ranges 0..4 so k*8+8 <= 32; loadu does not require
                // alignment.
                let dq_vec = _mm256_loadu_ps(dq.as_ptr().add(k * 8));
                let in_vec = _mm256_loadu_ps(block_input.as_ptr().add(k * 8));
                acc = _mm256_fmadd_ps(dq_vec, in_vec, acc);
            }
            // Horizontal sum: 256 → 128 halve-and-add → 128 → scalar.
            let lo = _mm256_castps256_ps128(acc);
            let hi = _mm256_extractf128_ps(acc, 1);
            let s = _mm_add_ps(lo, hi);
            let sh = _mm_movehl_ps(s, s);
            let s2 = _mm_add_ps(s, sh);
            let sh2 = _mm_shuffle_ps(s2, s2, 0x55);
            let s3 = _mm_add_ss(s2, sh2);
            total += _mm_cvtss_f32(s3);
        }
        total
    }
}

// ─── x86_64 AVX-512 SIMD dot products (Issue #13) ───────────────────────────
//
// Same algorithm as AVX2 but processes 64 bytes per iteration instead of 32.
// Gated behind `is_x86_feature_detected!("avx512bw")` at dispatch because
// AVX-512BW is what supplies `_mm512_maddubs_epi16` and `_mm512_madd_epi16`;
// mere AVX-512F is insufficient.

#[cfg(target_arch = "x86_64")]
#[allow(dead_code)]
mod avx512_dot {
    use super::*;
    use std::arch::x86_64::*;

    /// AVX-512 dot product of 64 unsigned nibbles × 64 signed i8 values.
    #[inline]
    #[target_feature(enable = "avx512bw")]
    unsafe fn dot_64_u4_i8_from_nibbles(acc: __m512i, nib64: __m512i, x: *const i8) -> __m512i {
        let xv = _mm512_loadu_si512(x.cast());
        let prod16 = _mm512_maddubs_epi16(nib64, xv);
        let ones = _mm512_set1_epi16(1);
        let prod32 = _mm512_madd_epi16(prod16, ones);
        _mm512_add_epi32(acc, prod32)
    }

    /// Unpack 32 packed nibbles (`q4[0..32]`) into 64 unsigned bytes `[0, 15]`.
    /// Low nibbles fill lanes 0..31, high nibbles fill 32..63 — matches the
    /// scalar `aux8` layout across two 32-lane sub-blocks.
    #[inline]
    #[target_feature(enable = "avx512bw")]
    unsafe fn unpack_32_q4_nibbles(q4: *const u8) -> __m512i {
        let bytes = _mm256_loadu_si256(q4.cast());
        let mask = _mm256_set1_epi8(0x0F);
        let lo = _mm256_and_si256(bytes, mask);
        let hi = _mm256_and_si256(_mm256_srli_epi16(bytes, 4), mask);
        _mm512_inserti64x4(_mm512_castsi256_si512(lo), hi, 1)
    }

    /// Q4_K × Q8_K dot product using AVX-512BW. Processes 2 sub-blocks per
    /// iteration (64 lanes = 2 × 32-element Q4_K sub-blocks).
    #[inline]
    #[target_feature(enable = "avx512bw")]
    pub(super) unsafe fn q4k_q8k_dot(q4k_block: &[u8], q8k: &BlockQ8K) -> f32 {
        const KMASK1: u32 = 0x3f3f_3f3f;
        const KMASK2: u32 = 0x0f0f_0f0f;
        const KMASK3: u32 = 0x0303_0303;

        let d = f16_to_f32(u16::from_le_bytes([q4k_block[0], q4k_block[1]]));
        let dmin = f16_to_f32(u16::from_le_bytes([q4k_block[2], q4k_block[3]]));
        let q4 = &q4k_block[16..144];

        let mut utmp = [0u32; 4];
        let sb = &q4k_block[4..16];
        utmp[0] = u32::from_le_bytes([sb[0], sb[1], sb[2], sb[3]]);
        utmp[1] = u32::from_le_bytes([sb[4], sb[5], sb[6], sb[7]]);
        utmp[2] = u32::from_le_bytes([sb[8], sb[9], sb[10], sb[11]]);
        utmp[3] = ((utmp[2] >> 4) & KMASK2) | (((utmp[1] >> 6) & KMASK3) << 4);
        let uaux = utmp[1] & KMASK1;
        utmp[1] = (utmp[2] & KMASK2) | (((utmp[0] >> 6) & KMASK3) << 4);
        utmp[2] = uaux;
        utmp[0] &= KMASK1;
        let s0 = utmp[0].to_le_bytes();
        let s1 = utmp[1].to_le_bytes();
        let m0 = utmp[2].to_le_bytes();
        let m1 = utmp[3].to_le_bytes();
        let scales = [s0[0], s0[1], s0[2], s0[3], s1[0], s1[1], s1[2], s1[3]];
        let mins = [m0[0], m0[1], m0[2], m0[3], m1[0], m1[1], m1[2], m1[3]];

        let mut sumi = 0i32;
        for j in 0..16 {
            sumi += q8k.bsums[j] as i32 * mins[j / 2] as i32;
        }

        // 4 iterations, each covering 32 packed bytes of q4 (= 64 nibbles
        // → 2 sub-blocks). Match the scalar sub-block layout exactly:
        //   sub-block 2g   = low  nibbles of q4[g*32 : g*32+32] · q8[g*64 : g*64+32]
        //   sub-block 2g+1 = high nibbles of q4[g*32 : g*32+32] · q8[g*64+32 : g*64+64]
        // Reused from the AVX2 fix; the earlier per-16-lane split
        // interleaved q8 windows and produced silently wrong sums.
        let mut total: i32 = 0;
        for g in 0..4 {
            let mut sub_dot_a = 0i32;
            let mut sub_dot_b = 0i32;
            for l in 0..32 {
                let lo = (q4[g * 32 + l] & 0x0F) as i32;
                let hi = ((q4[g * 32 + l] >> 4) & 0x0F) as i32;
                sub_dot_a += lo * q8k.qs[g * 64 + l] as i32;
                sub_dot_b += hi * q8k.qs[g * 64 + 32 + l] as i32;
            }
            total += scales[g * 2] as i32 * sub_dot_a;
            total += scales[g * 2 + 1] as i32 * sub_dot_b;
        }

        d * q8k.d * total as f32 - dmin * q8k.d * sumi as f32
    }

    /// 16-element i8 × i8 dot using AVX2 widening. Local copy of
    /// `avx2_dot::dot_16_i8_widened` — AVX-512BW implies AVX2 support so the
    /// intrinsics compile fine under the enclosing `avx512bw` feature gate.
    #[inline]
    #[target_feature(enable = "avx2")]
    unsafe fn dot_16_i8_widened_avx2(acc: __m256i, w: *const i8, x: *const i8) -> __m256i {
        let w16 = _mm256_cvtepi8_epi16(_mm_loadu_si128(w.cast()));
        let x16 = _mm256_cvtepi8_epi16(_mm_loadu_si128(x.cast()));
        let prod32 = _mm256_madd_epi16(w16, x16);
        _mm256_add_epi32(acc, prod32)
    }

    /// AVX2 horizontal-sum helper. Mirrors `avx2_dot::hsum_i32_avx2`.
    #[inline]
    #[target_feature(enable = "avx2")]
    unsafe fn hsum_i32_avx2(v: __m256i) -> i32 {
        let lo = _mm256_castsi256_si128(v);
        let hi = _mm256_extracti128_si256(v, 1);
        let sum128 = _mm_add_epi32(lo, hi);
        let sh = _mm_shuffle_epi32(sum128, 0b1110);
        let sum64 = _mm_add_epi32(sum128, sh);
        let sh2 = _mm_shufflelo_epi16(sum64, 0b1110);
        let sum32 = _mm_add_epi32(sum64, sh2);
        _mm_cvtsi128_si32(sum32)
    }

    /// Q6_K × Q8_K dot product using AVX-512BW.
    ///
    /// Two hot paths were previously scalar in this kernel:
    ///
    /// 1. **6-bit unpack** — 128 iterations of `((ql & 0xF) | ((qh >> …) << 4)) - 32`
    ///    per outer block. Replaced with the AVX2 parallel bit unpacking
    ///    pattern (mirrors `avx2_dot::q6k_q8k_dot` and the NEON kernel):
    ///    load `ql_lo` / `ql_hi` (two 32-byte AVX2 vectors) and `qh` (one
    ///    32-byte vector), then produce four 32-byte aux8 quadrants via
    ///    byte-wise shift/mask/or.
    /// 2. **Per-sub-block dot** — the previous implementation issued a
    ///    64-lane signed dot, discarded its result, and manually re-computed
    ///    the 16-element dots in scalar. Replaced with a proper
    ///    `_mm256_madd_epi16`-based 16-lane sub-block dot
    ///    (`dot_16_i8_widened_avx2`), matching the AVX2 kernel.
    #[inline]
    #[target_feature(enable = "avx512bw")]
    pub(super) unsafe fn q6k_q8k_dot(q6k_block: &[u8], q8k: &BlockQ8K) -> f32 {
        let ql = &q6k_block[0..128];
        let qh = &q6k_block[128..192];
        let scales = &q6k_block[192..208];
        let d = f16_to_f32(u16::from_le_bytes([q6k_block[208], q6k_block[209]]));

        // Vectorised bit unpacking (AVX2 subset, safe under avx512bw).
        let mut aux8 = [0i8; QK_K];
        let mask_nib = _mm256_set1_epi8(0x0F);
        let mask_hi = _mm256_set1_epi8(0x30);
        let bias = _mm256_set1_epi8(-32);
        let mut a_off = 0usize;
        let mut ql_off = 0usize;
        let mut qh_off = 0usize;
        for _ in 0..2 {
            let ql_lo = _mm256_loadu_si256(ql.as_ptr().add(ql_off).cast());
            let ql_hi = _mm256_loadu_si256(ql.as_ptr().add(ql_off + 32).cast());
            let qh_v = _mm256_loadu_si256(qh.as_ptr().add(qh_off).cast());

            // Q0: (ql_lo & 0xF) | ((qh << 4) & 0x30) - 32
            let hi0 = _mm256_and_si256(_mm256_slli_epi16(qh_v, 4), mask_hi);
            let q0 = _mm256_add_epi8(
                _mm256_or_si256(_mm256_and_si256(ql_lo, mask_nib), hi0),
                bias,
            );
            _mm256_storeu_si256(aux8.as_mut_ptr().add(a_off).cast(), q0);

            // Q1: (ql_hi & 0xF) | ((qh << 2) & 0x30) - 32
            let hi1 = _mm256_and_si256(_mm256_slli_epi16(qh_v, 2), mask_hi);
            let q1 = _mm256_add_epi8(
                _mm256_or_si256(_mm256_and_si256(ql_hi, mask_nib), hi1),
                bias,
            );
            _mm256_storeu_si256(aux8.as_mut_ptr().add(a_off + 32).cast(), q1);

            // Q2: (ql_lo >> 4) | (qh & 0x30) - 32
            let hi2 = _mm256_and_si256(qh_v, mask_hi);
            let q2 = _mm256_add_epi8(
                _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(ql_lo, 4), mask_nib), hi2),
                bias,
            );
            _mm256_storeu_si256(aux8.as_mut_ptr().add(a_off + 64).cast(), q2);

            // Q3: (ql_hi >> 4) | ((qh >> 2) & 0x30) - 32
            let hi3 = _mm256_and_si256(_mm256_srli_epi16(qh_v, 2), mask_hi);
            let q3 = _mm256_add_epi8(
                _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(ql_hi, 4), mask_nib), hi3),
                bias,
            );
            _mm256_storeu_si256(aux8.as_mut_ptr().add(a_off + 96).cast(), q3);

            a_off += 128;
            ql_off += 64;
            qh_off += 32;
        }

        // 16 sub-blocks of 16 elements — SIMD per-sub-block dot via
        // widened i8 → i16 → i32 madd. Matches the AVX2 kernel.
        let mut total: i32 = 0;
        for is in 0..16 {
            let a_ptr = aux8.as_ptr().add(is * 16);
            let q8_ptr = q8k.qs.as_ptr().add(is * 16);
            let acc0 = _mm256_setzero_si256();
            let acc = dot_16_i8_widened_avx2(acc0, a_ptr, q8_ptr);
            let sub_dot = hsum_i32_avx2(acc);
            total += scales[is] as i8 as i32 * sub_dot;
        }

        d * q8k.d * total as f32
    }

    /// Q5_K × Q8_K dot product using AVX-512BW.
    ///
    /// Reuses the AVX2 Q5_K algorithm but doubles the lane count: each `im`
    /// iteration handles all 64 elements in a single AVX-512 register.
    /// The qh bit extraction moves to 64-lane shift+mask, and the dot is
    /// computed via widened `i8 → i16 → i32` madd (saturation-safe).
    #[inline]
    #[target_feature(enable = "avx512bw")]
    pub(super) unsafe fn q5k_q8k_dot(q5k_block: &[u8], q8k: &BlockQ8K) -> f32 {
        let d = f16_to_f32(u16::from_le_bytes([q5k_block[0], q5k_block[1]]));
        let dmin = f16_to_f32(u16::from_le_bytes([q5k_block[2], q5k_block[3]]));
        let scales_raw = &q5k_block[4..16];
        let qh = &q5k_block[16..48];
        let qs = &q5k_block[48..176];

        // qh: 32 bytes — fits in the low half of a 64-lane AVX-512 register.
        // For AVX-512BW we need the same 32 bytes duplicated in both halves
        // so the OR against `[low_nibbles | high_nibbles]` (also 64 lanes,
        // built from a single 32-byte qs load) reaches the intended byte
        // positions.
        let qh256 = _mm256_loadu_si256(qh.as_ptr().cast());
        let qh_v = _mm512_broadcast_i32x8(qh256);
        let mask_bit0 = _mm512_set1_epi8(1);

        let mut sumf = 0.0f32;
        for im in 0..4u8 {
            let (sc1, m1) = get_scale_min_k4((im as usize) * 2, scales_raw);
            let (sc2, m2) = get_scale_min_k4((im as usize) * 2 + 1, scales_raw);

            // Load 32 packed qs bytes = 64 nibbles.
            let qs256 = _mm256_loadu_si256(qs.as_ptr().add((im as usize) * 32).cast());
            let qs_lo = _mm256_and_si256(qs256, _mm256_set1_epi8(0x0F));
            let qs_hi = _mm256_and_si256(_mm256_srli_epi16(qs256, 4), _mm256_set1_epi8(0x0F));
            // Concatenate low + high nibbles into a single 64-lane register.
            let nibbles64 = _mm512_inserti64x4(_mm512_castsi256_si512(qs_lo), qs_hi, 1);

            // Runtime-variable shifts via the `_srl_` sibling form
            // (`_mm512_srli_epi16` requires a const-imm shift amount).
            let shift_even = _mm_cvtsi32_si128(i32::from(im) * 2);
            let shift_odd = _mm_cvtsi32_si128(i32::from(im) * 2 + 1);
            let hbits = _mm512_and_si512(_mm512_srl_epi16(qh_v, shift_even), mask_bit0);
            let hbits_odd = _mm512_and_si512(_mm512_srl_epi16(qh_v, shift_odd), mask_bit0);
            // Assemble the 5th-bit mask so low-half bytes get the even bit
            // and high-half bytes get the odd bit.
            let hbits_pack = _mm512_inserti64x4(
                _mm512_castsi256_si512(_mm512_castsi512_si256(hbits)),
                _mm512_castsi512_si256(hbits_odd),
                1,
            );
            let hbits_hi = _mm512_slli_epi16(hbits_pack, 4);
            let q_all = _mm512_or_si512(nibbles64, hbits_hi);

            // Dot with 64 q8 activations via widened i16 accumulation, then
            // split the running sum by pulling the low/high 32 lanes apart
            // and hsumming each independently so scales[2*im] and
            // scales[2*im+1] can weight their own sub-blocks.
            let q_lo = _mm512_castsi512_si256(q_all);
            let q_hi = _mm512_extracti64x4_epi64(q_all, 1);
            let q8_ptr_a = q8k.qs.as_ptr().add((im as usize) * 64);
            let q8_ptr_b = q8k.qs.as_ptr().add((im as usize) * 64 + 32);

            let dot_a = {
                let acc0 = _mm256_setzero_si256();
                let acc = super::avx2_dot_bridge_u8_i8(acc0, q_lo, q8_ptr_a);
                super::avx2_hsum_i32_bridge(acc)
            };
            let dot_b = {
                let acc0 = _mm256_setzero_si256();
                let acc = super::avx2_dot_bridge_u8_i8(acc0, q_hi, q8_ptr_b);
                super::avx2_hsum_i32_bridge(acc)
            };

            let sum1 =
                q8k.bsums[(im as usize) * 4] as i32 + q8k.bsums[(im as usize) * 4 + 1] as i32;
            let sum2 =
                q8k.bsums[(im as usize) * 4 + 2] as i32 + q8k.bsums[(im as usize) * 4 + 3] as i32;

            sumf += d * sc1 as f32 * dot_a as f32 - dmin * m1 as f32 * sum1 as f32;
            sumf += d * sc2 as f32 * dot_b as f32 - dmin * m2 as f32 * sum2 as f32;
        }
        sumf * q8k.d
    }

    /// Fused Q8_0 dequantize + f32 matvec for a whole row using AVX-512F.
    ///
    /// Twice-wide port of the AVX2 [`super::avx2_dot::q8_0_matvec_row`]:
    /// two 16-lane FMA iterations cover each 32-element block instead of
    /// four 8-lane ones. Sign-extension and scaling are identical.
    #[inline]
    #[target_feature(enable = "avx512f")]
    pub(super) unsafe fn q8_0_matvec_row(input: &[f32], row_data: &[u8], cols: usize) -> f32 {
        let blocks_per_row = cols / super::QK8_0;
        let block_bytes = 34usize;
        let mut acc = _mm512_setzero_ps();
        for bi in 0..blocks_per_row {
            let off = bi * block_bytes;
            let d = super::f16_to_f32(u16::from_le_bytes([row_data[off], row_data[off + 1]]));
            let d_bcast = _mm512_set1_ps(d);
            let qs_ptr = row_data.as_ptr().add(off + 2).cast::<i8>();
            let col_base = bi * super::QK8_0;
            // 2 × 16-lane iterations = 32 elements per block.
            for k in 0..2 {
                // `_mm_loadu_si128` is the unaligned 16-byte load; the pointer
                // cast is only needed for the intrinsic's signature
                #[allow(clippy::cast_ptr_alignment)]
                let q8 = _mm_loadu_si128(qs_ptr.add(k * 16).cast::<__m128i>());
                let q32 = _mm512_cvtepi8_epi32(q8);
                let qf = _mm512_cvtepi32_ps(q32);
                let scaled = _mm512_mul_ps(qf, d_bcast);
                let x = _mm512_loadu_ps(input.as_ptr().add(col_base + k * 16));
                acc = _mm512_fmadd_ps(scaled, x, acc);
            }
        }
        _mm512_reduce_add_ps(acc)
    }

    /// Ternary bitmask × f32 input dot product for one row (AVX-512F).
    ///
    /// Uses AVX-512's native 16-bit mask register (`__mmask16`) to gate the
    /// per-lane contribution — no explicit compare + and dance. Two
    /// consecutive bytes of `pos_mask` / `neg_mask` combine into a
    /// `__mmask16` covering 16 lanes at once, so each iteration processes
    /// twice the input width of the AVX2 path.
    #[inline]
    #[target_feature(enable = "avx512f,avx512bw")]
    pub(super) unsafe fn ternary_dot_row_unscaled(
        pos_mask: &[u8],
        neg_mask: &[u8],
        input: &[f32],
        num_cols: usize,
    ) -> f32 {
        let mut pos_acc = _mm512_setzero_ps();
        let mut neg_acc = _mm512_setzero_ps();

        // 16-lane groups walk two mask bytes at a time.
        let full_pairs = num_cols / 16;
        for pair in 0..full_pairs {
            let pm = u16::from(pos_mask[pair * 2]) | (u16::from(pos_mask[pair * 2 + 1]) << 8);
            let nm = u16::from(neg_mask[pair * 2]) | (u16::from(neg_mask[pair * 2 + 1]) << 8);
            if (pm | nm) == 0 {
                continue;
            }
            let input_v = _mm512_loadu_ps(input.as_ptr().add(pair * 16));
            if pm != 0 {
                let k: __mmask16 = pm;
                pos_acc = _mm512_mask_add_ps(pos_acc, k, pos_acc, input_v);
            }
            if nm != 0 {
                let k: __mmask16 = nm;
                neg_acc = _mm512_mask_add_ps(neg_acc, k, neg_acc, input_v);
            }
        }

        let mut pos_sum = _mm512_reduce_add_ps(pos_acc);
        let mut neg_sum = _mm512_reduce_add_ps(neg_acc);

        // Tail: any elements past `full_pairs * 16`. We fall through to a
        // scalar loop for the up-to-15 remaining lanes so the mask packing
        // stays simple.
        let tail_start = full_pairs * 16;
        for idx in tail_start..num_cols {
            let byte_idx = idx / 8;
            let bit = idx % 8;
            let p = f32::from((pos_mask[byte_idx] >> bit) & 1);
            let n = f32::from((neg_mask[byte_idx] >> bit) & 1);
            pos_sum += p * input[idx];
            neg_sum += n * input[idx];
        }

        pos_sum - neg_sum
    }
}

// ─── AVX2 helpers used by AVX-512 kernels (bridge) ──────────────────────────
//
// AVX-512BW machines by definition also have AVX2 (VEX-encoded AVX-512
// implies AVX2). The Q5_K AVX-512 kernel calls these AVX2 helpers to reuse
// the widened i16 dot without duplicating the intrinsics dance in both
// modules.

#[cfg(target_arch = "x86_64")]
#[target_feature(enable = "avx2")]
unsafe fn avx2_dot_bridge_u8_i8(
    acc: std::arch::x86_64::__m256i,
    u: std::arch::x86_64::__m256i,
    x: *const i8,
) -> std::arch::x86_64::__m256i {
    use std::arch::x86_64::*;
    let xv = _mm256_loadu_si256(x.cast());
    let u_lo = _mm256_cvtepu8_epi16(_mm256_castsi256_si128(u));
    let u_hi = _mm256_cvtepu8_epi16(_mm256_extracti128_si256(u, 1));
    let x_lo = _mm256_cvtepi8_epi16(_mm256_castsi256_si128(xv));
    let x_hi = _mm256_cvtepi8_epi16(_mm256_extracti128_si256(xv, 1));
    let sum_lo = _mm256_madd_epi16(u_lo, x_lo);
    let sum_hi = _mm256_madd_epi16(u_hi, x_hi);
    _mm256_add_epi32(acc, _mm256_add_epi32(sum_lo, sum_hi))
}

#[cfg(target_arch = "x86_64")]
#[target_feature(enable = "avx2")]
unsafe fn avx2_hsum_i32_bridge(v: std::arch::x86_64::__m256i) -> i32 {
    use std::arch::x86_64::*;
    let lo = _mm256_castsi256_si128(v);
    let hi = _mm256_extracti128_si256(v, 1);
    let sum128 = _mm_add_epi32(lo, hi);
    let sh = _mm_shuffle_epi32(sum128, 0b1110);
    let sum64 = _mm_add_epi32(sum128, sh);
    let sh2 = _mm_shufflelo_epi16(sum64, 0b1110);
    let sum32 = _mm_add_epi32(sum64, sh2);
    _mm_cvtsi128_si32(sum32)
}

// ─── Runtime CPU feature cache (Issue #13) ──────────────────────────────────

#[cfg(target_arch = "x86_64")]
fn x86_avx2_supported() -> bool {
    use std::sync::OnceLock;
    static CACHE: OnceLock<bool> = OnceLock::new();
    *CACHE.get_or_init(|| is_x86_feature_detected!("avx2"))
}

#[cfg(target_arch = "x86_64")]
fn x86_avx512bw_supported() -> bool {
    use std::sync::OnceLock;
    static CACHE: OnceLock<bool> = OnceLock::new();
    *CACHE.get_or_init(|| is_x86_feature_detected!("avx512bw"))
}

// ─── Scalar dot product fallback ────────────────────────────────────────────

/// Scalar Q4_K × Q8_K dot product (fallback for non-NEON platforms).
#[inline]
#[allow(dead_code)]
fn q4k_q8k_dot_scalar(q4k_block: &[u8], q8k: &BlockQ8K) -> f32 {
    const KMASK1: u32 = 0x3f3f_3f3f;
    const KMASK2: u32 = 0x0f0f_0f0f;
    const KMASK3: u32 = 0x0303_0303;

    let d = f16_to_f32(u16::from_le_bytes([q4k_block[0], q4k_block[1]]));
    let dmin = f16_to_f32(u16::from_le_bytes([q4k_block[2], q4k_block[3]]));
    let q4 = &q4k_block[16..144];

    let mut aux8 = [0i8; QK_K];
    let mut a_off = 0usize;
    let mut q4_off = 0usize;
    for _ in 0..4 {
        for l in 0..32 {
            aux8[a_off + l] = (q4[q4_off + l] & 0xF) as i8;
        }
        a_off += 32;
        for l in 0..32 {
            aux8[a_off + l] = (q4[q4_off + l] >> 4) as i8;
        }
        a_off += 32;
        q4_off += 32;
    }

    let mut utmp = [0u32; 4];
    let sb = &q4k_block[4..16];
    utmp[0] = u32::from_le_bytes([sb[0], sb[1], sb[2], sb[3]]);
    utmp[1] = u32::from_le_bytes([sb[4], sb[5], sb[6], sb[7]]);
    utmp[2] = u32::from_le_bytes([sb[8], sb[9], sb[10], sb[11]]);
    utmp[3] = ((utmp[2] >> 4) & KMASK2) | (((utmp[1] >> 6) & KMASK3) << 4);
    let uaux = utmp[1] & KMASK1;
    utmp[1] = (utmp[2] & KMASK2) | (((utmp[0] >> 6) & KMASK3) << 4);
    utmp[2] = uaux;
    utmp[0] &= KMASK1;

    let s0 = utmp[0].to_le_bytes();
    let s1 = utmp[1].to_le_bytes();
    let m0 = utmp[2].to_le_bytes();
    let m1 = utmp[3].to_le_bytes();
    let scales = [s0[0], s0[1], s0[2], s0[3], s1[0], s1[1], s1[2], s1[3]];
    let mins = [m0[0], m0[1], m0[2], m0[3], m1[0], m1[1], m1[2], m1[3]];

    let mut sumi = 0i32;
    for j in 0..16 {
        sumi += q8k.bsums[j] as i32 * mins[j / 2] as i32;
    }

    let mut aux32 = [0i32; 8];
    let mut a_idx = 0usize;
    let mut q8_idx = 0usize;
    for is in 0..8 {
        let scale = scales[is] as i32;
        for _ in 0..4 {
            for l in 0..8 {
                aux32[l] += scale * (q8k.qs[q8_idx + l] as i32 * aux8[a_idx + l] as i32);
            }
            q8_idx += 8;
            a_idx += 8;
        }
    }

    let d_all = d * q8k.d;
    let dmin_all = dmin * q8k.d;
    let mut sumf = 0.0f32;
    for l in 0..8 {
        sumf += d_all * aux32[l] as f32;
    }
    sumf -= dmin_all * sumi as f32;
    sumf
}

/// Scalar Q6_K × Q8_K dot product (fallback for non-NEON platforms).
#[inline]
#[allow(dead_code)]
fn q6k_q8k_dot_scalar(q6k_block: &[u8], q8k: &BlockQ8K) -> f32 {
    let ql = &q6k_block[0..128];
    let qh = &q6k_block[128..192];
    let scales = &q6k_block[192..208];
    let d = f16_to_f32(u16::from_le_bytes([q6k_block[208], q6k_block[209]]));

    let mut aux8 = [0i8; QK_K];
    let mut a_off = 0usize;
    let mut ql_off = 0usize;
    let mut qh_off = 0usize;
    for _ in 0..2 {
        for l in 0..32 {
            aux8[a_off + l] = ((ql[ql_off + l] & 0xF) | ((qh[qh_off + l] & 3) << 4)) as i8 - 32;
            aux8[a_off + l + 32] =
                ((ql[ql_off + l + 32] & 0xF) | (((qh[qh_off + l] >> 2) & 3) << 4)) as i8 - 32;
            aux8[a_off + l + 64] =
                ((ql[ql_off + l] >> 4) | (((qh[qh_off + l] >> 4) & 3) << 4)) as i8 - 32;
            aux8[a_off + l + 96] =
                ((ql[ql_off + l + 32] >> 4) | (((qh[qh_off + l] >> 6) & 3) << 4)) as i8 - 32;
        }
        a_off += 128;
        ql_off += 64;
        qh_off += 32;
    }

    let mut aux32 = [0i32; 8];
    let mut a_idx = 0usize;
    let mut q8_idx = 0usize;
    for is in 0..16 {
        let scale = scales[is] as i8 as i32;
        for _ in 0..2 {
            for l in 0..8 {
                aux32[l] += scale * (q8k.qs[q8_idx + l] as i32 * aux8[a_idx + l] as i32);
            }
            q8_idx += 8;
            a_idx += 8;
        }
    }

    let d_all = d * q8k.d;
    let mut sumf = 0.0f32;
    for l in 0..8 {
        sumf += d_all * aux32[l] as f32;
    }
    sumf
}

// ─── Dispatch: NEON or scalar ───────────────────────────────────────────────

/// Q4_K × Q8_K dot product.
///
/// Dispatches at runtime to the fastest supported implementation:
/// x86_64: AVX-512BW → AVX2 → scalar fallback.
/// aarch64: NEON → scalar fallback. NEON is baseline on aarch64 targets
/// Rust supports, so the dispatch is compile-time cfg (no runtime probe).
/// The NEON kernel was gated behind bit-exact parity tests before being
/// wired here — see `q4k_neon_matches_scalar_bit_exact` for the guard.
#[inline]
fn q4k_q8k_dot(q4k_block: &[u8], q8k: &BlockQ8K) -> f32 {
    #[cfg(target_arch = "x86_64")]
    {
        // SAFETY: intrinsics gated on runtime CPU feature detection cached
        // in a `OnceLock`; the sub-functions carry `#[target_feature]`
        // annotations so the compiler emits the correct instruction set.
        if x86_avx512bw_supported() {
            return unsafe { avx512_dot::q4k_q8k_dot(q4k_block, q8k) };
        }
        if x86_avx2_supported() {
            return unsafe { avx2_dot::q4k_q8k_dot(q4k_block, q8k) };
        }
    }
    #[cfg(target_arch = "aarch64")]
    {
        // SAFETY: NEON is baseline on every aarch64 CPU Rust supports.
        return unsafe { neon_dot::q4k_q8k_dot(q4k_block, q8k) };
    }
    #[allow(unreachable_code)]
    q4k_q8k_dot_fallback_scalar(q4k_block, q8k)
}

/// Q4_K × Q8_K scalar fallback body extracted so the runtime dispatcher
/// can call it without duplicating the arithmetic. Kept close to the
/// dispatcher to preserve inlineability.
#[inline]
fn q4k_q8k_dot_fallback_scalar(q4k_block: &[u8], q8k: &BlockQ8K) -> f32 {
    const KMASK1: u32 = 0x3f3f_3f3f;
    const KMASK2: u32 = 0x0f0f_0f0f;
    const KMASK3: u32 = 0x0303_0303;

    let d = f16_to_f32(u16::from_le_bytes([q4k_block[0], q4k_block[1]]));
    let dmin = f16_to_f32(u16::from_le_bytes([q4k_block[2], q4k_block[3]]));
    let q4 = &q4k_block[16..144];

    let mut aux8 = [0u8; QK_K];
    for g in 0..4 {
        for l in 0..32 {
            aux8[g * 64 + l] = q4[g * 32 + l] & 0xF;
            aux8[g * 64 + 32 + l] = q4[g * 32 + l] >> 4;
        }
    }

    let mut utmp = [0u32; 4];
    let sb = &q4k_block[4..16];
    utmp[0] = u32::from_le_bytes([sb[0], sb[1], sb[2], sb[3]]);
    utmp[1] = u32::from_le_bytes([sb[4], sb[5], sb[6], sb[7]]);
    utmp[2] = u32::from_le_bytes([sb[8], sb[9], sb[10], sb[11]]);
    utmp[3] = ((utmp[2] >> 4) & KMASK2) | (((utmp[1] >> 6) & KMASK3) << 4);
    let uaux = utmp[1] & KMASK1;
    utmp[1] = (utmp[2] & KMASK2) | (((utmp[0] >> 6) & KMASK3) << 4);
    utmp[2] = uaux;
    utmp[0] &= KMASK1;
    let s0 = utmp[0].to_le_bytes();
    let s1 = utmp[1].to_le_bytes();
    let m0 = utmp[2].to_le_bytes();
    let m1 = utmp[3].to_le_bytes();
    let scales = [s0[0], s0[1], s0[2], s0[3], s1[0], s1[1], s1[2], s1[3]];
    let mins = [m0[0], m0[1], m0[2], m0[3], m1[0], m1[1], m1[2], m1[3]];

    let mut sumi = 0i32;
    for j in 0..16 {
        sumi += q8k.bsums[j] as i32 * mins[j / 2] as i32;
    }

    let mut total = 0i32;
    for is in 0..8 {
        let off = is * 32;
        let mut dot = 0i32;
        for l in 0..32 {
            dot += aux8[off + l] as i32 * q8k.qs[off + l] as i32;
        }
        total += scales[is] as i32 * dot;
    }

    d * q8k.d * total as f32 - dmin * q8k.d * sumi as f32
}

/// Q6_K × Q8_K dot product.
///
/// Runtime-dispatched on x86_64 (AVX-512BW → AVX2 → scalar); NEON on
/// aarch64. See `q4k_q8k_dot` for the design rationale and the
/// `q6k_neon_matches_scalar_bit_exact` parity gate that preceded
/// enabling the aarch64 path.
#[inline]
fn q6k_q8k_dot(q6k_block: &[u8], q8k: &BlockQ8K) -> f32 {
    #[cfg(target_arch = "x86_64")]
    {
        // SAFETY: same rationale as `q4k_q8k_dot`.
        if x86_avx512bw_supported() {
            return unsafe { avx512_dot::q6k_q8k_dot(q6k_block, q8k) };
        }
        if x86_avx2_supported() {
            return unsafe { avx2_dot::q6k_q8k_dot(q6k_block, q8k) };
        }
    }
    #[cfg(target_arch = "aarch64")]
    {
        // SAFETY: NEON is baseline on every aarch64 CPU Rust supports.
        return unsafe { neon_dot::q6k_q8k_dot(q6k_block, q8k) };
    }
    #[allow(unreachable_code)]
    q6k_q8k_dot_fallback_scalar(q6k_block, q8k)
}

/// Q6_K × Q8_K scalar fallback body.
#[inline]
fn q6k_q8k_dot_fallback_scalar(q6k_block: &[u8], q8k: &BlockQ8K) -> f32 {
    let ql = &q6k_block[0..128];
    let qh = &q6k_block[128..192];
    let scales = &q6k_block[192..208];
    let d = f16_to_f32(u16::from_le_bytes([q6k_block[208], q6k_block[209]]));

    let mut aux8 = [0i8; QK_K];
    let mut a_off = 0usize;
    let mut ql_off = 0usize;
    let mut qh_off = 0usize;
    for _ in 0..2 {
        for l in 0..32 {
            aux8[a_off + l] = ((ql[ql_off + l] & 0xF) | ((qh[qh_off + l] & 3) << 4)) as i8 - 32;
            aux8[a_off + l + 32] =
                ((ql[ql_off + l + 32] & 0xF) | (((qh[qh_off + l] >> 2) & 3) << 4)) as i8 - 32;
            aux8[a_off + l + 64] =
                ((ql[ql_off + l] >> 4) | (((qh[qh_off + l] >> 4) & 3) << 4)) as i8 - 32;
            aux8[a_off + l + 96] =
                ((ql[ql_off + l + 32] >> 4) | (((qh[qh_off + l] >> 6) & 3) << 4)) as i8 - 32;
        }
        a_off += 128;
        ql_off += 64;
        qh_off += 32;
    }

    let mut total = 0i32;
    for is in 0..16 {
        let scale = scales[is] as i8 as i32;
        let off = is * 16;
        let mut dot = 0i32;
        for l in 0..16 {
            dot += aux8[off + l] as i32 * q8k.qs[off + l] as i32;
        }
        total += scale * dot;
    }

    d * q8k.d * total as f32
}

/// Q2_K × Q8_K dot product.
#[inline]
fn q2k_q8k_dot(q2k_block: &[u8], q8k: &BlockQ8K) -> f32 {
    let scales = &q2k_block[0..16];
    let qs = &q2k_block[16..80];
    let d = f16_to_f32(u16::from_le_bytes([q2k_block[80], q2k_block[81]]));
    let dmin = f16_to_f32(u16::from_le_bytes([q2k_block[82], q2k_block[83]]));

    let mut sumf = 0.0f32;
    for group in 0..16 {
        let sc = (scales[group] & 0xF) as i32;
        let m = (scales[group] >> 4) as i32;
        let mut dot = 0i32;
        let mut sum_q8 = 0i32;
        for j in 0..16 {
            let flat = group * 16 + j;
            let byte_idx = flat / 4;
            let bit_shift = (flat % 4) * 2;
            let q = ((qs[byte_idx] >> bit_shift) & 3) as i32;
            dot += q * q8k.qs[flat] as i32;
            sum_q8 += q8k.qs[flat] as i32;
        }
        sumf += d * sc as f32 * dot as f32 - dmin * m as f32 * sum_q8 as f32;
    }
    sumf * q8k.d
}

/// Q5_K × Q8_K dot product.
///
/// Runtime-dispatched: AVX-512BW → AVX2 → scalar fallback on x86_64.
/// See `q4k_q8k_dot` for the design rationale.
#[inline]
fn q5k_q8k_dot(q5k_block: &[u8], q8k: &BlockQ8K) -> f32 {
    #[cfg(target_arch = "x86_64")]
    {
        // SAFETY: intrinsics gated on cached runtime CPU detection.
        if x86_avx512bw_supported() {
            return unsafe { avx512_dot::q5k_q8k_dot(q5k_block, q8k) };
        }
        if x86_avx2_supported() {
            return unsafe { avx2_dot::q5k_q8k_dot(q5k_block, q8k) };
        }
    }
    q5k_q8k_dot_fallback_scalar(q5k_block, q8k)
}

/// Q5_K × Q8_K scalar fallback body.
#[inline]
fn q5k_q8k_dot_fallback_scalar(q5k_block: &[u8], q8k: &BlockQ8K) -> f32 {
    let d = f16_to_f32(u16::from_le_bytes([q5k_block[0], q5k_block[1]]));
    let dmin = f16_to_f32(u16::from_le_bytes([q5k_block[2], q5k_block[3]]));
    let scales_raw = &q5k_block[4..16];
    let qh = &q5k_block[16..48];
    let qs = &q5k_block[48..176];

    let mut sumf = 0.0f32;
    let mut is = 0usize;
    let mut q_offset = 0usize;
    let mut q8_offset = 0usize;

    for im in 0..4u8 {
        let (sc1, m1) = get_scale_min_k4(is, scales_raw);
        let (sc2, m2) = get_scale_min_k4(is + 1, scales_raw);

        let mut dot1 = 0i32;
        let mut sum1 = 0i32;
        for l in 0..32 {
            let hbit = ((qh[l] >> (im * 2)) & 1) as i32;
            let q = ((qs[q_offset + l] & 0xF) as i32) | (hbit << 4);
            dot1 += q * q8k.qs[q8_offset + l] as i32;
            sum1 += q8k.qs[q8_offset + l] as i32;
        }
        sumf += d * sc1 as f32 * dot1 as f32 - dmin * m1 as f32 * sum1 as f32;

        let mut dot2 = 0i32;
        let mut sum2 = 0i32;
        for l in 0..32 {
            let hbit = ((qh[l] >> (im * 2 + 1)) & 1) as i32;
            let q = ((qs[q_offset + l] >> 4) as i32) | (hbit << 4);
            dot2 += q * q8k.qs[q8_offset + 32 + l] as i32;
            sum2 += q8k.qs[q8_offset + 32 + l] as i32;
        }
        sumf += d * sc2 as f32 * dot2 as f32 - dmin * m2 as f32 * sum2 as f32;

        q_offset += 32;
        q8_offset += 64;
        is += 2;
    }
    sumf * q8k.d
}

/// Q3_K × Q8_K dot product.
#[inline]
fn q3k_q8k_dot(q3k_block: &[u8], q8k: &BlockQ8K) -> f32 {
    let hmask = &q3k_block[0..32];
    let qs = &q3k_block[32..96];
    let d = f16_to_f32(u16::from_le_bytes([q3k_block[108], q3k_block[109]]));
    let scales = q3k_decode_scales(&q3k_block[96..108]);

    // Unpack 3-bit weights to i8
    let mut aux8 = [0i8; QK_K];
    let mut m: u8 = 1;
    let mut q_off = 0usize;
    for j in (0..QK_K).step_by(128) {
        for shift in 0..4u8 {
            for l in 0..32usize {
                let lo2 = (qs[q_off + l] >> (shift * 2)) & 3;
                let hi = if hmask[l] & m != 0 { 0i8 } else { -4i8 };
                aux8[j + shift as usize * 32 + l] = lo2 as i8 + hi;
            }
            m <<= 1;
        }
        q_off += 32;
    }

    // Dot product: 16 sub-blocks of 16 elements
    let mut total = 0i32;
    for is in 0..16 {
        let off = is * 16;
        let mut dot = 0i32;
        for l in 0..16 {
            dot += aux8[off + l] as i32 * q8k.qs[off + l] as i32;
        }
        total += scales[is] * dot;
    }

    d * q8k.d * total as f32
}

// ─── Fused quantized matvec ─────────────────────────────────────────────────

/// Q2_K matvec: quantizes input to Q8_K, then uses integer dot product.
pub fn q2k_matvec(input: &[f32], data: &[u8], rows: usize, cols: usize, output: &mut [f32]) {
    let q8_blocks = quantize_row_q8_k(input);
    q2k_matvec_preq(data, rows, cols, &q8_blocks, output);
}

/// Q2_K matvec with pre-quantized Q8_K input.
#[allow(unused_variables)]
pub fn q2k_matvec_preq(
    data: &[u8],
    rows: usize,
    cols: usize,
    q8_blocks: &[BlockQ8K],
    output: &mut [f32],
) {
    let blocks_per_row = cols / QK_K;
    let block_bytes = 84;
    let row_bytes = blocks_per_row * block_bytes;

    #[cfg(feature = "parallel")]
    {
        use rayon::prelude::*;
        debug_assert!(
            output.len() >= rows,
            "q2k_matvec_preq: output.len()={} < rows={}",
            output.len(),
            rows
        );
        output[..rows]
            .par_iter_mut()
            .enumerate()
            .for_each(|(row, out)| {
                let row_data = &data[row * row_bytes..(row + 1) * row_bytes];
                let mut sumf = 0.0f32;
                for bi in 0..blocks_per_row {
                    sumf += q2k_q8k_dot(
                        &row_data[bi * block_bytes..(bi + 1) * block_bytes],
                        &q8_blocks[bi],
                    );
                }
                *out = sumf;
            });
    }

    #[cfg(not(feature = "parallel"))]
    for row in 0..rows {
        let row_data = &data[row * row_bytes..(row + 1) * row_bytes];
        let mut sumf = 0.0f32;
        for bi in 0..blocks_per_row {
            sumf += q2k_q8k_dot(
                &row_data[bi * block_bytes..(bi + 1) * block_bytes],
                &q8_blocks[bi],
            );
        }
        output[row] = sumf;
    }
}

/// Q5_K matvec: quantizes input to Q8_K, then uses integer dot product.
pub fn q5k_matvec(input: &[f32], data: &[u8], rows: usize, cols: usize, output: &mut [f32]) {
    let q8_blocks = quantize_row_q8_k(input);
    q5k_matvec_preq(data, rows, cols, &q8_blocks, output);
}

/// Q5_K matvec with pre-quantized Q8_K input.
#[allow(unused_variables)]
pub fn q5k_matvec_preq(
    data: &[u8],
    rows: usize,
    cols: usize,
    q8_blocks: &[BlockQ8K],
    output: &mut [f32],
) {
    let blocks_per_row = cols / QK_K;
    let block_bytes = 176;
    let row_bytes = blocks_per_row * block_bytes;

    #[cfg(feature = "parallel")]
    {
        use rayon::prelude::*;
        debug_assert!(
            output.len() >= rows,
            "q5k_matvec_preq: output.len()={} < rows={}",
            output.len(),
            rows
        );
        output[..rows]
            .par_iter_mut()
            .enumerate()
            .for_each(|(row, out)| {
                let row_data = &data[row * row_bytes..(row + 1) * row_bytes];
                let mut sumf = 0.0f32;
                for bi in 0..blocks_per_row {
                    sumf += q5k_q8k_dot(
                        &row_data[bi * block_bytes..(bi + 1) * block_bytes],
                        &q8_blocks[bi],
                    );
                }
                *out = sumf;
            });
    }

    #[cfg(not(feature = "parallel"))]
    for row in 0..rows {
        let row_data = &data[row * row_bytes..(row + 1) * row_bytes];
        let mut sumf = 0.0f32;
        for bi in 0..blocks_per_row {
            sumf += q5k_q8k_dot(
                &row_data[bi * block_bytes..(bi + 1) * block_bytes],
                &q8_blocks[bi],
            );
        }
        output[row] = sumf;
    }
}

/// Q3_K matvec: quantizes input to Q8_K, then uses integer dot product.
pub fn q3k_matvec(input: &[f32], data: &[u8], rows: usize, cols: usize, output: &mut [f32]) {
    let q8_blocks = quantize_row_q8_k(input);
    q3k_matvec_preq(data, rows, cols, &q8_blocks, output);
}

/// Q3_K matvec with pre-quantized Q8_K input.
#[allow(unused_variables)]
pub fn q3k_matvec_preq(
    data: &[u8],
    rows: usize,
    cols: usize,
    q8_blocks: &[BlockQ8K],
    output: &mut [f32],
) {
    let blocks_per_row = cols / QK_K;
    let block_bytes = 110;
    let row_bytes = blocks_per_row * block_bytes;

    #[cfg(feature = "parallel")]
    {
        use rayon::prelude::*;
        debug_assert!(
            output.len() >= rows,
            "q3k_matvec_preq: output.len()={} < rows={}",
            output.len(),
            rows
        );
        output[..rows]
            .par_iter_mut()
            .enumerate()
            .for_each(|(row, out)| {
                let row_data = &data[row * row_bytes..(row + 1) * row_bytes];
                let mut sumf = 0.0f32;
                for bi in 0..blocks_per_row {
                    sumf += q3k_q8k_dot(
                        &row_data[bi * block_bytes..(bi + 1) * block_bytes],
                        &q8_blocks[bi],
                    );
                }
                *out = sumf;
            });
    }

    #[cfg(not(feature = "parallel"))]
    for row in 0..rows {
        let row_data = &data[row * row_bytes..(row + 1) * row_bytes];
        let mut sumf = 0.0f32;
        for bi in 0..blocks_per_row {
            sumf += q3k_q8k_dot(
                &row_data[bi * block_bytes..(bi + 1) * block_bytes],
                &q8_blocks[bi],
            );
        }
        output[row] = sumf;
    }
}

/// Q4_K matvec: quantizes input to Q8_K, then uses integer dot product.
pub fn q4k_matvec(input: &[f32], data: &[u8], rows: usize, cols: usize, output: &mut [f32]) {
    let q8_blocks = quantize_row_q8_k(input);
    q4k_matvec_preq(data, rows, cols, &q8_blocks, output);
}

/// Q4_K matvec with pre-quantized Q8_K input (avoids redundant quantization).
#[allow(unused_variables)]
pub fn q4k_matvec_preq(
    data: &[u8],
    rows: usize,
    cols: usize,
    q8_blocks: &[BlockQ8K],
    output: &mut [f32],
) {
    let blocks_per_row = cols / QK_K;
    let block_bytes = 144;
    let row_bytes = blocks_per_row * block_bytes;

    // Phase X.3.e.3.40 diagnostic: llama.cpp-compatible cross-block
    // accumulation pattern (matches `ggml_vec_dot_q4_K_q8_K_generic`).
    // Instead of collapsing each block's 8 sub-block int32 products into
    // a single f32 per block, this path keeps `sums[8]` f32 accumulators
    // per row and reduces them at the very end. Enabled with
    // ALICE_Q4K_LLAMACPP_ACCUM=1. Test target: PPL should approach
    // llama.cpp CPU 5.75 on Llama 3.2 3B 100 tok wikitext.
    if std::env::var_os("ALICE_Q4K_LLAMACPP_ACCUM").is_some() {
        q4k_matvec_preq_llamacpp_accum(data, rows, cols, q8_blocks, output);
        return;
    }

    // Phase X.3.e.3.37 diagnostic: bypass the Q4_K × Q8_K integer dot path
    // when ALICE_BYPASS_Q8K_ACTIVATION is set — dequantize both operands to
    // f32 and do a scalar f32 dot product instead. If this changes PPL
    // significantly, the integer dot kernel is the divergence source.
    // Not lossless (Q8_K quant of activation happened upstream and is
    // preserved in the block sums), but avoids any remaining bias inside
    // the integer accumulator / super-block scale unpacking.
    if std::env::var_os("ALICE_BYPASS_Q8K_ACTIVATION").is_some() {
        let mut input_f32 = vec![0.0f32; cols];
        for (b, block) in q8_blocks.iter().enumerate() {
            let base = b * QK_K;
            for i in 0..QK_K {
                if base + i >= cols {
                    break;
                }
                input_f32[base + i] = block.d * block.qs[i] as f32;
            }
        }
        let mut row_f32 = vec![0.0f32; cols];
        for row in 0..rows {
            let row_data = &data[row * row_bytes..(row + 1) * row_bytes];
            dequantize_q4_k(row_data, &mut row_f32);
            let mut acc: f64 = 0.0;
            for i in 0..cols {
                acc += (row_f32[i] as f64) * (input_f32[i] as f64);
            }
            output[row] = acc as f32;
        }
        return;
    }

    #[cfg(feature = "parallel")]
    {
        use rayon::prelude::*;
        debug_assert!(
            output.len() >= rows,
            "q4k_matvec_preq: output.len()={} < rows={}",
            output.len(),
            rows
        );
        output[..rows]
            .par_iter_mut()
            .enumerate()
            .for_each(|(row, out)| {
                let row_data = &data[row * row_bytes..(row + 1) * row_bytes];
                let mut sumf = 0.0f32;
                for bi in 0..blocks_per_row {
                    sumf += q4k_q8k_dot(
                        &row_data[bi * block_bytes..(bi + 1) * block_bytes],
                        &q8_blocks[bi],
                    );
                }
                *out = sumf;
            });
    }

    #[cfg(not(feature = "parallel"))]
    for row in 0..rows {
        let row_data = &data[row * row_bytes..(row + 1) * row_bytes];
        let mut sumf = 0.0f32;
        for bi in 0..blocks_per_row {
            sumf += q4k_q8k_dot(
                &row_data[bi * block_bytes..(bi + 1) * block_bytes],
                &q8_blocks[bi],
            );
        }
        output[row] = sumf;
    }
}

/// Phase X.3.e.3.40 diagnostic: Q4_K matmul with llama.cpp-compatible
/// cross-block f32 accumulation. Ports `ggml_vec_dot_q4_K_q8_K_generic`
/// verbatim: 8-lane `sums[f32; 8]` survives across blocks per row, and
/// only reduces at the end. This differs from ALICE's default scalar /
/// NEON paths which collapse the 8-lane int32 accumulator into a single
/// f32 per block. The two are algebraically equivalent but produce
/// different f32 rounding paths — this port lets us test whether the
/// 2.68× PPL divergence vs llama.cpp is explained by the accumulation
/// order (Phase 4 First-Divergence-Point analysis showed ~1-3e-3 abs
/// diff at Q_proj which compounds through 28 layers).
fn q4k_matvec_preq_llamacpp_accum(
    data: &[u8],
    rows: usize,
    cols: usize,
    q8_blocks: &[BlockQ8K],
    output: &mut [f32],
) {
    const KMASK1: u32 = 0x3f3f_3f3f;
    const KMASK2: u32 = 0x0f0f_0f0f;
    const KMASK3: u32 = 0x0303_0303;
    let blocks_per_row = cols / QK_K;
    let block_bytes = 144;
    let row_bytes = blocks_per_row * block_bytes;

    for row in 0..rows {
        let row_data = &data[row * row_bytes..(row + 1) * row_bytes];

        // llama.cpp: float sums[8] = {0}; survives across all blocks in row.
        let mut sums = [0.0f32; 8];
        let mut sumf = 0.0f32;

        for bi in 0..blocks_per_row {
            let block = &row_data[bi * block_bytes..(bi + 1) * block_bytes];
            let q8k = &q8_blocks[bi];

            // Unpack Q4_K header (matches scalar path exactly).
            let d = f16_to_f32(u16::from_le_bytes([block[0], block[1]]));
            let dmin = f16_to_f32(u16::from_le_bytes([block[2], block[3]]));
            let q4 = &block[16..144];

            // Unpack quantized values into aux8[256] (low then high nibbles).
            let mut aux8 = [0i8; QK_K];
            let mut a_off = 0usize;
            let mut q4_off = 0usize;
            for _ in 0..4 {
                for l in 0..32 {
                    aux8[a_off + l] = (q4[q4_off + l] & 0xF) as i8;
                }
                a_off += 32;
                for l in 0..32 {
                    aux8[a_off + l] = (q4[q4_off + l] >> 4) as i8;
                }
                a_off += 32;
                q4_off += 32;
            }

            // Unpack scales/mins via kmask bit-packing (matches scalar).
            let mut utmp = [0u32; 4];
            let sb = &block[4..16];
            utmp[0] = u32::from_le_bytes([sb[0], sb[1], sb[2], sb[3]]);
            utmp[1] = u32::from_le_bytes([sb[4], sb[5], sb[6], sb[7]]);
            utmp[2] = u32::from_le_bytes([sb[8], sb[9], sb[10], sb[11]]);
            utmp[3] = ((utmp[2] >> 4) & KMASK2) | (((utmp[1] >> 6) & KMASK3) << 4);
            let uaux = utmp[1] & KMASK1;
            utmp[1] = (utmp[2] & KMASK2) | (((utmp[0] >> 6) & KMASK3) << 4);
            utmp[2] = uaux;
            utmp[0] &= KMASK1;
            let s0 = utmp[0].to_le_bytes();
            let s1 = utmp[1].to_le_bytes();
            let m0 = utmp[2].to_le_bytes();
            let m1 = utmp[3].to_le_bytes();
            let scales = [s0[0], s0[1], s0[2], s0[3], s1[0], s1[1], s1[2], s1[3]];
            let mins = [m0[0], m0[1], m0[2], m0[3], m1[0], m1[1], m1[2], m1[3]];

            // Mins correction (matches scalar).
            let mut sumi = 0i32;
            for j in 0..16 {
                sumi += q8k.bsums[j] as i32 * mins[j / 2] as i32;
            }

            // Per-block 8-lane int32 accumulator (llama.cpp aux32[8]).
            let mut aux32 = [0i32; 8];
            let mut a_idx = 0usize;
            let mut q8_idx = 0usize;
            for is in 0..8 {
                let scale = scales[is] as i32;
                for _ in 0..4 {
                    for l in 0..8 {
                        aux32[l] += scale * (q8k.qs[q8_idx + l] as i32 * aux8[a_idx + l] as i32);
                    }
                    q8_idx += 8;
                    a_idx += 8;
                }
            }

            // Per-block scale and 8-lane f32 accumulation into sums[].
            // llama.cpp: for (l=0..8) sums[l] += d * aux32[l];
            let d_all = d * q8k.d;
            for l in 0..8 {
                sums[l] += d_all * aux32[l] as f32;
            }
            // Mins contribution accumulates directly into sumf, per block.
            let dmin_all = dmin * q8k.d;
            sumf -= dmin_all * sumi as f32;
        }

        // Final reduction: sumf += Σ sums[l]. This is the key ordering
        // difference from ALICE's default path — the 8 lanes stay separate
        // across all blocks and only merge at the very end, matching
        // llama.cpp's f32 rounding path exactly.
        for l in 0..8 {
            sumf += sums[l];
        }
        output[row] = sumf;
    }
}

/// Fused Q8_0 dequantize + matrix-vector multiply.
///
/// Runtime-dispatched on x86_64: AVX-512F → AVX2+FMA → scalar fallback.
pub fn q8_0_matvec(input: &[f32], data: &[u8], rows: usize, cols: usize, output: &mut [f32]) {
    let blocks_per_row = cols / QK8_0;
    let block_bytes = 34;
    let row_bytes = blocks_per_row * block_bytes;

    #[cfg(target_arch = "x86_64")]
    {
        // AVX-512 requires 16-lane loads from `input`, which forces the
        // column count to a multiple of 16. Q8_0's 32-lane block already
        // satisfies this — every FMA lane maps 1:1 to a block element.
        if x86_avx512bw_supported() {
            for row in 0..rows {
                let row_data = &data[row * row_bytes..(row + 1) * row_bytes];
                // SAFETY: guarded by runtime AVX-512F detection.
                output[row] = unsafe { avx512_dot::q8_0_matvec_row(input, row_data, cols) };
            }
            return;
        }
        if x86_avx2_supported() {
            for row in 0..rows {
                let row_data = &data[row * row_bytes..(row + 1) * row_bytes];
                // SAFETY: guarded by runtime AVX2 detection.
                output[row] = unsafe { avx2_dot::q8_0_matvec_row(input, row_data, cols) };
            }
            return;
        }
    }

    #[cfg(target_arch = "aarch64")]
    {
        // SAFETY: NEON is baseline on every aarch64 target Rust supports, so
        // no runtime feature probe is needed — the `#[target_feature]` on the
        // kernel matches unconditionally. Parity vs scalar is FMA-rounding
        // approximate (see `q8_0_neon_matches_scalar_within_tol`), not
        // bit-exact, because scalar uses separate mul + add whereas the NEON
        // path uses `vfmaq_f32` (fused mul-add, single rounding).
        for row in 0..rows {
            let row_data = &data[row * row_bytes..(row + 1) * row_bytes];
            output[row] = unsafe { neon_dot::q8_0_matvec_row(input, row_data, cols) };
        }
        return;
    }

    #[allow(unreachable_code)]
    for row in 0..rows {
        let mut acc = 0.0f32;
        let row_data = &data[row * row_bytes..(row + 1) * row_bytes];

        for bi in 0..blocks_per_row {
            let off = bi * block_bytes;
            let d = f16_to_f32(u16::from_le_bytes([row_data[off], row_data[off + 1]]));
            let col_base = bi * QK8_0;

            for l in 0..QK8_0 {
                let w = d * f32::from(row_data[off + 2 + l] as i8);
                acc += w * input[col_base + l];
            }
        }

        output[row] = acc;
    }
}

/// Fused Q4_0 dequantize + matrix-vector multiply.
pub fn q4_0_matvec(input: &[f32], data: &[u8], rows: usize, cols: usize, output: &mut [f32]) {
    let blocks_per_row = cols / QK8_0;
    let block_bytes = 18;
    let row_bytes = blocks_per_row * block_bytes;

    for row in 0..rows {
        let mut acc = 0.0f32;
        let row_data = &data[row * row_bytes..(row + 1) * row_bytes];

        for bi in 0..blocks_per_row {
            let off = bi * block_bytes;
            let d = f16_to_f32(u16::from_le_bytes([row_data[off], row_data[off + 1]]));
            let col_base = bi * QK8_0;

            for j in 0..16 {
                let qs = row_data[off + 2 + j];
                let x0 = ((qs & 0x0F) as i32) - 8;
                let x1 = ((qs >> 4) as i32) - 8;
                acc += d * (x0 as f32) * input[col_base + j];
                acc += d * (x1 as f32) * input[col_base + j + 16];
            }
        }

        output[row] = acc;
    }
}

/// Fused Q4_1 dequantize + matrix-vector multiply.
pub fn q4_1_matvec(input: &[f32], data: &[u8], rows: usize, cols: usize, output: &mut [f32]) {
    let blocks_per_row = cols / QK8_0;
    let block_bytes = 20;
    let row_bytes = blocks_per_row * block_bytes;

    for row in 0..rows {
        let mut acc = 0.0f32;
        let row_data = &data[row * row_bytes..(row + 1) * row_bytes];

        for bi in 0..blocks_per_row {
            let off = bi * block_bytes;
            let d = f16_to_f32(u16::from_le_bytes([row_data[off], row_data[off + 1]]));
            let m = f16_to_f32(u16::from_le_bytes([row_data[off + 2], row_data[off + 3]]));
            let col_base = bi * QK8_0;

            for j in 0..16 {
                let qs = row_data[off + 4 + j];
                let x0 = (qs & 0x0F) as f32;
                let x1 = (qs >> 4) as f32;
                acc += (d * x0 + m) * input[col_base + j];
                acc += (d * x1 + m) * input[col_base + j + 16];
            }
        }

        output[row] = acc;
    }
}

/// Fused Q5_0 dequantize + matrix-vector multiply.
pub fn q5_0_matvec(input: &[f32], data: &[u8], rows: usize, cols: usize, output: &mut [f32]) {
    let blocks_per_row = cols / QK8_0;
    let block_bytes = 22;
    let row_bytes = blocks_per_row * block_bytes;

    for row in 0..rows {
        let mut acc = 0.0f32;
        let row_data = &data[row * row_bytes..(row + 1) * row_bytes];

        for bi in 0..blocks_per_row {
            let off = bi * block_bytes;
            let d = f16_to_f32(u16::from_le_bytes([row_data[off], row_data[off + 1]]));
            let qh = u32::from_le_bytes([
                row_data[off + 2],
                row_data[off + 3],
                row_data[off + 4],
                row_data[off + 5],
            ]);
            let col_base = bi * QK8_0;

            for j in 0..16 {
                let qs = row_data[off + 6 + j];
                let xh_0 = ((qh >> j) << 4) & 0x10;
                let xh_1 = (qh >> (j + 12)) & 0x10;
                let x0 = (((qs & 0x0F) as u32) | xh_0) as i32 - 16;
                let x1 = (((qs >> 4) as u32) | xh_1) as i32 - 16;
                acc += d * (x0 as f32) * input[col_base + j];
                acc += d * (x1 as f32) * input[col_base + j + 16];
            }
        }

        output[row] = acc;
    }
}

/// Fused Q5_1 dequantize + matrix-vector multiply.
pub fn q5_1_matvec(input: &[f32], data: &[u8], rows: usize, cols: usize, output: &mut [f32]) {
    let blocks_per_row = cols / QK8_0;
    let block_bytes = 24;
    let row_bytes = blocks_per_row * block_bytes;

    for row in 0..rows {
        let mut acc = 0.0f32;
        let row_data = &data[row * row_bytes..(row + 1) * row_bytes];

        for bi in 0..blocks_per_row {
            let off = bi * block_bytes;
            let d = f16_to_f32(u16::from_le_bytes([row_data[off], row_data[off + 1]]));
            let m = f16_to_f32(u16::from_le_bytes([row_data[off + 2], row_data[off + 3]]));
            let qh = u32::from_le_bytes([
                row_data[off + 4],
                row_data[off + 5],
                row_data[off + 6],
                row_data[off + 7],
            ]);
            let col_base = bi * QK8_0;

            for j in 0..16 {
                let qs = row_data[off + 8 + j];
                let xh_0 = ((qh >> j) << 4) & 0x10;
                let xh_1 = (qh >> (j + 12)) & 0x10;
                let x0 = ((qs & 0x0F) as u32) | xh_0;
                let x1 = ((qs >> 4) as u32) | xh_1;
                acc += (d * (x0 as f32) + m) * input[col_base + j];
                acc += (d * (x1 as f32) + m) * input[col_base + j + 16];
            }
        }

        output[row] = acc;
    }
}

/// Fused IQ4_XS dequantize + matrix-vector multiply.
pub fn iq4_xs_matvec(input: &[f32], data: &[u8], rows: usize, cols: usize, output: &mut [f32]) {
    let blocks_per_row = cols / QK_K;
    let block_bytes = 136;
    let row_bytes = blocks_per_row * block_bytes;

    for row in 0..rows {
        let mut acc = 0.0f32;
        let row_data = &data[row * row_bytes..(row + 1) * row_bytes];

        for bi in 0..blocks_per_row {
            let off = bi * block_bytes;
            let d = f16_to_f32(u16::from_le_bytes([row_data[off], row_data[off + 1]]));
            let scales_h = u16::from_le_bytes([row_data[off + 2], row_data[off + 3]]);
            let scales_l = [
                row_data[off + 4],
                row_data[off + 5],
                row_data[off + 6],
                row_data[off + 7],
            ];
            let qs = &row_data[off + 8..off + 8 + 128];
            let col_base = bi * QK_K;

            for ib in 0..8 {
                let ls_lo = (scales_l[ib / 2] >> (4 * (ib as u8 & 1))) & 0x0F;
                let ls_hi = ((scales_h >> (2 * ib)) & 0x3) as u8;
                let ls = ((ls_hi << 4) | ls_lo) as i32;
                let dl = d * ((ls - 32) as f32);
                let qs_base = ib * 16;
                let sub_col_base = col_base + ib * 32;
                for j in 0..16 {
                    let byte = qs[qs_base + j];
                    let n0 = (byte & 0x0F) as usize;
                    let n1 = (byte >> 4) as usize;
                    acc += dl * f32::from(KVALUES_IQ4NL[n0]) * input[sub_col_base + j];
                    acc += dl * f32::from(KVALUES_IQ4NL[n1]) * input[sub_col_base + j + 16];
                }
            }
        }

        output[row] = acc;
    }
}

/// Fused F16 matvec.
pub fn f16_matvec(input: &[f32], data: &[u8], rows: usize, cols: usize, output: &mut [f32]) {
    for row in 0..rows {
        let mut acc = 0.0f32;
        let row_start = row * cols * 2;
        for col in 0..cols {
            let off = row_start + col * 2;
            let w = f16_to_f32(u16::from_le_bytes([data[off], data[off + 1]]));
            acc += w * input[col];
        }
        output[row] = acc;
    }
}

/// Fused F32 matvec.
pub fn f32_matvec(input: &[f32], data: &[u8], rows: usize, cols: usize, output: &mut [f32]) {
    for row in 0..rows {
        let mut acc = 0.0f32;
        let row_start = row * cols * 4;
        for col in 0..cols {
            let off = row_start + col * 4;
            let w = f32::from_le_bytes([data[off], data[off + 1], data[off + 2], data[off + 3]]);
            acc += w * input[col];
        }
        output[row] = acc;
    }
}

/// Q6_K matvec: quantizes input to Q8_K, then uses integer dot product.
pub fn q6k_matvec(input: &[f32], data: &[u8], rows: usize, cols: usize, output: &mut [f32]) {
    let q8_blocks = quantize_row_q8_k(input);
    q6k_matvec_preq(data, rows, cols, &q8_blocks, output);
}

/// Q6_K matvec with pre-quantized Q8_K input.
#[allow(unused_variables)]
pub fn q6k_matvec_preq(
    data: &[u8],
    rows: usize,
    cols: usize,
    q8_blocks: &[BlockQ8K],
    output: &mut [f32],
) {
    let blocks_per_row = cols / QK_K;
    let block_bytes = 210;
    let row_bytes = blocks_per_row * block_bytes;

    #[cfg(feature = "parallel")]
    {
        use rayon::prelude::*;
        debug_assert!(
            output.len() >= rows,
            "q6k_matvec_preq: output.len()={} < rows={}",
            output.len(),
            rows
        );
        output[..rows]
            .par_iter_mut()
            .enumerate()
            .for_each(|(row, out)| {
                let row_data = &data[row * row_bytes..(row + 1) * row_bytes];
                let mut sumf = 0.0f32;
                for bi in 0..blocks_per_row {
                    sumf += q6k_q8k_dot(
                        &row_data[bi * block_bytes..(bi + 1) * block_bytes],
                        &q8_blocks[bi],
                    );
                }
                *out = sumf;
            });
    }

    #[cfg(not(feature = "parallel"))]
    for row in 0..rows {
        let row_data = &data[row * row_bytes..(row + 1) * row_bytes];
        let mut sumf = 0.0f32;
        for bi in 0..blocks_per_row {
            sumf += q6k_q8k_dot(
                &row_data[bi * block_bytes..(bi + 1) * block_bytes],
                &q8_blocks[bi],
            );
        }
        output[row] = sumf;
    }
}

/// IQ4_XS block × Q8_K block dot product.
///
/// Each IQ4_XS block encodes 256 elements as 8 sub-blocks of 32 elements,
/// each sub-block sharing a 6-bit scale and 4-bit indices into
/// [`KVALUES_IQ4NL`]. The 6-bit scale is offset by −32 to allow signed
/// values (roughly −32..31). The final dequantized weight for element `k`
/// in sub-block `ib` is `d * (ls[ib] - 32) * KVALUES_IQ4NL[nibble_k]`.
///
/// The dot product accumulates in `i32` per sub-block, then applies the
/// per-block scale factors (`d_w * d_i`) once at the end.
fn iq4_xs_q8k_dot(iq4_xs_block: &[u8], q8k: &BlockQ8K) -> f32 {
    let d_w = f16_to_f32(u16::from_le_bytes([iq4_xs_block[0], iq4_xs_block[1]]));
    let scales_h = u16::from_le_bytes([iq4_xs_block[2], iq4_xs_block[3]]);
    let scales_l = [
        iq4_xs_block[4],
        iq4_xs_block[5],
        iq4_xs_block[6],
        iq4_xs_block[7],
    ];
    let qs = &iq4_xs_block[8..8 + 128];

    let mut sum_i32 = 0i64;
    for ib in 0..8 {
        let ls_lo = (scales_l[ib / 2] >> (4 * (ib as u8 & 1))) & 0x0F;
        let ls_hi = ((scales_h >> (2 * ib)) & 0x3) as u8;
        let ls = (((ls_hi << 4) | ls_lo) as i32) - 32;
        let qs_base = ib * 16;
        let q8_base = ib * 32;
        let mut sub_dot = 0i32;
        for j in 0..16 {
            let byte = qs[qs_base + j];
            let n0 = (byte & 0x0F) as usize;
            let n1 = (byte >> 4) as usize;
            sub_dot += i32::from(KVALUES_IQ4NL[n0]) * i32::from(q8k.qs[q8_base + j]);
            sub_dot += i32::from(KVALUES_IQ4NL[n1]) * i32::from(q8k.qs[q8_base + j + 16]);
        }
        sum_i32 += i64::from(ls) * i64::from(sub_dot);
    }
    d_w * q8k.d * (sum_i32 as f32)
}

/// Fused IQ4_XS matvec with pre-quantized Q8_K input.
pub fn iq4_xs_matvec_preq(
    data: &[u8],
    rows: usize,
    cols: usize,
    q8_blocks: &[BlockQ8K],
    output: &mut [f32],
) {
    // Historical comment (pre-fix): `rows` was unused under `feature =
    // "parallel"` because rayon iterated `output.par_iter_mut()` directly.
    // 2026-09-12: parallel path now iterates `output[..rows]` explicitly to
    // match the non-parallel path and prevent OOB panics when callers pass
    // an oversized output buffer (see llama3.rs `q_buf = vec![0; q_dim * 2]`
    // for gated_output arch shared with standard Llama arch). `rows` is now
    // used in both paths — no more unused-variable dance needed.
    let blocks_per_row = cols / QK_K;
    let block_bytes = 136;
    let row_bytes = blocks_per_row * block_bytes;

    #[cfg(feature = "parallel")]
    {
        use rayon::prelude::*;
        debug_assert!(
            output.len() >= rows,
            "iq4_xs_matvec_preq: output.len()={} < rows={}",
            output.len(),
            rows
        );
        output[..rows]
            .par_iter_mut()
            .enumerate()
            .for_each(|(row, out)| {
                let row_data = &data[row * row_bytes..(row + 1) * row_bytes];
                let mut sumf = 0.0f32;
                for bi in 0..blocks_per_row {
                    sumf += iq4_xs_q8k_dot(
                        &row_data[bi * block_bytes..(bi + 1) * block_bytes],
                        &q8_blocks[bi],
                    );
                }
                *out = sumf;
            });
    }

    #[cfg(not(feature = "parallel"))]
    for row in 0..rows {
        let row_data = &data[row * row_bytes..(row + 1) * row_bytes];
        let mut sumf = 0.0f32;
        for bi in 0..blocks_per_row {
            sumf += iq4_xs_q8k_dot(
                &row_data[bi * block_bytes..(bi + 1) * block_bytes],
                &q8_blocks[bi],
            );
        }
        output[row] = sumf;
    }
}

/// Dispatch matvec based on quantization type.
pub fn quantized_matvec(
    input: &[f32],
    data: &[u8],
    qtype: GgmlType,
    rows: usize,
    cols: usize,
    output: &mut [f32],
) {
    match qtype {
        GgmlType::Q2_K => q2k_matvec(input, data, rows, cols, output),
        GgmlType::Q3_K => q3k_matvec(input, data, rows, cols, output),
        GgmlType::Q4_K => q4k_matvec(input, data, rows, cols, output),
        GgmlType::Q5_K => q5k_matvec(input, data, rows, cols, output),
        GgmlType::Q6_K => q6k_matvec(input, data, rows, cols, output),
        GgmlType::Q4_0 => q4_0_matvec(input, data, rows, cols, output),
        GgmlType::Q4_1 => q4_1_matvec(input, data, rows, cols, output),
        GgmlType::Q5_0 => q5_0_matvec(input, data, rows, cols, output),
        GgmlType::Q5_1 => q5_1_matvec(input, data, rows, cols, output),
        GgmlType::IQ4_XS => iq4_xs_matvec(input, data, rows, cols, output),
        GgmlType::Q8_0 => q8_0_matvec(input, data, rows, cols, output),
        GgmlType::F16 => f16_matvec(input, data, rows, cols, output),
        GgmlType::F32 => f32_matvec(input, data, rows, cols, output),
        GgmlType::Q1_0 => q1_0_matvec_fallback(input, data, rows, cols, output),
        GgmlType::Q2_0 => q2_0_matvec_fallback(input, data, rows, cols, output),
        GgmlType::Mxfp4 => mxfp4_matvec_dispatched(input, data, rows, cols, output),
        // Phase X.4.b.7: IQ1_S matvec — dequantize each row on the fly
        // then call the standard f32_matvec pattern (correctness-first,
        // not optimized for K3 expert cubes).
        GgmlType::IQ1_S => iq1_s_matvec_fallback(input, data, rows, cols, output),
        GgmlType::Other(_) => panic!("unsupported quantization type: {qtype:?}"),
    }
}

/// Correctness-first MXFP4 matvec fallback: dequantize whole row to f32
/// then call an inline `f32_matvec`.
///
/// Retained as the parity reference for
/// [`mxfp4_matvec_fused_scalar`] — the two kernels must agree
/// bit-exact because both dequantize blocks the same way and
/// accumulate in `f32` in the same element order. Any future SIMD
/// kernel (Phase X.4.f.2+) is validated against this fallback.
///
/// Not on the hot path since Phase X.4.f.1 (`quantized_matvec`
/// routing switched to the fused kernel).
#[cfg(test)]
fn mxfp4_matvec_fallback(input: &[f32], data: &[u8], rows: usize, cols: usize, output: &mut [f32]) {
    let blocks_per_row = cols / QK_MXFP4;
    let row_bytes = blocks_per_row * 17;
    let mut row_f32 = vec![0.0f32; cols];
    for row in 0..rows {
        let row_data = &data[row * row_bytes..(row + 1) * row_bytes];
        dequantize_row_mxfp4(row_data, &mut row_f32);
        let mut acc = 0.0f32;
        for i in 0..cols {
            acc += row_f32[i] * input[i];
        }
        output[row] = acc;
    }
}

/// Fused scalar MXFP4 matvec kernel (Phase X.4.f.1, 2026-07-28).
///
/// Iterates rows and blocks, dequantizes each 32-element block into a
/// stack-resident `[f32; QK_MXFP4]` buffer via
/// [`dequantize_mxfp4_block`], and multiply-accumulates against the
/// input slice in place. Avoids the per-matvec `Vec<f32>` scratch
/// buffer used by [`mxfp4_matvec_fallback`] — the small `[f32; 32]`
/// stack array is L1-resident, and only the reduced `f32` accumulator
/// crosses block iterations.
///
/// # Numerical parity with the fallback
///
/// Both kernels call [`dequantize_mxfp4_block`] and accumulate in `f32`
/// in the same element order (block-major, then intra-block index
/// ascending). They therefore agree **bit-exact** on identical inputs;
/// the `mxfp4_matvec_fused_scalar_matches_fallback` unit test enforces
/// this for every future edit.
///
/// # Callers
///
/// - `quantized_matvec` dispatches `GgmlType::Mxfp4` here (Phase
///   X.4.f.1 landing switched from fallback → fused_scalar).
/// - The free function [`mxfp4_matvec`] iterates a [`MxfP4Matrix`] and
///   calls this per row.
///
/// # Panics
///
/// Panics if `cols` is not a multiple of `QK_MXFP4 = 32`, if `data.len()
/// < rows * (cols/32) * 17`, if `input.len() < cols`, or if
/// `output.len() < rows`.
fn mxfp4_matvec_fused_scalar(
    input: &[f32],
    data: &[u8],
    rows: usize,
    cols: usize,
    output: &mut [f32],
) {
    assert!(
        cols.is_multiple_of(QK_MXFP4),
        "cols {cols} must be a multiple of QK_MXFP4 = {QK_MXFP4}"
    );
    assert!(
        input.len() >= cols,
        "input length {} < cols {cols}",
        input.len()
    );
    assert!(
        output.len() >= rows,
        "output length {} < rows {rows}",
        output.len()
    );

    let blocks_per_row = cols / QK_MXFP4;
    let row_bytes = blocks_per_row * 17;
    assert!(
        data.len() >= rows * row_bytes,
        "data length {} < rows*row_bytes {}",
        data.len(),
        rows * row_bytes
    );

    for row in 0..rows {
        let row_data = &data[row * row_bytes..(row + 1) * row_bytes];
        let mut acc = 0.0_f32;
        for blk in 0..blocks_per_row {
            let block = &row_data[blk * 17..blk * 17 + 17];
            // Stack-allocated 32-element scratch (128 bytes) — L1-resident.
            let dq = dequantize_mxfp4_block(block);
            let inp_off = blk * QK_MXFP4;
            // Same accumulation order as fallback: block-major, intra-block ascending.
            for i in 0..QK_MXFP4 {
                acc += dq[i] * input[inp_off + i];
            }
        }
        output[row] = acc;
    }
}

/// Runtime-dispatched MXFP4 matvec (Phase X.4.f.2.a-b, 2026-07-28).
///
/// - **aarch64** (ARM64 embedded board / Apple Silicon / any ARMv8-A): dispatches per
///   row to [`neon_dot::mxfp4_dot_row`] which does scalar per-block
///   dequant followed by NEON `vfmaq_f32` 4-wide FMA × 8 iterations.
/// - **x86_64 with AVX2** (Ryzen / Xeon / any Haswell+): dispatches
///   per row to [`avx2_dot::mxfp4_dot_row`] which does scalar
///   per-block dequant followed by AVX2 `_mm256_fmadd_ps` 8-wide
///   FMA × 4 iterations. Runtime-detected via
///   `is_x86_feature_detected!("avx2")`; pre-AVX2 x86 machines
///   (Ivy Bridge and older) fall through to the scalar kernel.
/// - **other archs / non-AVX2 x86**: falls through to
///   [`mxfp4_matvec_fused_scalar`] (pure scalar, same semantics).
///
/// Both SIMD paths use vector horizontal reduction so they are
/// **tolerance-parity** with the scalar kernel (~1e-4 relative), not
/// bit-exact. See `mxfp4_matvec_neon_matches_scalar` (aarch64) and
/// `mxfp4_matvec_avx2_matches_scalar` (x86_64) in the tests.
fn mxfp4_matvec_dispatched(
    input: &[f32],
    data: &[u8],
    rows: usize,
    cols: usize,
    output: &mut [f32],
) {
    // Shape validation is architecture-independent; keep it out of the
    // cfg branches so every path shares the same error surface.
    assert!(
        cols.is_multiple_of(QK_MXFP4),
        "cols {cols} must be a multiple of QK_MXFP4 = {QK_MXFP4}"
    );
    assert!(
        input.len() >= cols,
        "input length {} < cols {cols}",
        input.len()
    );
    assert!(
        output.len() >= rows,
        "output length {} < rows {rows}",
        output.len()
    );
    let blocks_per_row = cols / QK_MXFP4;
    let row_bytes = blocks_per_row * 17;
    assert!(
        data.len() >= rows * row_bytes,
        "data length {} < rows*row_bytes {}",
        data.len(),
        rows * row_bytes
    );

    #[cfg(target_arch = "aarch64")]
    {
        for row in 0..rows {
            let row_data = &data[row * row_bytes..(row + 1) * row_bytes];
            // SAFETY: NEON is baseline on every aarch64 target Rust
            // supports; input length has been validated above.
            output[row] = unsafe { neon_dot::mxfp4_dot_row(row_data, &input[..cols]) };
        }
        return;
    }

    #[cfg(target_arch = "x86_64")]
    {
        if is_x86_feature_detected!("avx2") && is_x86_feature_detected!("fma") {
            for row in 0..rows {
                let row_data = &data[row * row_bytes..(row + 1) * row_bytes];
                // SAFETY: AVX2 + FMA feature-detected above; input
                // length has been validated above.
                output[row] = unsafe { avx2_dot::mxfp4_dot_row(row_data, &input[..cols]) };
            }
            return;
        }
    }

    // Fallback: pure scalar. Reached on non-AVX2 x86_64 machines
    // (Ivy Bridge and older) and on any architecture that is neither
    // aarch64 nor x86_64. On aarch64 the branch above always returns,
    // so the compiler flags this as unreachable — same pattern as
    // `q1_0_matvec_fallback` below.
    #[allow(unreachable_code)]
    mxfp4_matvec_fused_scalar(input, data, rows, cols, output);
}

/// Matvec entry for PrismML `Q1_0` (Bonsai 27B binary g128).
///
/// aarch64 → NEON `q1_0_dot_row` (bit-lane expansion trick, mirrors the
/// existing Ternary NEON path). Every other target → scalar `dequant + f32
/// dot` fallback. The scalar path is retained for cross-compile coverage
/// and to keep the parity test independent of the SIMD kernel.
fn q1_0_matvec_fallback(input: &[f32], data: &[u8], rows: usize, cols: usize, output: &mut [f32]) {
    let row_bytes = data.len() / rows;
    #[cfg(target_arch = "aarch64")]
    {
        // Precompute per-128-element block sums of the input vector once
        // and reuse across every row. The Q1_0 pos-only kernel folds them
        // into `d * (2 * pos_sum − block_sum)`, halving the per-byte NEON
        // instruction count.
        let n_blocks = cols / 128;
        let mut block_sums = vec![0.0f32; n_blocks];
        for (b, chunk) in input[..cols].as_chunks::<128>().0.iter().enumerate() {
            block_sums[b] = chunk.iter().sum();
        }
        for r in 0..rows {
            let row_data = &data[r * row_bytes..(r + 1) * row_bytes];
            // SAFETY: NEON is baseline on every aarch64 target Rust supports;
            // input length is validated by the caller via `cols`.
            output[r] =
                unsafe { neon_dot::q1_0_dot_row_pos_only(row_data, &input[..cols], &block_sums) };
        }
        return;
    }
    #[allow(unreachable_code)]
    {
        let mut row_buf = vec![0.0f32; cols];
        for r in 0..rows {
            let row_data = &data[r * row_bytes..(r + 1) * row_bytes];
            dequantize_q1_0(row_data, &mut row_buf);
            let mut acc = 0.0f32;
            for i in 0..cols {
                acc += row_buf[i] * input[i];
            }
            output[r] = acc;
        }
    }
}

/// Matvec entry for PrismML `Q2_0` (Bonsai 27B ternary g128).
///
/// aarch64 → NEON `q2_0_dot_row` (broadcast + shift/mask/sub trick to unpack
/// the 2-bit slots into `{−1, 0, +1, +2}` f32 lanes). Every other target →
/// scalar `dequant + f32 dot` fallback.
fn q2_0_matvec_fallback(input: &[f32], data: &[u8], rows: usize, cols: usize, output: &mut [f32]) {
    let row_bytes = data.len() / rows;
    #[cfg(target_arch = "aarch64")]
    {
        for r in 0..rows {
            let row_data = &data[r * row_bytes..(r + 1) * row_bytes];
            // SAFETY: NEON is baseline on aarch64; input length matches `cols`.
            output[r] = unsafe { neon_dot::q2_0_dot_row(row_data, &input[..cols]) };
        }
        return;
    }
    #[allow(unreachable_code)]
    {
        let mut row_buf = vec![0.0f32; cols];
        for r in 0..rows {
            let row_data = &data[r * row_bytes..(r + 1) * row_bytes];
            dequantize_q2_0(row_data, &mut row_buf);
            let mut acc = 0.0f32;
            for i in 0..cols {
                acc += row_buf[i] * input[i];
            }
            output[r] = acc;
        }
    }
}

/// IQ1_S matvec fallback (Phase X.4.b.7 + rayon parallel + fused block).
///
/// Real Kimi K3 GrEarl GGUF-IQ1_S expert cubes use this quant type.
/// Implementation strategy:
///
/// - **Row-parallel via rayon** (feature `parallel`): each output row
///   is a fully independent dequant-and-dot workload, so partition
///   `0..rows` across rayon threads (~10× speedup on an arm64 desktop
///   10-core, near-linear scaling).
/// - **Fused per-block dequant + dot**: instead of allocating a full
///   `cols`-length f32 row buffer, dequantize each 256-element IQ1_S
///   block into a stack-friendly `[f32; 256]` buffer inside the dot
///   loop. Keeps working set in L1 for K3's `n_embd_latent = 3584 =
///   14 blocks × 256`, and eliminates per-row heap allocation
///   (was `Vec<f32>` of 14 KB per row × 3072 rows = 43 MB churn).
///
/// K3 per-token workload: 92 MoE layers × 16 top-k experts × 3 matvec
/// (gate + up + down) = 4416 IQ1_S matvec calls. Combined with rayon
/// parallelism the previously ~30 min sequential path should collapse
/// to ~3-5 min on an arm64 desktop.
#[allow(dead_code)]
fn iq1_s_matvec_fallback(input: &[f32], data: &[u8], rows: usize, cols: usize, output: &mut [f32]) {
    assert!(rows > 0);
    let row_bytes = data.len() / rows;

    #[cfg(feature = "parallel")]
    {
        use rayon::prelude::*;
        output.par_iter_mut().enumerate().for_each(|(r, out)| {
            let row_data = &data[r * row_bytes..(r + 1) * row_bytes];
            *out = iq1_s_row_fused_dot(row_data, input, cols);
        });
    }

    #[cfg(not(feature = "parallel"))]
    for r in 0..rows {
        let row_data = &data[r * row_bytes..(r + 1) * row_bytes];
        output[r] = iq1_s_row_fused_dot(row_data, input, cols);
    }
}

/// Fused per-block IQ1_S dequant + dot for one row.
/// Zero heap allocation (stack-only `[f32; 256]` scratch).
#[allow(dead_code)]
fn iq1_s_row_fused_dot(row_data: &[u8], input: &[f32], cols: usize) -> f32 {
    const BLOCK_ELEMS: usize = 256;
    const BLOCK_BYTES: usize = 50;
    let n_blocks = cols / BLOCK_ELEMS;
    let mut block_buf = [0.0_f32; BLOCK_ELEMS];
    let mut acc = 0.0_f64;
    for b in 0..n_blocks {
        let block_data = &row_data[b * BLOCK_BYTES..(b + 1) * BLOCK_BYTES];
        crate::iq1_s::dequantize_iq1_s(block_data, &mut block_buf);
        let input_slice = &input[b * BLOCK_ELEMS..(b + 1) * BLOCK_ELEMS];
        let mut block_acc = 0.0_f32;
        for i in 0..BLOCK_ELEMS {
            block_acc += block_buf[i] * input_slice[i];
        }
        acc += f64::from(block_acc);
    }
    acc as f32
}

/// Matvec with pre-quantized Q8_K input. For Q4_K/Q6_K weight types,
/// avoids redundant quantization when the same input is used multiple times.
pub fn quantized_matvec_preq(
    data: &[u8],
    qtype: GgmlType,
    rows: usize,
    cols: usize,
    q8_blocks: &[BlockQ8K],
    output: &mut [f32],
) {
    match qtype {
        GgmlType::Q2_K => q2k_matvec_preq(data, rows, cols, q8_blocks, output),
        GgmlType::Q3_K => q3k_matvec_preq(data, rows, cols, q8_blocks, output),
        GgmlType::Q4_K => q4k_matvec_preq(data, rows, cols, q8_blocks, output),
        GgmlType::Q5_K => q5k_matvec_preq(data, rows, cols, q8_blocks, output),
        GgmlType::Q6_K => q6k_matvec_preq(data, rows, cols, q8_blocks, output),
        GgmlType::IQ4_XS => iq4_xs_matvec_preq(data, rows, cols, q8_blocks, output),
        // Bonsai / PrismML ternary formats + Q8_0 weights: no native `_preq`
        // path exists yet, so dequantise the pre-quantised Q8_K activations
        // back to f32 and delegate to the standard `quantized_matvec`
        // dispatcher, which hits the Q1_0 / Q2_0 fallback implementations
        // added in Phase X.1 and the SIMD-accelerated `q8_0_matvec` path.
        //
        // 2026-09-12: Q8_0 fallback added to unblock MiniCPM5-2B-Q8_0 (all
        // 296 linear weights are Q8_0-quantised) sidecar E2E via
        // `alice-llm-server --model MiniCPM5-2B-Q8_0.gguf`. Perf: dequant
        // adds a small overhead (`cols` × f32 conversion per matvec) but
        // Q8_0 × f32 matvec is already the fastest quant path, so end-to-end
        // stays competitive. Native Q8_0 × Q8_K int dot preq is future work.
        GgmlType::Q1_0 | GgmlType::Q2_0 | GgmlType::Q8_0 => {
            let mut input_f32 = vec![0.0f32; cols];
            for (b, block) in q8_blocks.iter().enumerate() {
                let base = b * QK_K;
                for i in 0..QK_K {
                    if base + i >= cols {
                        break;
                    }
                    input_f32[base + i] = block.d * block.qs[i] as f32;
                }
            }
            quantized_matvec(&input_f32, data, qtype, rows, cols, output);
        }
        _ => panic!(
            "quantized_matvec_preq only supports Q2_K-Q6_K, IQ4_XS, Q8_0 and Bonsai Q1_0/Q2_0, got {qtype:?}"
        ),
    }
}

// ─── Tokenizer from GGUF metadata ──────────────────────────────────────────

// ─── GPT-2 byte encoding ────────────────────────────────────────────────────

/// Build GPT-2 byte-to-unicode table.
/// Maps each byte 0-255 to a printable Unicode character.
/// Printable bytes (33-126, 161-172, 174-255) map to themselves.
/// Non-printable bytes map to Unicode 256+ in order.
fn gpt2_byte_to_char() -> [char; 256] {
    let mut table = ['\0'; 256];
    let mut n = 256u32;
    for b in 0u32..256 {
        let is_printable = matches!(b, 33..=126 | 161..=172 | 174..=255);
        let ch = if is_printable {
            b
        } else {
            let c = n;
            n += 1;
            c
        };
        table[b as usize] = char::from_u32(ch).unwrap();
    }
    table
}

/// Build reverse mapping: GPT-2 unicode char → original byte value.
fn gpt2_char_to_byte() -> HashMap<char, u8> {
    let table = gpt2_byte_to_char();
    let mut map = HashMap::with_capacity(256);
    for (b, &ch) in table.iter().enumerate() {
        map.insert(ch, b as u8);
    }
    map
}

// ─── Tokenizer ──────────────────────────────────────────────────────────────

// GGUF token type discriminants (per llama.cpp `enum llama_token_type` in
// `include/llama.h`). Populated in the `tokenizer.ggml.token_type` metadata
// array. See:
// https://github.com/ggerganov/llama.cpp/blob/master/include/llama.h
//
// The full set is declared for spec fidelity even when a variant is not
// currently referenced by production paths.
#[allow(dead_code)]
pub(crate) const TOKEN_TYPE_NORMAL: u32 = 1;
#[allow(dead_code)]
pub(crate) const TOKEN_TYPE_UNKNOWN: u32 = 2;
pub(crate) const TOKEN_TYPE_CONTROL: u32 = 3;
pub(crate) const TOKEN_TYPE_USER_DEFINED: u32 = 4;
#[allow(dead_code)]
pub(crate) const TOKEN_TYPE_UNUSED: u32 = 5;
#[allow(dead_code)]
pub(crate) const TOKEN_TYPE_BYTE: u32 = 6;

// SentencePiece byte fallback token format: `<0xNN>` where NN is a 2-digit
// uppercase or lowercase hex value. Used by SPM tokenizers (Llama-1/2, Gemma 2)
// to represent raw bytes that fall outside the vocabulary.
pub(crate) const SPM_BYTE_FALLBACK_PREFIX: &str = "<0x";
pub(crate) const SPM_BYTE_FALLBACK_SUFFIX: &str = ">";
pub(crate) const SPM_BYTE_FALLBACK_HEX_LEN: usize = 2;

// GPT-2 / HuggingFace special token pattern: `<|xxx|>` (e.g. `<|endoftext|>`,
// `<|im_start|>`). Used as a fallback when `tokenizer.ggml.token_type` is
// absent from the GGUF (older Llama-3 conversions).
pub(crate) const GPT2_SPECIAL_PREFIX: &str = "<|";
pub(crate) const GPT2_SPECIAL_SUFFIX: &str = "|>";

/// Parse a SentencePiece byte fallback token `<0xNN>` into its raw byte value.
///
/// Returns `None` if `s` does not match the exact `<0xNN>` format with
/// 2 hex digits.
pub(crate) fn parse_spm_byte_fallback(s: &str) -> Option<u8> {
    let hex = s
        .strip_prefix(SPM_BYTE_FALLBACK_PREFIX)?
        .strip_suffix(SPM_BYTE_FALLBACK_SUFFIX)?;
    if hex.len() != SPM_BYTE_FALLBACK_HEX_LEN {
        return None;
    }
    u8::from_str_radix(hex, 16).ok()
}

/// Returns true if the given GGUF `token_type` discriminant marks the token
/// as an atomic special token that must not be split by BPE
/// (e.g. Qwen 3 `<think>` / `</think>`, `<tool_call>`).
pub(crate) const fn is_atomic_special_token(token_type: u32) -> bool {
    matches!(token_type, TOKEN_TYPE_CONTROL | TOKEN_TYPE_USER_DEFINED)
}

/// Returns true if `s` is a GPT-2 / HuggingFace style special token
/// (`<|xxx|>`), used as a fallback special-token heuristic when the GGUF
/// omits `tokenizer.ggml.token_type`.
pub(crate) fn is_gpt2_special_marker(s: &str) -> bool {
    s.starts_with(GPT2_SPECIAL_PREFIX) && s.ends_with(GPT2_SPECIAL_SUFFIX)
}

/// GGUF tokenizer model type.
/// Detected from `tokenizer.ggml.model` metadata.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TokenizerModel {
    /// GPT-2 byte-level BPE (Llama-3, Qwen 2/3, etc.).
    Gpt2Bpe,
    /// SentencePiece with score-based BPE merging (Llama-1/2, Gemma 2).
    /// Uses `▁` (U+2581) as word boundary marker and byte fallback (`<0xNN>`).
    Spm,
}

/// GGUF tokenizer supporting both GPT-2 byte-level BPE and SentencePiece SPM.
pub struct GgufTokenizer {
    tokens: Vec<Vec<u8>>,
    merges: Vec<(Vec<u8>, Vec<u8>)>,
    /// Per-token scores (SPM only). Populated when `model_type == Spm`.
    scores: Vec<f32>,
    token_to_id: HashMap<Vec<u8>, u32>,
    /// Byte fallback tokens `<0x00>`..`<0xFF>` → token id (SPM only).
    byte_to_id: [Option<u32>; 256],
    /// Special tokens (e.g. `<|begin_of_text|>`, `<start_of_turn>`) sorted by
    /// length desc for greedy matching.
    special_tokens: Vec<(String, u32)>,
    /// GPT-2 byte→char mapping (for encoding input text, BPE only).
    byte_encoder: [char; 256],
    /// GPT-2 char→byte mapping (for decoding tokens to text, BPE only).
    byte_decoder: HashMap<char, u8>,
    /// Tokenizer model type (BPE vs SPM). Determines encode/decode path.
    model_type: TokenizerModel,
    pub bos_id: u32,
    pub eos_id: u32,
    /// Whether the tokenizer should prepend BOS to encoded sequences.
    /// Qwen 3 has add_bos_token=False; Llama family defaults to True.
    pub add_bos_token: bool,
}

/// SentencePiece symbol: pointer + length + doubly-linked list indices.
/// Used during SPM tokenization to represent contiguous merged pieces.
#[derive(Clone, Copy)]
struct SpmSymbol {
    /// Byte offset in the preprocessed text.
    text_start: usize,
    /// Byte length of this symbol (0 = merged away).
    text_len: usize,
    /// Index of previous active symbol, or -1 if head.
    prev: i32,
    /// Index of next active symbol, or -1 if tail.
    next: i32,
}

/// Candidate bigram merge with its score.
#[derive(Clone, Copy)]
struct SpmBigram {
    left: usize,
    right: usize,
    score: f32,
    /// Total byte size at enqueue time — used to detect stale entries
    /// whose constituent symbols have been merged elsewhere.
    size: usize,
}

impl PartialEq for SpmBigram {
    fn eq(&self, other: &Self) -> bool {
        self.score == other.score && self.left == other.left
    }
}
impl Eq for SpmBigram {}
impl PartialOrd for SpmBigram {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}
impl Ord for SpmBigram {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        // BinaryHeap is a max-heap: higher score first, then smaller left index.
        self.score
            .partial_cmp(&other.score)
            .unwrap_or(std::cmp::Ordering::Equal)
            .then_with(|| other.left.cmp(&self.left))
    }
}

impl GgufTokenizer {
    /// Load tokenizer from GGUF metadata.
    pub fn from_gguf(gguf: &GgufFile<'_>) -> Option<Self> {
        let tokens_meta = gguf.meta("tokenizer.ggml.tokens")?;
        let token_strs = tokens_meta.as_str_array()?;

        // Detect tokenizer model: "gpt2" (Llama-3, Qwen), "llama" (Gemma 2, Llama-1/2).
        let model_type = match gguf.meta_str("tokenizer.ggml.model") {
            Some("llama") => TokenizerModel::Spm,
            _ => TokenizerModel::Gpt2Bpe,
        };

        let mut tokens = Vec::with_capacity(token_strs.len());
        let mut token_to_id = HashMap::with_capacity(token_strs.len());
        let mut special_tokens = Vec::new();

        // Prefer GGUF `tokenizer.ggml.token_type`: 3 = CONTROL, 4 = USER_DEFINED
        // are atomic / must not be split by BPE (e.g. Qwen 3 `<think>` / `</think>`,
        // `<tool_call>` etc). Falls back to `<|...|>` string pattern when the
        // token_type array is absent (older Llama-3 conversions).
        let token_types = gguf
            .meta("tokenizer.ggml.token_type")
            .and_then(|m| m.as_u32_array());

        // Byte fallback table (SPM): maps byte value 0x00..0xFF → token id.
        // Populated from `<0xNN>` tokens (type=6 BYTE) in the vocabulary.
        let mut byte_to_id: [Option<u32>; 256] = [None; 256];

        for (i, t) in token_strs.iter().enumerate() {
            let bytes = t.as_bytes().to_vec();
            token_to_id.insert(bytes.clone(), i as u32);
            tokens.push(bytes);

            let is_special = match &token_types {
                Some(types) => types.get(i).copied().is_some_and(is_atomic_special_token),
                None => is_gpt2_special_marker(t),
            };
            if is_special {
                special_tokens.push((t.to_string(), i as u32));
            }

            // SPM byte fallback: parse `<0xNN>` token strings.
            if model_type == TokenizerModel::Spm {
                if let Some(b) = parse_spm_byte_fallback(t) {
                    byte_to_id[b as usize] = Some(i as u32);
                }
            }
        }

        // Sort special tokens by length descending for greedy matching
        special_tokens.sort_by_key(|t| core::cmp::Reverse(t.0.len()));

        let merges = if let Some(merges_meta) = gguf.meta("tokenizer.ggml.merges") {
            if let Some(merge_strs) = merges_meta.as_str_array() {
                merge_strs
                    .iter()
                    .filter_map(|s| {
                        let parts: Vec<&str> = s.splitn(2, ' ').collect();
                        if parts.len() == 2 {
                            Some((parts[0].as_bytes().to_vec(), parts[1].as_bytes().to_vec()))
                        } else {
                            None
                        }
                    })
                    .collect()
            } else {
                Vec::new()
            }
        } else {
            Vec::new()
        };

        // SPM scores (only meaningful for `tokenizer.ggml.model == "llama"`).
        // Fallback to empty vec (BPE path ignores scores).
        let scores: Vec<f32> = gguf
            .meta("tokenizer.ggml.scores")
            .and_then(|m| match m {
                MetaValue::Array(arr) => Some(
                    arr.iter()
                        .filter_map(MetaValue::as_f32)
                        .collect::<Vec<f32>>(),
                ),
                _ => None,
            })
            .unwrap_or_default();

        let bos_id = gguf.meta_u32("tokenizer.ggml.bos_token_id").unwrap_or(1);
        let eos_id = gguf.meta_u32("tokenizer.ggml.eos_token_id").unwrap_or(2);
        // Qwen 3: add_bos_token = False (default True for Llama family).
        let add_bos_token = gguf
            .meta_bool("tokenizer.ggml.add_bos_token")
            .unwrap_or(true);

        Some(Self {
            tokens,
            merges,
            scores,
            token_to_id,
            byte_to_id,
            special_tokens,
            byte_encoder: gpt2_byte_to_char(),
            byte_decoder: gpt2_char_to_byte(),
            model_type,
            bos_id,
            eos_id,
            add_bos_token,
        })
    }

    /// Encode text to token IDs.
    /// Handles special tokens as atomic units, then applies GPT-2 byte-level
    /// BPE (for GPT-2 vocab) or SentencePiece SPM (for llama vocab) to the
    /// remaining text segments.
    pub fn encode(&self, text: &str) -> Vec<u32> {
        let mut result = Vec::new();
        let mut remaining = text;

        while !remaining.is_empty() {
            // Try to match a special token (greedy, longest first)
            let mut matched = false;
            for (tok_str, tok_id) in &self.special_tokens {
                if remaining.starts_with(tok_str.as_str()) {
                    result.push(*tok_id);
                    remaining = &remaining[tok_str.len()..];
                    matched = true;
                    break;
                }
            }
            if matched {
                continue;
            }

            // Find next special token boundary
            let mut next_boundary = remaining.len();
            for (tok_str, _) in &self.special_tokens {
                if let Some(pos) = remaining.find(tok_str.as_str()) {
                    if pos > 0 && pos < next_boundary {
                        next_boundary = pos;
                    }
                }
            }

            // Encode the text chunk with the appropriate algorithm.
            let chunk = &remaining[..next_boundary];
            match self.model_type {
                TokenizerModel::Gpt2Bpe => result.extend(self.bpe_encode_chunk(chunk)),
                TokenizerModel::Spm => result.extend(self.spm_encode_chunk(chunk)),
            }
            remaining = &remaining[next_boundary..];
        }

        result
    }

    /// SentencePiece SPM encoding using score-based greedy bigram merging.
    /// Ported from llama.cpp `llm_tokenizer_spm_session::tokenize`.
    ///
    /// Preprocessing: replace ' ' with '▁' (U+2581, 3-byte UTF-8) and prepend
    /// '▁' if the chunk doesn't already start with one. Then the algorithm
    /// splits the text into UTF-8 codepoints and greedily merges highest-score
    /// bigrams whose concatenation exists in the vocab. Unmatched symbols fall
    /// back to byte tokens (`<0xNN>`).
    fn spm_encode_chunk(&self, text: &str) -> Vec<u32> {
        if text.is_empty() {
            return Vec::new();
        }

        // Preprocess: ' ' → '▁' (U+2581). Prepend '▁' if not already present.
        let mut preprocessed = String::with_capacity(text.len() + 3);
        if !text.starts_with('\u{2581}') {
            preprocessed.push('\u{2581}');
        }
        for c in text.chars() {
            if c == ' ' {
                preprocessed.push('\u{2581}');
            } else {
                preprocessed.push(c);
            }
        }

        let bytes = preprocessed.as_bytes();

        // Split into UTF-8 codepoints as initial symbols.
        let mut symbols: Vec<SpmSymbol> = Vec::new();
        for (byte_idx, c) in preprocessed.char_indices() {
            let clen = c.len_utf8();
            let idx = symbols.len();
            let prev = if idx == 0 { -1 } else { (idx - 1) as i32 };
            symbols.push(SpmSymbol {
                text_start: byte_idx,
                text_len: clen,
                prev,
                next: -1,
            });
        }
        let n_symbols = symbols.len();
        for i in 0..n_symbols.saturating_sub(1) {
            symbols[i].next = (i + 1) as i32;
        }

        // Priority queue of candidate bigrams (max-heap by score).
        let mut work_queue: std::collections::BinaryHeap<SpmBigram> =
            std::collections::BinaryHeap::new();

        let try_add = |symbols: &[SpmSymbol],
                       queue: &mut std::collections::BinaryHeap<SpmBigram>,
                       left: i32,
                       right: i32| {
            if left < 0 || right < 0 {
                return;
            }
            let l = left as usize;
            let r = right as usize;
            let start = symbols[l].text_start;
            let size = symbols[l].text_len + symbols[r].text_len;
            let merged = &bytes[start..start + size];
            if let Some(&id) = self.token_to_id.get(merged) {
                let score = self
                    .scores
                    .get(id as usize)
                    .copied()
                    .unwrap_or(f32::NEG_INFINITY);
                queue.push(SpmBigram {
                    left: l,
                    right: r,
                    score,
                    size,
                });
            }
        };

        // Seed with initial adjacent bigrams.
        for i in 1..n_symbols {
            try_add(&symbols, &mut work_queue, (i - 1) as i32, i as i32);
        }

        // Greedy merge highest-score bigrams.
        while let Some(bigram) = work_queue.pop() {
            let left_len = symbols[bigram.left].text_len;
            let right_len = symbols[bigram.right].text_len;
            // Skip if either symbol was already merged elsewhere.
            if left_len == 0 || right_len == 0 || left_len + right_len != bigram.size {
                continue;
            }
            // Merge right into left, remove right from linked list.
            symbols[bigram.left].text_len += right_len;
            symbols[bigram.right].text_len = 0;
            symbols[bigram.left].next = symbols[bigram.right].next;
            if symbols[bigram.right].next >= 0 {
                let next_idx = symbols[bigram.right].next as usize;
                symbols[next_idx].prev = bigram.left as i32;
            }
            // Enqueue new neighbor bigrams.
            let prev = symbols[bigram.left].prev;
            let next = symbols[bigram.left].next;
            try_add(&symbols, &mut work_queue, prev, bigram.left as i32);
            try_add(&symbols, &mut work_queue, bigram.left as i32, next);
        }

        // Traverse final linked list, emit token ids (with byte fallback).
        let mut result = Vec::new();
        let mut idx: i32 = 0;
        while idx != -1 {
            let sym = symbols[idx as usize];
            let segment = &bytes[sym.text_start..sym.text_start + sym.text_len];
            if let Some(&id) = self.token_to_id.get(segment) {
                result.push(id);
            } else {
                // Byte fallback: emit `<0xNN>` for each byte.
                for &b in segment {
                    if let Some(id) = self.byte_to_id[b as usize] {
                        result.push(id);
                    }
                }
            }
            idx = sym.next;
        }
        result
    }

    /// BPE encode a text chunk using GPT-2 byte encoding.
    /// Input bytes are mapped to GPT-2 unicode characters before BPE.
    fn bpe_encode_chunk(&self, text: &str) -> Vec<u32> {
        if text.is_empty() {
            return Vec::new();
        }

        // Convert each input byte to its GPT-2 unicode character (as UTF-8 bytes)
        let mut pieces: Vec<Vec<u8>> = text
            .bytes()
            .map(|b| {
                let ch = self.byte_encoder[b as usize];
                let mut buf = [0u8; 4];
                ch.encode_utf8(&mut buf).as_bytes().to_vec()
            })
            .collect();

        // Apply BPE merges in priority order (GPT-2 encoded pieces)
        for (left, right) in &self.merges {
            let mut i = 0;
            while i + 1 < pieces.len() {
                if pieces[i] == *left && pieces[i + 1] == *right {
                    let mut merged = pieces[i].clone();
                    merged.extend_from_slice(&pieces[i + 1]);
                    pieces[i] = merged;
                    pieces.remove(i + 1);
                } else {
                    i += 1;
                }
            }
        }

        // Map pieces to token IDs with fallback
        let mut ids = Vec::new();
        for p in &pieces {
            if let Some(&id) = self.token_to_id.get(p) {
                ids.push(id);
            } else if let Ok(s) = std::str::from_utf8(p) {
                // Fallback: try each GPT-2 char as individual token
                for ch in s.chars() {
                    let mut buf = [0u8; 4];
                    let ch_bytes = ch.encode_utf8(&mut buf).as_bytes().to_vec();
                    if let Some(&id) = self.token_to_id.get(&ch_bytes) {
                        ids.push(id);
                    }
                }
            }
        }
        ids
    }

    /// Decode token IDs to text.
    /// Dispatches to BPE (GPT-2 byte encoding) or SPM (`▁` → space) based on
    /// tokenizer model. Skips control tokens and handles `<0xNN>` byte tokens.
    pub fn decode(&self, ids: &[u32]) -> String {
        let mut bytes = Vec::new();
        for &id in ids {
            let Some(token) = self.tokens.get(id as usize) else {
                continue;
            };
            let Ok(s) = std::str::from_utf8(token) else {
                bytes.extend_from_slice(token);
                continue;
            };
            // Skip GPT-2 control tokens.
            if is_gpt2_special_marker(s) {
                continue;
            }
            // Skip SPM special tokens (single-piece `<...>` control markers).
            if self.model_type == TokenizerModel::Spm
                && s.starts_with('<')
                && s.ends_with(SPM_BYTE_FALLBACK_SUFFIX)
                && !s.starts_with(SPM_BYTE_FALLBACK_PREFIX)
                && !s.contains('\u{2581}')
            {
                continue;
            }
            // Byte fallback token `<0xNN>` — output raw byte.
            if let Some(byte_val) = parse_spm_byte_fallback(s) {
                bytes.push(byte_val);
                continue;
            }
            match self.model_type {
                TokenizerModel::Spm => {
                    // SPM: '▁' (U+2581) marks word boundary → convert to space.
                    for ch in s.chars() {
                        if ch == '\u{2581}' {
                            bytes.push(b' ');
                        } else {
                            let mut buf = [0u8; 4];
                            bytes.extend_from_slice(ch.encode_utf8(&mut buf).as_bytes());
                        }
                    }
                }
                TokenizerModel::Gpt2Bpe => {
                    // BPE: decode GPT-2 unicode chars → raw bytes.
                    for ch in s.chars() {
                        if let Some(&b) = self.byte_decoder.get(&ch) {
                            bytes.push(b);
                        } else {
                            let mut buf = [0u8; 4];
                            bytes.extend_from_slice(ch.encode_utf8(&mut buf).as_bytes());
                        }
                    }
                }
            }
        }
        String::from_utf8_lossy(&bytes).into_owned()
    }

    /// Vocabulary size.
    #[must_use]
    pub const fn vocab_size(&self) -> usize {
        self.tokens.len()
    }
}

// ─── Ternary quantization ────────────────────────────────────────────────────

/// Ternary-quantized weight row: {-1, 0, +1} with bitmask packing.
/// 2 bits per weight: pos_mask (positive) + neg_mask (negative).
/// Zero weights have neither bit set.
pub struct TernaryRow {
    pub pos_mask: Vec<u8>,
    pub neg_mask: Vec<u8>,
    pub scale: f32,
    pub num_cols: usize,
}

/// Ternary-quantized weight matrix.
pub struct TernaryMatrix {
    pub rows: Vec<TernaryRow>,
    pub num_rows: usize,
    pub num_cols: usize,
}

impl TernaryRow {
    /// Ternarize a f32 weight row. threshold_ratio controls sparsity
    /// (0.7 = weights below 0.7 × mean(|w|) become zero).
    pub fn from_f32(weights: &[f32], threshold_ratio: f32) -> Self {
        let num_cols = weights.len();
        let num_bytes = num_cols.div_ceil(8);

        let mean_abs: f32 = weights.iter().map(|w| w.abs()).sum::<f32>() / num_cols as f32;
        let threshold = threshold_ratio * mean_abs;

        let mut pos_mask = vec![0u8; num_bytes];
        let mut neg_mask = vec![0u8; num_bytes];
        let mut abs_sum = 0.0f32;
        let mut nonzero_count = 0usize;

        for (i, &w) in weights.iter().enumerate() {
            if w.abs() > threshold {
                let byte_idx = i / 8;
                let bit = 1u8 << (i % 8);
                if w > 0.0 {
                    pos_mask[byte_idx] |= bit;
                } else {
                    neg_mask[byte_idx] |= bit;
                }
                abs_sum += w.abs();
                nonzero_count += 1;
            }
        }

        let scale = if nonzero_count > 0 {
            abs_sum / nonzero_count as f32
        } else {
            0.0
        };

        Self {
            pos_mask,
            neg_mask,
            scale,
            num_cols,
        }
    }
}

impl TernaryMatrix {
    /// Ternarize from quantized GGUF data (dequantizes row-by-row, then ternarizes).
    pub fn from_quantized(
        data: &[u8],
        qtype: GgmlType,
        num_rows: usize,
        num_cols: usize,
        threshold_ratio: f32,
    ) -> Self {
        let elems_per_block = qtype.elements_per_block();
        let bytes_per_block = qtype.block_bytes();
        let blocks_per_row = num_cols / elems_per_block;
        let row_bytes = blocks_per_row * bytes_per_block;

        let mut rows = Vec::with_capacity(num_rows);
        let mut row_f32 = vec![0.0f32; num_cols];

        for r in 0..num_rows {
            let row_data = &data[r * row_bytes..(r + 1) * row_bytes];
            // Dequantize row to f32
            match qtype {
                GgmlType::Q2_K => dequantize_row_q2k(row_data, &mut row_f32),
                GgmlType::Q3_K => dequantize_row_q3k(row_data, &mut row_f32),
                GgmlType::Q4_K => dequantize_row_q4k(row_data, &mut row_f32),
                GgmlType::Q5_K => dequantize_row_q5k(row_data, &mut row_f32),
                GgmlType::Q6_K => dequantize_row_q6k(row_data, &mut row_f32),
                GgmlType::Q8_0 => dequantize_row_q8_0(row_data, &mut row_f32),
                GgmlType::F16 => dequantize_row_f16(row_data, &mut row_f32),
                GgmlType::F32 => {
                    for i in 0..num_cols {
                        row_f32[i] = f32::from_le_bytes([
                            row_data[i * 4],
                            row_data[i * 4 + 1],
                            row_data[i * 4 + 2],
                            row_data[i * 4 + 3],
                        ]);
                    }
                }
                _ => {
                    row_f32.fill(0.0);
                }
            }
            rows.push(TernaryRow::from_f32(&row_f32, threshold_ratio));
        }

        Self {
            rows,
            num_rows,
            num_cols,
        }
    }
}

/// Ternary matvec: output = TernaryMatrix × input.
/// No multiplications in the inner loop — only additions and subtractions.
pub fn ternary_matvec(matrix: &TernaryMatrix, input: &[f32], output: &mut [f32]) {
    ternary_matvec_impl(&matrix.rows, input, output);
}

fn ternary_matvec_impl(rows: &[TernaryRow], input: &[f32], output: &mut [f32]) {
    #[cfg(feature = "parallel")]
    {
        use rayon::prelude::*;
        output.par_iter_mut().enumerate().for_each(|(r, out)| {
            *out = ternary_dot_row(&rows[r], input);
        });
    }
    #[cfg(not(feature = "parallel"))]
    {
        for (r, out) in output.iter_mut().enumerate() {
            *out = ternary_dot_row(&rows[r], input);
        }
    }
}

#[inline]
fn ternary_dot_row(row: &TernaryRow, input: &[f32]) -> f32 {
    // Runtime-dispatched (Issue #24): AVX-512F → AVX2 → scalar.
    #[cfg(target_arch = "x86_64")]
    {
        if x86_avx512bw_supported() {
            // SAFETY: guarded by runtime CPU detection; input length is
            // validated against `row.num_cols` on the ternary matvec entry.
            let unscaled = unsafe {
                avx512_dot::ternary_dot_row_unscaled(
                    &row.pos_mask,
                    &row.neg_mask,
                    input,
                    row.num_cols,
                )
            };
            return row.scale * unscaled;
        }
        if x86_avx2_supported() {
            // SAFETY: guarded by runtime CPU detection.
            let unscaled = unsafe {
                avx2_dot::ternary_dot_row_unscaled(
                    &row.pos_mask,
                    &row.neg_mask,
                    input,
                    row.num_cols,
                )
            };
            return row.scale * unscaled;
        }
    }
    #[cfg(target_arch = "aarch64")]
    {
        // SAFETY: NEON is baseline on every aarch64 target Rust supports.
        // Parity with scalar is relative-tolerance (1e-5), not bit-exact,
        // due to differing summation order — same rationale as the AVX2
        // path documented in `ternary_avx2_matches_scalar_relative_tolerance`.
        let unscaled = unsafe {
            neon_dot::ternary_dot_row_unscaled(&row.pos_mask, &row.neg_mask, input, row.num_cols)
        };
        return row.scale * unscaled;
    }
    #[allow(unreachable_code)]
    ternary_dot_row_scalar(row, input)
}

/// Scalar fallback body for [`ternary_dot_row`]. Kept as a separate
/// function so the SIMD dispatch on x86_64 can call it directly when no
/// feature is available, and so the scalar tests exercise a stable
/// reference implementation.
#[inline]
fn ternary_dot_row_scalar(row: &TernaryRow, input: &[f32]) -> f32 {
    let mut pos_sum = 0.0f32;
    let mut neg_sum = 0.0f32;
    let num_bytes = row.pos_mask.len();

    for b in 0..num_bytes {
        let pm = row.pos_mask[b];
        let nm = row.neg_mask[b];
        if (pm | nm) == 0 {
            continue;
        }
        let base = b * 8;
        for bit in 0..8 {
            let idx = base + bit;
            if idx >= row.num_cols {
                break;
            }
            let p = f32::from((pm >> bit) & 1);
            let n = f32::from((nm >> bit) & 1);
            pos_sum += p * input[idx];
            neg_sum += n * input[idx];
        }
    }

    row.scale * (pos_sum - neg_sum)
}

// ─── Sparse Ternary (N:M Structured Sparsity) ──────────────────────────────

/// Block size for N:M structured sparsity. Within each block of SPARSE_BLOCK elements,
/// at most SPARSE_N are non-zero. This avoids per-element metadata overhead.
pub const SPARSE_BLOCK: usize = 16;

/// A sparse ternary row using N:M structured sparsity.
///
/// Within each block of SPARSE_BLOCK elements, non-zero positions are stored
/// as a compact bitmask (16 bits = 2 bytes per block), and signs are packed
/// in another bitmask (only for non-zero positions).
pub struct SparseTernaryRow {
    /// Per-block: which positions are non-zero (bit i = 1 means position i is active).
    pub active_masks: Vec<u16>,
    /// Per-block: sign of active positions (bit i = 1 means negative).
    pub sign_masks: Vec<u16>,
    /// Scale factor (mean of |non-zero weights|).
    pub scale: f32,
    pub num_cols: usize,
    pub num_blocks: usize,
}

/// Flat-buffer sparse ternary matrix for cache-friendly SIMD matvec.
/// All row masks are stored contiguously in a single allocation.
pub struct SparseTernaryMatrix {
    /// Flat buffer: [row0_active, row0_sign, row1_active, row1_sign, ...]
    /// Each row has `blocks_per_row` u16 active masks followed by `blocks_per_row` u16 sign masks.
    mask_buf: Vec<u16>,
    /// Packed 2-bit weights for LUT expansion: 4 weights per byte (row-major, for scalar/tail).
    /// Layout: contiguous [row0_blk0(4B), row0_blk1(4B), ..., row1_blk0(4B), ...]
    /// Encoding: 00=0, 01=+1, 11=-1 per 2-bit field.
    packed_2bit: Vec<u8>,
    /// Block-packed 2-bit weights for 4-row micro-kernel (TLB-friendly).
    /// Layout: groups of 4 rows, each group interleaved by block:
    /// [g0_r0_b0(4B), g0_r1_b0, g0_r2_b0, g0_r3_b0, g0_r0_b1, g0_r1_b1, ...]
    /// +16 padding for safe vld1q_u8 over-read.
    packed_blocked: Vec<u8>,
    /// Per-row scale factors (contiguous).
    scales: Vec<f32>,
    pub num_rows: usize,
    pub num_cols: usize,
    pub blocks_per_row: usize,
    /// Target sparsity: fraction of zeros per block (e.g. 0.5 = 8:16).
    pub target_sparsity: f32,
}

impl SparseTernaryRow {
    /// Create from a dense TernaryRow, enforcing N:M structured sparsity.
    /// Within each block of SPARSE_BLOCK, keeps only the top-N by magnitude
    /// (from the original f32 weights) and zeros the rest.
    pub fn from_ternary_row(row: &TernaryRow, original_weights: &[f32], n_keep: usize) -> Self {
        let num_cols = row.num_cols;
        let num_blocks = num_cols.div_ceil(SPARSE_BLOCK);
        let mut active_masks = vec![0u16; num_blocks];
        let mut sign_masks = vec![0u16; num_blocks];
        let mut abs_sum = 0.0f64;
        let mut nonzero_count = 0usize;

        for blk in 0..num_blocks {
            let base = blk * SPARSE_BLOCK;
            let end = (base + SPARSE_BLOCK).min(num_cols);
            let block_len = end - base;

            let mut candidates: Vec<(usize, f32)> = Vec::new();
            for i in 0..block_len {
                let global = base + i;
                let byte_idx = global / 8;
                let bit = 1u8 << (global % 8);
                let is_nonzero =
                    (row.pos_mask[byte_idx] & bit) != 0 || (row.neg_mask[byte_idx] & bit) != 0;
                if is_nonzero {
                    candidates.push((i, original_weights[global].abs()));
                }
            }

            candidates.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap());
            candidates.truncate(n_keep.min(block_len));

            for &(i, mag) in &candidates {
                let global = base + i;
                active_masks[blk] |= 1u16 << i;

                let byte_idx = global / 8;
                let bit = 1u8 << (global % 8);
                if (row.neg_mask[byte_idx] & bit) != 0 {
                    sign_masks[blk] |= 1u16 << i;
                }

                abs_sum += mag as f64;
                nonzero_count += 1;
            }
        }

        let scale = if nonzero_count > 0 {
            (abs_sum / nonzero_count as f64) as f32
        } else {
            row.scale
        };

        Self {
            active_masks,
            sign_masks,
            scale,
            num_cols,
            num_blocks,
        }
    }

    /// Create directly from f32 weights with N:M structured sparsity.
    pub fn from_f32(weights: &[f32], threshold_ratio: f32, n_keep: usize) -> Self {
        let row = TernaryRow::from_f32(weights, threshold_ratio);
        Self::from_ternary_row(&row, weights, n_keep)
    }

    /// Count of non-zero elements.
    pub fn nnz(&self) -> usize {
        self.active_masks
            .iter()
            .map(|m| m.count_ones() as usize)
            .sum()
    }

    /// Effective bits per parameter (including the 2-byte masks per block overhead).
    pub fn effective_bits_per_param(&self) -> f32 {
        if self.num_cols == 0 {
            return 0.0;
        }
        let mask_bits = self.num_blocks as f32 * 32.0;
        let total_bits = mask_bits + 32.0;
        total_bits / self.num_cols as f32
    }
}

#[allow(dead_code)]
impl SparseTernaryMatrix {
    /// Build from Vec of SparseTernaryRows, packing into flat contiguous buffer.
    /// Also precomputes packed 2-bit weights for LUT-based expansion.
    pub fn from_rows(
        rows: Vec<SparseTernaryRow>,
        num_rows: usize,
        num_cols: usize,
        target_sparsity: f32,
    ) -> Self {
        let blocks_per_row = num_cols.div_ceil(SPARSE_BLOCK);
        let stride = blocks_per_row * 2;
        let mut mask_buf = vec![0u16; num_rows * stride];
        let mut scales = Vec::with_capacity(num_rows);

        // 4 bytes per block (16 elements × 2 bits = 32 bits = 4 bytes)
        let bytes_per_block = SPARSE_BLOCK / 4;
        // +16 padding so vld1q_u8 can safely over-read at buffer tail
        let mut packed_2bit = vec![0u8; num_rows * blocks_per_row * bytes_per_block + 16];

        for (r, row) in rows.iter().enumerate() {
            let base = r * stride;
            mask_buf[base..base + blocks_per_row].copy_from_slice(&row.active_masks);
            mask_buf[base + blocks_per_row..base + stride].copy_from_slice(&row.sign_masks);
            scales.push(row.scale);

            // Pack active + sign into 2-bit format: 00=0, 01=+1, 11=-1
            let pack_base = r * blocks_per_row * bytes_per_block;
            for blk in 0..blocks_per_row {
                let active = row.active_masks[blk];
                let sign = row.sign_masks[blk];
                for byte_idx in 0..bytes_per_block {
                    let bit_off = byte_idx * 4;
                    let mut packed_byte = 0u8;
                    for j in 0..4 {
                        let bit = bit_off + j;
                        let is_active = (active >> bit) & 1;
                        let is_sign = (sign >> bit) & 1;
                        // 00=0, 01=+1, 11=-1
                        let code = if is_active == 0 {
                            0u8
                        } else if is_sign == 0 {
                            1
                        } else {
                            3
                        };
                        packed_byte |= code << (j * 2);
                    }
                    packed_2bit[pack_base + blk * bytes_per_block + byte_idx] = packed_byte;
                }
            }
        }

        // Build block-packed layout: groups of 4 rows, interleaved by block.
        // For 4-row micro-kernel: sequential memory access eliminates TLB misses.
        let num_groups = num_rows.div_ceil(4);
        let group_bytes = 4 * blocks_per_row * bytes_per_block; // 4 rows × all blocks
        let mut packed_blocked = vec![0u8; num_groups * group_bytes + 16]; // +16 padding

        for g in 0..num_groups {
            let r_base = g * 4;
            for blk in 0..blocks_per_row {
                for lane in 0..4 {
                    let r = r_base + lane;
                    if r < num_rows {
                        let src_off = r * blocks_per_row * bytes_per_block + blk * bytes_per_block;
                        let dst_off =
                            g * group_bytes + blk * 4 * bytes_per_block + lane * bytes_per_block;
                        packed_blocked[dst_off..dst_off + bytes_per_block]
                            .copy_from_slice(&packed_2bit[src_off..src_off + bytes_per_block]);
                    }
                }
            }
        }

        Self {
            mask_buf,
            packed_2bit,
            packed_blocked,
            scales,
            num_rows,
            num_cols,
            blocks_per_row,
            target_sparsity,
        }
    }

    /// Convert a dense TernaryMatrix to sparse, enforcing N:M structured sparsity.
    pub fn from_ternary(
        dense: &TernaryMatrix,
        original_weights_per_row: &[Vec<f32>],
        n_keep: usize,
    ) -> Self {
        let target_sparsity = 1.0 - (n_keep as f32 / SPARSE_BLOCK as f32);
        let rows: Vec<SparseTernaryRow> = dense
            .rows
            .iter()
            .zip(original_weights_per_row.iter())
            .map(|(row, orig)| SparseTernaryRow::from_ternary_row(row, orig, n_keep))
            .collect();
        Self::from_rows(rows, dense.num_rows, dense.num_cols, target_sparsity)
    }

    /// Create directly from f32 weight data (row-major).
    pub fn from_f32_weights(
        weights: &[f32],
        num_rows: usize,
        num_cols: usize,
        threshold_ratio: f32,
        n_keep: usize,
    ) -> Self {
        let target_sparsity = 1.0 - (n_keep as f32 / SPARSE_BLOCK as f32);
        let mut rows = Vec::with_capacity(num_rows);
        for r in 0..num_rows {
            let row_data = &weights[r * num_cols..(r + 1) * num_cols];
            rows.push(SparseTernaryRow::from_f32(
                row_data,
                threshold_ratio,
                n_keep,
            ));
        }
        Self::from_rows(rows, num_rows, num_cols, target_sparsity)
    }

    /// Get active_masks slice for a row.
    #[inline]
    fn active_masks(&self, row: usize) -> &[u16] {
        let base = row * self.blocks_per_row * 2;
        &self.mask_buf[base..base + self.blocks_per_row]
    }

    /// Get sign_masks slice for a row.
    #[inline]
    fn sign_masks(&self, row: usize) -> &[u16] {
        let base = row * self.blocks_per_row * 2 + self.blocks_per_row;
        &self.mask_buf[base..base + self.blocks_per_row]
    }

    /// Get packed 2-bit weight data for a row (4 bytes per block).
    #[inline]
    fn packed_weights(&self, row: usize) -> &[u8] {
        let bytes_per_block = SPARSE_BLOCK / 4;
        let base = row * self.blocks_per_row * bytes_per_block;
        let len = self.blocks_per_row * bytes_per_block;
        &self.packed_2bit[base..base + len]
    }

    /// Total non-zero elements across all rows.
    pub fn total_nnz(&self) -> usize {
        let stride = self.blocks_per_row * 2;
        let mut total = 0usize;
        for r in 0..self.num_rows {
            let base = r * stride;
            for b in 0..self.blocks_per_row {
                total += self.mask_buf[base + b].count_ones() as usize;
            }
        }
        total
    }

    /// Estimated memory in bytes (masks + packed_2bit + packed_blocked + scales).
    pub const fn memory_bytes(&self) -> usize {
        self.mask_buf.len() * 2
            + self.packed_2bit.len()
            + self.packed_blocked.len()
            + self.scales.len() * 4
    }
}

/// Quantize f32 activation vector to i8 with a single global scale.
/// Returns (quantized_i8, scale) where original ≈ quantized * scale.
/// On aarch64: NEON-accelerated abs-max search and narrowing conversion.
/// Only the aarch64 sparse matvec consumes it (the x86_64 path works on f32).
#[cfg(target_arch = "aarch64")]
fn quantize_activation_i8(input: &[f32]) -> (Vec<i8>, f32) {
    let n = input.len();
    let padded_len = n.div_ceil(SPARSE_BLOCK) * SPARSE_BLOCK;

    #[cfg(target_arch = "aarch64")]
    {
        // SAFETY: NEON mandatory on aarch64
        unsafe { quantize_activation_i8_neon(input, n, padded_len) }
    }
    #[cfg(not(target_arch = "aarch64"))]
    {
        let abs_max = input.iter().fold(0.0f32, |m, x| m.max(x.abs()));
        if abs_max == 0.0 {
            return (vec![0i8; padded_len], 1.0);
        }
        let inv_scale = 127.0 / abs_max;
        let scale = abs_max / 127.0;
        let mut quant = vec![0i8; padded_len];
        for (i, &val) in input.iter().enumerate() {
            quant[i] = (val * inv_scale).round().max(-127.0).min(127.0) as i8;
        }
        (quant, scale)
    }
}

/// NEON-accelerated activation quantization: f32 → i8.
/// vmaxvq_f32 for abs-max, vcvtnq_s32_f32 + vqmovn for narrowing.
#[cfg(target_arch = "aarch64")]
#[target_feature(enable = "neon")]
unsafe fn quantize_activation_i8_neon(
    input: &[f32],
    n: usize,
    padded_len: usize,
) -> (Vec<i8>, f32) {
    use std::arch::aarch64::*;

    // 1. Find abs max using NEON pairwise max
    let mut max_vec = vdupq_n_f32(0.0);
    let mut i = 0;
    while i + 16 <= n {
        let v0 = vabsq_f32(vld1q_f32(input.as_ptr().add(i)));
        let v1 = vabsq_f32(vld1q_f32(input.as_ptr().add(i + 4)));
        let v2 = vabsq_f32(vld1q_f32(input.as_ptr().add(i + 8)));
        let v3 = vabsq_f32(vld1q_f32(input.as_ptr().add(i + 12)));
        max_vec = vmaxq_f32(max_vec, vmaxq_f32(vmaxq_f32(v0, v1), vmaxq_f32(v2, v3)));
        i += 16;
    }
    while i + 4 <= n {
        let v = vabsq_f32(vld1q_f32(input.as_ptr().add(i)));
        max_vec = vmaxq_f32(max_vec, v);
        i += 4;
    }
    let mut abs_max = vmaxvq_f32(max_vec);
    while i < n {
        abs_max = abs_max.max(input[i].abs());
        i += 1;
    }

    if abs_max == 0.0 {
        return (vec![0i8; padded_len], 1.0);
    }

    let inv_scale = 127.0 / abs_max;
    let scale = abs_max / 127.0;
    let scale_vec = vdupq_n_f32(inv_scale);

    // 2. Quantize using NEON: f32 → round → i32 → saturating narrow i16 → i8
    let mut quant = vec![0i8; padded_len];
    i = 0;
    while i + 16 <= n {
        let v0 = vmulq_f32(vld1q_f32(input.as_ptr().add(i)), scale_vec);
        let v1 = vmulq_f32(vld1q_f32(input.as_ptr().add(i + 4)), scale_vec);
        let v2 = vmulq_f32(vld1q_f32(input.as_ptr().add(i + 8)), scale_vec);
        let v3 = vmulq_f32(vld1q_f32(input.as_ptr().add(i + 12)), scale_vec);

        // Round to nearest → i32
        let i0 = vcvtnq_s32_f32(v0);
        let i1 = vcvtnq_s32_f32(v1);
        let i2 = vcvtnq_s32_f32(v2);
        let i3 = vcvtnq_s32_f32(v3);

        // Saturating narrow: i32 → i16 → i8
        let s01 = vcombine_s16(vqmovn_s32(i0), vqmovn_s32(i1));
        let s23 = vcombine_s16(vqmovn_s32(i2), vqmovn_s32(i3));
        let result = vcombine_s8(vqmovn_s16(s01), vqmovn_s16(s23));

        vst1q_s8(quant.as_mut_ptr().add(i), result);
        i += 16;
    }
    // Scalar tail
    while i < n {
        quant[i] = (input[i] * inv_scale).round().max(-127.0).min(127.0) as i8;
        i += 1;
    }

    (quant, scale)
}

/// Sparse ternary matvec: output = SparseTernaryMatrix × input.
///
/// On aarch64 with dotprod: i8 dynamic quantization + vdotq_s32 (16 elements/instruction).
/// Branchless 4-row micro-kernel with shared activation loads.
pub fn sparse_ternary_matvec(matrix: &SparseTernaryMatrix, input: &[f32], output: &mut [f32]) {
    // Quantize activation to i8 once (shared across all rows)
    #[cfg(target_arch = "aarch64")]
    let (act_i8, act_scale) = quantize_activation_i8(input);

    #[cfg(all(target_arch = "aarch64", feature = "parallel"))]
    {
        use rayon::prelude::*;
        output
            .par_chunks_mut(8)
            .enumerate()
            .for_each(|(chunk_idx, chunk)| {
                let row_start = chunk_idx * 8;
                let mut pos = 0;
                // Process groups of 4 rows
                while pos + 4 <= chunk.len() {
                    unsafe {
                        sparse_ternary_microkernel_4row_sdot(
                            matrix,
                            &act_i8,
                            act_scale,
                            &mut chunk[pos..pos + 4],
                            row_start + pos,
                        );
                    }
                    pos += 4;
                }
                // Tail rows (1-3)
                while pos < chunk.len() {
                    unsafe {
                        chunk[pos] =
                            sparse_ternary_dot_sdot(matrix, &act_i8, act_scale, row_start + pos);
                    }
                    pos += 1;
                }
            });
    }

    #[cfg(all(target_arch = "aarch64", not(feature = "parallel")))]
    {
        let rows = matrix.num_rows;
        let mut r = 0;
        while r + 4 <= rows {
            unsafe {
                sparse_ternary_microkernel_4row_sdot(
                    matrix,
                    &act_i8,
                    act_scale,
                    &mut output[r..r + 4],
                    r,
                );
            }
            r += 4;
        }
        while r < rows {
            unsafe {
                output[r] = sparse_ternary_dot_sdot(matrix, &act_i8, act_scale, r);
            }
            r += 1;
        }
    }

    #[cfg(not(target_arch = "aarch64"))]
    {
        #[cfg(feature = "parallel")]
        {
            use rayon::prelude::*;
            output.par_iter_mut().enumerate().for_each(|(r, out)| {
                *out = sparse_ternary_dot_flat(matrix, r, input);
            });
        }
        #[cfg(not(feature = "parallel"))]
        {
            for (r, out) in output.iter_mut().enumerate() {
                *out = sparse_ternary_dot_flat(matrix, r, input);
            }
        }
    }
}

/// Scalar fallback dispatch for single-row dot product (non-aarch64).
#[inline]
#[allow(dead_code)]
fn sparse_ternary_dot_flat(matrix: &SparseTernaryMatrix, row: usize, input: &[f32]) -> f32 {
    sparse_ternary_dot_scalar(matrix, row, input)
}

/// Single-row sdot-based dot product for tail rows.
/// Uses packed 2-bit weights + vqtbl1q_s8 LUT expansion (~6 ops vs 14 ops mask-based).
#[cfg(target_arch = "aarch64")]
#[inline]
#[target_feature(enable = "neon")]
unsafe fn sparse_ternary_dot_sdot(
    matrix: &SparseTernaryMatrix,
    act_i8: &[i8],
    act_scale: f32,
    row: usize,
) -> f32 {
    use std::arch::aarch64::*;

    let row_scale = matrix.scales[row];
    let blocks = matrix.blocks_per_row;
    let packed = matrix.packed_weights(row);

    // i32 accumulator
    let mut acc = vdupq_n_s32(0);

    // LUT constants — live in registers across all blocks
    let lut = vld1q_s8([0i8, 1, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0].as_ptr());
    let replicate_idx = vld1q_u8([0u8, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3].as_ptr());
    let shift_amounts =
        vld1q_s8([0i8, -2, -4, -6, 0, -2, -4, -6, 0, -2, -4, -6, 0, -2, -4, -6].as_ptr());
    let mask_03 = vdupq_n_u8(0x03);

    // Branchless loop: LUT expand + sdot
    let bytes_per_block = SPARSE_BLOCK / 4; // 4
    for blk in 0..blocks {
        let aq = vld1q_s8(act_i8.as_ptr().add(blk * SPARSE_BLOCK));
        let w = expand_packed_2bit_lut(
            packed.as_ptr().add(blk * bytes_per_block),
            lut,
            replicate_idx,
            shift_amounts,
            mask_03,
        );
        acc = sdot_s32(acc, w, aq);
    }

    let sum_i32 = vaddvq_s32(acc);
    row_scale * act_scale * (sum_i32 as f32)
}

/// Branchless 4-row sdot micro-kernel (Block-Packed + LUT + SDOT).
/// - Block-packed layout: 4 rows' data for each block are contiguous in memory → TLB-friendly
/// - i8 quantized activation loaded ONCE per block, shared across 4 rows
/// - Packed 2-bit weights expanded via vqtbl1q_s8 LUT (~6 ops)
/// - sdot: 16 × i8×i8 → 4 × i32 in 1 instruction
/// - ZERO branches in inner loop
#[cfg(target_arch = "aarch64")]
#[target_feature(enable = "neon")]
unsafe fn sparse_ternary_microkernel_4row_sdot(
    matrix: &SparseTernaryMatrix,
    act_i8: &[i8],
    act_scale: f32,
    output: &mut [f32],
    row_start: usize,
) {
    use std::arch::aarch64::*;

    let blocks = matrix.blocks_per_row;
    let bytes_per_block = SPARSE_BLOCK / 4; // 4 bytes per block

    // 4 × i32 accumulators (one int32x4_t per row)
    let mut acc0 = vdupq_n_s32(0);
    let mut acc1 = vdupq_n_s32(0);
    let mut acc2 = vdupq_n_s32(0);
    let mut acc3 = vdupq_n_s32(0);

    // LUT constants — live in registers across all blocks
    let lut = vld1q_s8([0i8, 1, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0].as_ptr());
    let replicate_idx = vld1q_u8([0u8, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3].as_ptr());
    let shift_amounts =
        vld1q_s8([0i8, -2, -4, -6, 0, -2, -4, -6, 0, -2, -4, -6, 0, -2, -4, -6].as_ptr());
    let mask_03 = vdupq_n_u8(0x03);

    // Block-packed pointer: group's data is contiguous
    // Layout: [r0_b0(4B), r1_b0(4B), r2_b0(4B), r3_b0(4B), r0_b1, r1_b1, ...]
    let group = row_start / 4;
    let group_bytes = 4 * blocks * bytes_per_block;
    let group_ptr = matrix.packed_blocked.as_ptr().add(group * group_bytes);
    let stride_4 = 4 * bytes_per_block; // 16 bytes per block-group (4 rows × 4 bytes)

    for blk in 0..blocks {
        // 1. Load i8 activation ONCE — shared across 4 rows
        let aq = vld1q_s8(act_i8.as_ptr().add(blk * SPARSE_BLOCK));

        // Block-packed: all 4 rows for this block are at contiguous addresses
        let blk_base = group_ptr.add(blk * stride_4);

        // 2-5. Sequential loads from contiguous memory (1 cache line = 64B covers 4 blocks)
        let w0 = expand_packed_2bit_lut(blk_base, lut, replicate_idx, shift_amounts, mask_03);
        acc0 = sdot_s32(acc0, w0, aq);

        let w1 = expand_packed_2bit_lut(
            blk_base.add(bytes_per_block),
            lut,
            replicate_idx,
            shift_amounts,
            mask_03,
        );
        acc1 = sdot_s32(acc1, w1, aq);

        let w2 = expand_packed_2bit_lut(
            blk_base.add(2 * bytes_per_block),
            lut,
            replicate_idx,
            shift_amounts,
            mask_03,
        );
        acc2 = sdot_s32(acc2, w2, aq);

        let w3 = expand_packed_2bit_lut(
            blk_base.add(3 * bytes_per_block),
            lut,
            replicate_idx,
            shift_amounts,
            mask_03,
        );
        acc3 = sdot_s32(acc3, w3, aq);
    }

    // Horizontal reduction: i32 → f32, apply scales
    let s0 = matrix.scales[row_start] * act_scale;
    let s1 = matrix.scales[row_start + 1] * act_scale;
    let s2 = matrix.scales[row_start + 2] * act_scale;
    let s3 = matrix.scales[row_start + 3] * act_scale;

    output[0] = s0 * (vaddvq_s32(acc0) as f32);
    output[1] = s1 * (vaddvq_s32(acc1) as f32);
    output[2] = s2 * (vaddvq_s32(acc2) as f32);
    output[3] = s3 * (vaddvq_s32(acc3) as f32);
}

/// SDOT via inline assembly: acc.4s += a.16b · b.16b
/// Processes 16 × i8×i8 → 4 × i32 accumulation in 1 instruction.
///
/// Requires ARMv8.2-A dotprod extension. Enable via `-C target-feature=+dotprod`
/// or on toolchains where the base ABI already implies it (macOS aarch64-apple-darwin).
#[cfg(all(target_arch = "aarch64", target_feature = "dotprod"))]
#[inline(always)]
unsafe fn sdot_s32(
    acc: std::arch::aarch64::int32x4_t,
    a: std::arch::aarch64::int8x16_t,
    b: std::arch::aarch64::int8x16_t,
) -> std::arch::aarch64::int32x4_t {
    let mut result = acc;
    core::arch::asm!(
        "sdot {acc:v}.4s, {a:v}.16b, {b:v}.16b",
        acc = inout(vreg) result,
        a = in(vreg) a,
        b = in(vreg) b,
        options(nostack, preserves_flags),
    );
    result
}

/// NEON fallback when ARMv8.2-A dotprod is unavailable.
///
/// Reproduces `sdot` per-lane 4-element dot product semantics via
/// `vmulq_s16` + `vpaddlq_s16` + `vpaddq_s32`:
/// `acc.4s[i] += Σ_{k=0..3} a.i8[4i+k] * b.i8[4i+k]`.
///
/// Verified bit-identical to the dotprod inline asm path by
/// `sdot_s32_matches_manual_reference` / `sdot_s32_accumulates_onto_seed`.
#[cfg(all(target_arch = "aarch64", not(target_feature = "dotprod")))]
#[inline(always)]
unsafe fn sdot_s32(
    acc: std::arch::aarch64::int32x4_t,
    a: std::arch::aarch64::int8x16_t,
    b: std::arch::aarch64::int8x16_t,
) -> std::arch::aarch64::int32x4_t {
    use std::arch::aarch64::{
        vaddq_s32, vget_high_s8, vget_low_s8, vmovl_s8, vmulq_s16, vpaddlq_s16, vpaddq_s32,
    };
    // 1. Sign-extend i8×16 to i16×8 (low + high halves).
    let a_lo = vmovl_s8(vget_low_s8(a)); // a[0..8]  as i16×8
    let a_hi = vmovl_s8(vget_high_s8(a)); // a[8..16] as i16×8
    let b_lo = vmovl_s8(vget_low_s8(b));
    let b_hi = vmovl_s8(vget_high_s8(b));
    // 2. Element-wise multiply (i16 × i16 → i16; values fit since |i8·i8| ≤ 2^14).
    let prod_lo = vmulq_s16(a_lo, b_lo); // [p0,p1,p2,p3, p4,p5,p6,p7]
    let prod_hi = vmulq_s16(a_hi, b_hi); // [p8,..p11, p12,..p15]
                                         // 3. Pairwise widening add (i16×8 → i32×4): adjacent i16 pairs summed.
    let pair_lo = vpaddlq_s16(prod_lo); // [p0+p1, p2+p3, p4+p5, p6+p7]
    let pair_hi = vpaddlq_s16(prod_hi); // [p8+p9, p10+p11, p12+p13, p14+p15]
                                        // 4. Interleaved pairwise add (2×i32×4 → i32×4): concat the two low pairs
                                        //    of each input into per-lane 4-element sums matching SDOT lane layout.
    let sum = vpaddq_s32(pair_lo, pair_hi);
    // 5. Accumulate onto the seed.
    vaddq_s32(acc, sum)
}

/// LUT-based 2-bit → i8 expansion using vqtbl1q_s8.
/// Reads 4 packed bytes (16 weights), expands each 2-bit code via table lookup.
/// 00→0, 01→+1, 11→-1. Only ~6 ops vs 14 ops for mask-based expansion.
///
/// `packed_ptr`: pointer to 4 bytes of packed 2-bit weights.
/// `lut`: [0, 1, 0, -1, 0,0,0,0, 0,0,0,0, 0,0,0,0] preloaded in register.
/// `replicate_idx`: [0,0,0,0, 1,1,1,1, 2,2,2,2, 3,3,3,3] for byte replication.
/// `shift_amounts`: [0,-2,-4,-6, 0,-2,-4,-6, 0,-2,-4,-6, 0,-2,-4,-6] for 2-bit extraction.
/// `mask_03`: vdupq_n_u8(0x03) for isolating 2-bit codes.
#[cfg(target_arch = "aarch64")]
#[inline(always)]
unsafe fn expand_packed_2bit_lut(
    packed_ptr: *const u8,
    lut: std::arch::aarch64::int8x16_t,
    replicate_idx: std::arch::aarch64::uint8x16_t,
    shift_amounts: std::arch::aarch64::int8x16_t,
    mask_03: std::arch::aarch64::uint8x16_t,
) -> std::arch::aarch64::int8x16_t {
    use std::arch::aarch64::*;

    // Load 4 packed bytes into low 4 bytes of a NEON register
    // (use vld1q_u8 from a 16-byte aligned read is safest, but we only use 4 bytes)
    let raw = vld1q_u8(packed_ptr);

    // Replicate: byte 0 → lanes 0-3, byte 1 → lanes 4-7, etc.
    let replicated = vqtbl1q_u8(raw, replicate_idx);

    // Variable right-shift each lane to position its 2-bit field at bits [1:0]
    let shifted = vreinterpretq_u8_s8(vshlq_s8(vreinterpretq_s8_u8(replicated), shift_amounts));

    // Mask to isolate 2-bit code (0-3)
    let indices = vandq_u8(shifted, mask_03);

    // Table lookup: code → i8 weight
    vqtbl1q_s8(lut, indices)
}

/// Scalar fallback (used on non-aarch64 and as reference implementation).
#[inline]
#[allow(dead_code)]
fn sparse_ternary_dot_scalar(matrix: &SparseTernaryMatrix, row: usize, input: &[f32]) -> f32 {
    let active = matrix.active_masks(row);
    let signs = matrix.sign_masks(row);
    let scale = matrix.scales[row];

    let mut sum = 0.0f32;

    for blk in 0..matrix.blocks_per_row {
        let a = active[blk];
        if a == 0 {
            continue;
        }
        let s = signs[blk];
        let base = blk * SPARSE_BLOCK;

        let pos_mask = a & !s;
        let neg_mask = a & s;

        let mut m = pos_mask;
        while m != 0 {
            let bit = m.trailing_zeros() as usize;
            sum += input[base + bit];
            m &= m - 1;
        }

        m = neg_mask;
        while m != 0 {
            let bit = m.trailing_zeros() as usize;
            sum -= input[base + bit];
            m &= m - 1;
        }
    }

    scale * sum
}

/// Dequantize a single row of quantized weight data to f32.
/// Public API for use by model loading (sparse ternary conversion etc.).
pub fn dequantize_weight_row(data: &[u8], qtype: GgmlType, out: &mut [f32]) {
    match qtype {
        GgmlType::Q2_K => dequantize_row_q2k(data, out),
        GgmlType::Q3_K => dequantize_row_q3k(data, out),
        GgmlType::Q4_K => dequantize_row_q4k(data, out),
        GgmlType::Q5_K => dequantize_row_q5k(data, out),
        GgmlType::Q6_K => dequantize_row_q6k(data, out),
        GgmlType::Q8_0 => dequantize_row_q8_0(data, out),
        GgmlType::Mxfp4 => dequantize_row_mxfp4(data, out),
        GgmlType::F16 => {
            for i in 0..out.len() {
                let off = i * 2;
                out[i] = f16_to_f32(u16::from_le_bytes([data[off], data[off + 1]]));
            }
        }
        GgmlType::F32 => {
            for i in 0..out.len() {
                out[i] = f32::from_le_bytes([
                    data[i * 4],
                    data[i * 4 + 1],
                    data[i * 4 + 2],
                    data[i * 4 + 3],
                ]);
            }
        }
        _ => out.fill(0.0),
    }
}

/// Dequantize a single Q3_K row to f32.
fn dequantize_row_q2k(data: &[u8], out: &mut [f32]) {
    dequantize_q2_k(data, out);
}

fn dequantize_row_q3k(data: &[u8], out: &mut [f32]) {
    dequantize_q3_k(data, out);
}

fn dequantize_row_q5k(data: &[u8], out: &mut [f32]) {
    dequantize_q5_k(data, out);
}

/// Dequantize a single Q4_K row to f32.
fn dequantize_row_q4k(data: &[u8], out: &mut [f32]) {
    let blocks = out.len() / QK_K;
    let block_size = 144; // Q4_K block size

    for blk in 0..blocks {
        let b = &data[blk * block_size..];
        let d = f16_to_f32(u16::from_le_bytes([b[0], b[1]]));
        let dmin = f16_to_f32(u16::from_le_bytes([b[2], b[3]]));
        let scales_raw = &b[4..16];
        let qs = &b[16..144];

        let mut scales = [0u8; 8];
        let mut mins = [0u8; 8];
        for i in 0..4 {
            scales[i] = scales_raw[i] & 0x3F;
            mins[i] = scales_raw[i + 4] & 0x3F;
        }
        scales[4] = (scales_raw[8] & 0xF) | ((scales_raw[0] >> 6) << 4);
        scales[5] = (scales_raw[1 + 8] & 0xF) | ((scales_raw[1] >> 6) << 4);
        scales[6] = (scales_raw[2 + 8] & 0xF) | ((scales_raw[2] >> 6) << 4);
        scales[7] = (scales_raw[3 + 8] & 0xF) | ((scales_raw[3] >> 6) << 4);
        mins[4] = (scales_raw[8] >> 4) | ((scales_raw[4] >> 6) << 4);
        mins[5] = (scales_raw[1 + 8] >> 4) | ((scales_raw[5] >> 6) << 4);
        mins[6] = (scales_raw[2 + 8] >> 4) | ((scales_raw[6] >> 6) << 4);
        mins[7] = (scales_raw[3 + 8] >> 4) | ((scales_raw[7] >> 6) << 4);

        for is in 0..8 {
            let sc = d * scales[is] as f32;
            let mn = dmin * mins[is] as f32;
            let off = is * 32;
            for l in 0..32 {
                let qi = if l < 16 {
                    let byte_idx = (is / 2) * 32 + l;
                    if is % 2 == 0 {
                        qs[byte_idx] & 0xF
                    } else {
                        qs[byte_idx] >> 4
                    }
                } else {
                    let byte_idx = (is / 2) * 32 + 16 + (l - 16);
                    if is % 2 == 0 {
                        qs[byte_idx] & 0xF
                    } else {
                        qs[byte_idx] >> 4
                    }
                };
                out[blk * QK_K + off + l] = sc * qi as f32 - mn;
            }
        }
    }
}

/// Dequantize a single Q6_K row to f32 (matches llama.cpp bit-packing layout).
fn dequantize_row_q6k(data: &[u8], out: &mut [f32]) {
    let blocks = out.len() / QK_K;
    let block_bytes = 210;

    for blk in 0..blocks {
        let b = &data[blk * block_bytes..];
        let d = f16_to_f32(u16::from_le_bytes([b[208], b[209]]));
        let ql = &b[0..128];
        let qh = &b[128..192];
        let scales = &b[192..208];

        // Reconstruct 6-bit signed quants (same as q6k_q8k_dot)
        let mut aux8 = [0i8; QK_K];
        let mut a_off = 0usize;
        let mut ql_off = 0usize;
        let mut qh_off = 0usize;
        for _ in 0..2 {
            for l in 0..32 {
                aux8[a_off + l] = ((ql[ql_off + l] & 0xF) | ((qh[qh_off + l] & 3) << 4)) as i8 - 32;
                aux8[a_off + l + 32] =
                    ((ql[ql_off + l + 32] & 0xF) | (((qh[qh_off + l] >> 2) & 3) << 4)) as i8 - 32;
                aux8[a_off + l + 64] =
                    ((ql[ql_off + l] >> 4) | (((qh[qh_off + l] >> 4) & 3) << 4)) as i8 - 32;
                aux8[a_off + l + 96] =
                    ((ql[ql_off + l + 32] >> 4) | (((qh[qh_off + l] >> 6) & 3) << 4)) as i8 - 32;
            }
            a_off += 128;
            ql_off += 64;
            qh_off += 32;
        }

        // Apply scales and write output
        let base = blk * QK_K;
        for is in 0..16 {
            let scale = d * (scales[is] as i8) as f32;
            let off = is * 16;
            for l in 0..16 {
                out[base + off + l] = scale * aux8[off + l] as f32;
            }
        }
    }
}

/// Dequantize a single Q8_0 row to f32.
fn dequantize_row_q8_0(data: &[u8], out: &mut [f32]) {
    let blocks = out.len() / QK8_0;
    let block_size = 34; // 2 bytes f16 scale + 32 i8 values

    for blk in 0..blocks {
        let b = &data[blk * block_size..];
        let d = f16_to_f32(u16::from_le_bytes([b[0], b[1]]));
        for i in 0..QK8_0 {
            out[blk * QK8_0 + i] = d * (b[2 + i] as i8) as f32;
        }
    }
}

/// Dequantize a single F16 row to f32.
fn dequantize_row_f16(data: &[u8], out: &mut [f32]) {
    for i in 0..out.len() {
        out[i] = f16_to_f32(u16::from_le_bytes([data[i * 2], data[i * 2 + 1]]));
    }
}

// ─── MXFP4 quantization (OCP Microscaling FP4, MX v1.0) ────────────────────

/// E2M1 element decoding table.
///
/// Each 4-bit MXFP4 element is `S EE M` (1 sign + 2 exponent + 1 mantissa).
/// Per OCP MX v1.0 §Table 1, the 16 patterns decode to the following values
/// before the block's shared E8M0 scale is applied. E2M1 has no NaN or Inf
/// representation; both positive and negative zero exist but decode to the
/// same numeric value.
///
/// Ordering: index = 4-bit pattern value (0..16).
pub const E2M1_DECODE_TABLE: [f32; 16] = [
    0.0, 0.5, 1.0, 1.5, 2.0, 3.0, 4.0, 6.0, // +0 .. +6
    -0.0, -0.5, -1.0, -1.5, -2.0, -3.0, -4.0, -6.0, // -0 .. -6
];

/// Decode an E8M0 shared scale byte to an f32 multiplier.
///
/// Per OCP MX v1.0 §5.4, E8M0 encodes an unsigned 8-bit exponent representing
/// `2^(byte - 127)`. Byte value `0xFF` is reserved for NaN. There is no
/// mantissa field; all scales are exact powers of two.
#[must_use]
pub fn decode_e8m0_scale(byte: u8) -> f32 {
    if byte == 0xFF {
        return f32::NAN;
    }
    // `2^(byte - 127)` — use `powi` on the biased exponent so both very small
    // (byte=0 → 2^-127) and very large (byte=254 → 2^127) scales are exact.
    2.0f32.powi(i32::from(byte) - 127)
}

/// Dequantize one MXFP4 block (17 bytes → 32 f32 values).
///
/// Byte layout per OCP MX v1.0 §5.5:
/// - Byte 0: E8M0 shared exponent
/// - Bytes 1..17: 16 bytes packing 32 × E2M1 elements (2 elements per byte,
///   low nibble = element 2k, high nibble = element 2k+1).
///
/// # Panics
/// Panics if `block` has fewer than 17 bytes.
pub fn dequantize_mxfp4_block(block: &[u8]) -> [f32; QK_MXFP4] {
    assert!(
        block.len() >= 17,
        "MXFP4 block requires 17 bytes, got {}",
        block.len()
    );
    let scale = decode_e8m0_scale(block[0]);
    let mut out = [0.0f32; QK_MXFP4];
    for i in 0..16 {
        let byte = block[1 + i];
        let low = usize::from(byte & 0x0F);
        let high = usize::from(byte >> 4);
        out[2 * i] = E2M1_DECODE_TABLE[low] * scale;
        out[2 * i + 1] = E2M1_DECODE_TABLE[high] * scale;
    }
    out
}

/// Dequantize a full MXFP4 row (N × 17 bytes → N × 32 f32 values).
pub fn dequantize_row_mxfp4(data: &[u8], out: &mut [f32]) {
    let blocks = out.len() / QK_MXFP4;
    for blk in 0..blocks {
        let block = &data[blk * 17..blk * 17 + 17];
        let dq = dequantize_mxfp4_block(block);
        out[blk * QK_MXFP4..(blk + 1) * QK_MXFP4].copy_from_slice(&dq);
    }
}

/// MXFP4-quantized weight row (one row of a matrix stored as MXFP4 blocks).
///
/// Follows the same `Vec<u8>` layout used by `TernaryRow` so downstream matvec
/// implementations can share the SIMD dispatch pattern once landed. Row length
/// (number of elements) is stored explicitly because the raw byte length rounds
/// up to a whole number of 17-byte blocks and the last block may hold padding.
#[derive(Debug, Clone)]
pub struct MxfP4Row {
    /// Packed MXFP4 blocks. Length = `n_blocks * 17`.
    pub bytes: Vec<u8>,
    /// Logical element count (blocks × 32 minus any tail padding the loader
    /// tracks separately).
    pub n_elements: usize,
}

impl MxfP4Row {
    /// Number of 32-element blocks in this row.
    #[must_use]
    pub const fn n_blocks(&self) -> usize {
        self.n_elements.div_ceil(QK_MXFP4)
    }

    /// Dequantize this row into a caller-provided buffer.
    ///
    /// # Panics
    /// Panics if `out.len() < self.n_elements`.
    pub fn dequantize(&self, out: &mut [f32]) {
        assert!(
            out.len() >= self.n_elements,
            "output buffer too small: got {}, need {}",
            out.len(),
            self.n_elements
        );
        let blocks = self.n_blocks();
        for blk in 0..blocks {
            let start = blk * 17;
            let block = &self.bytes[start..start + 17];
            let dq = dequantize_mxfp4_block(block);
            let out_start = blk * QK_MXFP4;
            let take = QK_MXFP4.min(self.n_elements - out_start);
            out[out_start..out_start + take].copy_from_slice(&dq[..take]);
        }
    }
}

/// MXFP4-quantized weight matrix (rows-major).
#[derive(Debug, Clone)]
pub struct MxfP4Matrix {
    pub rows: Vec<MxfP4Row>,
    pub n_cols: usize,
}

/// MXFP4 matrix-vector multiply via the fused scalar kernel
/// (Phase X.4.f.1 landing, 2026-07-28).
///
/// Iterates each row of `matrix` and calls the shared fused scalar
/// kernel on its packed bytes; the kernel dequantizes MXFP4 blocks
/// into a stack-resident `[f32; 32]` buffer and multiply-accumulates
/// against `input` in place, avoiding the per-row `Vec<f32>` scratch
/// that the correctness-first `mxfp4_matvec_fallback` uses.
///
/// # Panics
///
/// Panics if any row's byte length is not a multiple of 17
/// (MXFP4 block size), if `input.len() < n_cols`, or if
/// `output.len() < matrix.rows.len()`.
///
/// # Numerics
///
/// Bit-exact parity with [`mxfp4_matvec_fallback`] — both kernels
/// dequantize blocks the same way (via
/// [`dequantize_mxfp4_block`]) and accumulate in `f32` in the same
/// element order. A dedicated unit test enforces the parity so any
/// future SIMD kernel additions (Phase X.4.f.2+) can be validated
/// against this reference.
pub fn mxfp4_matvec(matrix: &MxfP4Matrix, input: &[f32], output: &mut [f32]) {
    assert!(
        output.len() >= matrix.rows.len(),
        "output buffer too small: got {}, need {}",
        output.len(),
        matrix.rows.len(),
    );
    assert!(
        input.len() >= matrix.n_cols,
        "input buffer too small: got {}, need {}",
        input.len(),
        matrix.n_cols,
    );
    let n_cols = matrix.n_cols;
    for (row_idx, row) in matrix.rows.iter().enumerate() {
        // `MxfP4Row` stores whole-block bytes only; padding for a
        // partial last block is tracked via `n_elements`, so use
        // `matrix.n_cols` as the number of valid columns.
        assert!(
            row.bytes.len() % 17 == 0,
            "row {row_idx} bytes length {} is not a multiple of 17",
            row.bytes.len(),
        );
        // Fused scalar kernel operates on one row at a time.
        let mut single_row = [0.0_f32; 1];
        mxfp4_matvec_fused_scalar(input, &row.bytes, 1, n_cols, &mut single_row);
        output[row_idx] = single_row[0];
    }
}

// ─── MXFP4 GPU upload padding (Phase X.4.g.0) ───────────────────────────────
//
// GPU storage buffers require 4-byte (word) alignment per element. The
// native MXFP4 block is 17 bytes = 1 E8M0 scale + 16 packed E2M1
// nibbles, which is not word-aligned. Following the existing Q8_0 GPU
// upload pattern (34 bytes → 36 bytes = 9 u32 words), the MXFP4
// upload path pads each 17-byte block to 20 bytes = 5 u32 words:
//
//   word 0: [scale(u8, low 8 bits) | 0 (padding, high 24 bits)]
//   words 1..5: 16 bytes of packed E2M1 (4 u32 = 16 bytes total)
//
// The WGSL shader (X.4.g.1, deferred) reads `word 0 & 0xFF` for the
// scale byte, decodes via the E8M0 formula on-GPU, then unpacks each
// E2M1 nibble via bit ops on words 1..5.

/// Number of `u32` words per MXFP4 block after GPU upload padding.
/// `5 words × 4 bytes = 20 bytes`, with `bytes 17..20` reserved as
/// zero-fill padding to keep every following block word-aligned.
pub const MXFP4_GPU_WORDS_PER_BLOCK: usize = 5;

/// Byte size of one MXFP4 block after GPU upload padding
/// (`MXFP4_GPU_WORDS_PER_BLOCK * 4 = 20`).
pub const MXFP4_GPU_BYTES_PER_BLOCK: usize = MXFP4_GPU_WORDS_PER_BLOCK * 4;

/// Pack a row of native MXFP4 blocks (17 bytes each) into the padded
/// GPU upload layout (20 bytes each) required by the wgpu shader
/// pipeline scheduled at Phase X.4.g.1.
///
/// # Layout
///
/// Per block, the padded output looks like:
///
/// ```text
/// bytes  0.. 1  scale byte + 0-fill high byte  (packed as u32 low 8 bits)
/// bytes  1.. 4  0-fill padding                 (rest of word 0)
/// bytes  4..20  16 bytes of packed E2M1 nibbles
/// ```
///
/// The shader reads `u32 word0 = data[block_offset + 0]` and extracts
/// the scale byte via `word0 & 0xFFu`, then reads words 1..5 for the
/// 32 packed E2M1 elements. Layout matches Q8_0's `9 u32 word` block
/// (`f16 d + 32 int8 qs`), just with fewer words per block.
///
/// # Arguments
///
/// - `native_row`: MXFP4 row in native GGUF layout, `n_blocks × 17`
///   bytes.
///
/// # Returns
///
/// Padded row: `n_blocks × 20` bytes, ready for direct GPU buffer
/// upload as a `&[u8]` cast to `&[u32]` on the shader side.
///
/// # Panics
///
/// Panics if `native_row.len()` is not a multiple of 17.
#[must_use]
pub fn pack_mxfp4_for_gpu_upload(native_row: &[u8]) -> Vec<u8> {
    assert!(
        native_row.len().is_multiple_of(17),
        "native MXFP4 row length {} is not a multiple of 17",
        native_row.len()
    );
    let n_blocks = native_row.len() / 17;
    let mut padded = vec![0u8; n_blocks * MXFP4_GPU_BYTES_PER_BLOCK];
    for blk in 0..n_blocks {
        let src = &native_row[blk * 17..blk * 17 + 17];
        let dst_base = blk * MXFP4_GPU_BYTES_PER_BLOCK;
        // Byte 0 of the padded block = the E8M0 scale (low byte of word 0).
        padded[dst_base] = src[0];
        // Bytes 1..4 stay 0 (word 0 high 24 bits).
        // Bytes 4..20 = the 16 packed E2M1 bytes (words 1..5).
        padded[dst_base + 4..dst_base + 20].copy_from_slice(&src[1..17]);
    }
    padded
}

/// Inverse of [`pack_mxfp4_for_gpu_upload`]: unpack a padded GPU
/// upload byte buffer back into native 17-byte-per-block MXFP4
/// layout.
///
/// Included for GPU → CPU byte-for-byte parity tests. Not on any
/// production hot path.
///
/// # Panics
///
/// Panics if `padded_row.len()` is not a multiple of
/// [`MXFP4_GPU_BYTES_PER_BLOCK`], or if any block's padding bytes
/// (positions 1..4 of each 20-byte block) are non-zero (indicates
/// a producer bug rather than valid MXFP4 data).
#[must_use]
pub fn unpack_mxfp4_from_gpu_upload(padded_row: &[u8]) -> Vec<u8> {
    assert!(
        padded_row.len().is_multiple_of(MXFP4_GPU_BYTES_PER_BLOCK),
        "padded MXFP4 row length {} is not a multiple of {MXFP4_GPU_BYTES_PER_BLOCK}",
        padded_row.len()
    );
    let n_blocks = padded_row.len() / MXFP4_GPU_BYTES_PER_BLOCK;
    let mut native = vec![0u8; n_blocks * 17];
    for blk in 0..n_blocks {
        let src_base = blk * MXFP4_GPU_BYTES_PER_BLOCK;
        // Assert padding invariant so a malformed buffer surfaces
        // during the round-trip rather than silently deserializing.
        for offset in 1..4 {
            assert_eq!(
                padded_row[src_base + offset],
                0,
                "block {blk} padding byte at offset {offset} must be 0, got {}",
                padded_row[src_base + offset]
            );
        }
        let dst_base = blk * 17;
        native[dst_base] = padded_row[src_base];
        native[dst_base + 1..dst_base + 17]
            .copy_from_slice(&padded_row[src_base + 4..src_base + 20]);
    }
    native
}

// ─── Tests ──────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_f16_to_f32_basic() {
        // 0x0000 = 0.0
        assert_eq!(f16_to_f32(0x0000), 0.0);
        // 0x3C00 = 1.0
        assert!((f16_to_f32(0x3C00) - 1.0).abs() < 1e-6);
        // 0xBC00 = -1.0
        assert!((f16_to_f32(0xBC00) - (-1.0)).abs() < 1e-6);
        // 0x4000 = 2.0
        assert!((f16_to_f32(0x4000) - 2.0).abs() < 1e-6);
        // 0x3800 = 0.5
        assert!((f16_to_f32(0x3800) - 0.5).abs() < 1e-6);
    }

    #[test]
    fn test_f16_inf_nan() {
        // +Inf = 0x7C00
        assert!(f16_to_f32(0x7C00).is_infinite());
        assert!(f16_to_f32(0x7C00) > 0.0);
        // -Inf = 0xFC00
        assert!(f16_to_f32(0xFC00).is_infinite());
        assert!(f16_to_f32(0xFC00) < 0.0);
        // NaN = 0x7C01
        assert!(f16_to_f32(0x7C01).is_nan());
    }

    // ─── MXFP4 (OCP Microscaling FP4) tests ──────────────────────────────

    #[test]
    fn test_e2m1_decode_table_bit_exact() {
        // Per OCP MX v1.0 §Table 1, the 16 E2M1 patterns decode to:
        //   0000 →  +0.0     0001 →  +0.5     0010 →  +1.0     0011 →  +1.5
        //   0100 →  +2.0     0101 →  +3.0     0110 →  +4.0     0111 →  +6.0
        //   1000 →  -0.0     1001 →  -0.5     1010 →  -1.0     1011 →  -1.5
        //   1100 →  -2.0     1101 →  -3.0     1110 →  -4.0     1111 →  -6.0
        let expected: [f32; 16] = [
            0.0, 0.5, 1.0, 1.5, 2.0, 3.0, 4.0, 6.0, -0.0, -0.5, -1.0, -1.5, -2.0, -3.0, -4.0, -6.0,
        ];
        for (i, &v) in expected.iter().enumerate() {
            assert_eq!(
                E2M1_DECODE_TABLE[i].to_bits(),
                v.to_bits(),
                "E2M1 pattern {i:04b} expected {v} but got {}",
                E2M1_DECODE_TABLE[i]
            );
        }
    }

    #[test]
    fn test_e2m1_no_nan_no_inf() {
        // E2M1 has no reserved bit pattern for NaN or Inf — every entry is
        // a finite representable value.
        for &v in &E2M1_DECODE_TABLE {
            assert!(v.is_finite(), "E2M1 table contains non-finite value {v}");
        }
    }

    #[test]
    fn test_e8m0_scale_decode_endpoints() {
        // byte 127 → 2^0 = 1.0 (identity scale, no exponent shift).
        assert!((decode_e8m0_scale(127) - 1.0).abs() < 1e-12);
        // byte 128 → 2^1 = 2.0.
        assert!((decode_e8m0_scale(128) - 2.0).abs() < 1e-12);
        // byte 126 → 2^-1 = 0.5.
        assert!((decode_e8m0_scale(126) - 0.5).abs() < 1e-12);
        // byte 0 → 2^-127, smallest normal-ish scale.
        assert!(decode_e8m0_scale(0) > 0.0);
        assert!(decode_e8m0_scale(0) < 1e-30);
        // byte 254 → 2^127, largest finite scale before NaN.
        assert!(decode_e8m0_scale(254) > 1e30);
        assert!(decode_e8m0_scale(254).is_finite());
    }

    #[test]
    fn test_e8m0_scale_nan_reserved() {
        // Byte 0xFF is reserved for NaN per spec §5.4.
        assert!(decode_e8m0_scale(0xFF).is_nan());
    }

    #[test]
    fn test_mxfp4_block_dequant_identity_scale() {
        // Construct a block with scale = 2^0 = 1.0 (byte 127) and elements
        // stepping through the 16 E2M1 patterns twice (32 total).
        // Byte layout: byte 0 = scale, bytes 1..17 = packed E2M1 (low nibble
        // = element 2k, high nibble = element 2k+1).
        let mut block = [0u8; 17];
        block[0] = 127; // scale = 1.0
                        // Pack elements 0..32 = repeating 0..16, so byte i contains
                        // (2i+1)<<4 | (2i) but modulo 16. Simpler: byte i = ((2i+1) & 0xF) << 4 | ((2i) & 0xF)
        for i in 0..16 {
            let low = (2 * i) & 0xF;
            let high = (2 * i + 1) & 0xF;
            block[1 + i] = ((high as u8) << 4) | (low as u8);
        }

        let out = dequantize_mxfp4_block(&block);

        // With identity scale, the output should match the E2M1 table twice.
        for i in 0..32 {
            let expected = E2M1_DECODE_TABLE[i & 0xF];
            assert_eq!(
                out[i].to_bits(),
                expected.to_bits(),
                "block[{i}] expected {expected} but got {}",
                out[i]
            );
        }
    }

    #[test]
    fn test_mxfp4_block_dequant_with_scale() {
        // Scale = 2^2 = 4.0 (byte 129). Element pattern 0111 = +6.0.
        // Expected output: 6.0 * 4.0 = 24.0 for every element.
        let mut block = [0u8; 17];
        block[0] = 129; // scale = 4.0
        for i in 0..16 {
            // Both nibbles = 0x7 (pattern +6.0).
            block[1 + i] = 0x77;
        }
        let out = dequantize_mxfp4_block(&block);
        for (i, &v) in out.iter().enumerate() {
            assert!(
                (v - 24.0).abs() < 1e-6,
                "block[{i}]: expected 24.0, got {v}"
            );
        }
    }

    #[test]
    fn test_mxfp4_block_dequant_signed_symmetric() {
        // Scale = 1.0 (byte 127). Alternate patterns 0011 (+1.5) and 1011 (-1.5).
        // low nibble = 0011 (+1.5), high nibble = 1011 (-1.5).
        let mut block = [0u8; 17];
        block[0] = 127;
        for i in 0..16 {
            block[1 + i] = (0xB << 4) | 0x3; // high=1011, low=0011
        }
        let out = dequantize_mxfp4_block(&block);
        for i in 0..32 {
            let expected = if i % 2 == 0 { 1.5 } else { -1.5 };
            assert!(
                (out[i] - expected).abs() < 1e-6,
                "block[{i}]: expected {expected}, got {}",
                out[i]
            );
        }
    }

    #[test]
    fn test_mxfp4_row_dequant_multi_block() {
        // Two consecutive blocks: first with scale 1.0 all +1.0, second with
        // scale 0.5 all +2.0 → output should be all 1.0 for the whole row.
        let mut data = vec![0u8; 34];
        // Block 0: scale = 127 (2^0 = 1.0), all elements = 0x22 → +1.0/+1.0
        data[0] = 127;
        for i in 0..16 {
            data[1 + i] = 0x22;
        }
        // Block 1: scale = 126 (2^-1 = 0.5), all elements = 0x44 → +2.0/+2.0 * 0.5 = 1.0
        data[17] = 126;
        for i in 0..16 {
            data[18 + i] = 0x44;
        }

        let mut out = vec![0.0f32; 64];
        dequantize_row_mxfp4(&data, &mut out);

        for (i, &v) in out.iter().enumerate() {
            assert!((v - 1.0).abs() < 1e-6, "out[{i}]: expected 1.0, got {v}");
        }
    }

    #[test]
    fn test_ggml_type_mxfp4_metadata() {
        let t = GgmlType::from_u32(39);
        assert_eq!(t, GgmlType::Mxfp4);
        assert_eq!(t.block_bytes(), 17);
        assert_eq!(t.elements_per_block(), QK_MXFP4);
        assert_eq!(QK_MXFP4, 32);
    }

    #[test]
    fn test_mxfp4_row_dequantize_via_struct() {
        // MxfP4Row::dequantize should match the free function when n_elements
        // is exactly a multiple of QK_MXFP4.
        let mut bytes = vec![0u8; 17];
        bytes[0] = 127;
        for i in 0..16 {
            bytes[1 + i] = 0x22; // all +1.0 / +1.0
        }
        let row = MxfP4Row {
            bytes,
            n_elements: 32,
        };
        assert_eq!(row.n_blocks(), 1);

        let mut out = vec![0.0f32; 32];
        row.dequantize(&mut out);
        for (i, &v) in out.iter().enumerate() {
            assert!((v - 1.0).abs() < 1e-6, "out[{i}]: expected 1.0, got {v}");
        }
    }

    // ─── Phase X.4.f.1: fused scalar MXFP4 matvec kernel tests ────────

    /// Build a deterministic MXFP4 tensor byte buffer of shape
    /// `[rows, cols]` with a synthetic E8M0 scale byte and repeating
    /// E2M1 nibbles per row. Used to feed the parity tests below.
    fn mxfp4_synthetic_tensor(rows: usize, cols: usize, scale_byte: u8, nibble: u8) -> Vec<u8> {
        assert!(cols.is_multiple_of(QK_MXFP4));
        assert!(nibble < 16, "E2M1 index must fit in 4 bits");
        let blocks_per_row = cols / QK_MXFP4;
        let row_bytes = blocks_per_row * 17;
        let mut data = vec![0u8; rows * row_bytes];
        // Same nibble in both halves of every packed byte.
        let packed = (nibble << 4) | nibble;
        for row in 0..rows {
            for blk in 0..blocks_per_row {
                let base = row * row_bytes + blk * 17;
                data[base] = scale_byte;
                // Vary per-row so different rows give different matvec outputs.
                let bump = ((row + blk) & 0xF) as u8;
                let bumped_nibble = (nibble + bump) & 0xF;
                let bumped_packed = (bumped_nibble << 4) | bumped_nibble;
                for byte in &mut data[base + 1..base + 17] {
                    *byte = bumped_packed;
                }
                // Leave block[0] scale, restore uniform packing for first
                // block of every row so the identity-row test below is
                // meaningful.
                if blk == 0 {
                    for byte in &mut data[base + 1..base + 17] {
                        *byte = packed;
                    }
                }
            }
        }
        data
    }

    #[test]
    fn mxfp4_matvec_fused_scalar_matches_fallback() {
        // Non-trivial dimensions: 4 rows × 128 cols = 4 blocks per row.
        // Every block uses scale byte 127 (2^0 = 1.0) and nibble 0x7
        // (a mid-magnitude positive E2M1 value from the table); the
        // synthetic tensor helper varies packing per (row, block) to
        // avoid degenerate identical rows.
        let rows = 4;
        let cols = 128;
        let data = mxfp4_synthetic_tensor(rows, cols, 127, 0x7);
        let input: Vec<f32> = (0..cols).map(|i| (i as f32) * 0.01 - 0.5).collect();
        let mut out_fallback = vec![0.0f32; rows];
        let mut out_fused = vec![0.0f32; rows];
        mxfp4_matvec_fallback(&input, &data, rows, cols, &mut out_fallback);
        mxfp4_matvec_fused_scalar(&input, &data, rows, cols, &mut out_fused);
        // Both kernels dequantize with the same routine and accumulate
        // in the same order, so parity must be bit-exact.
        assert_eq!(
            out_fallback, out_fused,
            "fused kernel must be bit-exact vs fallback"
        );
    }

    #[test]
    fn mxfp4_matvec_free_fn_matches_kernel() {
        // The MxfP4Matrix wrapper (public free function) must produce
        // identical results to the internal kernel it dispatches to.
        let rows = 3;
        let cols = 64;
        let data = mxfp4_synthetic_tensor(rows, cols, 127, 0x5);
        let row_bytes = (cols / QK_MXFP4) * 17;
        let matrix = MxfP4Matrix {
            rows: (0..rows)
                .map(|r| MxfP4Row {
                    bytes: data[r * row_bytes..(r + 1) * row_bytes].to_vec(),
                    n_elements: cols,
                })
                .collect(),
            n_cols: cols,
        };
        let input: Vec<f32> = (0..cols).map(|i| (i as f32).sin()).collect();
        let mut out_kernel = vec![0.0f32; rows];
        let mut out_wrapper = vec![0.0f32; rows];
        mxfp4_matvec_fused_scalar(&input, &data, rows, cols, &mut out_kernel);
        mxfp4_matvec(&matrix, &input, &mut out_wrapper);
        assert_eq!(
            out_kernel, out_wrapper,
            "wrapper must match kernel bit-exact"
        );
    }

    #[test]
    fn mxfp4_matvec_zero_input_returns_zero() {
        let rows = 2;
        let cols = 32;
        let data = mxfp4_synthetic_tensor(rows, cols, 127, 0x9);
        let input = vec![0.0f32; cols];
        let mut out = vec![f32::NAN; rows];
        mxfp4_matvec_fused_scalar(&input, &data, rows, cols, &mut out);
        for (i, &v) in out.iter().enumerate() {
            assert_eq!(
                v, 0.0,
                "output[{i}] = {v} but zero input must give zero output"
            );
        }
    }

    #[test]
    fn mxfp4_matvec_unit_input_recovers_row_sum() {
        // input = all ones → output[row] = sum of dequantized row values.
        // Cross-check against the sum of the dequantized bytes.
        let rows = 2;
        let cols = 32;
        let data = mxfp4_synthetic_tensor(rows, cols, 127, 0x7);
        let input = vec![1.0f32; cols];
        let mut out = vec![0.0f32; rows];
        mxfp4_matvec_fused_scalar(&input, &data, rows, cols, &mut out);

        // Independent oracle: dequantize each row and sum.
        for row in 0..rows {
            let mut row_f32 = vec![0.0f32; cols];
            let row_bytes = (cols / QK_MXFP4) * 17;
            dequantize_row_mxfp4(&data[row * row_bytes..(row + 1) * row_bytes], &mut row_f32);
            let expected: f32 = row_f32.iter().sum();
            assert!(
                (out[row] - expected).abs() < 1e-4,
                "row {row}: kernel {} vs oracle sum {}",
                out[row],
                expected
            );
        }
    }

    #[test]
    #[cfg(target_arch = "x86_64")]
    fn mxfp4_matvec_avx2_matches_scalar() {
        // Phase X.4.f.2.b AVX2 parity test. Skips gracefully if the
        // host CPU lacks AVX2 (very old x86_64: Ivy Bridge and older).
        // Like NEON, uses horizontal reduce so parity is
        // tolerance-based (~1e-4 relative), not bit-exact.
        if !is_x86_feature_detected!("avx2") || !is_x86_feature_detected!("fma") {
            eprintln!("skipping mxfp4_matvec_avx2_matches_scalar: host CPU lacks AVX2+FMA");
            return;
        }
        let rows = 4;
        let cols = 128;
        let data = mxfp4_synthetic_tensor(rows, cols, 127, 0x5);
        let input: Vec<f32> = (0..cols).map(|i| ((i as f32) * 0.03).sin()).collect();

        let mut out_scalar = vec![0.0_f32; rows];
        mxfp4_matvec_fused_scalar(&input, &data, rows, cols, &mut out_scalar);

        let mut out_avx2 = vec![0.0_f32; rows];
        // Dispatched call routes to AVX2 on x86_64 with the feature bits set.
        mxfp4_matvec_dispatched(&input, &data, rows, cols, &mut out_avx2);

        for row in 0..rows {
            let s = out_scalar[row];
            let a = out_avx2[row];
            let abs_err = (s - a).abs();
            let rel_err = if s.abs() > 1e-6 {
                abs_err / s.abs()
            } else {
                abs_err
            };
            assert!(
                rel_err < 1e-4,
                "row {row}: scalar {s}, avx2 {a}, rel_err {rel_err}"
            );
        }
    }

    #[test]
    #[cfg(target_arch = "aarch64")]
    fn mxfp4_matvec_neon_matches_scalar() {
        // Phase X.4.f.2.a NEON parity test. NEON uses vaddvq_f32
        // horizontal reduction which differs from the scalar kernel's
        // strict ascending element order, so parity is tolerance-based
        // (~1e-4 relative) rather than bit-exact.
        let rows = 4;
        let cols = 128;
        let data = mxfp4_synthetic_tensor(rows, cols, 127, 0x5);
        let input: Vec<f32> = (0..cols).map(|i| ((i as f32) * 0.03).sin()).collect();

        let mut out_scalar = vec![0.0_f32; rows];
        mxfp4_matvec_fused_scalar(&input, &data, rows, cols, &mut out_scalar);

        let mut out_neon = vec![0.0_f32; rows];
        // Dispatched call routes to NEON on aarch64.
        mxfp4_matvec_dispatched(&input, &data, rows, cols, &mut out_neon);

        for row in 0..rows {
            let s = out_scalar[row];
            let n = out_neon[row];
            let abs_err = (s - n).abs();
            let rel_err = if s.abs() > 1e-6 {
                abs_err / s.abs()
            } else {
                abs_err
            };
            assert!(
                rel_err < 1e-4,
                "row {row}: scalar {s}, neon {n}, rel_err {rel_err}"
            );
        }
    }

    #[test]
    fn mxfp4_matvec_dispatched_zero_input_returns_zero() {
        // Cross-arch smoke test: dispatched entry should behave the
        // same as the scalar kernel on zero input regardless of NEON.
        let rows = 2;
        let cols = 32;
        let data = mxfp4_synthetic_tensor(rows, cols, 127, 0x9);
        let input = vec![0.0_f32; cols];
        let mut out = vec![f32::NAN; rows];
        mxfp4_matvec_dispatched(&input, &data, rows, cols, &mut out);
        for (i, &v) in out.iter().enumerate() {
            assert_eq!(v, 0.0, "dispatched output[{i}] = {v} but expected 0.0");
        }
    }

    #[test]
    fn mxfp4_matvec_single_block_hand_computed() {
        // Build one block of 32 elements: scale = 2.0 (E8M0 byte 128),
        // all nibbles = 0x2 (E2M1 index → 1.0 per the OCP table),
        // so every element dequantizes to 1.0 · 2.0 = 2.0.
        // input = [1, 0, 0, ..., 0] → matvec output = 2.0.
        let cols = QK_MXFP4;
        // E2M1 index 0x2 corresponds to +1.0 per the OCP decode table.
        assert_eq!(E2M1_DECODE_TABLE[0x2], 1.0);
        let scale_byte = 128; // E8M0: exponent 128 - 127 = 1 → 2^1 = 2.0
        assert!((decode_e8m0_scale(scale_byte) - 2.0).abs() < 1e-6);
        let mut data = vec![0u8; 17];
        data[0] = scale_byte;
        // Nibble 0x2 in both halves → byte = 0x22.
        for byte in &mut data[1..17] {
            *byte = 0x22;
        }
        let mut input = vec![0.0f32; cols];
        input[0] = 1.0;
        let mut out = [0.0f32; 1];
        mxfp4_matvec_fused_scalar(&input, &data, 1, cols, &mut out);
        // input[0] · dq[0] = 1.0 · (1.0 · 2.0) = 2.0.
        assert!((out[0] - 2.0).abs() < 1e-6, "expected 2.0, got {}", out[0]);
    }

    // ─── Phase X.4.g.0: MXFP4 GPU upload padding helper tests ────────

    #[test]
    fn mxfp4_gpu_pad_constants_are_20_bytes_5_words() {
        // Sanity: match the Q8_0 GPU pattern (9 u32 words per block).
        // MXFP4 native = 17 bytes → padded = 20 bytes = 5 u32 words.
        assert_eq!(MXFP4_GPU_WORDS_PER_BLOCK, 5);
        assert_eq!(MXFP4_GPU_BYTES_PER_BLOCK, 20);
    }

    #[test]
    fn pack_mxfp4_places_scale_in_word0_low_byte() {
        // One block, scale byte = 42, all packed bytes = 0xAB.
        let mut native = vec![0u8; 17];
        native[0] = 42;
        for byte in &mut native[1..17] {
            *byte = 0xAB;
        }
        let padded = pack_mxfp4_for_gpu_upload(&native);
        assert_eq!(padded.len(), 20);
        // Byte 0 of padded = scale.
        assert_eq!(padded[0], 42);
        // Bytes 1..4 = zero-fill padding.
        for offset in 1..4 {
            assert_eq!(padded[offset], 0, "padding byte at {offset} must be 0");
        }
        // Bytes 4..20 = the 16 packed E2M1 bytes.
        for offset in 4..20 {
            assert_eq!(padded[offset], 0xAB, "packed byte at {offset} must be 0xAB");
        }
    }

    #[test]
    fn pack_mxfp4_roundtrip_preserves_native_layout() {
        // Multi-block roundtrip: pack then unpack must recover the
        // original native bytes exactly.
        let n_blocks = 4;
        let mut native = vec![0u8; n_blocks * 17];
        for blk in 0..n_blocks {
            let base = blk * 17;
            native[base] = (blk as u8).wrapping_mul(37).wrapping_add(1);
            for i in 1..17 {
                native[base + i] = ((blk * 17 + i) & 0xFF) as u8;
            }
        }
        let padded = pack_mxfp4_for_gpu_upload(&native);
        assert_eq!(padded.len(), n_blocks * 20);
        let recovered = unpack_mxfp4_from_gpu_upload(&padded);
        assert_eq!(recovered, native, "roundtrip must preserve every byte");
    }

    #[test]
    fn pack_mxfp4_matches_dequant_end_to_end() {
        // Roundtrip preserves dequantized output as well: pack → unpack
        // → dequant should give the same f32 values as the direct
        // dequant of the original native row.
        let native = mxfp4_synthetic_tensor(1, 32, 127, 0x5);
        assert_eq!(native.len(), 17);
        let padded = pack_mxfp4_for_gpu_upload(&native);
        let recovered = unpack_mxfp4_from_gpu_upload(&padded);
        let mut direct = vec![0.0f32; 32];
        dequantize_row_mxfp4(&native, &mut direct);
        let mut via_roundtrip = vec![0.0f32; 32];
        dequantize_row_mxfp4(&recovered, &mut via_roundtrip);
        assert_eq!(
            direct, via_roundtrip,
            "roundtrip must preserve dequant output"
        );
    }

    #[test]
    #[should_panic(expected = "not a multiple of 17")]
    fn pack_mxfp4_rejects_malformed_native_length() {
        let native = vec![0u8; 16]; // 16 is not a multiple of 17
        let _ = pack_mxfp4_for_gpu_upload(&native);
    }

    #[test]
    #[should_panic(expected = "padding byte at offset 1")]
    fn unpack_mxfp4_rejects_dirty_padding() {
        // Producer-side bug simulation: padding bytes are non-zero,
        // so unpacking must panic rather than silently produce
        // garbage native bytes.
        let mut padded = vec![0u8; 20];
        padded[0] = 42; // scale
        padded[1] = 0xFF; // dirty padding
        let _ = unpack_mxfp4_from_gpu_upload(&padded);
    }

    #[test]
    fn test_ggml_type_block_sizes() {
        assert_eq!(GgmlType::F32.block_bytes(), 4);
        assert_eq!(GgmlType::F16.block_bytes(), 2);
        assert_eq!(GgmlType::Q8_0.block_bytes(), 34);
        assert_eq!(GgmlType::Q4_K.block_bytes(), 144);
        assert_eq!(GgmlType::Q4_K.elements_per_block(), 256);
        assert_eq!(GgmlType::Q8_0.elements_per_block(), 32);
        // Q1_0 / Q2_0: PrismML Bonsai 27B group-128 layout.
        assert_eq!(GgmlType::Q1_0.block_bytes(), 18);
        assert_eq!(GgmlType::Q1_0.elements_per_block(), 128);
        assert_eq!(GgmlType::Q2_0.block_bytes(), 34);
        assert_eq!(GgmlType::Q2_0.elements_per_block(), 128);
        // Type IDs from the PrismML llama.cpp fork.
        assert_eq!(GgmlType::from_u32(41), GgmlType::Q1_0);
        assert_eq!(GgmlType::from_u32(42), GgmlType::Q2_0);
    }

    #[test]
    fn q1_0_dequantize_binary_ternary() {
        // Two group-128 blocks: scale=1.0, alternating +1/-1 pattern in block 0,
        // scale=0.5, all +1 in block 1.
        let mut data = vec![0u8; 2 * 18];
        let scale_1_f16: u16 = 0x3C00; // f16(1.0)
        let scale_half_f16: u16 = 0x3800; // f16(0.5)

        // Block 0: alternating pattern 0b10101010 = 0xAA (bit0=0→−1, bit1=1→+1, ...)
        data[0..2].copy_from_slice(&scale_1_f16.to_le_bytes());
        for b in 0..16 {
            data[2 + b] = 0xAA;
        }
        // Block 1: all 1s → +0.5
        data[18..20].copy_from_slice(&scale_half_f16.to_le_bytes());
        for b in 0..16 {
            data[18 + 2 + b] = 0xFF;
        }

        let mut out = vec![0.0f32; 256];
        dequantize_q1_0(&data, &mut out);

        // Block 0 alternating {−1, +1, −1, +1, ...}
        for (i, chunk) in out[0..128].as_chunks::<2>().0.iter().enumerate() {
            assert!(
                (chunk[0] - -1.0).abs() < 1e-4,
                "b0 elem {}: got {}, expected -1",
                i * 2,
                chunk[0]
            );
            assert!(
                (chunk[1] - 1.0).abs() < 1e-4,
                "b0 elem {}: got {}, expected +1",
                i * 2 + 1,
                chunk[1]
            );
        }
        // Block 1 all +0.5
        for (i, &v) in out[128..256].iter().enumerate() {
            assert!(
                (v - 0.5).abs() < 1e-4,
                "b1 elem {i}: got {v}, expected +0.5"
            );
        }
    }

    #[test]
    fn q2_0_dequantize_ternary_values() {
        // Single group-128 block: scale=2.0, alternating slot pattern
        //   slot 0 = 0b00 → −1
        //   slot 1 = 0b01 → 0
        //   slot 2 = 0b10 → +1
        //   slot 3 = 0b11 → +2 (reserved, but the formula gives +2)
        // Byte = 0b_11_10_01_00 = 0xE4 repeated 32 times.
        let mut data = vec![0u8; 34];
        let scale_2_f16: u16 = 0x4000; // f16(2.0)
        data[0..2].copy_from_slice(&scale_2_f16.to_le_bytes());
        for b in 0..32 {
            data[2 + b] = 0xE4;
        }

        let mut out = vec![0.0f32; 128];
        dequantize_q2_0(&data, &mut out);

        // Pattern repeats every 4 elements: [−2, 0, +2, +4] × scale=1
        // (with scale=2, we get [−2, 0, +2, +4]).
        let expected = [-2.0, 0.0, 2.0, 4.0];
        for (i, &v) in out.iter().enumerate() {
            let want = expected[i % 4];
            assert!(
                (v - want).abs() < 1e-4,
                "q2_0 elem {i}: got {v}, expected {want}"
            );
        }
    }

    #[test]
    fn q2_0_matvec_fallback_sanity() {
        // 1×128 matrix, ternary weights = alternating (+1, 0, -1, 0, ...)
        //   slot 0 = 0b10 → +1
        //   slot 1 = 0b01 → 0
        //   slot 2 = 0b00 → −1
        //   slot 3 = 0b01 → 0
        // Byte = 0b_01_00_01_10 = 0x46 repeated 32 times.
        let mut data = vec![0u8; 34];
        let scale_1_f16: u16 = 0x3C00; // f16(1.0)
        data[0..2].copy_from_slice(&scale_1_f16.to_le_bytes());
        for b in 0..32 {
            data[2 + b] = 0x46;
        }

        // Input: [1, 2, 3, 4, ...128]. Dot with weights (+1, 0, −1, 0, +1, 0, −1, 0, ...)
        // = Σ (+1 · x[4k]) + Σ (−1 · x[4k+2])
        // For x[i] = i+1: pairs contribute (4k+1) − (4k+3) = −2 per group of 4.
        // Total = 32 groups × −2 = −64.
        let input: Vec<f32> = (1..=128).map(|i| i as f32).collect();
        let mut out = vec![0.0f32; 1];
        q2_0_matvec_fallback(&input, &data, 1, 128, &mut out);
        assert!(
            (out[0] - -64.0).abs() < 1e-3,
            "q2_0 matvec: got {}, expected −64",
            out[0]
        );
    }

    /// Runs the Q1_0 matvec on a deterministically-generated 4-row × 256-col
    /// matrix and asserts the output matches a scalar reference within
    /// `rel_err < 1e-4` (loose enough to absorb the ~1.5e-5 ulp drift from
    /// per-block `d * (pos_sum − neg_sum)` vs the scalar row-wise sum).
    /// On aarch64 the primary path hits the NEON kernel;
    /// on other targets it hits the scalar fallback and reduces to a trivial
    /// consistency check. Guards against future silent regressions in the
    /// bit-lane expansion trick used inside `neon_dot::q1_0_dot_row`.
    #[test]
    fn q1_0_matvec_matches_scalar_ref() {
        let rows = 4usize;
        let cols = 256usize;
        let blocks_per_row = cols / 128;
        let row_bytes = blocks_per_row * 18;
        let mut data = vec![0u8; rows * row_bytes];
        for r in 0..rows {
            for b in 0..blocks_per_row {
                let base = r * row_bytes + b * 18;
                // Scale in [0.25, 1.0] via f16(0.5) baseline shifted per row.
                let scale_f16: u16 = 0x3800u16.wrapping_add((r as u16) * 0x0080);
                data[base..base + 2].copy_from_slice(&scale_f16.to_le_bytes());
                for k in 0..16 {
                    data[base + 2 + k] = ((r as u8)
                        .wrapping_mul(37)
                        .wrapping_add((b as u8) * 11 + k as u8))
                        ^ 0xA5;
                }
            }
        }
        let input: Vec<f32> = (0..cols).map(|i| ((i as f32) * 0.037).sin()).collect();

        let mut out_simd = vec![0.0f32; rows];
        q1_0_matvec_fallback(&input, &data, rows, cols, &mut out_simd);

        // Scalar reference: dequant + dot.
        let mut out_ref = vec![0.0f32; rows];
        let mut row_buf = vec![0.0f32; cols];
        for r in 0..rows {
            let rd = &data[r * row_bytes..(r + 1) * row_bytes];
            dequantize_q1_0(rd, &mut row_buf);
            let mut acc = 0.0f32;
            for i in 0..cols {
                acc += row_buf[i] * input[i];
            }
            out_ref[r] = acc;
        }

        for (r, (&s, &g)) in out_simd.iter().zip(out_ref.iter()).enumerate() {
            let denom = g.abs().max(1e-6);
            let rel = (s - g).abs() / denom;
            assert!(
                rel < 1e-4,
                "q1_0 row {r}: simd={s} scalar={g} rel_err={rel}"
            );
        }
    }

    /// Sibling of `q1_0_matvec_matches_scalar_ref` for Q2_0. Exercises the
    /// per-byte broadcast + right-shift + mask + subtract-1 path inside
    /// `neon_dot::q2_0_dot_row` on aarch64, scalar fallback everywhere else.
    #[test]
    fn q2_0_matvec_matches_scalar_ref() {
        let rows = 4usize;
        let cols = 256usize;
        let blocks_per_row = cols / 128;
        let row_bytes = blocks_per_row * 34;
        let mut data = vec![0u8; rows * row_bytes];
        for r in 0..rows {
            for b in 0..blocks_per_row {
                let base = r * row_bytes + b * 34;
                let scale_f16: u16 = 0x3800u16.wrapping_add((r as u16) * 0x0080);
                data[base..base + 2].copy_from_slice(&scale_f16.to_le_bytes());
                for k in 0..32 {
                    data[base + 2 + k] = ((r as u8)
                        .wrapping_mul(53)
                        .wrapping_add((b as u8) * 7 + k as u8))
                        ^ 0x3C;
                }
            }
        }
        let input: Vec<f32> = (0..cols).map(|i| ((i as f32) * 0.041).cos()).collect();

        let mut out_simd = vec![0.0f32; rows];
        q2_0_matvec_fallback(&input, &data, rows, cols, &mut out_simd);

        let mut out_ref = vec![0.0f32; rows];
        let mut row_buf = vec![0.0f32; cols];
        for r in 0..rows {
            let rd = &data[r * row_bytes..(r + 1) * row_bytes];
            dequantize_q2_0(rd, &mut row_buf);
            let mut acc = 0.0f32;
            for i in 0..cols {
                acc += row_buf[i] * input[i];
            }
            out_ref[r] = acc;
        }

        for (r, (&s, &g)) in out_simd.iter().zip(out_ref.iter()).enumerate() {
            let denom = g.abs().max(1e-6);
            let rel = (s - g).abs() / denom;
            assert!(
                rel < 1e-4,
                "q2_0 row {r}: simd={s} scalar={g} rel_err={rel}"
            );
        }
    }

    #[test]
    fn test_q8_0_dequantize_roundtrip() {
        // Create a Q8_0 block: scale=0.1, values=[0,1,2,...,31]
        let mut block = vec![0u8; 34];
        // scale = 0.1 in f16 ≈ 0x2E66
        let scale_f16: u16 = 0x2E66;
        block[0] = scale_f16 as u8;
        block[1] = (scale_f16 >> 8) as u8;
        for i in 0..32 {
            block[2 + i] = i as u8;
        }

        let mut out = vec![0.0f32; 32];
        dequantize_q8_0(&block, &mut out);

        let scale = f16_to_f32(scale_f16);
        for i in 0..32 {
            let expected = scale * i as f32;
            assert!(
                (out[i] - expected).abs() < 1e-4,
                "q8_0[{i}]: got {}, expected {expected}",
                out[i]
            );
        }
    }

    #[test]
    fn test_q8_0_fused_matvec() {
        // 2x32 identity-like matrix: row0=[1,0,0,...], row1=[0,1,0,...]
        // Scale = 1.0 (f16 = 0x3C00)
        let mut data = vec![0u8; 2 * 34];
        let scale: u16 = 0x3C00;

        // Row 0
        data[0] = scale as u8;
        data[1] = (scale >> 8) as u8;
        data[2] = 1; // qs[0] = 1

        // Row 1
        data[34] = scale as u8;
        data[35] = (scale >> 8) as u8;
        data[36] = 0; // qs[0] = 0
        data[37] = 1; // qs[1] = 1

        let mut input = vec![0.0f32; 32];
        input[0] = 3.0;
        input[1] = 7.0;

        let mut output = vec![0.0f32; 2];
        q8_0_matvec(&input, &data, 2, 32, &mut output);

        assert!((output[0] - 3.0).abs() < 1e-4);
        assert!((output[1] - 7.0).abs() < 1e-4);
    }

    #[test]
    fn test_get_scale_min_k4() {
        let scales = [10, 20, 30, 40, 50, 60, 70, 80, 0xAB, 0xCD, 0xEF, 0x12];

        // j < 4: sc = scales[j] & 63, m = scales[j+4] & 63
        let (sc, m) = get_scale_min_k4(0, &scales);
        assert_eq!(sc, 10); // 10 & 63
        assert_eq!(m, 50); // 50 & 63

        let (sc, m) = get_scale_min_k4(3, &scales);
        assert_eq!(sc, 40); // 40 & 63
        assert_eq!(m, 16); // 80 & 63
    }

    #[test]
    fn test_gguf_parse_invalid_magic() {
        let data = [0u8; 64];
        assert!(GgufFile::parse(&data).is_none());
    }

    #[test]
    fn test_tensor_info_n_elements() {
        let info = TensorInfo {
            name: "test".into(),
            n_dims: 2,
            dims: vec![4096, 4096],
            qtype: GgmlType::Q4_K,
            offset: 0,
        };
        assert_eq!(info.n_elements(), 4096 * 4096);
    }

    #[test]
    fn test_tensor_info_data_size() {
        let info = TensorInfo {
            name: "test".into(),
            n_dims: 2,
            dims: vec![4096, 4096],
            qtype: GgmlType::Q4_K,
            offset: 0,
        };
        let n = 4096 * 4096;
        let n_blocks = n / 256;
        assert_eq!(info.data_size(), n_blocks * 144);
    }

    #[test]
    fn test_meta_value_conversions() {
        let u = MetaValue::U32(42);
        assert_eq!(u.as_u32(), Some(42));
        assert_eq!(u.as_f32(), None);

        let f = MetaValue::F32(3.14);
        assert!((f.as_f32().unwrap() - 3.14).abs() < 1e-6);

        let s = MetaValue::Str("hello".into());
        assert_eq!(s.as_str(), Some("hello"));
    }

    #[test]
    fn test_sparse_ternary_row_from_f32() {
        // 32 weights, keep 8 per block of 16 (50% sparsity)
        let weights: Vec<f32> = (0..32).map(|i| (i as f32 - 15.5) * 0.1).collect();
        let row = SparseTernaryRow::from_f32(&weights, 0.3, 8);

        assert_eq!(row.num_cols, 32);
        assert_eq!(row.num_blocks, 2);
        // At most 8 active per block
        assert!(row.active_masks[0].count_ones() <= 8);
        assert!(row.active_masks[1].count_ones() <= 8);
        assert!(row.scale > 0.0);
    }

    #[test]
    fn test_sparse_ternary_enforces_nm_sparsity() {
        // 16 weights, keep 4 per block (75% sparsity = 4:16)
        let weights: Vec<f32> = vec![
            1.0, -2.0, 0.5, -0.1, 3.0, -0.3, 0.8, -0.05, 0.02, -1.5, 0.7, -0.9, 0.01, -0.4, 2.5,
            -0.6,
        ];
        let row = SparseTernaryRow::from_f32(&weights, 0.1, 4);
        assert_eq!(row.active_masks[0].count_ones(), 4);
        assert_eq!(row.nnz(), 4);
    }

    #[test]
    fn test_sparse_ternary_matvec_matches_dense() {
        // Create a small matrix, compare sparse vs dense ternary matvec
        let weights = vec![
            1.0, -2.0, 0.5, -0.1, 3.0, -0.3, 0.8, -0.05, 0.02, -1.5, 0.7, -0.9, 0.01, -0.4, 2.5,
            -0.6, // Row 2
            -1.0, 2.0, -0.5, 0.1, -3.0, 0.3, -0.8, 0.05, -0.02, 1.5, -0.7, 0.9, -0.01, 0.4, -2.5,
            0.6,
        ];
        let num_rows = 2;
        let num_cols = 16;

        // Dense ternary (keep all)
        let dense = TernaryMatrix {
            rows: (0..num_rows)
                .map(|r| TernaryRow::from_f32(&weights[r * num_cols..(r + 1) * num_cols], 0.05))
                .collect(),
            num_rows,
            num_cols,
        };

        // Sparse ternary (keep 16 = all, so should match dense)
        let sparse = SparseTernaryMatrix::from_f32_weights(&weights, num_rows, num_cols, 0.05, 16);

        let input = vec![
            1.0, 0.5, -1.0, 2.0, 0.3, -0.7, 1.5, -0.2, 0.8, -1.2, 0.4, 0.6, -0.9, 1.1, -0.3, 0.7,
        ];
        let mut dense_out = vec![0.0f32; num_rows];
        let mut sparse_out = vec![0.0f32; num_rows];

        ternary_matvec(&dense, &input, &mut dense_out);
        sparse_ternary_matvec(&sparse, &input, &mut sparse_out);

        // With n_keep=16 (all), sparse should match dense
        // Tolerance widened for i8 activation quantization (~2% relative error)
        for i in 0..num_rows {
            let tol = dense_out[i].abs().max(0.01) * 0.05;
            assert!(
                (dense_out[i] - sparse_out[i]).abs() < tol,
                "row {i}: dense={} sparse={}",
                dense_out[i],
                sparse_out[i]
            );
        }
    }

    #[test]
    fn test_sparse_ternary_memory_reduction() {
        // 1024-col row, dense ternary = 2 bitmasks of 128 bytes each = 256 bytes + scale
        // sparse ternary = 64 blocks × (2+2) bytes = 256 bytes + scale
        // But with 50% sparsity, fewer ops in matvec (not less storage for masks)
        let weights: Vec<f32> = (0..1024).map(|i| ((i as f32 * 0.7).sin()) * 2.0).collect();
        let sparse = SparseTernaryMatrix::from_f32_weights(&weights, 1, 1024, 0.5, 8);

        // Verify nnz is roughly 50% of 1024
        let nnz = sparse.total_nnz();
        assert!(nnz <= 1024 / 2 + 64, "nnz={nnz} should be ~512");
        assert!(nnz > 0);

        // Verify memory is reasonable
        let mem = sparse.memory_bytes();
        assert!(mem > 0 && mem < 1024 * 1024, "memory={mem} bytes");
    }

    #[test]
    fn test_sparse_ternary_dot_correctness() {
        // Manual test: 16 weights, keep top-2, verify dot product
        let weights = vec![
            0.0, 0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -3.0, 0.0, 0.0, 0.0, 0.0, 0.0,
        ];
        let row = SparseTernaryRow::from_f32(&weights, 0.01, 2);

        // Only positions 3 (+5.0→+1) and 10 (-3.0→-1) should be active
        assert_eq!(row.nnz(), 2);
        let scale = row.scale;

        // Build 1-row matrix for matvec test
        let matrix = SparseTernaryMatrix::from_rows(vec![row], 1, 16, 0.5);

        let mut input_test = vec![0.0f32; 16];
        input_test[3] = 10.0;
        input_test[10] = 7.0;

        let mut output = vec![0.0f32; 1];
        sparse_ternary_matvec(&matrix, &input_test, &mut output);

        // Expected: scale * (10.0 - 7.0) = scale * 3.0
        // Tolerance widened for i8 activation quantization (~0.5% error)
        let expected = scale * 3.0;
        assert!(
            (output[0] - expected).abs() < expected.abs() * 0.05,
            "result={}, expected={expected}",
            output[0]
        );
    }

    /// Verify `sdot_s32` matches the manual reference for known i8 inputs.
    /// This test runs on both dotprod-enabled (inline asm) and fallback
    /// (NEON `vmlal_s16`) implementations, chosen at compile time by
    /// `#[cfg(target_feature = "dotprod")]`, so it validates semantic
    /// equivalence between the two paths across ARMv8.2-A and pre-dotprod
    /// aarch64 targets.
    #[cfg(target_arch = "aarch64")]
    #[test]
    fn sdot_s32_matches_manual_reference() {
        use std::arch::aarch64::{vdupq_n_s32, vld1q_s8, vst1q_s32};
        unsafe {
            let acc = vdupq_n_s32(0);
            let a_arr: [i8; 16] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];
            let b_arr: [i8; 16] = [1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1];
            let a = vld1q_s8(a_arr.as_ptr());
            let b = vld1q_s8(b_arr.as_ptr());
            let result = super::sdot_s32(acc, a, b);
            let mut out = [0i32; 4];
            vst1q_s32(out.as_mut_ptr(), result);
            // lane i accumulates Σ_{k=0..3} a[4i+k] * b[4i+k].
            // lane 0: 1 - 2 + 3 - 4 = -2
            // lane 1: 5 - 6 + 7 - 8 = -2
            // lane 2: 9 - 10 + 11 - 12 = -2
            // lane 3: 13 - 14 + 15 - 16 = -2
            assert_eq!(out, [-2, -2, -2, -2]);
        }
    }

    /// Verify `sdot_s32` accumulates onto a non-zero seed correctly.
    /// Complements `sdot_s32_matches_manual_reference` by exercising
    /// the `acc +=` semantics (rather than `acc = 0`).
    #[cfg(target_arch = "aarch64")]
    #[test]
    fn sdot_s32_accumulates_onto_seed() {
        use std::arch::aarch64::{vld1q_s32, vld1q_s8, vst1q_s32};
        unsafe {
            let seed: [i32; 4] = [100, 200, 300, 400];
            let acc = vld1q_s32(seed.as_ptr());
            let a_arr: [i8; 16] = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1];
            let b_arr: [i8; 16] = [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2];
            let a = vld1q_s8(a_arr.as_ptr());
            let b = vld1q_s8(b_arr.as_ptr());
            let result = super::sdot_s32(acc, a, b);
            let mut out = [0i32; 4];
            vst1q_s32(out.as_mut_ptr(), result);
            // Each lane adds 1*2 + 1*2 + 1*2 + 1*2 = 8 to the seed.
            assert_eq!(out, [108, 208, 308, 408]);
        }
    }

    // ─── Tokenizer const + helper regression tests (Issue #14) ──────────────

    #[test]
    fn token_type_constants_stable() {
        // Regression detection: llama.cpp `enum llama_token_type` in llama.h
        // The GGUF spec pins these discriminants, so accidental drift here
        // silently breaks special-token detection for every GGUF ever produced.
        assert_eq!(TOKEN_TYPE_NORMAL, 1);
        assert_eq!(TOKEN_TYPE_UNKNOWN, 2);
        assert_eq!(TOKEN_TYPE_CONTROL, 3);
        assert_eq!(TOKEN_TYPE_USER_DEFINED, 4);
        assert_eq!(TOKEN_TYPE_UNUSED, 5);
        assert_eq!(TOKEN_TYPE_BYTE, 6);
    }

    #[test]
    fn is_atomic_special_token_matches_control_and_user_defined() {
        assert!(is_atomic_special_token(TOKEN_TYPE_CONTROL));
        assert!(is_atomic_special_token(TOKEN_TYPE_USER_DEFINED));
        assert!(!is_atomic_special_token(TOKEN_TYPE_NORMAL));
        assert!(!is_atomic_special_token(TOKEN_TYPE_UNKNOWN));
        assert!(!is_atomic_special_token(TOKEN_TYPE_UNUSED));
        assert!(!is_atomic_special_token(TOKEN_TYPE_BYTE));
    }

    #[test]
    fn parse_spm_byte_fallback_valid() {
        assert_eq!(parse_spm_byte_fallback("<0x00>"), Some(0x00));
        assert_eq!(parse_spm_byte_fallback("<0xFF>"), Some(0xFF));
        assert_eq!(parse_spm_byte_fallback("<0xAB>"), Some(0xAB));
        // Lowercase hex is accepted by `u8::from_str_radix`.
        assert_eq!(parse_spm_byte_fallback("<0xab>"), Some(0xAB));
        assert_eq!(parse_spm_byte_fallback("<0xA5>"), Some(0xA5));
    }

    #[test]
    fn parse_spm_byte_fallback_rejects_invalid() {
        // Non-hex digit
        assert_eq!(parse_spm_byte_fallback("<0xG0>"), None);
        // Too short (1 hex digit)
        assert_eq!(parse_spm_byte_fallback("<0x0>"), None);
        // Too long (3 hex digits)
        assert_eq!(parse_spm_byte_fallback("<0x000>"), None);
        // Missing wrapping angle brackets
        assert_eq!(parse_spm_byte_fallback("0xFF"), None);
        // GPT-2 special marker (starts with `<|`)
        assert_eq!(parse_spm_byte_fallback("<|endoftext|>"), None);
        // Empty
        assert_eq!(parse_spm_byte_fallback(""), None);
        // SPM special marker (`<...>` but not `<0x`)
        assert_eq!(parse_spm_byte_fallback("<unk>"), None);
    }

    #[test]
    fn is_gpt2_special_marker_matches_expected_pattern() {
        assert!(is_gpt2_special_marker("<|endoftext|>"));
        assert!(is_gpt2_special_marker("<|im_start|>"));
        assert!(is_gpt2_special_marker("<|im_end|>"));
        assert!(!is_gpt2_special_marker("<unk>"));
        assert!(!is_gpt2_special_marker("<0xFF>"));
        assert!(!is_gpt2_special_marker("hello"));
        assert!(!is_gpt2_special_marker(""));
    }

    // ── x86_64 SIMD quantized dot product parity (Issue #13) ─────────────

    /// Build a deterministic Q4_K block filled with `seed`-derived bytes so
    /// the SIMD ↔ scalar comparison covers every byte of the layout.
    #[cfg(target_arch = "x86_64")]
    fn make_q4k_block(seed: u8) -> Vec<u8> {
        let mut block = vec![0u8; 144];
        // Header: pick small non-zero f16 values so `d * total` doesn't
        // saturate to inf or underflow to 0.
        block[0..2].copy_from_slice(&0x3800u16.to_le_bytes()); // f16(0.5)
        block[2..4].copy_from_slice(&0x3400u16.to_le_bytes()); // f16(0.25)
                                                               // Scales / mins header (12 bytes): fill with the seed so the utmp
                                                               // reconstruction hits every branch.
        for i in 0..12 {
            block[4 + i] = seed.wrapping_add(i as u8);
        }
        // 128 packed nibble bytes: spread the seed so both low and high
        // nibbles vary across the block.
        for i in 0..128 {
            block[16 + i] = seed.wrapping_mul(3).wrapping_add(i as u8);
        }
        block
    }

    /// Build a deterministic Q6_K block (210 bytes).
    #[cfg(target_arch = "x86_64")]
    fn make_q6k_block(seed: u8) -> Vec<u8> {
        let mut block = vec![0u8; 210];
        // ql: 128 bytes
        for i in 0..128 {
            block[i] = seed.wrapping_add(i as u8);
        }
        // qh: 64 bytes (top 2 bits of each 6-bit value)
        for i in 0..64 {
            block[128 + i] = seed.wrapping_mul(5).wrapping_add(i as u8);
        }
        // scales: 16 bytes, treated as signed by the dispatcher
        for i in 0..16 {
            block[192 + i] = (i as u8).wrapping_sub(4);
        }
        // d: small non-zero f16
        block[208..210].copy_from_slice(&0x3800u16.to_le_bytes());
        block
    }

    /// Build a Q8_K block whose contents cover both positive and negative
    /// activations, so the sign trick paths are actually exercised.
    #[cfg(target_arch = "x86_64")]
    fn make_q8k_block(seed: u8) -> BlockQ8K {
        let mut block = BlockQ8K {
            d: 0.125,
            qs: [0i8; QK_K],
            bsums: [0i16; 16],
        };
        for i in 0..QK_K {
            let raw = seed.wrapping_add(i as u8);
            block.qs[i] = (raw as i8).wrapping_sub(64);
        }
        for j in 0..16 {
            let mut acc = 0i32;
            for l in 0..16 {
                acc += block.qs[j * 16 + l] as i32;
            }
            block.bsums[j] = acc as i16;
        }
        block
    }

    #[cfg(target_arch = "x86_64")]
    #[test]
    fn q4k_avx2_matches_scalar_bit_exact() {
        if !x86_avx2_supported() {
            eprintln!("SKIP: AVX2 unavailable on this runner");
            return;
        }
        for seed in [0u8, 1, 7, 128, 200, 255] {
            let q4k = make_q4k_block(seed);
            let q8k = make_q8k_block(seed.wrapping_mul(13));
            let scalar = q4k_q8k_dot_fallback_scalar(&q4k, &q8k);
            // SAFETY: guarded by `x86_avx2_supported()` above.
            let simd = unsafe { avx2_dot::q4k_q8k_dot(&q4k, &q8k) };
            assert_eq!(
                scalar.to_bits(),
                simd.to_bits(),
                "seed={seed}: scalar={scalar} simd={simd}"
            );
        }
    }

    #[cfg(target_arch = "x86_64")]
    #[test]
    fn q6k_avx2_matches_scalar_bit_exact() {
        if !x86_avx2_supported() {
            eprintln!("SKIP: AVX2 unavailable on this runner");
            return;
        }
        for seed in [0u8, 1, 7, 128, 200, 255] {
            let q6k = make_q6k_block(seed);
            let q8k = make_q8k_block(seed.wrapping_mul(11));
            let scalar = q6k_q8k_dot_fallback_scalar(&q6k, &q8k);
            // SAFETY: guarded by AVX2 detection.
            let simd = unsafe { avx2_dot::q6k_q8k_dot(&q6k, &q8k) };
            assert_eq!(
                scalar.to_bits(),
                simd.to_bits(),
                "seed={seed}: scalar={scalar} simd={simd}"
            );
        }
    }

    #[cfg(target_arch = "x86_64")]
    #[test]
    fn q4k_avx512_matches_scalar_bit_exact() {
        if !x86_avx512bw_supported() {
            eprintln!("SKIP: AVX-512BW unavailable on this runner");
            return;
        }
        for seed in [0u8, 1, 7, 128, 200, 255] {
            let q4k = make_q4k_block(seed);
            let q8k = make_q8k_block(seed.wrapping_mul(13));
            let scalar = q4k_q8k_dot_fallback_scalar(&q4k, &q8k);
            // SAFETY: guarded by AVX-512BW detection.
            let simd = unsafe { avx512_dot::q4k_q8k_dot(&q4k, &q8k) };
            assert_eq!(
                scalar.to_bits(),
                simd.to_bits(),
                "seed={seed}: scalar={scalar} simd={simd}"
            );
        }
    }

    #[cfg(target_arch = "x86_64")]
    #[test]
    fn q6k_avx512_matches_scalar_bit_exact() {
        if !x86_avx512bw_supported() {
            eprintln!("SKIP: AVX-512BW unavailable on this runner");
            return;
        }
        for seed in [0u8, 1, 7, 128, 200, 255] {
            let q6k = make_q6k_block(seed);
            let q8k = make_q8k_block(seed.wrapping_mul(11));
            let scalar = q6k_q8k_dot_fallback_scalar(&q6k, &q8k);
            // SAFETY: guarded by AVX-512BW detection.
            let simd = unsafe { avx512_dot::q6k_q8k_dot(&q6k, &q8k) };
            assert_eq!(
                scalar.to_bits(),
                simd.to_bits(),
                "seed={seed}: scalar={scalar} simd={simd}"
            );
        }
    }

    #[cfg(target_arch = "x86_64")]
    #[test]
    fn feature_detection_caches_result() {
        // OnceLock ensures the CPU detection cost is paid at most once.
        let first = x86_avx2_supported();
        let second = x86_avx2_supported();
        assert_eq!(first, second);
        let first_512 = x86_avx512bw_supported();
        let second_512 = x86_avx512bw_supported();
        assert_eq!(first_512, second_512);
    }

    /// Build a deterministic Q5_K block (176 bytes) so the qh + qs
    /// combined 5-bit path is exercised on both scalar and SIMD paths.
    #[cfg(target_arch = "x86_64")]
    fn make_q5k_block(seed: u8) -> Vec<u8> {
        let mut block = vec![0u8; 176];
        // d, dmin: small non-zero f16.
        block[0..2].copy_from_slice(&0x3800u16.to_le_bytes()); // f16(0.5)
        block[2..4].copy_from_slice(&0x3400u16.to_le_bytes()); // f16(0.25)
                                                               // 12-byte scales/mins packed header.
        for i in 0..12 {
            block[4 + i] = seed.wrapping_add(i as u8);
        }
        // qh: 32 bytes — 5th bit per element, varied so every im iteration
        // touches a different bit-position pattern.
        for i in 0..32 {
            block[16 + i] = seed.wrapping_mul(7).wrapping_add(i as u8);
        }
        // qs: 128 packed nibble bytes.
        for i in 0..128 {
            block[48 + i] = seed.wrapping_mul(3).wrapping_add(i as u8);
        }
        block
    }

    #[cfg(target_arch = "x86_64")]
    #[test]
    fn q5k_avx2_matches_scalar_bit_exact() {
        if !x86_avx2_supported() {
            eprintln!("SKIP: AVX2 unavailable on this runner");
            return;
        }
        for seed in [0u8, 1, 7, 128, 200, 255] {
            let q5k = make_q5k_block(seed);
            let q8k = make_q8k_block(seed.wrapping_mul(17));
            let scalar = q5k_q8k_dot_fallback_scalar(&q5k, &q8k);
            // SAFETY: guarded by AVX2 detection.
            let simd = unsafe { avx2_dot::q5k_q8k_dot(&q5k, &q8k) };
            assert_eq!(
                scalar.to_bits(),
                simd.to_bits(),
                "seed={seed}: scalar={scalar} simd={simd}"
            );
        }
    }

    #[cfg(target_arch = "x86_64")]
    #[test]
    fn q5k_avx512_matches_scalar_bit_exact() {
        if !x86_avx512bw_supported() {
            eprintln!("SKIP: AVX-512BW unavailable on this runner");
            return;
        }
        for seed in [0u8, 1, 7, 128, 200, 255] {
            let q5k = make_q5k_block(seed);
            let q8k = make_q8k_block(seed.wrapping_mul(17));
            let scalar = q5k_q8k_dot_fallback_scalar(&q5k, &q8k);
            // SAFETY: guarded by AVX-512BW detection.
            let simd = unsafe { avx512_dot::q5k_q8k_dot(&q5k, &q8k) };
            assert_eq!(
                scalar.to_bits(),
                simd.to_bits(),
                "seed={seed}: scalar={scalar} simd={simd}"
            );
        }
    }

    // ── Q8_0 SIMD parity (f32 FMA path, not integer madd) ───────────────

    /// Build a deterministic Q8_0-formatted row for a 1-row × `cols`
    /// weight matrix. Each block = 2-byte f16 scale + 32 i8 quantised
    /// weights, so the row occupies `blocks_per_row * 34` bytes.
    #[cfg(target_arch = "x86_64")]
    fn make_q8_0_row_bytes(seed: u8, cols: usize) -> Vec<u8> {
        let blocks_per_row = cols / QK8_0;
        let mut data = Vec::with_capacity(blocks_per_row * 34);
        for bi in 0..blocks_per_row {
            let scale_bits = 0x3800u16.wrapping_add(bi as u16); // varies per block
            data.extend_from_slice(&scale_bits.to_le_bytes());
            for l in 0..QK8_0 {
                let raw = seed.wrapping_add((bi * QK8_0 + l) as u8);
                data.push((raw as i8).wrapping_sub(32) as u8);
            }
        }
        data
    }

    #[cfg(target_arch = "x86_64")]
    fn make_q8_0_input(seed: u8, cols: usize) -> Vec<f32> {
        (0..cols)
            .map(|i| {
                let raw = seed.wrapping_add(i as u8) as i8;
                f32::from(raw) * 0.01
            })
            .collect()
    }

    #[cfg(target_arch = "x86_64")]
    #[test]
    fn q8_0_avx2_matches_scalar_relative_tolerance() {
        if !x86_avx2_supported() {
            eprintln!("SKIP: AVX2 unavailable on this runner");
            return;
        }
        const COLS: usize = 128;
        for seed in [0u8, 1, 7, 128, 200, 255] {
            let data = make_q8_0_row_bytes(seed, COLS);
            let input = make_q8_0_input(seed.wrapping_mul(3), COLS);
            let mut scalar_out = [0.0f32];
            let mut simd_out = [0.0f32];
            // Scalar baseline uses the pre-existing dispatch (before the
            // AVX2 branch it was the only implementation), so we call the
            // dispatched entry twice with matching inputs and compare the
            // f32 outputs within a relative tolerance — the SIMD FMA and
            // scalar sums evaluate in different orders and may drift by
            // one or two ulps of accumulated round-off.
            let row_data = &data[..];
            // Force scalar arm by directly running the fallback body.
            let mut acc = 0.0f32;
            for bi in 0..(COLS / QK8_0) {
                let off = bi * 34;
                let d = f16_to_f32(u16::from_le_bytes([row_data[off], row_data[off + 1]]));
                let col_base = bi * QK8_0;
                for l in 0..QK8_0 {
                    let w = d * f32::from(row_data[off + 2 + l] as i8);
                    acc += w * input[col_base + l];
                }
            }
            scalar_out[0] = acc;
            // SIMD via dispatched entry point (runtime feature detection
            // will pick AVX2 on this test's precondition).
            q8_0_matvec(&input, row_data, 1, COLS, &mut simd_out);
            let diff = (scalar_out[0] - simd_out[0]).abs();
            let rel = diff / scalar_out[0].abs().max(1.0);
            assert!(
                rel < 1e-4,
                "seed={seed}: scalar={} simd={} rel={rel}",
                scalar_out[0],
                simd_out[0]
            );
        }
    }

    // ── Ternary bitmask SIMD parity (Issue #24) ────────────────────────

    /// Build a deterministic TernaryRow for a given seed and column count.
    /// Alternates between +1 / -1 / 0 driven by the seed so the SIMD path
    /// exercises both `pos_mask` and `neg_mask` in every 8-lane group.
    #[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
    fn make_ternary_row(seed: u8, num_cols: usize, scale: f32) -> TernaryRow {
        let num_bytes = num_cols.div_ceil(8);
        let mut pos_mask = vec![0u8; num_bytes];
        let mut neg_mask = vec![0u8; num_bytes];
        for i in 0..num_cols {
            let raw = seed.wrapping_mul(3).wrapping_add(i as u8) as i8;
            let byte_idx = i / 8;
            let bit = 1u8 << (i % 8);
            if raw > 20 {
                pos_mask[byte_idx] |= bit;
            } else if raw < -20 {
                neg_mask[byte_idx] |= bit;
            }
        }
        TernaryRow {
            pos_mask,
            neg_mask,
            scale,
            num_cols,
        }
    }

    #[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
    fn make_ternary_input(seed: u8, num_cols: usize) -> Vec<f32> {
        (0..num_cols)
            .map(|i| f32::from(seed.wrapping_add(i as u8) as i8) * 0.01)
            .collect()
    }

    #[cfg(target_arch = "x86_64")]
    #[test]
    fn ternary_avx2_matches_scalar_relative_tolerance() {
        if !x86_avx2_supported() {
            eprintln!("SKIP: AVX2 unavailable on this runner");
            return;
        }
        // 128 covers 16 mask bytes = plenty of both empty and populated
        // 8-lane groups; the divisible-by-8 count also matches typical
        // hidden-dim shapes so the fast path (no tail) is dominant.
        const COLS: usize = 128;
        for seed in [0u8, 1, 7, 128, 200, 255] {
            let row = make_ternary_row(seed, COLS, 0.125);
            let input = make_ternary_input(seed.wrapping_mul(7), COLS);
            let scalar = ternary_dot_row_scalar(&row, &input);
            // Force AVX2 arm via the runtime dispatch (test precondition
            // covers AVX-512 skip via feature detection above).
            let simd = ternary_dot_row(&row, &input);
            let diff = (scalar - simd).abs();
            let rel = diff / scalar.abs().max(1e-6);
            assert!(
                rel < 1e-4,
                "seed={seed}: scalar={scalar} simd={simd} rel={rel}"
            );
        }
    }

    /// Tail path (non-multiple-of-8 column count) must not be silently
    /// dropped by the SIMD full-byte loop.
    #[cfg(target_arch = "x86_64")]
    #[test]
    fn ternary_avx2_handles_non_multiple_of_8_cols() {
        if !x86_avx2_supported() {
            return;
        }
        // 100 = 12 full bytes + 4 tail bits → forces the scalar tail loop.
        const COLS: usize = 100;
        for seed in [1u8, 42, 200] {
            let row = make_ternary_row(seed, COLS, 0.5);
            let input = make_ternary_input(seed.wrapping_mul(11), COLS);
            let scalar = ternary_dot_row_scalar(&row, &input);
            let simd = ternary_dot_row(&row, &input);
            let rel = (scalar - simd).abs() / scalar.abs().max(1e-6);
            assert!(
                rel < 1e-4,
                "seed={seed}: scalar={scalar} simd={simd} rel={rel}"
            );
        }
    }

    #[cfg(target_arch = "x86_64")]
    #[test]
    fn ternary_avx512_matches_scalar_relative_tolerance() {
        if !x86_avx512bw_supported() {
            eprintln!("SKIP: AVX-512BW unavailable on this runner");
            return;
        }
        // 16-lane grouping: 128 cols = 8 full 16-lane groups.
        const COLS: usize = 128;
        for seed in [0u8, 1, 7, 128, 200, 255] {
            let row = make_ternary_row(seed, COLS, 0.125);
            let input = make_ternary_input(seed.wrapping_mul(7), COLS);
            let scalar = ternary_dot_row_scalar(&row, &input);
            let simd = ternary_dot_row(&row, &input);
            let rel = (scalar - simd).abs() / scalar.abs().max(1e-6);
            assert!(
                rel < 1e-4,
                "seed={seed}: scalar={scalar} simd={simd} rel={rel}"
            );
        }
    }

    // ── aarch64 NEON parity tests (gates the dispatch enable) ─────────

    /// Build a deterministic Q4_K block for parity testing. Same shape as
    /// the x86 tests so the aarch64 CI runs the same coverage matrix.
    #[cfg(target_arch = "aarch64")]
    fn make_q4k_block_aarch64(seed: u8) -> Vec<u8> {
        let mut block = vec![0u8; 144];
        block[0..2].copy_from_slice(&0x3800u16.to_le_bytes());
        block[2..4].copy_from_slice(&0x3400u16.to_le_bytes());
        for i in 0..12 {
            block[4 + i] = seed.wrapping_add(i as u8);
        }
        for i in 0..128 {
            block[16 + i] = seed.wrapping_mul(3).wrapping_add(i as u8);
        }
        block
    }

    #[cfg(target_arch = "aarch64")]
    fn make_q6k_block_aarch64(seed: u8) -> Vec<u8> {
        let mut block = vec![0u8; 210];
        for i in 0..128 {
            block[i] = seed.wrapping_add(i as u8);
        }
        for i in 0..64 {
            block[128 + i] = seed.wrapping_mul(5).wrapping_add(i as u8);
        }
        for i in 0..16 {
            block[192 + i] = (i as u8).wrapping_sub(4);
        }
        block[208..210].copy_from_slice(&0x3800u16.to_le_bytes());
        block
    }

    #[cfg(target_arch = "aarch64")]
    fn make_q8k_block_aarch64(seed: u8) -> BlockQ8K {
        let mut block = BlockQ8K {
            d: 0.125,
            qs: [0i8; QK_K],
            bsums: [0i16; 16],
        };
        for i in 0..QK_K {
            let raw = seed.wrapping_add(i as u8);
            block.qs[i] = (raw as i8).wrapping_sub(64);
        }
        for j in 0..16 {
            let mut acc = 0i32;
            for l in 0..16 {
                acc += block.qs[j * 16 + l] as i32;
            }
            block.bsums[j] = acc as i16;
        }
        block
    }

    /// Verify the dormant NEON Q4_K kernel matches the scalar reference
    /// before the dispatcher is allowed to route to it. NEON was compiled
    /// but unused since PR #21; this test is the gate that decides whether
    /// enabling the dispatch is safe.
    #[cfg(target_arch = "aarch64")]
    #[test]
    fn q4k_neon_matches_scalar_bit_exact() {
        for seed in [0u8, 1, 7, 128, 200, 255] {
            let q4k = make_q4k_block_aarch64(seed);
            let q8k = make_q8k_block_aarch64(seed.wrapping_mul(13));
            let scalar = q4k_q8k_dot_fallback_scalar(&q4k, &q8k);
            // SAFETY: NEON is baseline on all aarch64 targets Rust supports,
            // so the `#[target_feature(enable = "neon")]` annotation matches
            // the compile target unconditionally.
            let neon = unsafe { neon_dot::q4k_q8k_dot(&q4k, &q8k) };
            assert_eq!(
                scalar.to_bits(),
                neon.to_bits(),
                "seed={seed}: scalar={scalar} neon={neon}"
            );
        }
    }

    #[cfg(target_arch = "aarch64")]
    #[test]
    fn q6k_neon_matches_scalar_bit_exact() {
        for seed in [0u8, 1, 7, 128, 200, 255] {
            let q6k = make_q6k_block_aarch64(seed);
            let q8k = make_q8k_block_aarch64(seed.wrapping_mul(11));
            let scalar = q6k_q8k_dot_fallback_scalar(&q6k, &q8k);
            // SAFETY: same rationale as `q4k_neon_matches_scalar_bit_exact`.
            let neon = unsafe { neon_dot::q6k_q8k_dot(&q6k, &q8k) };
            assert_eq!(
                scalar.to_bits(),
                neon.to_bits(),
                "seed={seed}: scalar={scalar} neon={neon}"
            );
        }
    }

    /// Ternary NEON is verified within a relative tolerance (1e-5) rather
    /// than bit-exact because the parallel adds re-order summation vs the
    /// scalar sequential path — same rationale as the AVX2 kernel.
    #[cfg(target_arch = "aarch64")]
    #[test]
    fn ternary_neon_matches_scalar_relative_tolerance() {
        const COLS: usize = 128;
        for seed in [0u8, 1, 7, 128, 200, 255] {
            let row = make_ternary_row(seed, COLS, 0.125);
            let input = make_ternary_input(seed.wrapping_mul(7), COLS);
            let scalar = ternary_dot_row_scalar(&row, &input);
            let simd = ternary_dot_row(&row, &input);
            let rel = (scalar - simd).abs() / scalar.abs().max(1e-6);
            assert!(
                rel < 1e-4,
                "seed={seed}: scalar={scalar} simd={simd} rel={rel}"
            );
        }
    }

    /// Tail path (non-multiple-of-8 column count) must not be silently
    /// dropped by the SIMD full-byte loop.
    #[cfg(target_arch = "aarch64")]
    #[test]
    fn ternary_neon_handles_non_multiple_of_8_cols() {
        const COLS: usize = 100;
        for seed in [1u8, 42, 200] {
            let row = make_ternary_row(seed, COLS, 0.5);
            let input = make_ternary_input(seed.wrapping_mul(11), COLS);
            let scalar = ternary_dot_row_scalar(&row, &input);
            let simd = ternary_dot_row(&row, &input);
            let rel = (scalar - simd).abs() / scalar.abs().max(1e-6);
            assert!(
                rel < 1e-4,
                "seed={seed}: scalar={scalar} simd={simd} rel={rel}"
            );
        }
    }

    /// Q8_0 NEON is verified against scalar within a tight relative tolerance
    /// rather than bit-exact because the paths use different accumulation:
    /// scalar does `w * input + acc` (two roundings) whereas NEON uses
    /// `vfmaq_f32(acc, f, x)` (single fused rounding). The tolerance below is
    /// chosen empirically: for a 4096-column matvec, cumulative float error
    /// stays under 1e-4 relative on `f32::EPSILON * cols`-order inputs.
    #[cfg(target_arch = "aarch64")]
    #[test]
    fn q8_0_neon_matches_scalar_within_tol() {
        const COLS: usize = 4096;
        const BLOCK_BYTES: usize = 34;
        const BLOCKS: usize = COLS / QK8_0;
        for seed in [0u8, 1, 7, 128, 200, 255] {
            let mut row_data = vec![0u8; BLOCKS * BLOCK_BYTES];
            for bi in 0..BLOCKS {
                let off = bi * BLOCK_BYTES;
                // f16 scale from a safe range (no denormals, no Inf/NaN).
                // 0x3400 = 0.25, 0x3800 = 0.5, 0x3c00 = 1.0. Vary within
                // 0x3400..0x3c00 so scales stay in [0.25, 1.0).
                let d_bits = 0x3400u16.wrapping_add((seed as u16 ^ bi as u16) & 0x07FF);
                row_data[off..off + 2].copy_from_slice(&d_bits.to_le_bytes());
                for l in 0..QK8_0 {
                    row_data[off + 2 + l] = seed
                        .wrapping_add((bi as u8).wrapping_mul(3))
                        .wrapping_add(l as u8);
                }
            }
            let input: Vec<f32> = (0..COLS)
                .map(|i| (i as f32 * 0.017 + seed as f32).sin() * 0.5)
                .collect();

            // Scalar reference.
            let mut scalar_out = [0.0f32];
            {
                let mut acc = 0.0f32;
                for bi in 0..BLOCKS {
                    let off = bi * BLOCK_BYTES;
                    let d = f16_to_f32(u16::from_le_bytes([row_data[off], row_data[off + 1]]));
                    let col_base = bi * QK8_0;
                    for l in 0..QK8_0 {
                        let w = d * f32::from(row_data[off + 2 + l] as i8);
                        acc += w * input[col_base + l];
                    }
                }
                scalar_out[0] = acc;
            }
            // SAFETY: NEON is baseline on all aarch64 targets.
            let neon = unsafe { neon_dot::q8_0_matvec_row(&input, &row_data, COLS) };

            let diff = (scalar_out[0] - neon).abs();
            let rel = diff / scalar_out[0].abs().max(f32::EPSILON);
            assert!(
                rel < 1e-4,
                "seed={seed}: scalar={} neon={neon} diff={diff} rel={rel}",
                scalar_out[0]
            );
        }
    }
}