whisper-apr 0.3.1

WASM-first automatic speech recognition engine implementing OpenAI Whisper
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
#![allow(clippy::all, clippy::pedantic, clippy::restriction, clippy::nursery)]
//! Multi-head attention implementation
//!
//! Implements scaled dot-product attention and multi-head attention
//! as used in the Whisper transformer architecture.
//!
//! # Algorithm
//!
//! Attention(Q, K, V) = softmax(QK^T / sqrt(d_k)) V
//!
//! Multi-head attention splits Q, K, V into multiple heads,
//! computes attention in parallel, and concatenates results.
//!
//! # Parallelization (§11.3.2)
//!
//! Each attention head is independent [31], enabling embarrassingly parallel
//! computation. With `parallel` feature enabled, heads are computed via rayon.
//!
//! # FlashAttention-2 Integration (WAPR-PERF-004 Phase 2)
//!
//! When `realizar-inference` feature is enabled, uses realizar's optimized
//! FlashAttention-2 implementation (Tri Dao, 2023) for O(N) memory attention.
//!
//! # References
//!
//! - [31] Vaswani et al. (2017): "Attention Is All You Need"
//! - Radford et al. (2023): "Robust Speech Recognition via Large-Scale Weak Supervision"
//! - [FlashAttn] Dao et al. (2022): "FlashAttention: Fast and Memory-Efficient Attention"
//! - [FlashAttn2] Dao (2023): "FlashAttention-2: Faster Attention with Better Parallelism"

use crate::error::{WhisperError, WhisperResult};
use crate::parallel::{parallel_map, parallel_try_map};
use crate::simd;
use trueno::Matrix;

// Import realizar's FlashAttention when feature is enabled
#[cfg(feature = "realizar-inference")]
use realizar::layers::Attention as RealizarAttention;
#[cfg(feature = "realizar-inference")]
use realizar::tensor::Tensor as RealizarTensor;

/// Weight storage format for linear layers
///
/// Supports both f32 and fp16 storage. fp16 halves DRAM bandwidth
/// for memory-bandwidth-bound single-token decoder inference.
#[derive(Debug, Clone)]
pub enum WeightStorage {
    /// Full precision f32 (4 bytes/weight)
    F32,
    /// Half precision fp16 stored as u16 bit patterns (2 bytes/weight)
    F16(Vec<u16>),
}

/// Linear projection weights for attention
///
/// Note: Clone is derived but will clone the cached Matrix (expensive).
/// Prefer to finalize_weights() after cloning if needed.
pub struct LinearWeights {
    /// Weight matrix (out_features x in_features) row-major
    pub weight: Vec<f32>,
    /// Bias vector (out_features)
    pub bias: Vec<f32>,
    /// Input features
    pub in_features: usize,
    /// Output features
    pub out_features: usize,
    /// Cached transposed weight matrix (in_features x out_features) for SIMD matmul
    /// Pre-computed by finalize_weights() to avoid runtime transpose
    weight_transposed: Option<Vec<f32>>,
    /// Cached trueno Matrix for zero-copy matmul (WAPR-BENCH-001 optimization)
    /// This avoids repeated Matrix::from_vec() calls in the hot path
    weight_matrix: Option<Matrix<f32>>,
    /// fp16 weight storage: when set, forward_simd uses fp16 path for single-token inference.
    /// Halves DRAM reads (2 bytes/weight vs 4) — the dominant bottleneck in decoder inference.
    weight_f16: Option<Vec<u16>>,
    /// INT8 weight storage (pv:int8-symmetric-quant-v1): per-row symmetric quantization.
    /// Quarters DRAM reads vs f32 (1 byte/weight vs 4). Uses per-row f32 scales.
    weight_i8: Option<Vec<i8>>,
    /// Per-row scales for INT8 dequantization: w_f32 ≈ w_i8 * scale.
    weight_i8_scales: Option<Vec<f32>>,
    /// INT4 weight storage (GPTQ/AWQ).
    /// Packs 2 weights per byte.
    weight_i4: Option<Vec<u8>>,
    /// Per-group scales for INT4 (group_size=128).
    weight_i4_scales: Option<Vec<f32>>,
    /// Pre-packed B matrix for BLIS GEMM (WAPR-KAIZEN Cycle 12).
    /// Eliminates redundant B packing in parallel GEMM by pre-packing weight
    /// tiles once at finalize_weights() time.
    weight_prepacked_b: Option<trueno::blis::PrepackedB>,
}

impl Clone for LinearWeights {
    fn clone(&self) -> Self {
        Self {
            weight: self.weight.clone(),
            bias: self.bias.clone(),
            in_features: self.in_features,
            out_features: self.out_features,
            weight_transposed: self.weight_transposed.clone(),
            // Don't clone the Matrix/PrepackedB caches - rebuilt on finalize_weights()
            weight_matrix: None,
            weight_f16: self.weight_f16.clone(),
            weight_i8: self.weight_i8.clone(),
            weight_i8_scales: self.weight_i8_scales.clone(),
            weight_i4: self.weight_i4.clone(),
            weight_i4_scales: self.weight_i4_scales.clone(),
            weight_prepacked_b: None,
        }
    }
}

impl std::fmt::Debug for LinearWeights {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("LinearWeights")
            .field("in_features", &self.in_features)
            .field("out_features", &self.out_features)
            .field("weight_len", &self.weight.len())
            .field("bias_len", &self.bias.len())
            .field("is_finalized", &self.weight_transposed.is_some())
            .field("has_matrix_cache", &self.weight_matrix.is_some())
            .field("has_f16", &self.weight_f16.is_some())
            .field("has_i8", &self.weight_i8.is_some())
            .finish()
    }
}

impl LinearWeights {
    /// Create new linear weights
    #[must_use]
    pub fn new(in_features: usize, out_features: usize) -> Self {
        Self {
            weight: vec![0.0; out_features * in_features],
            bias: vec![0.0; out_features],
            in_features,
            out_features,
            weight_transposed: None,
            weight_matrix: None,
            weight_f16: None,
            weight_i8: None,
            weight_i8_scales: None,
            weight_i4: None,
            weight_i4_scales: None,
            weight_prepacked_b: None,
        }
    }

    /// Pre-compute and cache transposed weight matrix for SIMD matmul
    ///
    /// Call this after loading weights to avoid runtime transpose overhead.
    /// The transposed matrix is used by `forward_simd()` for efficient matmul.
    ///
    /// Also caches a trueno Matrix for zero-copy matmul operations.
    /// Skips caching if weights are stored as fp16 (fp16 path uses tiled_matvec_f16 directly).
    pub fn finalize_weights(&mut self) {
        // fp16 weights use tiled_matvec_f16 directly — no transpose/Matrix cache needed.
        // Caching f32 dequanted weights would add ~75MB for decoder, causing L3 cache pressure
        // that hurts fp16 matvec performance for the 23 subsequent single-token decodes.
        if self.weight_f16.is_some() {
            return;
        }

        let weight_t = simd::transpose(&self.weight, self.out_features, self.in_features);

        // WAPR-KAIZEN Cycle 12: Pre-pack B matrix for BLIS GEMM.
        // weight_t is (in_features × out_features), which is the B matrix for
        // input [batch × in_features] @ weight_t [in_features × out_features].
        // Pre-packing eliminates redundant B packing in parallel GEMM.
        //
        // Only prepack large weight matrices (>256K elements). Small attention
        // weights (384×384 = 147K) pack quickly and prepacking adds cache pressure
        // without measurable benefit. FFN weights (384×1536 = 589K) benefit.
        const PREPACK_THRESHOLD: usize = 0;
        if self.in_features * self.out_features >= PREPACK_THRESHOLD {
            self.weight_prepacked_b = Some(trueno::blis::PrepackedB::pack(
                &weight_t,
                self.in_features,
                self.out_features,
            ));
        }

        // Create trueno Matrix from transposed weights (WAPR-BENCH-001 optimization)
        // This avoids repeated Matrix::from_vec() calls in forward_simd()
        self.weight_matrix = Some(
            Matrix::from_vec(self.in_features, self.out_features, weight_t.clone())
                .expect("failed to create matrix from weights"),
        );

        self.weight_transposed = Some(weight_t);
    }

    /// Same as finalize_weights but forces caching of f32 weights for encoder.
    /// The encoder runs large batch matrix multiplications which are compute bound,
    /// so caching the f32 transposed weights avoids repeated dequant+transpose overhead.
    pub fn finalize_weights_encoder(&mut self) {
        if self.weight_f16.is_some() {
            return; // fp16 weights use tiled_matmul_f16_into directly
        }

        let weight_t = crate::simd::transpose(&self.weight, self.out_features, self.in_features);

        // Disabled prepacking for now because it seems to be killing parallel scaling
        // Using tiled_matmul_into is faster for the encoder's large batch sequence lengths
        self.weight_prepacked_b = None;

        self.weight_matrix = Some(
            trueno::Matrix::from_vec(self.in_features, self.out_features, weight_t.clone())
                .expect("failed to create matrix from weights"),
        );

        self.weight_transposed = Some(weight_t);
    }

    /// Check if weights have been finalized
    #[must_use]
    pub fn is_finalized(&self) -> bool {
        self.weight_transposed.is_some()
    }

    /// Get fp16 weight storage, if present.
    #[must_use]
    pub fn weight_f16(&self) -> Option<&[u16]> {
        self.weight_f16.as_deref()
    }

    /// Clear cached transposed weights (useful after modifying weights)
    pub fn invalidate_cache(&mut self) {
        self.weight_transposed = None;
        self.weight_matrix = None;
        self.weight_prepacked_b = None;
    }

    /// Set weight values from a slice
    ///
    /// Note: This invalidates any cached transposed weights.
    /// Call `finalize_weights()` after setting all weights.
    pub fn set_weight(&mut self, values: &[f32]) {
        let len = values.len().min(self.weight.len());
        self.weight[..len].copy_from_slice(&values[..len]);
        self.invalidate_cache();
    }

    /// Set bias values from a slice
    pub fn set_bias(&mut self, values: &[f32]) {
        let len = values.len().min(self.bias.len());
        self.bias[..len].copy_from_slice(&values[..len]);
    }

    /// Set weight values from fp16 (u16 bit pattern) data.
    ///
    /// The f32 weight field is cleared to free memory. All forward operations
    /// will use the fp16 path (dequant-per-row + SIMD dot).
    pub fn set_weight_f16(&mut self, values: &[u16]) {
        self.weight_f16 = Some(values.to_vec());
        // Free f32 weight memory — fp16 path doesn't need it
        self.weight = Vec::new();
        self.invalidate_cache();
    }

    /// Convert f32 weights to fp16 in-place, freeing the f32 memory.
    ///
    /// This halves weight memory (2 bytes/element vs 4) and enables the
    /// fp16 forward path which halves DRAM bandwidth during inference.
    pub fn convert_to_f16(&mut self) {
        if self.weight_f16.is_some() || self.weight.is_empty() {
            return;
        }
        self.weight_f16 = Some(simd::quant_f32_to_f16(&self.weight));
        // Free f32 weight memory
        self.weight = Vec::new();
        self.invalidate_cache();
    }

    /// Convert fp16 weights to INT8 symmetric per-row quantization (pv:int8-symmetric-quant-v1).
    ///
    /// Halves memory vs fp16 (1 byte/weight vs 2). Per-row scale factors preserve accuracy.
    /// Falls back to quantizing from f32 if fp16 weights are not available.
    pub fn convert_to_i8(&mut self) {
        if self.weight_i8.is_some() {
            return;
        }

        // Source: prefer fp16 (most common in decoder), fallback to f32
        let rows = self.out_features;
        let cols = self.in_features;

        if let Some(ref w_f16) = self.weight_f16 {
            let mut all_i8 = Vec::with_capacity(rows * cols);
            let mut scales = Vec::with_capacity(rows);
            let mut row_buf = vec![0.0_f32; cols];
            for r in 0..rows {
                let offset = r * cols;
                for (j, v) in row_buf.iter_mut().enumerate() {
                    *v = half::f16::from_bits(w_f16[offset + j]).to_f32();
                }
                let (q_row, scale) = simd::quant_f32_row_to_i8(&row_buf);
                all_i8.extend_from_slice(&q_row);
                scales.push(scale);
            }
            self.weight_i8 = Some(all_i8);
            self.weight_i8_scales = Some(scales);
        } else if !self.weight.is_empty() {
            let mut all_i8 = Vec::with_capacity(rows * cols);
            let mut scales = Vec::with_capacity(rows);
            for r in 0..rows {
                let offset = r * cols;
                let (q_row, scale) = simd::quant_f32_row_to_i8(&self.weight[offset..offset + cols]);
                all_i8.extend_from_slice(&q_row);
                scales.push(scale);
            }
            self.weight_i8 = Some(all_i8);
            self.weight_i8_scales = Some(scales);
        }
    }

    /// Convert weights to INT4 (GPTQ/AWQ) with group_size=128.
    pub fn convert_to_i4(&mut self) {
        if self.weight_i4.is_some() {
            return;
        }

        let rows = self.out_features;
        let cols = self.in_features;
        let group_size = 128;

        if let Some(ref w_f16) = self.weight_f16 {
            let mut all_i4 = Vec::with_capacity(rows * cols / 2);
            let mut all_scales = Vec::with_capacity(rows * (cols / group_size));
            let mut row_buf = vec![0.0_f32; cols];
            for r in 0..rows {
                let offset = r * cols;
                for (j, v) in row_buf.iter_mut().enumerate() {
                    *v = half::f16::from_bits(w_f16[offset + j]).to_f32();
                }
                let (q_row, scales) = simd::quant_f32_row_to_i4(&row_buf, group_size);
                all_i4.extend_from_slice(&q_row);
                all_scales.extend_from_slice(&scales);
            }
            self.weight_i4 = Some(all_i4);
            self.weight_i4_scales = Some(all_scales);
        } else if !self.weight.is_empty() {
            let mut all_i4 = Vec::with_capacity(rows * cols / 2);
            let mut all_scales = Vec::with_capacity(rows * (cols / group_size));
            for r in 0..rows {
                let offset = r * cols;
                let (q_row, scales) =
                    simd::quant_f32_row_to_i4(&self.weight[offset..offset + cols], group_size);
                all_i4.extend_from_slice(&q_row);
                all_scales.extend_from_slice(&scales);
            }
            self.weight_i4 = Some(all_i4);
            self.weight_i4_scales = Some(all_scales);
        }
    }

    /// Check if weights are stored as fp16
    #[must_use]
    pub fn is_f16(&self) -> bool {
        self.weight_f16.is_some()
    }

    /// Check if weights are stored as INT8
    #[must_use]
    pub fn is_i8(&self) -> bool {
        self.weight_i8.is_some()
    }

    /// Get weight storage type
    #[must_use]
    pub fn storage_type(&self) -> WeightStorage {
        if let Some(ref f16) = self.weight_f16 {
            WeightStorage::F16(f16.clone())
        } else {
            WeightStorage::F32
        }
    }

    /// Apply linear projection: y = xW^T + b
    ///
    /// # Arguments
    /// * `input` - Input tensor (batch_size x seq_len x in_features) flattened
    /// * `seq_len` - Sequence length
    ///
    /// # Returns
    /// Output tensor (batch_size x seq_len x out_features) flattened
    pub fn forward(&self, input: &[f32], seq_len: usize) -> WhisperResult<Vec<f32>> {
        if input.len() % (seq_len * self.in_features) != 0 {
            return Err(WhisperError::Model("input size mismatch".into()));
        }

        let batch_size = input.len() / (seq_len * self.in_features);
        debug_assert!(batch_size > 0, "batch_size must be positive");
        let mut output = vec![0.0_f32; batch_size * seq_len * self.out_features];

        for b in 0..batch_size {
            for s in 0..seq_len {
                for o in 0..self.out_features {
                    let mut sum = self.bias[o];
                    for i in 0..self.in_features {
                        let input_idx = b * seq_len * self.in_features + s * self.in_features + i;
                        let weight_idx = o * self.in_features + i;
                        sum += input[input_idx] * self.weight[weight_idx];
                    }
                    let output_idx = b * seq_len * self.out_features + s * self.out_features + o;
                    output[output_idx] = sum;
                }
            }
        }

        debug_assert_eq!(
            output.len(),
            batch_size * seq_len * self.out_features,
            "output dimensions must match batch × seq × out_features"
        );

        Ok(output)
    }

    /// SIMD-accelerated linear projection: y = xW^T + b
    ///
    /// Uses the simd module for optimized matrix operations.
    /// If `finalize_weights()` was called, uses cached trueno Matrix for
    /// zero-copy matmul (WAPR-BENCH-001 optimization).
    ///
    /// For single-token inference (autoregressive decoding), uses tiled_matvec
    /// for cache-efficient matrix-vector multiplication (WAPR-PERF-004).
    ///
    /// When fp16 weights are available, uses `tiled_matvec_f16` which halves
    /// DRAM bandwidth by dequantizing one row at a time into L1 cache.
    ///
    /// # Arguments
    /// * `input` - Input tensor (batch_size x seq_len x in_features) flattened
    /// * `seq_len` - Sequence length
    ///
    /// # Returns
    /// Output tensor (batch_size x seq_len x out_features) flattened
    pub fn forward_simd(&self, input: &[f32], seq_len: usize) -> WhisperResult<Vec<f32>> {
        if input.len() % (seq_len * self.in_features) != 0 {
            return Err(WhisperError::Model("input size mismatch".into()));
        }

        let batch_size = input.len() / (seq_len * self.in_features);
        let total_tokens = batch_size * seq_len;

        // fp16 weight path
        if let Some(ref w_f16) = self.weight_f16 {
            // Threshold: matvec is memory-bound (fp16 wins 2-5x),
            // matmul is compute-bound (fp16 dequant overhead, no win).
            const FP16_MATVEC_BATCH_LIMIT: usize = 8;
            let mut output = if total_tokens <= FP16_MATVEC_BATCH_LIMIT {
                // Small batch: fused F16C matvec per token (memory-bound, 2-5x win)
                let mut out = vec![0.0_f32; total_tokens * self.out_features];
                for t in 0..total_tokens {
                    let tok_in = &input[t * self.in_features..(t + 1) * self.in_features];
                    let tok_out = &mut out[t * self.out_features..(t + 1) * self.out_features];
                    simd::tiled_matvec_f16_into(
                        w_f16,
                        tok_in,
                        tok_out,
                        self.out_features,
                        self.in_features,
                    );
                }
                out
            } else if let Some(ref prepacked) = self.weight_prepacked_b {
                simd::matmul_with_prepacked(
                    input,
                    prepacked,
                    total_tokens,
                    self.in_features,
                    self.out_features,
                )
            } else if let Some(weight_matrix) = &self.weight_matrix {
                simd::matmul_with_matrix(input, weight_matrix, total_tokens, self.in_features)
            } else if let Some(weight_t) = &self.weight_transposed {
                simd::matmul(
                    input,
                    weight_t,
                    total_tokens,
                    self.in_features,
                    self.out_features,
                )
            } else {
                // Large batch (encoder): dequant once + matmul (compute-bound)
                let mut buf = vec![0.0_f32; w_f16.len()];
                simd::dequant_f16_row(w_f16, &mut buf);
                let weight_t = simd::transpose(&buf, self.out_features, self.in_features);
                simd::matmul(
                    input,
                    &weight_t,
                    total_tokens,
                    self.in_features,
                    self.out_features,
                )
            };
            simd::broadcast_add_inplace(&mut output, &self.bias, total_tokens, self.out_features);
            return Ok(output);
        }

        // WAPR-PERF-004: Use tiled_matvec for single-token inference (autoregressive decoding)
        // This is ~2x faster than general matmul for the common case of decoding one token at a time
        let mut output = if total_tokens == 1 {
            // Single token: use cache-efficient tiled matrix-vector multiplication
            // weight is (out_features x in_features), input is (in_features,)
            // output = weight @ input = (out_features,)
            simd::tiled_matvec(&self.weight, input, self.out_features, self.in_features)
        } else if let Some(ref prepacked) = self.weight_prepacked_b {
            // WAPR-KAIZEN Cycle 12: Pre-packed B eliminates redundant packing in parallel GEMM
            simd::matmul_with_prepacked(
                input,
                prepacked,
                total_tokens,
                self.in_features,
                self.out_features,
            )
        } else if let Some(prepacked_b) = &self.weight_prepacked_b {
            // Use pre-packed B for SIMD GEMM
            simd::matmul_with_prepacked(
                input,
                prepacked_b,
                total_tokens,
                self.in_features,
                self.out_features,
            )
        } else {
            // General batched case for F32
            // Weight is [out_features, in_features] in self.weight
            let mut output = vec![0.0_f32; total_tokens * self.out_features];
            crate::simd::optimized::tiled_matmul_into(
                &self.weight,
                input,
                &mut output,
                total_tokens,
                self.out_features,
                self.in_features,
            );
            output
        };

        // Add bias to each token using SIMD broadcast add
        simd::broadcast_add_inplace(&mut output, &self.bias, total_tokens, self.out_features);

        Ok(output)
    }

    /// Forward pass into a pre-allocated output buffer (PMAT-014 O1).
    ///
    /// For single-token decoder inference this avoids per-token allocation.
    /// `output` must be at least `total_tokens * out_features` elements.
    pub fn forward_simd_into(
        &self,
        input: &[f32],
        seq_len: usize,
        output: &mut [f32],
    ) -> WhisperResult<()> {
        if input.len() % (seq_len * self.in_features) != 0 {
            return Err(WhisperError::Model("input size mismatch".into()));
        }

        let batch_size = input.len() / (seq_len * self.in_features);
        let total_tokens = batch_size * seq_len;

        // INT4 weight path (GPTQ/AWQ)
        if let (Some(ref w_i4), Some(ref scales)) = (&self.weight_i4, &self.weight_i4_scales) {
            let group_size = 128;
            for t in 0..total_tokens {
                let tok_in = &input[t * self.in_features..(t + 1) * self.in_features];
                let tok_out = &mut output[t * self.out_features..(t + 1) * self.out_features];
                simd::tiled_matvec_i4_into(
                    w_i4,
                    scales,
                    tok_in,
                    tok_out,
                    self.out_features,
                    self.in_features,
                    group_size,
                );
            }
            simd::broadcast_add_inplace(output, &self.bias, total_tokens, self.out_features);
            return Ok(());
        }

        // INT8 weight path (pv:int8-symmetric-quant-v1) — highest priority, lowest bandwidth
        if let (Some(ref w_i8), Some(ref scales)) = (&self.weight_i8, &self.weight_i8_scales) {
            for t in 0..total_tokens {
                let tok_in = &input[t * self.in_features..(t + 1) * self.in_features];
                let tok_out = &mut output[t * self.out_features..(t + 1) * self.out_features];
                simd::tiled_matvec_i8_into(
                    w_i8,
                    scales,
                    tok_in,
                    tok_out,
                    self.out_features,
                    self.in_features,
                );
            }
            simd::broadcast_add_inplace(output, &self.bias, total_tokens, self.out_features);
            return Ok(());
        }

        // fp16 weight path
        if let Some(ref w_f16) = self.weight_f16 {
            const FP16_MATVEC_BATCH_LIMIT: usize = 8;
            if total_tokens <= FP16_MATVEC_BATCH_LIMIT {
                for t in 0..total_tokens {
                    let tok_in = &input[t * self.in_features..(t + 1) * self.in_features];
                    let tok_out = &mut output[t * self.out_features..(t + 1) * self.out_features];
                    simd::tiled_matvec_f16_into(
                        w_f16,
                        tok_in,
                        tok_out,
                        self.out_features,
                        self.in_features,
                    );
                }
            } else if let Some(ref prepacked) = self.weight_prepacked_b {
                let tmp = simd::matmul_with_prepacked(
                    input,
                    prepacked,
                    total_tokens,
                    self.in_features,
                    self.out_features,
                );
                output[..tmp.len()].copy_from_slice(&tmp);
            } else if let Some(weight_matrix) = &self.weight_matrix {
                let tmp =
                    simd::matmul_with_matrix(input, weight_matrix, total_tokens, self.in_features);
                output[..tmp.len()].copy_from_slice(&tmp);
            } else if let Some(weight_t) = &self.weight_transposed {
                let tmp = simd::matmul(
                    input,
                    weight_t,
                    total_tokens,
                    self.in_features,
                    self.out_features,
                );
                output[..tmp.len()].copy_from_slice(&tmp);
            } else {
                let mut buf = vec![0.0_f32; w_f16.len()];
                simd::dequant_f16_row(w_f16, &mut buf);
                let weight_t = simd::transpose(&buf, self.out_features, self.in_features);
                let tmp = simd::matmul(
                    input,
                    &weight_t,
                    total_tokens,
                    self.in_features,
                    self.out_features,
                );
                output[..tmp.len()].copy_from_slice(&tmp);
            }
            simd::broadcast_add_inplace(output, &self.bias, total_tokens, self.out_features);
            return Ok(());
        }

        // Single-token: tiled_matvec_into (zero-alloc)
        if total_tokens == 1 {
            simd::tiled_matvec_into(
                &self.weight,
                input,
                output,
                self.out_features,
                self.in_features,
            );
        } else if let Some(ref prepacked) = self.weight_prepacked_b {
            // WAPR-KAIZEN Cycle 12: Pre-packed B path
            let tmp = simd::matmul_with_prepacked(
                input,
                prepacked,
                total_tokens,
                self.in_features,
                self.out_features,
            );
            output[..tmp.len()].copy_from_slice(&tmp);
        } else if let Some(prepacked_b) = &self.weight_prepacked_b {
            let tmp = simd::matmul_with_prepacked(
                input,
                prepacked_b,
                total_tokens,
                self.in_features,
                self.out_features,
            );
            output[..tmp.len()].copy_from_slice(&tmp);
        } else {
            // General batched case for F32
            crate::simd::optimized::tiled_matmul_into(
                &self.weight,
                input,
                output,
                total_tokens,
                self.out_features,
                self.in_features,
            );
        }

        simd::broadcast_add_inplace(output, &self.bias, total_tokens, self.out_features);
        Ok(())
    }
}

/// Default block size for Flash Attention (tuned for L1 cache)
/// Larger blocks reduce per-block overhead at the cost of more temporary memory.
/// 128 gives ~4x fewer iterations than 32 for 1500-token encoder sequences.
pub const FLASH_ATTENTION_BLOCK_SIZE: usize = 128;

/// Threshold above which Flash Attention is used for memory efficiency
pub const FLASH_ATTENTION_THRESHOLD: usize = 128;

/// Configuration for Flash Attention
#[derive(Debug, Clone, Copy)]
pub struct FlashAttentionConfig {
    /// Query sequence length
    pub seq_len: usize,
    /// Key/Value sequence length
    pub kv_len: usize,
    /// Per-head dimension
    pub d_head: usize,
    /// Block size for tiling
    pub block_size: usize,
}

impl FlashAttentionConfig {
    /// Create a new Flash Attention configuration
    #[must_use]
    pub const fn new(seq_len: usize, kv_len: usize, d_head: usize, block_size: usize) -> Self {
        Self {
            seq_len,
            kv_len,
            d_head,
            block_size,
        }
    }

    /// Create with default block size
    #[must_use]
    #[allow(dead_code)] // Public API for future use
    pub const fn with_default_block_size(seq_len: usize, kv_len: usize, d_head: usize) -> Self {
        Self::new(seq_len, kv_len, d_head, FLASH_ATTENTION_BLOCK_SIZE)
    }
}

/// Block computation context for Flash Attention
struct BlockContext {
    q_idx: usize,
    kv_block_start: usize,
    kv_block_end: usize,
    scale: f32,
}

/// Compute block attention scores for a single query position
#[inline]
fn compute_block_scores(
    query: &[f32],
    key: &[f32],
    config: &FlashAttentionConfig,
    ctx: &BlockContext,
    mask: Option<&[f32]>,
) -> Vec<f32> {
    let mut scores = Vec::with_capacity(ctx.kv_block_end - ctx.kv_block_start);
    for k_idx in ctx.kv_block_start..ctx.kv_block_end {
        let mut dot = 0.0_f32;
        for d in 0..config.d_head {
            dot += query[ctx.q_idx * config.d_head + d] * key[k_idx * config.d_head + d];
        }
        let mut score = dot * ctx.scale;
        if let Some(m) = mask {
            score += m[ctx.q_idx * config.kv_len + k_idx];
        }
        scores.push(score);
    }
    scores
}

/// Update output with online softmax accumulation
#[inline]
fn update_output_with_block(
    output: &mut [f32],
    row_sum: &mut f32,
    row_max: &mut f32,
    block_scores: &[f32],
    value: &[f32],
    ctx: &BlockContext,
    d_head: usize,
) {
    let block_max = block_scores
        .iter()
        .fold(f32::NEG_INFINITY, |a, &b| a.max(b));

    let prev_max = *row_max;
    let new_max = prev_max.max(block_max);
    let scale_prev = (prev_max - new_max).exp();

    *row_sum *= scale_prev;
    let out_start = ctx.q_idx * d_head;
    for d in 0..d_head {
        output[out_start + d] *= scale_prev;
    }

    for (local_k_idx, &score) in block_scores.iter().enumerate() {
        let k_idx = ctx.kv_block_start + local_k_idx;
        let exp_score = (score - new_max).exp();
        *row_sum += exp_score;
        for d in 0..d_head {
            output[out_start + d] += exp_score * value[k_idx * d_head + d];
        }
    }

    *row_max = new_max;
}

/// Normalize output row by softmax sum
#[inline]
fn normalize_row(output: &mut [f32], row_sum: f32, q_idx: usize, d_head: usize) {
    let inv_sum = if row_sum > 1e-10 { 1.0 / row_sum } else { 0.0 };
    let start = q_idx * d_head;
    for d in 0..d_head {
        output[start + d] *= inv_sum;
    }
}

/// Flash Attention: O(n) memory instead of O(n²)
///
/// Implements the Flash Attention algorithm from Dao et al. (2022).
/// Processes attention in blocks to minimize memory usage while maintaining
/// numerical correctness through online softmax computation.
///
/// # Arguments
/// * `query` - Query tensor (config.seq_len × config.d_head) flattened row-major
/// * `key` - Key tensor (config.kv_len × config.d_head) flattened row-major
/// * `value` - Value tensor (config.kv_len × config.d_head) flattened row-major
/// * `config` - Flash attention configuration (dimensions and block size)
/// * `mask` - Optional attention mask (seq_len × kv_len), -inf for masked positions
///
/// # Returns
/// Attention output (seq_len × d_head) flattened row-major
///
/// # Reference
/// Dao, T., et al. (2022). "FlashAttention: Fast and Memory-Efficient Exact
/// Attention with IO-Awareness." NeurIPS 2022.
#[must_use]
pub fn flash_attention(
    query: &[f32],
    key: &[f32],
    value: &[f32],
    config: FlashAttentionConfig,
    mask: Option<&[f32]>,
) -> Vec<f32> {
    let scale = 1.0 / (config.d_head as f32).sqrt();

    let mut output = vec![0.0_f32; config.seq_len * config.d_head];
    let mut row_max = vec![f32::NEG_INFINITY; config.seq_len];
    let mut row_sum = vec![0.0_f32; config.seq_len];

    // Process key-value blocks
    for kv_block_start in (0..config.kv_len).step_by(config.block_size) {
        let kv_block_end = (kv_block_start + config.block_size).min(config.kv_len);

        // Process all queries for this KV block
        for q_idx in 0..config.seq_len {
            let ctx = BlockContext {
                q_idx,
                kv_block_start,
                kv_block_end,
                scale,
            };

            let block_scores = compute_block_scores(query, key, &config, &ctx, mask);

            update_output_with_block(
                &mut output,
                &mut row_sum[q_idx],
                &mut row_max[q_idx],
                &block_scores,
                value,
                &ctx,
                config.d_head,
            );
        }
    }

    // Final normalization
    for (q_idx, &sum) in row_sum.iter().enumerate() {
        normalize_row(&mut output, sum, q_idx, config.d_head);
    }

    output
}

/// Compute block scores using SIMD dot products
#[inline]
fn compute_block_scores_simd(
    query: &[f32],
    key: &[f32],
    config: &FlashAttentionConfig,
    ctx: &BlockContext,
    mask: Option<&[f32]>,
) -> Vec<f32> {
    let q_offset = ctx.q_idx * config.d_head;
    let mut scores = Vec::with_capacity(ctx.kv_block_end - ctx.kv_block_start);
    for k_idx in ctx.kv_block_start..ctx.kv_block_end {
        let k_offset = k_idx * config.d_head;
        let dot = simd::dot(
            &query[q_offset..q_offset + config.d_head],
            &key[k_offset..k_offset + config.d_head],
        );
        let mut score = dot * ctx.scale;
        if let Some(m) = mask {
            score += m[ctx.q_idx * config.kv_len + k_idx];
        }
        scores.push(score);
    }
    scores
}

/// Update output with SIMD operations
#[inline]
fn update_output_simd(
    output: &mut [f32],
    row_sum: &mut f32,
    row_max: &mut f32,
    block_scores: &[f32],
    value: &[f32],
    ctx: &BlockContext,
    d_head: usize,
) {
    let block_max = simd::max_element(block_scores);
    let prev_max = *row_max;
    let new_max = prev_max.max(block_max);
    let scale_prev = (prev_max - new_max).exp();

    *row_sum *= scale_prev;
    let q_offset = ctx.q_idx * d_head;
    simd::scale_inplace(&mut output[q_offset..q_offset + d_head], scale_prev);

    for (local_k_idx, &score) in block_scores.iter().enumerate() {
        let k_idx = ctx.kv_block_start + local_k_idx;
        let exp_score = (score - new_max).exp();
        *row_sum += exp_score;
        let v_offset = k_idx * d_head;
        simd::axpy(
            exp_score,
            &value[v_offset..v_offset + d_head],
            &mut output[q_offset..q_offset + d_head],
        );
    }
    *row_max = new_max;
}

/// SIMD-accelerated Flash Attention
///
/// Uses SIMD operations for the inner loops when available.
#[must_use]
pub fn flash_attention_simd(
    query: &[f32],
    key: &[f32],
    value: &[f32],
    config: FlashAttentionConfig,
    mask: Option<&[f32]>,
) -> Vec<f32> {
    let scale = 1.0 / (config.d_head as f32).sqrt();

    let mut output = vec![0.0_f32; config.seq_len * config.d_head];
    let mut row_max = vec![f32::NEG_INFINITY; config.seq_len];
    let mut row_sum = vec![0.0_f32; config.seq_len];

    for kv_block_start in (0..config.kv_len).step_by(config.block_size) {
        let kv_block_end = (kv_block_start + config.block_size).min(config.kv_len);

        for q_idx in 0..config.seq_len {
            let ctx = BlockContext {
                q_idx,
                kv_block_start,
                kv_block_end,
                scale,
            };

            let block_scores = compute_block_scores_simd(query, key, &config, &ctx, mask);

            update_output_simd(
                &mut output,
                &mut row_sum[q_idx],
                &mut row_max[q_idx],
                &block_scores,
                value,
                &ctx,
                config.d_head,
            );
        }
    }

    // Final normalization
    for (q_idx, &sum) in row_sum.iter().enumerate() {
        let inv_sum = if sum > 1e-10 { 1.0 / sum } else { 0.0 };
        let start = q_idx * config.d_head;
        let end = (q_idx + 1) * config.d_head;
        simd::scale_inplace(&mut output[start..end], inv_sum);
    }

    output
}

/// Parallel Flash Attention: parallelizes over Q-rows instead of heads.
///
/// Each Q-row is independent (own row_max, row_sum, output slice).
/// For encoder (1500 Q-rows, 16 threads), this gives 94 rows/thread —
/// much better utilization than parallel_map over 6 heads.
///
/// Uses zero-allocation SIMD operations (dot_nalloc, inline max/scale/axpy)
/// to avoid ~28M heap allocations per encoder pass through Vector::from_slice.
#[cfg(feature = "parallel")]
#[must_use]
pub fn flash_attention_simd_parallel(
    query: &[f32],
    key: &[f32],
    value: &[f32],
    config: FlashAttentionConfig,
    mask: Option<&[f32]>,
) -> Vec<f32> {
    use rayon::prelude::*;

    let scale = 1.0 / (config.d_head as f32).sqrt();
    let d_head = config.d_head;
    let kv_len = config.kv_len;
    let block_size = config.block_size;

    let mut output = vec![0.0_f32; config.seq_len * d_head];

    // par_chunks_mut gives each thread a mutable slice of d_head floats
    output
        .par_chunks_mut(d_head)
        .enumerate()
        .for_each(|(q_idx, out_row)| {
            let mut row_max = f32::NEG_INFINITY;
            let mut row_sum = 0.0_f32;
            // Reuse scratch across KV blocks (avoids 12 allocations per Q-row)
            let mut block_scores = Vec::with_capacity(block_size);

            let q_offset = q_idx * d_head;

            for kv_block_start in (0..kv_len).step_by(block_size) {
                let kv_block_end = (kv_block_start + block_size).min(kv_len);

                block_scores.clear();
                for k_idx in kv_block_start..kv_block_end {
                    let k_offset = k_idx * d_head;
                    // Zero-allocation AVX2+FMA dot product (avoids Vector::from_slice alloc)
                    let dot = simd::dot_nalloc(
                        &query[q_offset..q_offset + d_head],
                        &key[k_offset..k_offset + d_head],
                    );
                    let mut score = dot * scale;
                    if let Some(m) = mask {
                        score += m[q_idx * kv_len + k_idx];
                    }
                    block_scores.push(score);
                }

                // Online softmax update (numerically stable)
                // Inline max to avoid Vector::from_slice allocation in simd::max_element
                let block_max = block_scores
                    .iter()
                    .copied()
                    .fold(f32::NEG_INFINITY, f32::max);
                let new_max = row_max.max(block_max);
                let scale_prev = (row_max - new_max).exp();

                row_sum *= scale_prev;
                // scale_inplace and axpy are already zero-allocation (simple loops)
                simd::scale_inplace(out_row, scale_prev);

                for (local_k_idx, &score) in block_scores.iter().enumerate() {
                    let k_idx = kv_block_start + local_k_idx;
                    let exp_score = (score - new_max).exp();
                    row_sum += exp_score;
                    let v_offset = k_idx * d_head;
                    simd::axpy(exp_score, &value[v_offset..v_offset + d_head], out_row);
                }
                row_max = new_max;
            }

            // Normalize
            let inv_sum = if row_sum > 1e-10 { 1.0 / row_sum } else { 0.0 };
            simd::scale_inplace(out_row, inv_sum);
        });

    output
}

/// Multi-head attention module
///
/// Implements the multi-head attention mechanism from the Transformer architecture.
/// Supports both self-attention and cross-attention (encoder-decoder attention).
#[derive(Debug, Clone)]
pub struct MultiHeadAttention {
    /// Number of attention heads
    n_heads: usize,
    /// Hidden state dimension
    d_model: usize,
    /// Per-head dimension (d_model / n_heads)
    d_head: usize,
    /// Query projection weights
    w_q: LinearWeights,
    /// Key projection weights
    w_k: LinearWeights,
    /// Value projection weights
    w_v: LinearWeights,
    /// Output projection weights
    w_o: LinearWeights,
    /// Scale factor for attention scores (1/sqrt(d_head))
    scale: f32,
    /// Fused QKV weights (pv:fused-qkv-projection-v1): [W_q; W_k; W_v] concatenated fp16.
    /// When present, `forward_qkv_into` does a single matvec instead of three.
    w_qkv_f16: Option<Vec<u16>>,
    /// Fused QKV bias: [b_q; b_k; b_v] concatenated.
    b_qkv: Option<Vec<f32>>,
}

impl MultiHeadAttention {
    /// Create a new multi-head attention module
    ///
    /// # Arguments
    /// * `n_heads` - Number of attention heads
    /// * `d_model` - Hidden state dimension (must be divisible by n_heads)
    ///
    /// # Panics
    /// Panics if d_model is not divisible by n_heads
    #[must_use]
    pub fn new(n_heads: usize, d_model: usize) -> Self {
        assert!(
            d_model % n_heads == 0,
            "d_model ({d_model}) must be divisible by n_heads ({n_heads})"
        );

        let d_head = d_model / n_heads;

        Self {
            n_heads,
            d_model,
            d_head,
            w_q: LinearWeights::new(d_model, d_model),
            w_k: LinearWeights::new(d_model, d_model),
            w_v: LinearWeights::new(d_model, d_model),
            w_o: LinearWeights::new(d_model, d_model),
            scale: 1.0 / (d_head as f32).sqrt(),
            w_qkv_f16: None,
            b_qkv: None,
        }
    }

    /// Compute scaled dot-product attention
    ///
    /// Attention(Q, K, V) = softmax(QK^T / sqrt(d_k)) V
    ///
    /// # Arguments
    /// * `query` - Query tensor (seq_len x d_head)
    /// * `key` - Key tensor (kv_len x d_head)
    /// * `value` - Value tensor (kv_len x d_head)
    /// * `mask` - Optional attention mask (seq_len x kv_len), -inf for masked positions
    ///
    /// # Returns
    /// Attention output (seq_len x d_head)
    pub fn scaled_dot_product_attention(
        &self,
        query: &[f32],
        key: &[f32],
        value: &[f32],
        mask: Option<&[f32]>,
    ) -> WhisperResult<Vec<f32>> {
        let seq_len = query.len() / self.d_head;
        let kv_len = key.len() / self.d_head;

        if query.len() % self.d_head != 0 {
            return Err(WhisperError::Model("query size mismatch".into()));
        }
        if key.len() % self.d_head != 0 || value.len() % self.d_head != 0 {
            return Err(WhisperError::Model("key/value size mismatch".into()));
        }
        if key.len() != value.len() {
            return Err(WhisperError::Model(
                "key and value must have same length".into(),
            ));
        }

        // Compute attention scores: QK^T / sqrt(d_k)
        let mut scores = vec![0.0_f32; seq_len * kv_len];

        for q_idx in 0..seq_len {
            for k_idx in 0..kv_len {
                let mut dot = 0.0_f32;
                for d in 0..self.d_head {
                    dot += query[q_idx * self.d_head + d] * key[k_idx * self.d_head + d];
                }
                scores[q_idx * kv_len + k_idx] = dot * self.scale;
            }
        }

        // Apply mask if provided
        if let Some(m) = mask {
            if m.len() != seq_len * kv_len {
                return Err(WhisperError::Model("mask size mismatch".into()));
            }
            for i in 0..scores.len() {
                scores[i] += m[i];
            }
        }

        // Softmax over key dimension
        Self::apply_row_softmax(&mut scores, seq_len, kv_len);

        // Compute output: attention_weights @ V
        let mut output = vec![0.0_f32; seq_len * self.d_head];

        for q_idx in 0..seq_len {
            for d in 0..self.d_head {
                let mut sum = 0.0_f32;
                for k_idx in 0..kv_len {
                    sum += scores[q_idx * kv_len + k_idx] * value[k_idx * self.d_head + d];
                }
                output[q_idx * self.d_head + d] = sum;
            }
        }

        Ok(output)
    }

    /// Apply row-wise softmax in-place over a (seq_len x kv_len) score matrix.
    fn apply_row_softmax(scores: &mut [f32], seq_len: usize, kv_len: usize) {
        for q_idx in 0..seq_len {
            let row_start = q_idx * kv_len;
            let row_end = row_start + kv_len;

            let max_score = scores[row_start..row_end]
                .iter()
                .fold(f32::NEG_INFINITY, |a, &b| a.max(b));

            let mut sum = 0.0_f32;
            for k_idx in 0..kv_len {
                let exp_val = (scores[row_start + k_idx] - max_score).exp();
                scores[row_start + k_idx] = exp_val;
                sum += exp_val;
            }

            let inv_sum = if sum > 1e-10 { 1.0 / sum } else { 0.0 };
            for k_idx in 0..kv_len {
                scores[row_start + k_idx] *= inv_sum;
            }
        }
    }

    /// SIMD-accelerated scaled dot-product attention
    ///
    /// Uses the simd module for optimized operations.
    ///
    /// # Arguments
    /// * `query` - Query tensor (seq_len x d_head)
    /// * `key` - Key tensor (kv_len x d_head)
    /// * `value` - Value tensor (kv_len x d_head)
    /// * `mask` - Optional attention mask (seq_len x kv_len), -inf for masked positions
    ///
    /// # Returns
    /// Attention output (seq_len x d_head)
    pub fn scaled_dot_product_attention_simd(
        &self,
        query: &[f32],
        key: &[f32],
        value: &[f32],
        mask: Option<&[f32]>,
    ) -> WhisperResult<Vec<f32>> {
        let seq_len = query.len() / self.d_head;

        if query.len() % self.d_head != 0 {
            return Err(WhisperError::Model("query size mismatch".into()));
        }
        if key.len() % self.d_head != 0 || value.len() % self.d_head != 0 {
            return Err(WhisperError::Model("key/value size mismatch".into()));
        }
        if key.len() != value.len() {
            return Err(WhisperError::Model(
                "key and value must have same length".into(),
            ));
        }

        // Use SIMD attention implementation
        let output =
            simd::scaled_dot_product_attention(query, key, value, seq_len, self.d_head, mask);

        // Apply scale factor (simd::scaled_dot_product_attention uses sqrt(d_model) internally)
        // So we need to adjust if our d_head differs
        Ok(output)
    }

    /// Create a causal attention mask
    ///
    /// Returns a mask where position i can only attend to positions <= i
    #[must_use]
    pub fn causal_mask(seq_len: usize) -> Vec<f32> {
        let mut mask = vec![0.0_f32; seq_len * seq_len];
        for i in 0..seq_len {
            for j in 0..seq_len {
                if j > i {
                    mask[i * seq_len + j] = f32::NEG_INFINITY;
                }
            }
        }
        mask
    }

    /// Forward pass for self-attention with automatic SIMD dispatch
    ///
    /// Dispatches to SIMD-optimized path when `simd` feature is enabled,
    /// otherwise falls back to scalar implementation.
    ///
    /// # Arguments
    /// * `x` - Input tensor (seq_len x d_model)
    /// * `mask` - Optional attention mask
    ///
    /// # Returns
    /// Output tensor (seq_len x d_model)
    pub fn forward(&self, x: &[f32], mask: Option<&[f32]>) -> WhisperResult<Vec<f32>> {
        self.forward_cross_dispatch(x, x, mask)
    }

    /// Forward pass for cross-attention with automatic SIMD dispatch
    ///
    /// Dispatches to SIMD-optimized path when `simd` feature is enabled,
    /// otherwise falls back to scalar implementation.
    ///
    /// # Arguments
    /// * `x` - Query input tensor (seq_len x d_model)
    /// * `context` - Key/Value input tensor (kv_len x d_model)
    /// * `mask` - Optional attention mask
    ///
    /// # Returns
    /// Output tensor (seq_len x d_model)
    /// Forward pass with optimal dispatch: SIMD + Flash Attention when beneficial
    ///
    /// Dispatch logic (aligned with realizar patterns):
    /// - With `realizar-inference`: Uses FlashAttention-2 for long sequences
    /// - Long sequences (>128 tokens): Flash Attention (O(n) memory)
    /// - Short sequences: Standard attention (lower overhead)
    /// - SIMD: Always used when feature enabled
    #[cfg(feature = "realizar-inference")]
    pub fn forward_cross_dispatch(
        &self,
        x: &[f32],
        context: &[f32],
        mask: Option<&[f32]>,
    ) -> WhisperResult<Vec<f32>> {
        // Use realizar's FlashAttention-2 for optimal performance
        self.forward_cross_optimal(x, context, mask)
    }

    /// Forward pass with optimal dispatch: SIMD + Flash Attention when beneficial
    ///
    /// Dispatch logic (aligned with realizar patterns):
    /// - Long sequences (>128 tokens): Flash Attention (O(n) memory)
    /// - Short sequences: Standard attention (lower overhead)
    /// - SIMD: Always used when feature enabled
    #[cfg(not(feature = "realizar-inference"))]
    pub fn forward_cross_dispatch(
        &self,
        x: &[f32],
        context: &[f32],
        mask: Option<&[f32]>,
    ) -> WhisperResult<Vec<f32>> {
        let seq_len = x.len() / self.d_model;
        let kv_len = context.len() / self.d_model;

        // Use Flash Attention for long sequences (matches realizar threshold)
        if seq_len > FLASH_ATTENTION_THRESHOLD || kv_len > FLASH_ATTENTION_THRESHOLD {
            // Flash Attention already uses SIMD internally
            self.forward_cross_flash(x, context, mask, FLASH_ATTENTION_BLOCK_SIZE)
        } else if cfg!(feature = "simd") {
            self.forward_cross_simd(x, context, mask)
        } else {
            self.forward_cross(x, context, mask)
        }
    }

    /// Forward pass for cross-attention (encoder-decoder attention)
    ///
    /// # Arguments
    /// * `x` - Query input tensor (seq_len x d_model)
    /// * `context` - Key/Value input tensor (kv_len x d_model)
    /// * `mask` - Optional attention mask
    ///
    /// # Returns
    /// Output tensor (seq_len x d_model)
    pub fn forward_cross(
        &self,
        x: &[f32],
        context: &[f32],
        mask: Option<&[f32]>,
    ) -> WhisperResult<Vec<f32>> {
        // Dispatch to SIMD or scalar based on feature flag
        if cfg!(feature = "simd") {
            self.forward_cross_simd(x, context, mask)
        } else {
            self.forward_cross_scalar(x, context, mask)
        }
    }

    /// Scalar forward pass (fallback)
    fn forward_cross_scalar(
        &self,
        x: &[f32],
        context: &[f32],
        mask: Option<&[f32]>,
    ) -> WhisperResult<Vec<f32>> {
        let seq_len = x.len() / self.d_model;
        let kv_len = context.len() / self.d_model;

        if x.len() % self.d_model != 0 {
            return Err(WhisperError::Model("input size mismatch".into()));
        }
        if context.len() % self.d_model != 0 {
            return Err(WhisperError::Model("context size mismatch".into()));
        }

        // Project Q, K, V using scalar matmul
        let q = self.w_q.forward(x, seq_len)?;
        let k = self.w_k.forward(context, kv_len)?;
        let v = self.w_v.forward(context, kv_len)?;

        // Compute attention for each head (parallel when feature enabled)
        // Per §11.3.2: Each head is independent [31], enabling parallel computation
        let head_outputs = parallel_try_map(0..self.n_heads, |head| {
            let q_head = self.extract_head(&q, seq_len, head);
            let k_head = self.extract_head(&k, kv_len, head);
            let v_head = self.extract_head(&v, kv_len, head);
            self.scaled_dot_product_attention(&q_head, &k_head, &v_head, mask)
        })?;

        // Concatenate heads and project output
        let concat = self.concat_heads(&head_outputs, seq_len);
        self.w_o.forward(&concat, seq_len)
    }

    /// SIMD-accelerated forward pass
    fn forward_cross_simd(
        &self,
        x: &[f32],
        context: &[f32],
        mask: Option<&[f32]>,
    ) -> WhisperResult<Vec<f32>> {
        let seq_len = x.len() / self.d_model;
        let kv_len = context.len() / self.d_model;

        if x.len() % self.d_model != 0 {
            return Err(WhisperError::Model("input size mismatch".into()));
        }
        if context.len() % self.d_model != 0 {
            return Err(WhisperError::Model("context size mismatch".into()));
        }

        // Project Q, K, V — independent matmuls run in parallel when feature enabled
        #[cfg(feature = "parallel")]
        let (q, k, v) = {
            let (q, (k, v)) = rayon::join(
                || self.w_q.forward_simd(x, seq_len),
                || {
                    rayon::join(
                        || self.w_k.forward_simd(context, kv_len),
                        || self.w_v.forward_simd(context, kv_len),
                    )
                },
            );
            (q?, k?, v?)
        };
        #[cfg(not(feature = "parallel"))]
        let (q, k, v) = {
            let q = self.w_q.forward_simd(x, seq_len)?;
            let k = self.w_k.forward_simd(context, kv_len)?;
            let v = self.w_v.forward_simd(context, kv_len)?;
            (q, k, v)
        };

        // Compute attention for each head using SIMD (parallel when feature enabled)
        // Per §11.3.2: Each head is independent [31], enabling parallel computation
        let head_outputs = parallel_try_map(0..self.n_heads, |head| {
            let q_head = self.extract_head(&q, seq_len, head);
            let k_head = self.extract_head(&k, kv_len, head);
            let v_head = self.extract_head(&v, kv_len, head);

            // Use SIMD attention
            self.scaled_dot_product_attention_simd(&q_head, &k_head, &v_head, mask)
        })?;

        // Concatenate heads and project output using SIMD
        let concat = self.concat_heads(&head_outputs, seq_len);
        self.w_o.forward_simd(&concat, seq_len)
    }

    /// Flash Attention forward pass for memory-efficient long sequences
    ///
    /// Uses block-based attention computation with O(n) memory instead of O(n²).
    /// Best for sequences longer than ~128 tokens where memory savings matter.
    ///
    /// # Arguments
    /// * `x` - Query input tensor (seq_len x d_model)
    /// * `context` - Key/Value input tensor (kv_len x d_model)
    /// * `mask` - Optional attention mask
    /// * `block_size` - Block size for tiling (default: 32)
    ///
    /// # Returns
    /// Output tensor (seq_len x d_model)
    pub fn forward_cross_flash(
        &self,
        x: &[f32],
        context: &[f32],
        mask: Option<&[f32]>,
        block_size: usize,
    ) -> WhisperResult<Vec<f32>> {
        let seq_len = x.len() / self.d_model;
        let kv_len = context.len() / self.d_model;

        if x.len() % self.d_model != 0 {
            return Err(WhisperError::Model("input size mismatch".into()));
        }
        if context.len() % self.d_model != 0 {
            return Err(WhisperError::Model("context size mismatch".into()));
        }

        // Project Q, K, V sequentially — each GEMM uses the full thread pool
        // via gemm_blis_parallel internally. Sequential avoids nested rayon contention.
        let q = self.w_q.forward_simd(x, seq_len)?;
        let k = self.w_k.forward_simd(context, kv_len)?;
        let v = self.w_v.forward_simd(context, kv_len)?;

        // Per-head Q-row parallelism: extract_head gives contiguous per-head data
        // (critical for cache performance), then flash_attention_simd_parallel
        // parallelizes over 1500 Q-rows within each head (all 16 threads active).
        // Heads processed sequentially to avoid thread contention.
        #[cfg(feature = "parallel")]
        let head_outputs: Vec<Vec<f32>> = (0..self.n_heads)
            .map(|head| {
                let q_head = self.extract_head(&q, seq_len, head);
                let k_head = self.extract_head(&k, kv_len, head);
                let v_head = self.extract_head(&v, kv_len, head);
                let config = FlashAttentionConfig::new(seq_len, kv_len, self.d_head, block_size);
                flash_attention_simd_parallel(&q_head, &k_head, &v_head, config, mask)
            })
            .collect();

        #[cfg(not(feature = "parallel"))]
        let head_outputs: Vec<Vec<f32>> = (0..self.n_heads)
            .map(|head| {
                let q_head = self.extract_head(&q, seq_len, head);
                let k_head = self.extract_head(&k, kv_len, head);
                let v_head = self.extract_head(&v, kv_len, head);
                let config = FlashAttentionConfig::new(seq_len, kv_len, self.d_head, block_size);
                if cfg!(feature = "simd") {
                    flash_attention_simd(&q_head, &k_head, &v_head, config, mask)
                } else {
                    flash_attention(&q_head, &k_head, &v_head, config, mask)
                }
            })
            .collect();

        // Concatenate heads and project output using SIMD
        let concat = self.concat_heads(&head_outputs, seq_len);
        self.w_o.forward_simd(&concat, seq_len)
    }

    /// Forward pass with automatic Flash Attention selection
    ///
    /// Uses Flash Attention for long sequences (>128 tokens) to reduce memory,
    /// and standard attention for shorter sequences.
    pub fn forward_cross_auto(
        &self,
        x: &[f32],
        context: &[f32],
        mask: Option<&[f32]>,
    ) -> WhisperResult<Vec<f32>> {
        let seq_len = x.len() / self.d_model;
        let kv_len = context.len() / self.d_model;

        // Use Flash Attention for sequences where O(n²) memory is significant
        if seq_len > FLASH_ATTENTION_THRESHOLD || kv_len > FLASH_ATTENTION_THRESHOLD {
            self.forward_cross_flash(x, context, mask, FLASH_ATTENTION_BLOCK_SIZE)
        } else {
            self.forward_cross(x, context, mask)
        }
    }

    /// Forward pass using realizar's FlashAttention-2 (WAPR-PERF-004 Phase 2)
    ///
    /// Uses realizar's optimized FlashAttention-2 implementation for:
    /// - O(N) memory instead of O(N²)
    /// - Better parallelism via tiled processing
    /// - SIMD-accelerated inner loops
    ///
    /// # References
    /// - Dao et al. (2022): "FlashAttention: Fast and Memory-Efficient Attention"
    /// - Dao (2023): "FlashAttention-2: Faster Attention with Better Parallelism"
    ///
    /// # Arguments
    /// * `x` - Query input tensor (seq_len x d_model)
    /// * `context` - Key/Value input tensor (kv_len x d_model)
    /// * `mask` - Optional attention mask
    ///
    /// # Returns
    /// Output tensor (seq_len x d_model)
    #[cfg(feature = "realizar-inference")]
    pub fn forward_cross_flash_v2(
        &self,
        x: &[f32],
        context: &[f32],
        mask: Option<&[f32]>,
    ) -> WhisperResult<Vec<f32>> {
        let seq_len = x.len() / self.d_model;
        let kv_len = context.len() / self.d_model;

        if x.len() % self.d_model != 0 {
            return Err(WhisperError::Model("input size mismatch".into()));
        }
        if context.len() % self.d_model != 0 {
            return Err(WhisperError::Model("context size mismatch".into()));
        }

        // Project Q, K, V sequentially — each GEMM uses the full thread pool
        // via gemm_blis_parallel internally. Sequential avoids nested rayon contention.
        let q = self.w_q.forward_simd(x, seq_len)?;
        let k = self.w_k.forward_simd(context, kv_len)?;
        let v = self.w_v.forward_simd(context, kv_len)?;

        // Compute attention for each head using realizar's FlashAttention-2
        let head_outputs = parallel_map(0..self.n_heads, |head| {
            let q_head = self.extract_head(&q, seq_len, head);
            let k_head = self.extract_head(&k, kv_len, head);
            let v_head = self.extract_head(&v, kv_len, head);

            // Convert to realizar Tensors
            let q_tensor = RealizarTensor::from_vec(vec![seq_len, self.d_head], q_head)
                .expect("valid Q tensor");
            let k_tensor = RealizarTensor::from_vec(vec![kv_len, self.d_head], k_head)
                .expect("valid K tensor");
            let v_tensor = RealizarTensor::from_vec(vec![kv_len, self.d_head], v_head)
                .expect("valid V tensor");

            // Use realizar's FlashAttention-2
            // Block size of 32 matches our FLASH_ATTENTION_BLOCK_SIZE
            let attn = RealizarAttention::new(self.d_head).expect("valid Attention");

            // Call FlashAttention-2 (flash_forward_v2 with block_size)
            // Note: realizar's flash_forward_v2 doesn't take a mask directly
            let result = attn
                .flash_forward_v2(&q_tensor, &k_tensor, &v_tensor, FLASH_ATTENTION_BLOCK_SIZE)
                .expect("FlashAttention-2 forward");

            // Apply mask if present (post-attention masking for causal)
            let mut output = result.data().to_vec();
            if let Some(m) = mask {
                // For fully masked positions, zero out the output
                for (i, out_val) in output.iter_mut().enumerate() {
                    let q_idx = i / self.d_head;
                    if q_idx < seq_len {
                        let mask_row_start = q_idx * kv_len;
                        // Check if all mask values for this row are -inf (fully masked)
                        let all_masked = (0..kv_len).all(|k| m[mask_row_start + k] < -1e9);
                        if all_masked {
                            *out_val = 0.0;
                        }
                    }
                }
            }

            output
        });

        // Concatenate heads and project output using SIMD
        let concat = self.concat_heads(&head_outputs, seq_len);
        self.w_o.forward_simd(&concat, seq_len)
    }

    /// Forward pass with optimal dispatch including FlashAttention-2
    ///
    /// Dispatch logic:
    /// - Long sequences (>128): Custom SIMD FlashAttention (lower overhead than realizar)
    /// - Short sequences: Standard SIMD attention
    ///
    /// Note: We use our custom FlashAttention implementation for long sequences
    /// because realizar's FlashAttention-2 has per-head tensor allocation overhead
    /// that dominates for encoder-like workloads (1500 seq_len × 6 heads).
    /// The custom implementation avoids this by working directly with slices.
    #[cfg(feature = "realizar-inference")]
    pub fn forward_cross_optimal(
        &self,
        x: &[f32],
        context: &[f32],
        mask: Option<&[f32]>,
    ) -> WhisperResult<Vec<f32>> {
        let seq_len = x.len() / self.d_model;
        let kv_len = context.len() / self.d_model;

        // Use custom SIMD FlashAttention for long sequences (lower overhead)
        // realizar's FlashAttention-2 creates tensors per head which adds ~640ms overhead
        if seq_len > FLASH_ATTENTION_THRESHOLD || kv_len > FLASH_ATTENTION_THRESHOLD {
            self.forward_cross_flash(x, context, mask, FLASH_ATTENTION_BLOCK_SIZE)
        } else if cfg!(feature = "simd") {
            self.forward_cross_simd(x, context, mask)
        } else {
            self.forward_cross(x, context, mask)
        }
    }

    /// Streaming attention with pre-computed key/value cache
    ///
    /// This method is optimized for incremental inference where:
    /// - Query is computed only for new positions
    /// - Key and Value are retrieved from a cache
    ///
    /// # Arguments
    /// * `x` - Current input tensor (new_seq_len × d_model)
    /// * `cached_key` - Cached key tensor (cache_len × d_model)
    /// * `cached_value` - Cached value tensor (cache_len × d_model)
    /// * `mask` - Optional attention mask (new_seq_len × cache_len)
    ///
    /// # Returns
    /// * `(output, new_key, new_value)` - Output and K/V for caching
    ///
    /// # Use Case
    /// Streaming transcription where each new audio chunk produces tokens
    /// that attend to previously cached context.
    pub fn forward_streaming(
        &self,
        x: &[f32],
        cached_key: &[f32],
        cached_value: &[f32],
        mask: Option<&[f32]>,
    ) -> WhisperResult<(Vec<f32>, Vec<f32>, Vec<f32>)> {
        let seq_len = x.len() / self.d_model;
        let cache_len = if cached_key.is_empty() {
            0
        } else {
            cached_key.len() / self.d_model
        };

        // Project query from current input
        let q = self.w_q.forward(x, seq_len)?;

        // Project new key/value from current input (to be cached)
        let new_k = self.w_k.forward(x, seq_len)?;
        let new_v = self.w_v.forward(x, seq_len)?;

        // Compute attention for each head (parallel when feature enabled)
        // Per §11.3.2: Each head is independent [31], enabling parallel computation
        let head_outputs = parallel_try_map(0..self.n_heads, |head| {
            // Extract this head's query
            let head_q = self.extract_head(&q, seq_len, head);

            // For K/V, combine cached and new if cache exists
            let (head_k, head_v, total_kv_len) = if cache_len > 0 {
                let cached_head_k = self.extract_head(cached_key, cache_len, head);
                let cached_head_v = self.extract_head(cached_value, cache_len, head);
                let new_head_k = self.extract_head(&new_k, seq_len, head);
                let new_head_v = self.extract_head(&new_v, seq_len, head);

                // Concatenate cached + new
                let mut combined_k = cached_head_k;
                combined_k.extend_from_slice(&new_head_k);
                let mut combined_v = cached_head_v;
                combined_v.extend_from_slice(&new_head_v);

                (combined_k, combined_v, cache_len + seq_len)
            } else {
                let new_head_k = self.extract_head(&new_k, seq_len, head);
                let new_head_v = self.extract_head(&new_v, seq_len, head);
                (new_head_k, new_head_v, seq_len)
            };

            // Compute attention with optional Flash Attention
            if total_kv_len > FLASH_ATTENTION_THRESHOLD {
                let config = FlashAttentionConfig::new(
                    seq_len,
                    total_kv_len,
                    self.d_head,
                    FLASH_ATTENTION_BLOCK_SIZE,
                );
                Ok(flash_attention_simd(
                    &head_q, &head_k, &head_v, config, mask,
                ))
            } else {
                self.scaled_dot_product_attention(&head_q, &head_k, &head_v, mask)
            }
        })?;

        // Concatenate head outputs
        let concat = self.concat_heads(&head_outputs, seq_len);

        // Output projection
        let output = self.w_o.forward(&concat, seq_len)?;

        Ok((output, new_k, new_v))
    }

    /// Streaming self-attention with automatic KV cache management
    ///
    /// Simplified interface for streaming self-attention that automatically
    /// handles the query-only computation pattern.
    ///
    /// # Arguments
    /// * `x` - Current input tensor (new_seq_len × d_model)
    /// * `cached_key` - Previously cached keys (cache_len × d_model)
    /// * `cached_value` - Previously cached values (cache_len × d_model)
    ///
    /// # Returns
    /// Attention output for the new tokens
    #[allow(dead_code)] // Public API for streaming support
    pub fn forward_self_streaming(
        &self,
        x: &[f32],
        cached_key: &[f32],
        cached_value: &[f32],
    ) -> WhisperResult<(Vec<f32>, Vec<f32>, Vec<f32>)> {
        // For self-attention, we use a causal mask that prevents
        // attending to future positions within the new sequence
        let seq_len = x.len() / self.d_model;
        let cache_len = cached_key.len() / self.d_model;

        // Build streaming causal mask
        // New tokens can attend to all cached tokens + previous new tokens
        let mask = if seq_len > 1 {
            let total_len = cache_len + seq_len;
            let mut mask_data = vec![0.0_f32; seq_len * total_len];

            for q in 0..seq_len {
                // Can attend to all cached positions + positions 0..=q in new
                let max_attend = cache_len + q + 1;
                for k in max_attend..total_len {
                    mask_data[q * total_len + k] = f32::NEG_INFINITY;
                }
            }
            Some(mask_data)
        } else {
            None
        };

        self.forward_streaming(x, cached_key, cached_value, mask.as_deref())
    }

    /// Extract a single head's data from multi-head tensor
    fn extract_head(&self, tensor: &[f32], seq_len: usize, head: usize) -> Vec<f32> {
        let mut head_data = vec![0.0_f32; seq_len * self.d_head];

        for s in 0..seq_len {
            let src_offset = s * self.d_model + head * self.d_head;
            let dst_offset = s * self.d_head;
            head_data[dst_offset..dst_offset + self.d_head]
                .copy_from_slice(&tensor[src_offset..src_offset + self.d_head]);
        }

        head_data
    }

    /// Concatenate head outputs back into full tensor
    fn concat_heads(&self, heads: &[Vec<f32>], seq_len: usize) -> Vec<f32> {
        let mut concat = vec![0.0_f32; seq_len * self.d_model];

        for (head, head_data) in heads.iter().enumerate() {
            for s in 0..seq_len {
                let src_offset = s * self.d_head;
                let dst_offset = s * self.d_model + head * self.d_head;
                concat[dst_offset..dst_offset + self.d_head]
                    .copy_from_slice(&head_data[src_offset..src_offset + self.d_head]);
            }
        }

        concat
    }

    /// Get number of heads
    #[must_use]
    pub const fn n_heads(&self) -> usize {
        self.n_heads
    }

    /// Get model dimension
    #[must_use]
    pub const fn d_model(&self) -> usize {
        self.d_model
    }

    /// Get per-head dimension
    #[must_use]
    pub const fn d_head(&self) -> usize {
        self.d_head
    }

    /// Get scale factor
    #[must_use]
    pub fn scale(&self) -> f32 {
        self.scale
    }

    /// Get query weights reference
    #[must_use]
    pub const fn w_q(&self) -> &LinearWeights {
        &self.w_q
    }

    /// Get key weights reference
    #[must_use]
    pub const fn w_k(&self) -> &LinearWeights {
        &self.w_k
    }

    /// Get value weights reference
    #[must_use]
    pub const fn w_v(&self) -> &LinearWeights {
        &self.w_v
    }

    /// Get output weights reference
    #[must_use]
    pub const fn w_o(&self) -> &LinearWeights {
        &self.w_o
    }

    /// Set query weights from a slice
    pub fn set_query_weight(&mut self, values: &[f32]) {
        self.w_q.set_weight(values);
    }

    /// Set key weights from a slice
    pub fn set_key_weight(&mut self, values: &[f32]) {
        self.w_k.set_weight(values);
    }

    /// Set value weights from a slice
    pub fn set_value_weight(&mut self, values: &[f32]) {
        self.w_v.set_weight(values);
    }

    /// Set output weights from a slice
    pub fn set_out_weight(&mut self, values: &[f32]) {
        self.w_o.set_weight(values);
    }

    /// Set query bias from a slice
    pub fn set_query_bias(&mut self, values: &[f32]) {
        self.w_q.set_bias(values);
    }

    /// Set key bias from a slice
    pub fn set_key_bias(&mut self, values: &[f32]) {
        self.w_k.set_bias(values);
    }

    /// Set value bias from a slice
    pub fn set_value_bias(&mut self, values: &[f32]) {
        self.w_v.set_bias(values);
    }

    /// Set output bias from a slice
    pub fn set_out_bias(&mut self, values: &[f32]) {
        self.w_o.set_bias(values);
    }

    /// Set query weights from fp16 (u16 bit patterns)
    pub fn set_query_weight_f16(&mut self, values: &[u16]) {
        self.w_q.set_weight_f16(values);
    }

    /// Set key weights from fp16 (u16 bit patterns)
    pub fn set_key_weight_f16(&mut self, values: &[u16]) {
        self.w_k.set_weight_f16(values);
    }

    /// Set value weights from fp16 (u16 bit patterns)
    pub fn set_value_weight_f16(&mut self, values: &[u16]) {
        self.w_v.set_weight_f16(values);
    }

    /// Set output weights from fp16 (u16 bit patterns)
    pub fn set_out_weight_f16(&mut self, values: &[u16]) {
        self.w_o.set_weight_f16(values);
    }

    /// Convert all linear layer weights to fp16 in-place
    pub fn convert_to_f16(&mut self) {
        self.w_q.convert_to_f16();
        self.w_k.convert_to_f16();
        self.w_v.convert_to_f16();
        self.w_o.convert_to_f16();
    }

    /// Convert all linear layer weights to INT8 symmetric quantization (pv:int8-symmetric-quant-v1).
    pub fn convert_to_i8(&mut self) {
        self.w_q.convert_to_i8();
        self.w_k.convert_to_i8();
        self.w_v.convert_to_i8();
        self.w_o.convert_to_i8();
    }

    /// Pre-compute and cache transposed weights for all linear layers
    ///
    /// Call this after loading all weights to optimize SIMD matmul performance.
    pub fn finalize_weights(&mut self) {
        self.w_q.finalize_weights();
        self.w_k.finalize_weights();
        self.w_v.finalize_weights();
        self.w_o.finalize_weights();
    }

    /// Same as finalize_weights but forces caching of f32 weights for encoder.
    pub fn finalize_weights_encoder(&mut self) {
        self.w_q.finalize_weights_encoder();
        self.w_k.finalize_weights_encoder();
        self.w_v.finalize_weights_encoder();
        self.w_o.finalize_weights_encoder();
    }

    /// Check if all weights have been finalized
    #[must_use]
    pub fn is_finalized(&self) -> bool {
        self.w_q.is_finalized()
            && self.w_k.is_finalized()
            && self.w_v.is_finalized()
            && self.w_o.is_finalized()
    }

    /// Get mutable query weights reference (for loading weights)
    pub fn w_q_mut(&mut self) -> &mut LinearWeights {
        &mut self.w_q
    }

    /// Get mutable key weights reference (for loading weights)
    pub fn w_k_mut(&mut self) -> &mut LinearWeights {
        &mut self.w_k
    }

    /// Get mutable value weights reference (for loading weights)
    pub fn w_v_mut(&mut self) -> &mut LinearWeights {
        &mut self.w_v
    }

    /// Get mutable output weights reference (for loading weights)
    pub fn w_o_mut(&mut self) -> &mut LinearWeights {
        &mut self.w_o
    }

    /// Fuse Q, K, V fp16 weights into a single contiguous `W_qkv` matrix (pv:fused-qkv-projection-v1).
    ///
    /// After calling this, `forward_qkv_into` will use a single matvec instead of three.
    /// Must be called after weights are loaded (e.g., after `set_*_weight_f16`).
    pub fn fuse_qkv_weights(&mut self) {
        // Fuse fp16 QKV weights
        if let (Some(wq), Some(wk), Some(wv)) = (
            self.w_q.weight_f16().map(|s| s.to_vec()),
            self.w_k.weight_f16().map(|s| s.to_vec()),
            self.w_v.weight_f16().map(|s| s.to_vec()),
        ) {
            let mut fused = Vec::with_capacity(wq.len() + wk.len() + wv.len());
            fused.extend_from_slice(&wq);
            fused.extend_from_slice(&wk);
            fused.extend_from_slice(&wv);
            self.w_qkv_f16 = Some(fused);
        }

        // Concatenate biases: [b_q; b_k; b_v]
        let mut b = Vec::with_capacity(self.d_model * 3);
        b.extend_from_slice(&self.w_q.bias);
        b.extend_from_slice(&self.w_k.bias);
        b.extend_from_slice(&self.w_v.bias);
        self.b_qkv = Some(b);
    }

    /// Check whether fused QKV weights are available.
    #[must_use]
    pub fn has_fused_qkv(&self) -> bool {
        self.w_qkv_f16.is_some() || self.w_q.is_i8()
    }

    /// Fused Q+K+V projection into pre-allocated buffer (pv:fused-qkv-projection-v1).
    ///
    /// Tries INT8 path first (lowest bandwidth), then fp16, then separate fallback.
    /// Output layout: `[q[0..d]; k[d..2d]; v[2d..3d]]`.
    pub fn forward_qkv_into(&self, input: &[f32], qkv_out: &mut [f32]) -> WhisperResult<()> {
        let d = self.d_model;
        debug_assert_eq!(qkv_out.len(), 3 * d);

        // Fused fp16 path
        if let (Some(w_qkv), Some(b_qkv)) = (&self.w_qkv_f16, &self.b_qkv) {
            simd::tiled_matvec_f16_into(w_qkv, input, qkv_out, 3 * d, d);
            simd::broadcast_add_inplace(qkv_out, b_qkv, 1, 3 * d);
            Ok(())
        } else {
            // Fallback: three separate projections (uses INT8 path if available via forward_simd_into)
            self.w_q.forward_simd_into(input, 1, &mut qkv_out[..d])?;
            self.w_k
                .forward_simd_into(input, 1, &mut qkv_out[d..2 * d])?;
            self.w_v
                .forward_simd_into(input, 1, &mut qkv_out[2 * d..3 * d])?;
            Ok(())
        }
    }

    /// Batched Fused Q+K+V projection into pre-allocated buffer.
    ///
    /// Input: [batch_size, d_model]
    /// Output: [batch_size, 3 * d_model] where each batch item has [q; k; v]
    pub fn forward_qkv_batch_into(
        &self,
        input: &[f32],
        batch_size: usize,
        qkv_out: &mut [f32],
    ) -> WhisperResult<()> {
        let d = self.d_model;
        debug_assert_eq!(input.len(), batch_size * d);
        debug_assert_eq!(qkv_out.len(), batch_size * 3 * d);

        // Fused fp16 path
        if let (Some(w_qkv), Some(b_qkv)) = (&self.w_qkv_f16, &self.b_qkv) {
            const FP16_MATVEC_BATCH_LIMIT: usize = 8;
            if batch_size <= FP16_MATVEC_BATCH_LIMIT {
                for i in 0..batch_size {
                    let in_start = i * d;
                    let out_start = i * 3 * d;
                    simd::tiled_matvec_f16_into(
                        w_qkv,
                        &input[in_start..in_start + d],
                        &mut qkv_out[out_start..out_start + 3 * d],
                        3 * d,
                        d,
                    );
                }
            } else {
                // True single matmul for QKV (pv:batched-beam-search-v1)
                let mut buf = vec![0.0_f32; w_qkv.len()];
                simd::dequant_f16_row(w_qkv, &mut buf);
                let weight_t = simd::transpose(&buf, 3 * d, d);
                let tmp = simd::matmul(input, &weight_t, batch_size, d, 3 * d);
                qkv_out.copy_from_slice(&tmp);
            }
            simd::broadcast_add_inplace(qkv_out, b_qkv, batch_size, 3 * d);
            Ok(())
        } else {
            // Fallback: separate batch projections
            let mut q = vec![0.0; batch_size * d];
            let mut k = vec![0.0; batch_size * d];
            let mut v = vec![0.0; batch_size * d];
            self.w_q.forward_simd_into(input, batch_size, &mut q)?;
            self.w_k.forward_simd_into(input, batch_size, &mut k)?;
            self.w_v.forward_simd_into(input, batch_size, &mut v)?;
            for i in 0..batch_size {
                let out_start = i * 3 * d;
                let in_start = i * d;
                qkv_out[out_start..out_start + d].copy_from_slice(&q[in_start..in_start + d]);
                qkv_out[out_start + d..out_start + 2 * d]
                    .copy_from_slice(&k[in_start..in_start + d]);
                qkv_out[out_start + 2 * d..out_start + 3 * d]
                    .copy_from_slice(&v[in_start..in_start + d]);
            }
            Ok(())
        }
    }
}

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

    // =========================================================================
    // Linear Weights Tests
    // =========================================================================

    #[test]
    fn test_linear_weights_new() {
        let linear = LinearWeights::new(64, 128);
        assert_eq!(linear.in_features, 64);
        assert_eq!(linear.out_features, 128);
        assert_eq!(linear.weight.len(), 128 * 64);
        assert_eq!(linear.bias.len(), 128);
    }

    #[test]
    fn test_linear_forward_identity() {
        let mut linear = LinearWeights::new(4, 4);
        // Set up identity matrix
        for i in 0..4 {
            linear.weight[i * 4 + i] = 1.0;
        }

        let input = vec![1.0, 2.0, 3.0, 4.0];
        let output = linear.forward(&input, 1).expect("forward should succeed");

        assert_eq!(output.len(), 4);
        for i in 0..4 {
            assert!(
                (output[i] - input[i]).abs() < 1e-5,
                "Identity should preserve input"
            );
        }
    }

    #[test]
    fn test_linear_forward_with_bias() {
        let mut linear = LinearWeights::new(2, 2);
        // Identity weights
        linear.weight = vec![1.0, 0.0, 0.0, 1.0];
        linear.bias = vec![1.0, 2.0];

        let input = vec![3.0, 4.0];
        let output = linear.forward(&input, 1).expect("forward should succeed");

        assert!((output[0] - 4.0).abs() < 1e-5); // 3 + 1
        assert!((output[1] - 6.0).abs() < 1e-5); // 4 + 2
    }

    // =========================================================================
    // Multi-Head Attention Construction Tests
    // =========================================================================

    #[test]
    fn test_attention_new() {
        let attn = MultiHeadAttention::new(8, 512);
        assert_eq!(attn.n_heads(), 8);
        assert_eq!(attn.d_model(), 512);
        assert_eq!(attn.d_head(), 64);
    }

    #[test]
    fn test_attention_scale() {
        let attn = MultiHeadAttention::new(8, 512);
        let expected_scale = 1.0 / (64.0_f32).sqrt();
        assert!((attn.scale() - expected_scale).abs() < 1e-6);
    }

    #[test]
    #[should_panic(expected = "must be divisible")]
    fn test_attention_invalid_dimensions() {
        let _ = MultiHeadAttention::new(8, 100); // 100 not divisible by 8
    }

    // =========================================================================
    // Scaled Dot-Product Attention Tests
    // =========================================================================

    #[test]
    fn test_scaled_dot_product_attention_basic() {
        let attn = MultiHeadAttention::new(1, 4);

        // Simple Q, K, V with seq_len=2, d_head=4
        let query = vec![1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0];
        let key = vec![1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0];
        let value = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0];

        let output = attn
            .scaled_dot_product_attention(&query, &key, &value, None)
            .expect("attention should succeed");

        assert_eq!(output.len(), 8); // seq_len * d_head
    }

    #[test]
    fn test_scaled_dot_product_attention_with_mask() {
        let attn = MultiHeadAttention::new(1, 4);

        let query = vec![1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0];
        let key = vec![1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0];
        let value = vec![1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0];

        // Causal mask: second position can't attend to first
        let mask = vec![0.0, f32::NEG_INFINITY, 0.0, 0.0];

        let output = attn
            .scaled_dot_product_attention(&query, &key, &value, Some(&mask))
            .expect("attention should succeed");

        assert_eq!(output.len(), 8);
    }

    #[test]
    fn test_attention_softmax_sums_to_one() {
        let attn = MultiHeadAttention::new(1, 4);

        let query = vec![1.0, 2.0, 3.0, 4.0]; // seq_len=1
        let key = vec![1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0]; // kv_len=2
        let value = vec![1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0];

        let _output = attn
            .scaled_dot_product_attention(&query, &key, &value, None)
            .expect("attention should succeed");

        // Output should be a weighted combination of values
        // (we can't easily check softmax directly without exposing internals)
    }

    // =========================================================================
    // Causal Mask Tests
    // =========================================================================

    #[test]
    fn test_causal_mask_shape() {
        let mask = MultiHeadAttention::causal_mask(4);
        assert_eq!(mask.len(), 16); // 4x4
    }

    #[test]
    fn test_causal_mask_values() {
        let mask = MultiHeadAttention::causal_mask(3);

        // Position 0 can only attend to position 0
        assert_eq!(mask[0], 0.0); // [0,0]
        assert_eq!(mask[1], f32::NEG_INFINITY); // [0,1]
        assert_eq!(mask[2], f32::NEG_INFINITY); // [0,2]

        // Position 1 can attend to 0 and 1
        assert_eq!(mask[3], 0.0); // [1,0]
        assert_eq!(mask[4], 0.0); // [1,1]
        assert_eq!(mask[5], f32::NEG_INFINITY); // [1,2]

        // Position 2 can attend to all
        assert_eq!(mask[6], 0.0); // [2,0]
        assert_eq!(mask[7], 0.0); // [2,1]
        assert_eq!(mask[8], 0.0); // [2,2]
    }

    // =========================================================================
    // Forward Pass Tests
    // =========================================================================

    #[test]
    fn test_forward_basic() {
        let attn = MultiHeadAttention::new(2, 8);

        // Input: seq_len=2, d_model=8
        let input = vec![0.0_f32; 16];

        let output = attn.forward(&input, None).expect("forward should succeed");

        assert_eq!(output.len(), 16); // seq_len * d_model
    }

    #[test]
    fn test_forward_cross_basic() {
        let attn = MultiHeadAttention::new(2, 8);

        let x = vec![0.0_f32; 16]; // seq_len=2
        let context = vec![0.0_f32; 24]; // kv_len=3

        let output = attn
            .forward_cross(&x, &context, None)
            .expect("forward_cross should succeed");

        assert_eq!(output.len(), 16); // Same as query seq_len * d_model
    }

    #[test]
    fn test_forward_with_causal_mask() {
        let attn = MultiHeadAttention::new(2, 8);
        let input = vec![0.0_f32; 16]; // seq_len=2
        let mask = MultiHeadAttention::causal_mask(2);

        let output = attn
            .forward(&input, Some(&mask))
            .expect("forward should succeed");

        assert_eq!(output.len(), 16);
    }

    // =========================================================================
    // Head Extraction/Concatenation Tests
    // =========================================================================

    #[test]
    fn test_extract_head() {
        let attn = MultiHeadAttention::new(2, 8);

        // Create tensor with distinct values for each head
        // seq_len=2, d_model=8, so 2 heads with d_head=4 each
        let tensor: Vec<f32> = (0..16).map(|i| i as f32).collect();

        let head0 = attn.extract_head(&tensor, 2, 0);
        let head1 = attn.extract_head(&tensor, 2, 1);

        assert_eq!(head0.len(), 8); // seq_len * d_head
        assert_eq!(head1.len(), 8);

        // First position, head 0
        assert_eq!(head0[0..4], [0.0, 1.0, 2.0, 3.0]);
        // First position, head 1
        assert_eq!(head1[0..4], [4.0, 5.0, 6.0, 7.0]);
    }

    #[test]
    fn test_concat_heads() {
        let attn = MultiHeadAttention::new(2, 8);

        let head0 = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]; // seq_len=2, d_head=4
        let head1 = vec![9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0];

        let concat = attn.concat_heads(&[head0, head1], 2);

        assert_eq!(concat.len(), 16);
        // First position
        assert_eq!(concat[0..8], [1.0, 2.0, 3.0, 4.0, 9.0, 10.0, 11.0, 12.0]);
        // Second position
        assert_eq!(concat[8..16], [5.0, 6.0, 7.0, 8.0, 13.0, 14.0, 15.0, 16.0]);
    }

    #[test]
    fn test_extract_concat_roundtrip() {
        let attn = MultiHeadAttention::new(2, 8);
        let original: Vec<f32> = (0..16).map(|i| i as f32).collect();

        let head0 = attn.extract_head(&original, 2, 0);
        let head1 = attn.extract_head(&original, 2, 1);
        let reconstructed = attn.concat_heads(&[head0, head1], 2);

        assert_eq!(original, reconstructed);
    }

    // =========================================================================
    // Error Handling Tests
    // =========================================================================

    #[test]
    fn test_attention_size_mismatch() {
        let attn = MultiHeadAttention::new(2, 8);

        let query = vec![0.0_f32; 8]; // seq_len=1, d_head=4
        let key = vec![0.0_f32; 8];
        let value = vec![0.0_f32; 12]; // Different size!

        let result = attn.scaled_dot_product_attention(&query, &key, &value, None);
        assert!(result.is_err());
    }

    #[test]
    fn test_forward_size_mismatch() {
        let attn = MultiHeadAttention::new(2, 8);
        let input = vec![0.0_f32; 15]; // Not divisible by d_model=8

        let result = attn.forward(&input, None);
        assert!(result.is_err());
    }

    // =========================================================================
    // Accessor Tests
    // =========================================================================

    #[test]
    fn test_weight_accessors() {
        let attn = MultiHeadAttention::new(4, 64);

        assert_eq!(attn.w_q().in_features, 64);
        assert_eq!(attn.w_k().in_features, 64);
        assert_eq!(attn.w_v().in_features, 64);
        assert_eq!(attn.w_o().in_features, 64);
    }

    // =========================================================================
    // Linear Weight Setter Tests
    // =========================================================================

    #[test]
    fn test_linear_set_weight() {
        let mut linear = LinearWeights::new(4, 4);
        let weights = vec![1.0_f32; 16];

        linear.set_weight(&weights);
        assert!((linear.weight[0] - 1.0).abs() < f32::EPSILON);
        assert!((linear.weight[15] - 1.0).abs() < f32::EPSILON);
    }

    #[test]
    fn test_linear_set_bias() {
        let mut linear = LinearWeights::new(4, 4);
        let biases = vec![0.5_f32; 4];

        linear.set_bias(&biases);
        assert!((linear.bias[0] - 0.5).abs() < f32::EPSILON);
        assert!((linear.bias[3] - 0.5).abs() < f32::EPSILON);
    }

    #[test]
    fn test_linear_set_weight_partial() {
        let mut linear = LinearWeights::new(4, 4);
        let weights = vec![2.0_f32; 8]; // Only half the weights

        linear.set_weight(&weights);
        assert!((linear.weight[0] - 2.0).abs() < f32::EPSILON);
        assert!((linear.weight[7] - 2.0).abs() < f32::EPSILON);
        assert!((linear.weight[8] - 0.0).abs() < f32::EPSILON); // Unchanged
    }

    // =========================================================================
    // MultiHeadAttention Setter Tests
    // =========================================================================

    #[test]
    fn test_attention_set_query_weight() {
        let mut attn = MultiHeadAttention::new(2, 8);
        let weights = vec![1.0_f32; 64];

        attn.set_query_weight(&weights);
        assert!((attn.w_q().weight[0] - 1.0).abs() < f32::EPSILON);
    }

    #[test]
    fn test_attention_set_key_weight() {
        let mut attn = MultiHeadAttention::new(2, 8);
        let weights = vec![2.0_f32; 64];

        attn.set_key_weight(&weights);
        assert!((attn.w_k().weight[0] - 2.0).abs() < f32::EPSILON);
    }

    #[test]
    fn test_attention_set_value_weight() {
        let mut attn = MultiHeadAttention::new(2, 8);
        let weights = vec![3.0_f32; 64];

        attn.set_value_weight(&weights);
        assert!((attn.w_v().weight[0] - 3.0).abs() < f32::EPSILON);
    }

    #[test]
    fn test_attention_set_out_weight() {
        let mut attn = MultiHeadAttention::new(2, 8);
        let weights = vec![4.0_f32; 64];

        attn.set_out_weight(&weights);
        assert!((attn.w_o().weight[0] - 4.0).abs() < f32::EPSILON);
    }

    // =========================================================================
    // SIMD-Accelerated Tests
    // =========================================================================

    #[test]
    fn test_linear_forward_simd_identity() {
        let mut linear = LinearWeights::new(4, 4);
        // Set up identity matrix
        for i in 0..4 {
            linear.weight[i * 4 + i] = 1.0;
        }

        let input = vec![1.0, 2.0, 3.0, 4.0];
        let output = linear
            .forward_simd(&input, 1)
            .expect("forward_simd should succeed");

        assert_eq!(output.len(), 4);
        for i in 0..4 {
            assert!(
                (output[i] - input[i]).abs() < 1e-4,
                "SIMD Identity should preserve input: got {} expected {}",
                output[i],
                input[i]
            );
        }
    }

    #[test]
    fn test_linear_forward_simd_with_bias() {
        let mut linear = LinearWeights::new(2, 2);
        // Identity weights
        linear.weight = vec![1.0, 0.0, 0.0, 1.0];
        linear.bias = vec![1.0, 2.0];

        let input = vec![3.0, 4.0];
        let output = linear
            .forward_simd(&input, 1)
            .expect("forward_simd should succeed");

        assert!(
            (output[0] - 4.0).abs() < 1e-4,
            "expected 4.0, got {}",
            output[0]
        ); // 3 + 1
        assert!(
            (output[1] - 6.0).abs() < 1e-4,
            "expected 6.0, got {}",
            output[1]
        ); // 4 + 2
    }

    #[test]
    fn test_linear_forward_simd_batch() {
        let mut linear = LinearWeights::new(2, 2);
        // Simple scaling weights
        linear.weight = vec![2.0, 0.0, 0.0, 3.0];
        linear.bias = vec![0.0, 0.0];

        // Two tokens: [[1,2], [3,4]]
        let input = vec![1.0, 2.0, 3.0, 4.0];
        let output = linear
            .forward_simd(&input, 2)
            .expect("forward_simd should succeed");

        assert_eq!(output.len(), 4);
        assert!((output[0] - 2.0).abs() < 1e-4); // 1*2
        assert!((output[1] - 6.0).abs() < 1e-4); // 2*3
        assert!((output[2] - 6.0).abs() < 1e-4); // 3*2
        assert!((output[3] - 12.0).abs() < 1e-4); // 4*3
    }

    #[test]
    fn test_linear_forward_consistency() {
        // Test that forward and forward_simd produce the same results
        let mut linear = LinearWeights::new(4, 4);
        // Random-ish weights
        for i in 0..16 {
            linear.weight[i] = (i as f32) * 0.1;
        }
        linear.bias = vec![0.1, 0.2, 0.3, 0.4];

        let input = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]; // seq_len=2

        let output_regular = linear.forward(&input, 2).expect("forward should succeed");
        let output_simd = linear
            .forward_simd(&input, 2)
            .expect("forward_simd should succeed");

        assert_eq!(output_regular.len(), output_simd.len());
        for i in 0..output_regular.len() {
            assert!(
                (output_regular[i] - output_simd[i]).abs() < 1e-3,
                "SIMD and regular forward should match at index {}: {} vs {}",
                i,
                output_regular[i],
                output_simd[i]
            );
        }
    }

    #[test]
    fn test_linear_finalize_weights() {
        let mut linear = LinearWeights::new(4, 4);
        for i in 0..16 {
            linear.weight[i] = (i as f32) * 0.1;
        }
        linear.bias = vec![0.1, 0.2, 0.3, 0.4];

        // Before finalization
        assert!(!linear.is_finalized());

        // Finalize weights
        linear.finalize_weights();
        assert!(linear.is_finalized());

        // Run forward_simd which should use cached transpose
        let input = vec![1.0, 2.0, 3.0, 4.0];
        let output_finalized = linear
            .forward_simd(&input, 1)
            .expect("forward_simd should succeed");

        // Compare with unfinalized version (should match)
        linear.invalidate_cache();
        assert!(!linear.is_finalized());

        let output_unfinalized = linear
            .forward_simd(&input, 1)
            .expect("forward_simd should succeed");

        assert_eq!(output_finalized.len(), output_unfinalized.len());
        for i in 0..output_finalized.len() {
            assert!(
                (output_finalized[i] - output_unfinalized[i]).abs() < 1e-6,
                "Finalized and unfinalized should match"
            );
        }
    }

    #[test]
    fn test_attention_finalize_weights() {
        let mut attn = MultiHeadAttention::new(2, 8);

        // Before finalization
        assert!(!attn.is_finalized());

        // Finalize weights
        attn.finalize_weights();
        assert!(attn.is_finalized());

        // All internal linear layers should be finalized
        assert!(attn.w_q().is_finalized());
        assert!(attn.w_k().is_finalized());
        assert!(attn.w_v().is_finalized());
        assert!(attn.w_o().is_finalized());
    }

    #[test]
    fn test_scaled_dot_product_attention_simd_basic() {
        let attn = MultiHeadAttention::new(1, 4);

        // Simple Q, K, V with seq_len=2, d_head=4
        let query = vec![1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0];
        let key = vec![1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0];
        let value = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0];

        let output = attn
            .scaled_dot_product_attention_simd(&query, &key, &value, None)
            .expect("SIMD attention should succeed");

        assert_eq!(output.len(), 8); // seq_len * d_head
    }

    #[test]
    fn test_scaled_dot_product_attention_simd_with_mask() {
        let attn = MultiHeadAttention::new(1, 4);

        let query = vec![1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0];
        let key = vec![1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0];
        let value = vec![1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0];

        // Causal mask
        let mask = vec![0.0, f32::NEG_INFINITY, 0.0, 0.0];

        let output = attn
            .scaled_dot_product_attention_simd(&query, &key, &value, Some(&mask))
            .expect("SIMD attention with mask should succeed");

        assert_eq!(output.len(), 8);
    }

    #[test]
    fn test_attention_simd_error_handling() {
        let attn = MultiHeadAttention::new(2, 8);

        // Query with wrong dimensions
        let query = vec![0.0_f32; 9]; // Not divisible by d_head
        let key = vec![0.0_f32; 8];
        let value = vec![0.0_f32; 8];

        let result = attn.scaled_dot_product_attention_simd(&query, &key, &value, None);
        assert!(result.is_err());
    }

    // =========================================================================
    // Flash Attention Tests
    // =========================================================================

    #[test]
    fn test_flash_attention_basic() {
        // Simple test: seq_len=2, kv_len=2, d_head=4
        let query = vec![1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0];
        let key = vec![1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0];
        let value = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0];

        let config = FlashAttentionConfig::new(2, 2, 4, 2);
        let output = flash_attention(&query, &key, &value, config, None);

        assert_eq!(output.len(), 8);
        // All outputs should be finite
        for &v in &output {
            assert!(v.is_finite(), "Flash attention output should be finite");
        }
    }

    #[test]
    fn test_flash_attention_matches_standard() {
        // Verify Flash Attention produces same results as standard attention
        let attn = MultiHeadAttention::new(1, 4);

        let query = vec![1.0, 0.5, 0.0, 0.0, 0.5, 1.0, 0.0, 0.0];
        let key = vec![1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.5, 0.5, 0.0, 0.0];
        let value = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 2.0, 3.0, 4.0, 5.0];

        // Standard attention
        let standard = attn
            .scaled_dot_product_attention(&query, &key, &value, None)
            .expect("standard attention");

        // Flash attention
        let config = FlashAttentionConfig::new(2, 3, 4, 2);
        let flash = flash_attention(&query, &key, &value, config, None);

        assert_eq!(standard.len(), flash.len());
        for i in 0..standard.len() {
            assert!(
                (standard[i] - flash[i]).abs() < 1e-4,
                "Flash attention should match standard at index {}: {} vs {}",
                i,
                standard[i],
                flash[i]
            );
        }
    }

    #[test]
    fn test_flash_attention_with_mask() {
        // Test with causal mask
        let query = vec![1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0];
        let key = vec![1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0];
        let value = vec![1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0];

        // Causal mask: position 0 can only see position 0
        let mask = vec![0.0, f32::NEG_INFINITY, 0.0, 0.0];

        let config = FlashAttentionConfig::new(2, 2, 4, 2);
        let output = flash_attention(&query, &key, &value, config, Some(&mask));

        assert_eq!(output.len(), 8);
        // First query should only see first value due to mask
        assert!(
            (output[0] - 1.0).abs() < 1e-4,
            "First output[0] should be 1.0"
        );
    }

    #[test]
    fn test_flash_attention_simd_matches_scalar() {
        let query = vec![1.0, 0.5, 0.0, 0.0, 0.5, 1.0, 0.0, 0.0];
        let key = vec![1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0];
        let value = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0];

        let config = FlashAttentionConfig::new(2, 2, 4, 2);
        let scalar = flash_attention(&query, &key, &value, config, None);
        let simd = flash_attention_simd(&query, &key, &value, config, None);

        assert_eq!(scalar.len(), simd.len());
        for i in 0..scalar.len() {
            assert!(
                (scalar[i] - simd[i]).abs() < 1e-5,
                "SIMD flash attention should match scalar at index {}: {} vs {}",
                i,
                scalar[i],
                simd[i]
            );
        }
    }

    #[test]
    fn test_flash_attention_different_block_sizes() {
        let query = vec![1.0; 32]; // seq_len=8, d_head=4
        let key = vec![1.0; 32];
        let value = vec![1.0; 32];

        // Test with different block sizes
        let config_2 = FlashAttentionConfig::new(8, 8, 4, 2);
        let config_4 = FlashAttentionConfig::new(8, 8, 4, 4);
        let config_8 = FlashAttentionConfig::new(8, 8, 4, 8);

        let out_block_2 = flash_attention(&query, &key, &value, config_2, None);
        let out_block_4 = flash_attention(&query, &key, &value, config_4, None);
        let out_block_8 = flash_attention(&query, &key, &value, config_8, None);

        // All should produce the same result
        for i in 0..out_block_2.len() {
            assert!(
                (out_block_2[i] - out_block_4[i]).abs() < 1e-5,
                "Block size 2 vs 4 mismatch at {i}"
            );
            assert!(
                (out_block_4[i] - out_block_8[i]).abs() < 1e-5,
                "Block size 4 vs 8 mismatch at {i}"
            );
        }
    }

    #[test]
    fn test_forward_cross_flash() {
        let attn = MultiHeadAttention::new(2, 8);

        // Simple inputs: seq_len=4, d_model=8
        let x = vec![0.1_f32; 32];
        let context = vec![0.2_f32; 32];

        let output = attn
            .forward_cross_flash(&x, &context, None, FLASH_ATTENTION_BLOCK_SIZE)
            .expect("forward_cross_flash");

        assert_eq!(output.len(), 32); // seq_len * d_model
    }

    #[test]
    #[cfg(feature = "realizar-inference")]
    fn test_forward_cross_flash_v2() {
        let attn = MultiHeadAttention::new(2, 8);

        // Simple inputs: seq_len=4, d_model=8
        let x = vec![0.1_f32; 32];
        let context = vec![0.2_f32; 32];

        let output = attn
            .forward_cross_flash_v2(&x, &context, None)
            .expect("forward_cross_flash_v2");

        assert_eq!(output.len(), 32); // seq_len * d_model

        // Compare with standard flash attention
        let output_v1 = attn
            .forward_cross_flash(&x, &context, None, FLASH_ATTENTION_BLOCK_SIZE)
            .expect("forward_cross_flash");

        // Results should be numerically equivalent (within tolerance)
        for i in 0..output.len() {
            assert!(
                (output[i] - output_v1[i]).abs() < 1e-3,
                "FlashAttention-2 should match custom at index {}: {} vs {}",
                i,
                output[i],
                output_v1[i]
            );
        }
    }

    #[test]
    #[cfg(feature = "realizar-inference")]
    fn test_forward_cross_optimal() {
        let attn = MultiHeadAttention::new(2, 8);

        // Test with short sequence
        let x_short = vec![0.1_f32; 32]; // seq_len=4
        let ctx_short = vec![0.2_f32; 32];

        let output = attn
            .forward_cross_optimal(&x_short, &ctx_short, None)
            .expect("forward_cross_optimal");

        assert_eq!(output.len(), 32);

        // Long sequence should use FlashAttention-2
        let x_long = vec![0.1_f32; 1024 * 8]; // seq_len=1024
        let ctx_long = vec![0.2_f32; 1024 * 8];

        let output_long = attn
            .forward_cross_optimal(&x_long, &ctx_long, None)
            .expect("forward_cross_optimal long");

        assert_eq!(output_long.len(), 1024 * 8);
    }

    #[test]
    fn test_forward_cross_auto() {
        let attn = MultiHeadAttention::new(2, 8);

        // Short sequence (should use standard attention)
        let x_short = vec![0.1_f32; 32]; // seq_len=4
        let ctx_short = vec![0.2_f32; 32];

        let output_short = attn
            .forward_cross_auto(&x_short, &ctx_short, None)
            .expect("forward_cross_auto short");

        assert_eq!(output_short.len(), 32);

        // Both methods should produce similar results for short sequences
        let output_standard = attn
            .forward_cross(&x_short, &ctx_short, None)
            .expect("forward_cross standard");

        for i in 0..output_short.len() {
            assert!(
                (output_short[i] - output_standard[i]).abs() < 1e-3,
                "Auto should match standard for short sequences at {i}"
            );
        }
    }

    #[test]
    fn test_forward_streaming_no_cache() {
        let attn = MultiHeadAttention::new(2, 8);

        // Single token, no cache
        let x = vec![0.1_f32; 8]; // seq_len=1, d_model=8
        let empty_key: Vec<f32> = vec![];
        let empty_value: Vec<f32> = vec![];

        let (output, new_k, new_v) = attn
            .forward_streaming(&x, &empty_key, &empty_value, None)
            .expect("forward_streaming no cache");

        assert_eq!(output.len(), 8);
        assert_eq!(new_k.len(), 8); // Key for caching
        assert_eq!(new_v.len(), 8); // Value for caching
    }

    #[test]
    fn test_forward_streaming_with_cache() {
        let attn = MultiHeadAttention::new(2, 8);

        // First token
        let x1 = vec![0.1_f32; 8];
        let (_, k1, v1) = attn
            .forward_streaming(&x1, &[], &[], None)
            .expect("first streaming call");

        // Second token with cached K/V
        let x2 = vec![0.2_f32; 8];
        let (output2, k2, v2) = attn
            .forward_streaming(&x2, &k1, &v1, None)
            .expect("second streaming call");

        assert_eq!(output2.len(), 8);
        assert_eq!(k2.len(), 8); // New key for this token
        assert_eq!(v2.len(), 8); // New value for this token
    }

    #[test]
    fn test_forward_streaming_multi_token() {
        let attn = MultiHeadAttention::new(2, 8);

        // Multiple tokens at once
        let x = vec![0.1_f32; 24]; // seq_len=3, d_model=8
        let (output, new_k, new_v) = attn
            .forward_streaming(&x, &[], &[], None)
            .expect("forward_streaming multi-token");

        assert_eq!(output.len(), 24);
        assert_eq!(new_k.len(), 24);
        assert_eq!(new_v.len(), 24);
    }

    #[test]
    fn test_forward_self_streaming() {
        let attn = MultiHeadAttention::new(2, 8);

        // Single token
        let x = vec![0.1_f32; 8];
        let (output, new_k, new_v) = attn
            .forward_self_streaming(&x, &[], &[])
            .expect("forward_self_streaming");

        assert_eq!(output.len(), 8);
        assert_eq!(new_k.len(), 8);
        assert_eq!(new_v.len(), 8);
    }

    // =========================================================================
    // Regression Tests for SIMD Optimizations (Sprint 3)
    // =========================================================================

    /// Regression test: Flash attention must match standard attention numerically
    #[test]
    fn regression_flash_attention_accuracy() {
        let attn = MultiHeadAttention::new(1, 64);
        let seq_len = 32;
        let d_head = 64;

        // Generate reproducible test data
        let q: Vec<f32> = (0..seq_len * d_head)
            .map(|i| (i as f32 * 0.1).sin() * 0.5)
            .collect();
        let k: Vec<f32> = (0..seq_len * d_head)
            .map(|i| (i as f32 * 0.2).cos() * 0.5)
            .collect();
        let v: Vec<f32> = (0..seq_len * d_head)
            .map(|i| (i as f32 * 0.3).sin() * 0.5)
            .collect();

        // Standard attention
        let standard = attn
            .scaled_dot_product_attention(&q, &k, &v, None)
            .expect("standard attention");

        // Flash attention scalar
        let config = FlashAttentionConfig::with_default_block_size(seq_len, seq_len, d_head);
        let flash_scalar = flash_attention(&q, &k, &v, config, None);

        // Flash attention SIMD
        let flash_simd = flash_attention_simd(&q, &k, &v, config, None);

        // Verify numerical accuracy (tolerance for floating point)
        let tolerance = 1e-4;
        for i in 0..standard.len() {
            assert!(
                (standard[i] - flash_scalar[i]).abs() < tolerance,
                "Flash scalar mismatch at {i}: {} vs {}",
                standard[i],
                flash_scalar[i]
            );
            assert!(
                (standard[i] - flash_simd[i]).abs() < tolerance,
                "Flash SIMD mismatch at {i}: {} vs {}",
                standard[i],
                flash_simd[i]
            );
        }
    }

    /// Regression test: Streaming attention consistency across cache updates
    #[test]
    fn regression_streaming_attention_consistency() {
        let attn = MultiHeadAttention::new(2, 16);
        let d_model = 16;

        // Simulate incremental decoding: process tokens one at a time
        let mut cached_k = Vec::new();
        let mut cached_v = Vec::new();
        let mut all_outputs = Vec::new();

        // Process 5 tokens incrementally
        for i in 0..5 {
            let x: Vec<f32> = (0..d_model)
                .map(|j| (i * d_model + j) as f32 * 0.01)
                .collect();

            let (output, new_k, new_v) = attn
                .forward_streaming(&x, &cached_k, &cached_v, None)
                .expect("streaming attention");

            // Accumulate cache
            cached_k.extend_from_slice(&new_k);
            cached_v.extend_from_slice(&new_v);
            all_outputs.push(output);
        }

        // Verify output shapes
        for (i, output) in all_outputs.iter().enumerate() {
            assert_eq!(output.len(), d_model, "Token {i} output size mismatch");
        }

        // Verify cache grew correctly
        assert_eq!(cached_k.len(), 5 * d_model);
        assert_eq!(cached_v.len(), 5 * d_model);
    }

    /// Regression test: SIMD functions preserve numerical properties
    #[test]
    fn regression_simd_numerical_stability() {
        use crate::simd;

        // Test with values that could cause numerical issues
        let large_vals: Vec<f32> = (0..256).map(|i| 100.0 + i as f32 * 0.1).collect();
        let small_vals: Vec<f32> = (0..256).map(|i| 1e-6 + i as f32 * 1e-8).collect();
        let mixed_vals: Vec<f32> = (0..256)
            .map(|i| if i % 2 == 0 { 100.0 } else { 0.001 })
            .collect();

        // Softmax stability: large values shouldn't overflow
        let softmax_large = simd::softmax(&large_vals);
        assert!(
            softmax_large.iter().all(|&x| x.is_finite()),
            "Softmax produced non-finite values for large inputs"
        );
        let sum: f32 = softmax_large.iter().sum();
        assert!((sum - 1.0).abs() < 1e-5, "Softmax sum not 1.0: {}", sum);

        // Softmax stability: small values shouldn't underflow to all zeros
        let softmax_small = simd::softmax(&small_vals);
        assert!(
            softmax_small.iter().any(|&x| x > 0.0),
            "Softmax underflowed to all zeros"
        );

        // Dot product: mixed magnitude values
        let dot_mixed = simd::dot(&mixed_vals, &mixed_vals);
        assert!(
            dot_mixed.is_finite(),
            "Dot product produced non-finite value"
        );
    }

    /// Regression test: Block sizes don't affect Flash attention results
    #[test]
    fn regression_flash_attention_block_size_invariance() {
        let seq_len = 64;
        let d_head = 32;

        let q: Vec<f32> = (0..seq_len * d_head)
            .map(|i| (i as f32 * 0.1).sin())
            .collect();
        let k: Vec<f32> = (0..seq_len * d_head)
            .map(|i| (i as f32 * 0.2).cos())
            .collect();
        let v: Vec<f32> = (0..seq_len * d_head)
            .map(|i| (i as f32 * 0.15).sin())
            .collect();

        // Test with different block sizes
        let block_sizes = [8, 16, 32, 64];
        let mut results = Vec::new();

        for &block_size in &block_sizes {
            let config = FlashAttentionConfig::new(seq_len, seq_len, d_head, block_size);
            let output = flash_attention_simd(&q, &k, &v, config, None);
            results.push(output);
        }

        // All results should be numerically equivalent
        let tolerance = 1e-5;
        for i in 1..results.len() {
            for j in 0..results[0].len() {
                assert!(
                    (results[0][j] - results[i][j]).abs() < tolerance,
                    "Block size {} differs from block size {} at position {}: {} vs {}",
                    block_sizes[0],
                    block_sizes[i],
                    j,
                    results[0][j],
                    results[i][j]
                );
            }
        }
    }

    /// Regression test: Multi-head attention output shape consistency
    #[test]
    fn regression_multihead_output_shapes() {
        // Test various configurations
        let configs = [
            (6, 384),  // tiny
            (8, 512),  // base
            (12, 768), // small
        ];

        for (n_heads, d_model) in configs {
            let attn = MultiHeadAttention::new(n_heads, d_model);

            for seq_len in [1, 10, 100] {
                let x = vec![0.1_f32; seq_len * d_model];
                let context = vec![0.2_f32; seq_len * d_model];

                let output = attn
                    .forward_cross(&x, &context, None)
                    .expect("forward_cross");

                assert_eq!(
                    output.len(),
                    seq_len * d_model,
                    "Output shape mismatch for n_heads={}, d_model={}, seq_len={}",
                    n_heads,
                    d_model,
                    seq_len
                );
            }
        }
    }

    // =========================================================================
    // EXTREME TDD: SIMD Dispatch Tests (WAPR-SIMD-001)
    // =========================================================================

    /// EXTREME TDD: Verify forward_cross() and forward_cross_simd() produce identical results
    ///
    /// This test validates that the SIMD-optimized path produces numerically
    /// equivalent results to the scalar baseline within floating-point tolerance.
    #[test]
    fn tdd_simd_dispatch_forward_cross_matches_scalar() {
        let mut attn = MultiHeadAttention::new(6, 384); // whisper-tiny config
        attn.finalize_weights();

        let seq_len = 10;
        let d_model = 384;
        let tolerance = 1e-4;

        // Generate deterministic input
        let x: Vec<f32> = (0..seq_len * d_model)
            .map(|i| (i as f32 * 0.01).sin())
            .collect();
        let context: Vec<f32> = (0..seq_len * d_model)
            .map(|i| (i as f32 * 0.02).cos())
            .collect();

        // Run scalar path
        let scalar_output = attn
            .forward_cross(&x, &context, None)
            .expect("scalar forward_cross");

        // Run SIMD path
        let simd_output = attn
            .forward_cross_simd(&x, &context, None)
            .expect("simd forward_cross");

        // Verify shapes match
        assert_eq!(
            scalar_output.len(),
            simd_output.len(),
            "Output shapes must match"
        );

        // Verify numerical accuracy
        let mut max_diff: f32 = 0.0;
        for (i, (s, simd)) in scalar_output.iter().zip(simd_output.iter()).enumerate() {
            let diff = (s - simd).abs();
            max_diff = max_diff.max(diff);
            assert!(
                diff < tolerance,
                "SIMD mismatch at index {}: scalar={}, simd={}, diff={}",
                i,
                s,
                simd,
                diff
            );
        }
        println!(
            "SIMD accuracy test passed: max_diff={:.2e} (tolerance={:.2e})",
            max_diff, tolerance
        );
    }

    /// EXTREME TDD: Verify forward() uses SIMD dispatch when feature is enabled
    ///
    /// Tests that the unified forward() method dispatches correctly based on
    /// the simd feature flag.
    #[test]
    fn tdd_forward_uses_simd_dispatch() {
        let mut attn = MultiHeadAttention::new(4, 64);
        attn.finalize_weights();

        let seq_len = 5;
        let d_model = 64;
        let tolerance = 1e-4;

        let x: Vec<f32> = (0..seq_len * d_model)
            .map(|i| (i as f32 * 0.1).sin())
            .collect();

        // forward() should use SIMD when feature is enabled
        let forward_output = attn.forward(&x, None).expect("forward");

        // Directly call SIMD version for comparison
        let simd_output = attn
            .forward_cross_simd(&x, &x, None)
            .expect("forward_cross_simd");

        // If simd feature is enabled, outputs should match SIMD version
        if cfg!(feature = "simd") {
            for (i, (f, s)) in forward_output.iter().zip(simd_output.iter()).enumerate() {
                let diff = (f - s).abs();
                assert!(
                    diff < tolerance,
                    "forward() should match forward_cross_simd() when simd enabled. Index {}: {} vs {}, diff={}",
                    i, f, s, diff
                );
            }
            println!("SIMD dispatch verified: forward() uses forward_cross_simd()");
        }
    }

    /// EXTREME TDD: Verify forward_cross dispatches to SIMD when feature enabled
    #[test]
    fn tdd_forward_cross_auto_dispatch() {
        let mut attn = MultiHeadAttention::new(2, 32);
        attn.finalize_weights();

        let seq_len = 4;
        let d_model = 32;

        let x: Vec<f32> = (0..seq_len * d_model).map(|i| i as f32 * 0.01).collect();
        let ctx: Vec<f32> = (0..seq_len * d_model).map(|i| i as f32 * 0.02).collect();

        // Call the auto-dispatching method
        let output = attn
            .forward_cross_dispatch(&x, &ctx, None)
            .expect("forward_cross_dispatch");

        assert_eq!(output.len(), seq_len * d_model);
        println!(
            "Auto-dispatch forward_cross completed with {} elements",
            output.len()
        );
    }

    // =========================================================================
    // Fused QKV Property Tests (pv:fused-qkv-projection-v1)
    // =========================================================================

    /// Helper: create MHA with random fp16 weights for testing fused QKV.
    fn make_fused_mha(d_model: usize) -> MultiHeadAttention {
        let n_heads = if d_model >= 8 { d_model / 64.max(1) } else { 1 };
        let adjusted_d = n_heads * (d_model / n_heads); // ensure divisible
        let n_heads = if adjusted_d == 0 {
            1
        } else {
            adjusted_d / (adjusted_d / n_heads)
        };
        let mut attn = MultiHeadAttention::new(n_heads, d_model);
        // Fill with deterministic pseudo-random fp16 weights
        let n = d_model * d_model;
        let make_f16 = |seed: u32| -> Vec<u16> {
            (0..n)
                .map(|i| {
                    let v = ((i as f32 + seed as f32) * 0.001).sin() * 0.1;
                    half::f16::from_f32(v).to_bits()
                })
                .collect()
        };
        attn.set_query_weight_f16(&make_f16(1));
        attn.set_key_weight_f16(&make_f16(7));
        attn.set_value_weight_f16(&make_f16(13));
        // Set biases
        let make_bias = |seed: u32| -> Vec<f32> {
            (0..d_model)
                .map(|i| ((i as f32 + seed as f32) * 0.01).cos() * 0.05)
                .collect()
        };
        attn.set_query_bias(&make_bias(2));
        attn.set_key_bias(&make_bias(8));
        attn.set_value_bias(&make_bias(14));
        attn.fuse_qkv_weights();
        attn
    }

    #[test]
    fn pv_fused_qkv_equivalence() {
        let d = 384; // Whisper tiny
        let attn = make_fused_mha(d);
        assert!(attn.has_fused_qkv());

        let input: Vec<f32> = (0..d).map(|i| (i as f32 * 0.01).sin()).collect();

        // Separate path
        let mut q_sep = vec![0.0f32; d];
        let mut k_sep = vec![0.0f32; d];
        let mut v_sep = vec![0.0f32; d];
        attn.w_q().forward_simd_into(&input, 1, &mut q_sep).unwrap();
        attn.w_k().forward_simd_into(&input, 1, &mut k_sep).unwrap();
        attn.w_v().forward_simd_into(&input, 1, &mut v_sep).unwrap();

        // Fused path
        let mut qkv = vec![0.0f32; 3 * d];
        attn.forward_qkv_into(&input, &mut qkv).unwrap();

        for i in 0..d {
            let diff_q = (q_sep[i] - qkv[i]).abs();
            let diff_k = (k_sep[i] - qkv[d + i]).abs();
            let diff_v = (v_sep[i] - qkv[2 * d + i]).abs();
            assert!(
                diff_q < 1e-4,
                "q[{i}]: sep={}, fused={}, diff={diff_q}",
                q_sep[i],
                qkv[i]
            );
            assert!(
                diff_k < 1e-4,
                "k[{i}]: sep={}, fused={}, diff={diff_k}",
                k_sep[i],
                qkv[d + i]
            );
            assert!(
                diff_v < 1e-4,
                "v[{i}]: sep={}, fused={}, diff={diff_v}",
                v_sep[i],
                qkv[2 * d + i]
            );
        }
    }

    #[test]
    fn pv_fused_qkv_output_dimension() {
        for d in [64, 384, 512, 768] {
            let n_heads = d / 64;
            let mut attn = MultiHeadAttention::new(n_heads, d);
            let n = d * d;
            let zeros_f16: Vec<u16> = vec![0; n];
            attn.set_query_weight_f16(&zeros_f16);
            attn.set_key_weight_f16(&zeros_f16);
            attn.set_value_weight_f16(&zeros_f16);
            attn.fuse_qkv_weights();

            let input = vec![1.0f32; d];
            let mut qkv = vec![0.0f32; 3 * d];
            attn.forward_qkv_into(&input, &mut qkv).unwrap();
            assert_eq!(qkv.len(), 3 * d, "d_model={d}");
        }
    }

    #[test]
    fn pv_fused_qkv_weight_layout() {
        let d = 64;
        let attn = make_fused_mha(d);
        let w_qkv = attn.w_qkv_f16.as_ref().unwrap();
        let wq = attn.w_q.weight_f16().unwrap();
        let wk = attn.w_k.weight_f16().unwrap();
        let wv = attn.w_v.weight_f16().unwrap();

        let n = d * d;
        assert_eq!(w_qkv.len(), 3 * n);
        assert_eq!(&w_qkv[..n], wq);
        assert_eq!(&w_qkv[n..2 * n], wk);
        assert_eq!(&w_qkv[2 * n..3 * n], wv);
    }

    #[test]
    fn pv_fused_qkv_bias_layout() {
        let d = 64;
        let attn = make_fused_mha(d);
        let b_qkv = attn.b_qkv.as_ref().unwrap();

        assert_eq!(b_qkv.len(), 3 * d);
        assert_eq!(&b_qkv[..d], attn.w_q.bias.as_slice());
        assert_eq!(&b_qkv[d..2 * d], attn.w_k.bias.as_slice());
        assert_eq!(&b_qkv[2 * d..3 * d], attn.w_v.bias.as_slice());
    }

    #[test]
    fn pv_fused_qkv_whisper_dimensions() {
        // Test all Whisper model sizes: tiny=384, base=512, small=768, medium=1024, large=1280
        for d in [384, 512, 768, 1024, 1280] {
            let n_heads = d / 64;
            let attn = make_fused_mha(d);
            assert!(attn.has_fused_qkv(), "d_model={d}");

            let input: Vec<f32> = (0..d).map(|i| (i as f32 * 0.005).sin()).collect();
            let mut qkv = vec![0.0f32; 3 * d];
            attn.forward_qkv_into(&input, &mut qkv).unwrap();

            // Verify each sub-vector has non-trivial values (not all zero)
            let q_sum: f32 = qkv[..d].iter().map(|x| x.abs()).sum();
            let k_sum: f32 = qkv[d..2 * d].iter().map(|x| x.abs()).sum();
            let v_sum: f32 = qkv[2 * d..].iter().map(|x| x.abs()).sum();
            assert!(q_sum > 0.0, "d={d}: Q all zero");
            assert!(k_sum > 0.0, "d={d}: K all zero");
            assert!(v_sum > 0.0, "d={d}: V all zero");
            let _ = n_heads;
        }
    }
}