1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
//! CUDA GPU acceleration for Whisper inference.
//!
//! Provides GPU-resident model execution via trueno-gpu PTX kernels
//! through realizar's `CudaExecutor`.
//!
//! # Architecture
//!
//! The GPU path pre-uploads model weights to VRAM once at initialization,
//! then runs encoder/decoder forward passes entirely on GPU with minimal
//! host synchronization.
//!
//! # APR-Style Tracing (WAPR-PERF-004)
//!
//! Integrates with realizar's `InferenceTracer` for step-level performance
//! visibility per AWS Step Functions event model. Each step (ENCODE, EMBED,
//! TRANSFORMER_BLOCK, LM_HEAD, SAMPLE, DECODE) emits TaskStateEntered/Exited
//! events with TensorStats for anomaly detection (Jidoka).
//!
//! # Usage
//!
//! ```rust,ignore
//! use whisper_apr::cuda::WhisperCuda;
//! use realizar::inference_trace::{TraceConfig, InferenceTracer};
//!
//! // Create GPU-accelerated model with tracing
//! let model = WhisperModel::load("tiny.apr")?;
//! let mut cuda_model = WhisperCuda::new(model, 0)?;
//! cuda_model.enable_tracing(TraceConfig::enabled());
//!
//! // Run inference on GPU - trace events collected automatically
//! let result = cuda_model.transcribe_gpu(&audio, options)?;
//!
//! // Analyze trace for bottlenecks
//! for event in cuda_model.tracer().events() {
//! println!("{}: {:?} took {}µs", event.step.name(), event.stats, event.duration_us);
//! }
//! ```
use crate::audio::{MelConfig, MelFilterbank, SAMPLE_RATE};
use crate::error::{WhisperError, WhisperResult};
use crate::model::{Decoder, DecoderKVCache, Encoder, ModelConfig};
use crate::tokenizer::BpeTokenizer;
use crate::{DecodingStrategy, Task, TranscribeOptions, TranscriptionResult};
use realizar::cuda::CudaExecutor;
use realizar::inference_trace::{InferenceTracer, ModelInfo, TraceConfig, TraceStep};
// GPU-Resident Tensor imports (WAPR-PERF-004)
#[cfg(feature = "cuda")]
#[allow(unused_imports)] // total_d2h_transfers, total_h2d_transfers used only in tests
use trueno_gpu::memory::resident::{
batched_multihead_attention,
forward_encoder_block_gpu,
incremental_attention_gpu,
incremental_attention_gpu_with_stream, // WAPR-PERF-014: shared stream variant
kernel_cache_hits,
kernel_cache_misses,
kv_cache_scatter_gpu,
reset_transfer_counters,
total_d2h_transfers,
total_h2d_transfers,
GpuConvFrontendWeights,
GpuDecoderBlockWeights,
GpuDecoderConfig,
GpuEncoderBlockWeights,
GpuEncoderConfig,
GpuKvCache,
GpuResidentTensor,
TransferStats,
};
/// GELU activation function.
#[inline]
fn gelu(x: f32) -> f32 {
0.5 * x * (1.0 + ((2.0_f32 / std::f32::consts::PI).sqrt() * (x + 0.044715 * x * x * x)).tanh())
}
/// GPU-accelerated Whisper model.
///
/// Wraps encoder and decoder with CUDA execution capability.
/// Model weights are pre-uploaded to GPU memory for minimal latency.
///
/// # APR-Style Tracing (WAPR-PERF-004)
///
/// Integrates with realizar's `InferenceTracer` for performance visibility:
/// - Enable tracing: `cuda_model.enable_tracing(TraceConfig::enabled())`
/// - Access events: `cuda_model.tracer().events()`
/// - Print summary: `cuda_model.tracer().print_summary()`
pub struct WhisperCuda {
/// Whisper encoder (audio → hidden states)
encoder: Encoder,
/// Whisper decoder (hidden states → tokens)
decoder: Decoder,
/// CUDA executor for GPU kernel dispatch
executor: CudaExecutor,
/// Model configuration
config: ModelConfig,
/// BPE tokenizer
tokenizer: BpeTokenizer,
/// Mel filterbank
mel_filters: MelFilterbank,
/// GPU device name (e.g., "NVIDIA GeForce RTX 4090")
device_name: String,
/// GPU memory info (free_bytes, total_bytes)
memory_info: (usize, usize),
/// Whether GPU weights have been uploaded
weights_uploaded: bool,
/// Whether GPU KV caches are initialized
kv_cache_initialized: bool,
/// Inference tracer for APR-style step-level visibility (realizar::InferenceTracer)
tracer: InferenceTracer,
/// GPU-resident encoder block weights (WAPR-PERF-004: Total Offload)
#[cfg(feature = "cuda")]
gpu_encoder_weights: Option<Vec<GpuEncoderBlockWeights>>,
/// GPU encoder configuration
#[cfg(feature = "cuda")]
gpu_encoder_config: Option<GpuEncoderConfig>,
/// WAPR-PERF-012: GPU-resident conv frontend weights
#[cfg(feature = "cuda")]
gpu_conv_weights: Option<GpuConvFrontendWeights>,
/// WAPR-PERF-013: GPU-resident decoder block weights
#[cfg(feature = "cuda")]
gpu_decoder_weights: Option<Vec<GpuDecoderBlockWeights>>,
/// WAPR-PERF-013: GPU decoder configuration
#[cfg(feature = "cuda")]
gpu_decoder_config: Option<GpuDecoderConfig>,
/// WAPR-PERF-013: GPU-resident KV caches (self-attention per layer)
#[cfg(feature = "cuda")]
gpu_self_kv_cache: Option<Vec<GpuKvCache>>,
/// WAPR-PERF-013: GPU-resident cross-attention KV cache (encoder K/V, per layer)
#[cfg(feature = "cuda")]
gpu_cross_kv_cache: Option<Vec<GpuKvCache>>,
/// WAPR-PERF-013: Head-first self-attention K cache [n_heads, max_seq_len, head_dim]
#[cfg(feature = "cuda")]
gpu_self_k_head_first: Option<Vec<GpuResidentTensor<f32>>>,
/// WAPR-PERF-013: Head-first self-attention V cache [n_heads, max_seq_len, head_dim]
#[cfg(feature = "cuda")]
gpu_self_v_head_first: Option<Vec<GpuResidentTensor<f32>>>,
/// WAPR-PERF-013: Head-first cross-attention K cache [n_heads, enc_seq_len, head_dim]
#[cfg(feature = "cuda")]
gpu_cross_k_head_first: Option<Vec<GpuResidentTensor<f32>>>,
/// WAPR-PERF-013: Head-first cross-attention V cache [n_heads, enc_seq_len, head_dim]
#[cfg(feature = "cuda")]
gpu_cross_v_head_first: Option<Vec<GpuResidentTensor<f32>>>,
/// WAPR-PERF-013: Current sequence position for decoder
#[cfg(feature = "cuda")]
gpu_decoder_pos: usize,
/// WAPR-PERF-019: GPU-resident encoder post-norm gamma (final layer norm)
#[cfg(feature = "cuda")]
gpu_enc_ln_post_gamma: Option<GpuResidentTensor<f32>>,
/// WAPR-PERF-019: GPU-resident encoder post-norm beta (final layer norm)
#[cfg(feature = "cuda")]
gpu_enc_ln_post_beta: Option<GpuResidentTensor<f32>>,
}
impl WhisperCuda {
/// Create a new CUDA-accelerated Whisper model.
///
/// # Arguments
///
/// * `encoder` - Pre-loaded Whisper encoder
/// * `decoder` - Pre-loaded Whisper decoder
/// * `config` - Model configuration
/// * `device_ordinal` - GPU device index (0 for first GPU)
///
/// # Errors
///
/// Returns error if CUDA is not available or device doesn't exist.
pub fn new(
encoder: Encoder,
decoder: Decoder,
config: ModelConfig,
device_ordinal: i32,
) -> WhisperResult<Self> {
Self::new_with_tokenizer(
encoder,
decoder,
config,
BpeTokenizer::with_base_tokens(),
device_ordinal,
)
}
/// Create a new CUDA-accelerated Whisper model with a pre-loaded tokenizer.
///
/// This is the preferred constructor when converting from WhisperApr, as it
/// preserves the full vocabulary from the APR file.
///
/// # Arguments
///
/// * `encoder` - Pre-loaded Whisper encoder
/// * `decoder` - Pre-loaded Whisper decoder
/// * `config` - Model configuration
/// * `tokenizer` - Pre-loaded BPE tokenizer with full vocabulary
/// * `device_ordinal` - GPU device index (0 for first GPU)
///
/// # Errors
///
/// Returns error if CUDA is not available or device doesn't exist.
pub fn new_with_tokenizer(
encoder: Encoder,
decoder: Decoder,
config: ModelConfig,
tokenizer: BpeTokenizer,
device_ordinal: i32,
) -> WhisperResult<Self> {
let mel_filters = MelFilterbank::new(&MelConfig {
n_mels: config.n_mels as usize,
..MelConfig::whisper()
});
Self::new_with_components(
encoder,
decoder,
config,
tokenizer,
mel_filters,
device_ordinal,
)
}
/// Create a new CUDA-accelerated Whisper model with all components.
///
/// This is the preferred constructor when converting from WhisperApr, as it
/// preserves all components including the mel filterbank loaded from APR.
///
/// # Arguments
///
/// * `encoder` - Pre-loaded Whisper encoder
/// * `decoder` - Pre-loaded Whisper decoder
/// * `config` - Model configuration
/// * `tokenizer` - Pre-loaded BPE tokenizer with full vocabulary
/// * `mel_filters` - Pre-loaded mel filterbank
/// * `device_ordinal` - GPU device index (0 for first GPU)
///
/// # Errors
///
/// Returns error if CUDA is not available or device doesn't exist.
pub fn new_with_components(
encoder: Encoder,
decoder: Decoder,
config: ModelConfig,
tokenizer: BpeTokenizer,
mel_filters: MelFilterbank,
device_ordinal: i32,
) -> WhisperResult<Self> {
if !CudaExecutor::is_available() {
return Err(WhisperError::Inference(
"CUDA not available. Install CUDA drivers or use CPU backend.".into(),
));
}
let executor = CudaExecutor::new(device_ordinal)
.map_err(|e| WhisperError::Inference(format!("CUDA initialization failed: {e}")))?;
let device_name = executor
.device_name()
.unwrap_or_else(|_| "Unknown GPU".into());
let memory_info = executor.memory_info().unwrap_or((0, 0));
// Initialize tracer with model info (disabled by default for zero overhead)
let mut tracer = InferenceTracer::disabled();
tracer.set_model_info(ModelInfo {
name: device_name.clone(),
num_layers: config.n_text_layer as usize,
hidden_dim: config.n_text_state as usize,
vocab_size: config.n_vocab as usize,
num_heads: config.n_text_head as usize,
quant_type: None, // f32 for now
});
let mut model = Self {
encoder,
decoder,
executor,
config,
tokenizer,
mel_filters,
device_name,
memory_info,
weights_uploaded: false,
kv_cache_initialized: false,
tracer,
#[cfg(feature = "cuda")]
gpu_encoder_weights: None,
#[cfg(feature = "cuda")]
gpu_encoder_config: None,
#[cfg(feature = "cuda")]
gpu_conv_weights: None,
#[cfg(feature = "cuda")]
gpu_decoder_weights: None,
#[cfg(feature = "cuda")]
gpu_decoder_config: None,
#[cfg(feature = "cuda")]
gpu_self_kv_cache: None,
#[cfg(feature = "cuda")]
gpu_cross_kv_cache: None,
#[cfg(feature = "cuda")]
gpu_self_k_head_first: None,
#[cfg(feature = "cuda")]
gpu_self_v_head_first: None,
#[cfg(feature = "cuda")]
gpu_cross_k_head_first: None,
#[cfg(feature = "cuda")]
gpu_cross_v_head_first: None,
#[cfg(feature = "cuda")]
gpu_decoder_pos: 0,
#[cfg(feature = "cuda")]
gpu_enc_ln_post_gamma: None,
#[cfg(feature = "cuda")]
gpu_enc_ln_post_beta: None,
};
// Initialize GPU KV caches for decoder self-attention
model.init_gpu_kv_cache()?;
Ok(model)
}
/// Enable APR-style inference tracing (realizar::InferenceTracer).
///
/// When enabled, trace events are collected for each inference step:
/// - ENCODE: Audio preprocessing and encoder forward pass
/// - EMBED: Token embedding lookup
/// - TRANSFORMER_BLOCK: Each decoder layer (×n_layers per token)
/// - LM_HEAD: Output projection to vocabulary
/// - SAMPLE: Token sampling (argmax/beam/top-k)
/// - DECODE: Token detokenization
///
/// # Performance Note
///
/// Tracing adds ~1-5% overhead when enabled. Use `TraceConfig::enabled()`
/// for full visibility or configure specific steps to trace.
///
/// # Example
///
/// ```rust,ignore
/// use realizar::inference_trace::TraceConfig;
/// cuda_model.enable_tracing(TraceConfig::enabled());
/// ```
pub fn enable_tracing(&mut self, config: TraceConfig) {
self.tracer = InferenceTracer::new(config);
self.tracer.set_model_info(ModelInfo {
name: self.device_name.clone(),
num_layers: self.config.n_text_layer as usize,
hidden_dim: self.config.n_text_state as usize,
vocab_size: self.config.n_vocab as usize,
num_heads: self.config.n_text_head as usize,
quant_type: None,
});
}
/// Get reference to the inference tracer.
///
/// Use this to access collected trace events for analysis.
pub fn tracer(&self) -> &InferenceTracer {
&self.tracer
}
/// Get mutable reference to the inference tracer.
pub fn tracer_mut(&mut self) -> &mut InferenceTracer {
&mut self.tracer
}
/// Reset tracer for a new inference run.
pub fn reset_tracer(&mut self) {
let config = if self.tracer.is_enabled() {
TraceConfig::enabled()
} else {
TraceConfig::default()
};
self.tracer = InferenceTracer::new(config);
self.tracer.set_model_info(ModelInfo {
name: self.device_name.clone(),
num_layers: self.config.n_text_layer as usize,
hidden_dim: self.config.n_text_state as usize,
vocab_size: self.config.n_vocab as usize,
num_heads: self.config.n_text_head as usize,
quant_type: None,
});
}
/// Get GPU device name.
pub fn device_name(&self) -> &str {
&self.device_name
}
/// Get GPU memory info (free_bytes, total_bytes).
pub fn memory_info(&self) -> (usize, usize) {
self.memory_info
}
/// Check if model weights are uploaded to GPU.
pub fn weights_uploaded(&self) -> bool {
self.weights_uploaded
}
/// Get model configuration.
pub fn config(&self) -> &ModelConfig {
&self.config
}
/// Upload model weights to GPU memory.
///
/// This is called automatically on first inference if not done explicitly.
/// Pre-uploading weights avoids latency on first transcription.
///
/// Uploads:
/// - Output projection (token embedding): 51865 × 384 ≈ 80MB
/// - All decoder block weights (attention + FFN): ~90MB for tiny
///
/// # Returns
///
/// Number of bytes uploaded to GPU.
pub fn upload_weights(&mut self) -> WhisperResult<usize> {
if self.weights_uploaded {
return Ok(0);
}
let mut total_bytes = 0_usize;
// Upload the most expensive weight: token embedding for output projection
// This is [n_vocab × d_model] = [51865 × 384] ≈ 80MB for tiny model
// WAPR-PERF-014 FIX: GEMV kernel expects [K × N] but token_emb is [N × K]
// Must transpose from [n_vocab × d_model] to [d_model × n_vocab]
let token_emb = self.decoder.token_embedding();
let n_vocab = self.config.n_vocab as usize;
let d_model = self.config.n_text_state as usize;
let mut token_emb_transposed = vec![0.0f32; n_vocab * d_model];
for row in 0..n_vocab {
for col in 0..d_model {
// Source: [row, col] = row * d_model + col
// Dest: [col, row] = col * n_vocab + row
token_emb_transposed[col * n_vocab + row] = token_emb[row * d_model + col];
}
}
let bytes = self
.executor
.load_weights("whisper_output_proj", &token_emb_transposed)
.map_err(|e| {
WhisperError::Inference(format!("Failed to upload output projection: {e}"))
})?;
total_bytes += bytes;
// Upload all decoder block weights for full GPU acceleration
// Each block has: self_attn (Q,K,V,O), cross_attn (Q,K,V,O), ffn (fc1, fc2)
for (block_idx, block) in self.decoder.blocks().iter().enumerate() {
// Self-attention weights
let name = format!("dec_b{block_idx}_self_q");
let bytes = self
.executor
.load_weights(&name, &block.self_attn.w_q().weight)
.map_err(|e| WhisperError::Inference(format!("Failed to upload {name}: {e}")))?;
total_bytes += bytes;
let name = format!("dec_b{block_idx}_self_k");
let bytes = self
.executor
.load_weights(&name, &block.self_attn.w_k().weight)
.map_err(|e| WhisperError::Inference(format!("Failed to upload {name}: {e}")))?;
total_bytes += bytes;
let name = format!("dec_b{block_idx}_self_v");
let bytes = self
.executor
.load_weights(&name, &block.self_attn.w_v().weight)
.map_err(|e| WhisperError::Inference(format!("Failed to upload {name}: {e}")))?;
total_bytes += bytes;
let name = format!("dec_b{block_idx}_self_o");
let bytes = self
.executor
.load_weights(&name, &block.self_attn.w_o().weight)
.map_err(|e| WhisperError::Inference(format!("Failed to upload {name}: {e}")))?;
total_bytes += bytes;
// Cross-attention weights
let name = format!("dec_b{block_idx}_cross_q");
let bytes = self
.executor
.load_weights(&name, &block.cross_attn.w_q().weight)
.map_err(|e| WhisperError::Inference(format!("Failed to upload {name}: {e}")))?;
total_bytes += bytes;
let name = format!("dec_b{block_idx}_cross_k");
let bytes = self
.executor
.load_weights(&name, &block.cross_attn.w_k().weight)
.map_err(|e| WhisperError::Inference(format!("Failed to upload {name}: {e}")))?;
total_bytes += bytes;
let name = format!("dec_b{block_idx}_cross_v");
let bytes = self
.executor
.load_weights(&name, &block.cross_attn.w_v().weight)
.map_err(|e| WhisperError::Inference(format!("Failed to upload {name}: {e}")))?;
total_bytes += bytes;
let name = format!("dec_b{block_idx}_cross_o");
let bytes = self
.executor
.load_weights(&name, &block.cross_attn.w_o().weight)
.map_err(|e| WhisperError::Inference(format!("Failed to upload {name}: {e}")))?;
total_bytes += bytes;
// FFN weights
let name = format!("dec_b{block_idx}_ffn_fc1");
let bytes = self
.executor
.load_weights(&name, &block.ffn.fc1.weight)
.map_err(|e| WhisperError::Inference(format!("Failed to upload {name}: {e}")))?;
total_bytes += bytes;
let name = format!("dec_b{block_idx}_ffn_fc2");
let bytes = self
.executor
.load_weights(&name, &block.ffn.fc2.weight)
.map_err(|e| WhisperError::Inference(format!("Failed to upload {name}: {e}")))?;
total_bytes += bytes;
}
self.weights_uploaded = true;
Ok(total_bytes)
}
/// Upload encoder weights to GPU memory.
///
/// # WAPR-PERF-005: GPU Encoder
///
/// Uploads encoder weights for GPU-accelerated encoding:
/// - Conv1/Conv2 frontend weights
/// - Encoder block attention weights (Q, K, V, O per layer)
/// - Encoder block FFN weights (fc1, fc2 per layer)
pub fn upload_encoder_weights(&mut self) -> WhisperResult<usize> {
let mut total_bytes = 0_usize;
// Upload encoder block weights
for (block_idx, block) in self.encoder.blocks().iter().enumerate() {
// Self-attention weights
let name = format!("enc_b{block_idx}_self_q");
let bytes = self
.executor
.load_weights(&name, &block.self_attn.w_q().weight)
.map_err(|e| WhisperError::Inference(format!("Failed to upload {name}: {e}")))?;
total_bytes += bytes;
let name = format!("enc_b{block_idx}_self_k");
let bytes = self
.executor
.load_weights(&name, &block.self_attn.w_k().weight)
.map_err(|e| WhisperError::Inference(format!("Failed to upload {name}: {e}")))?;
total_bytes += bytes;
let name = format!("enc_b{block_idx}_self_v");
let bytes = self
.executor
.load_weights(&name, &block.self_attn.w_v().weight)
.map_err(|e| WhisperError::Inference(format!("Failed to upload {name}: {e}")))?;
total_bytes += bytes;
let name = format!("enc_b{block_idx}_self_o");
let bytes = self
.executor
.load_weights(&name, &block.self_attn.w_o().weight)
.map_err(|e| WhisperError::Inference(format!("Failed to upload {name}: {e}")))?;
total_bytes += bytes;
// FFN weights
let name = format!("enc_b{block_idx}_ffn_fc1");
let bytes = self
.executor
.load_weights(&name, &block.ffn.fc1.weight)
.map_err(|e| WhisperError::Inference(format!("Failed to upload {name}: {e}")))?;
total_bytes += bytes;
let name = format!("enc_b{block_idx}_ffn_fc2");
let bytes = self
.executor
.load_weights(&name, &block.ffn.fc2.weight)
.map_err(|e| WhisperError::Inference(format!("Failed to upload {name}: {e}")))?;
total_bytes += bytes;
}
Ok(total_bytes)
}
/// GPU-accelerated encoder forward pass.
///
/// # WAPR-PERF-005: 20x Speedup Target
///
/// Current: 6.15s on CPU (98.7% of total time)
/// Target: <300ms on GPU (matching whisper.cpp)
///
/// Uses `flash_attention_multi_head` for encoder self-attention.
pub fn encode_gpu(&mut self, mel: &[f32]) -> WhisperResult<Vec<f32>> {
let _n_mels = self.config.n_mels as usize;
let d_model = self.config.n_audio_state as usize;
let n_heads = self.config.n_audio_head as usize;
let head_dim = d_model / n_heads;
let n_layers = self.config.n_audio_layer as usize;
// Step 1: Convolutional frontend (CPU - small compared to attention)
let conv_frontend = self.encoder.conv_frontend().ok_or_else(|| {
WhisperError::Inference("no conv frontend (Whisper models require it)".into())
})?;
let conv_output = conv_frontend.forward(mel)?;
let seq_len = conv_output.len() / d_model;
// Step 2: Add positional embedding
let mut x = conv_output;
let pos_emb = self.encoder.positional_embedding();
for pos in 0..seq_len {
for d in 0..d_model {
x[pos * d_model + d] += pos_emb[pos * d_model + d];
}
}
// Step 3: Process encoder blocks with GPU attention
for layer_idx in 0..n_layers {
x = self.forward_encoder_block_gpu(layer_idx, &x, seq_len, n_heads, head_dim)?;
}
// Step 4: Final layer norm (CPU)
self.encoder.ln_post().forward(&x)
}
/// Upload all encoder weights to GPU for Total Offload (WAPR-PERF-004)
///
/// Pre-uploads all encoder block weights once at initialization.
/// Subsequent encode calls use GPU-resident weights with zero transfer overhead.
///
/// IMPORTANT: Weight matrices are transposed before upload because:
/// - CPU stores weights as [out_features, in_features] for y = x @ W.T + b
/// - GPU linear expects [in_features, out_features] for y = x @ W + b
#[cfg(feature = "cuda")]
pub fn upload_encoder_weights_to_gpu(&mut self) -> WhisperResult<()> {
if self.gpu_encoder_weights.is_some() {
return Ok(()); // Already uploaded
}
// Helper to transpose weight matrix from [rows, cols] to [cols, rows]
fn transpose_weights(weights: &[f32], rows: usize, cols: usize) -> Vec<f32> {
let mut transposed = vec![0.0_f32; weights.len()];
for r in 0..rows {
for c in 0..cols {
transposed[c * rows + r] = weights[r * cols + c];
}
}
transposed
}
let ctx = self.executor.context();
let d_model = self.config.n_audio_state as usize;
let n_heads = self.config.n_audio_head as usize;
let n_layers = self.config.n_audio_layer as usize;
let d_ff = d_model * 4; // Standard 4x expansion
let mut gpu_weights = Vec::with_capacity(n_layers);
for layer_idx in 0..n_layers {
let block = &self.encoder.blocks()[layer_idx];
// Upload LayerNorm 1 weights (no transpose needed for 1D vectors)
let ln1_gamma = GpuResidentTensor::from_host(ctx, &block.ln1.weight)
.map_err(|e| WhisperError::Inference(format!("ln1_gamma upload: {e}")))?;
let ln1_beta = GpuResidentTensor::from_host(ctx, &block.ln1.bias)
.map_err(|e| WhisperError::Inference(format!("ln1_beta upload: {e}")))?;
// Upload Q/K/V/O projection weights (TRANSPOSED from [out, in] to [in, out])
// CPU: [d_model, d_model] as [out_features, in_features]
// GPU: [d_model, d_model] as [in_features, out_features]
let w_q_t = transpose_weights(&block.self_attn.w_q().weight, d_model, d_model);
let w_q = GpuResidentTensor::from_host(ctx, &w_q_t)
.map_err(|e| WhisperError::Inference(format!("w_q upload: {e}")))?;
let b_q = GpuResidentTensor::from_host(ctx, &block.self_attn.w_q().bias)
.map_err(|e| WhisperError::Inference(format!("b_q upload: {e}")))?;
let w_k_t = transpose_weights(&block.self_attn.w_k().weight, d_model, d_model);
let w_k = GpuResidentTensor::from_host(ctx, &w_k_t)
.map_err(|e| WhisperError::Inference(format!("w_k upload: {e}")))?;
let b_k = GpuResidentTensor::from_host(ctx, &block.self_attn.w_k().bias)
.map_err(|e| WhisperError::Inference(format!("b_k upload: {e}")))?;
let w_v_t = transpose_weights(&block.self_attn.w_v().weight, d_model, d_model);
let w_v = GpuResidentTensor::from_host(ctx, &w_v_t)
.map_err(|e| WhisperError::Inference(format!("w_v upload: {e}")))?;
let b_v = GpuResidentTensor::from_host(ctx, &block.self_attn.w_v().bias)
.map_err(|e| WhisperError::Inference(format!("b_v upload: {e}")))?;
let w_o_t = transpose_weights(&block.self_attn.w_o().weight, d_model, d_model);
let w_o = GpuResidentTensor::from_host(ctx, &w_o_t)
.map_err(|e| WhisperError::Inference(format!("w_o upload: {e}")))?;
let b_o = GpuResidentTensor::from_host(ctx, &block.self_attn.w_o().bias)
.map_err(|e| WhisperError::Inference(format!("b_o upload: {e}")))?;
// Upload LayerNorm 2 weights (no transpose needed for 1D vectors)
let ln2_gamma = GpuResidentTensor::from_host(ctx, &block.ln2.weight)
.map_err(|e| WhisperError::Inference(format!("ln2_gamma upload: {e}")))?;
let ln2_beta = GpuResidentTensor::from_host(ctx, &block.ln2.bias)
.map_err(|e| WhisperError::Inference(format!("ln2_beta upload: {e}")))?;
// Upload FFN weights (TRANSPOSED)
// FFN up: [d_ff, d_model] -> transposed to [d_model, d_ff]
let ffn_up_t = transpose_weights(&block.ffn.fc1.weight, d_ff, d_model);
let ffn_up_w = GpuResidentTensor::from_host(ctx, &ffn_up_t)
.map_err(|e| WhisperError::Inference(format!("ffn_up_w upload: {e}")))?;
let ffn_up_b = GpuResidentTensor::from_host(ctx, &block.ffn.fc1.bias)
.map_err(|e| WhisperError::Inference(format!("ffn_up_b upload: {e}")))?;
// FFN down: [d_model, d_ff] -> transposed to [d_ff, d_model]
let ffn_down_t = transpose_weights(&block.ffn.fc2.weight, d_model, d_ff);
let ffn_down_w = GpuResidentTensor::from_host(ctx, &ffn_down_t)
.map_err(|e| WhisperError::Inference(format!("ffn_down_w upload: {e}")))?;
let ffn_down_b = GpuResidentTensor::from_host(ctx, &block.ffn.fc2.bias)
.map_err(|e| WhisperError::Inference(format!("ffn_down_b upload: {e}")))?;
gpu_weights.push(GpuEncoderBlockWeights {
ln1_gamma,
ln1_beta,
w_q,
b_q,
w_k,
b_k,
w_v,
b_v,
w_o,
b_o,
ln2_gamma,
ln2_beta,
ffn_up_w,
ffn_up_b,
ffn_down_w,
ffn_down_b,
});
}
self.gpu_encoder_weights = Some(gpu_weights);
self.gpu_encoder_config = Some(GpuEncoderConfig {
d_model: d_model as u32,
n_heads: n_heads as u32,
ffn_dim: d_ff as u32,
});
// WAPR-PERF-019: Upload encoder post-norm weights to eliminate D2H→CPU→H2D round-trip
let ln_post = self.encoder.ln_post();
self.gpu_enc_ln_post_gamma = Some(
GpuResidentTensor::from_host(ctx, &ln_post.weight)
.map_err(|e| WhisperError::Inference(format!("enc ln_post_gamma upload: {e}")))?,
);
self.gpu_enc_ln_post_beta = Some(
GpuResidentTensor::from_host(ctx, &ln_post.bias)
.map_err(|e| WhisperError::Inference(format!("enc ln_post_beta upload: {e}")))?,
);
Ok(())
}
/// WAPR-PERF-012: Upload convolutional frontend weights to GPU
///
/// Uploads conv1/conv2 weights and biases for GPU-accelerated audio processing.
/// Target: Move 588ms CPU conv to GPU (<50ms).
#[cfg(feature = "cuda")]
pub fn upload_conv_weights_to_gpu(&mut self) -> WhisperResult<()> {
if self.gpu_conv_weights.is_some() {
return Ok(()); // Already uploaded
}
let ctx = self.executor.context();
let conv_frontend = self
.encoder
.conv_frontend()
.ok_or_else(|| WhisperError::Inference("no conv frontend for GPU upload".into()))?;
// Upload conv1 weights [out_channels, in_channels, kernel_size]
let conv1_weight = GpuResidentTensor::from_host(ctx, &conv_frontend.conv1.weight)
.map_err(|e| WhisperError::Inference(format!("conv1_weight upload: {e}")))?;
let conv1_bias = GpuResidentTensor::from_host(ctx, &conv_frontend.conv1.bias)
.map_err(|e| WhisperError::Inference(format!("conv1_bias upload: {e}")))?;
// Upload conv2 weights [out_channels, in_channels, kernel_size]
let conv2_weight = GpuResidentTensor::from_host(ctx, &conv_frontend.conv2.weight)
.map_err(|e| WhisperError::Inference(format!("conv2_weight upload: {e}")))?;
let conv2_bias = GpuResidentTensor::from_host(ctx, &conv_frontend.conv2.bias)
.map_err(|e| WhisperError::Inference(format!("conv2_bias upload: {e}")))?;
self.gpu_conv_weights = Some(GpuConvFrontendWeights {
conv1_weight,
conv1_bias,
conv2_weight,
conv2_bias,
});
Ok(())
}
/// WAPR-PERF-013: Upload decoder block weights to GPU
///
/// Uploads all decoder weights for full GPU residence:
/// - Self-attention: LN1, Q/K/V/O projections
/// - Cross-attention: LN2, Q/K/V/O projections
/// - FFN: LN3, FC1, FC2
#[cfg(feature = "cuda")]
pub fn upload_decoder_weights_to_gpu(&mut self) -> WhisperResult<()> {
if self.gpu_decoder_weights.is_some() {
return Ok(()); // Already uploaded
}
// Helper to transpose weight matrix from [rows, cols] to [cols, rows]
fn transpose_weights(weights: &[f32], rows: usize, cols: usize) -> Vec<f32> {
let mut transposed = vec![0.0_f32; weights.len()];
for r in 0..rows {
for c in 0..cols {
transposed[c * rows + r] = weights[r * cols + c];
}
}
transposed
}
let ctx = self.executor.context();
let d_model = self.config.n_text_state as usize;
let n_heads = self.config.n_text_head as usize;
let n_layers = self.config.n_text_layer as usize;
let d_ff = d_model * 4; // Standard 4x expansion
let max_seq_len = self.config.n_text_ctx as usize;
let mut gpu_weights = Vec::with_capacity(n_layers);
for layer_idx in 0..n_layers {
let block = &self.decoder.blocks()[layer_idx];
// Self-Attention weights
let ln1_gamma = GpuResidentTensor::from_host(ctx, &block.ln1.weight)
.map_err(|e| WhisperError::Inference(format!("dec ln1_gamma L{layer_idx}: {e}")))?;
let ln1_beta = GpuResidentTensor::from_host(ctx, &block.ln1.bias)
.map_err(|e| WhisperError::Inference(format!("dec ln1_beta L{layer_idx}: {e}")))?;
// Self-attention Q/K/V/O (transposed for GPU linear: [in, out])
let self_w_q_t = transpose_weights(&block.self_attn.w_q().weight, d_model, d_model);
let self_w_q = GpuResidentTensor::from_host(ctx, &self_w_q_t)
.map_err(|e| WhisperError::Inference(format!("dec self_w_q L{layer_idx}: {e}")))?;
let self_b_q = GpuResidentTensor::from_host(ctx, &block.self_attn.w_q().bias)
.map_err(|e| WhisperError::Inference(format!("dec self_b_q L{layer_idx}: {e}")))?;
let self_w_k_t = transpose_weights(&block.self_attn.w_k().weight, d_model, d_model);
let self_w_k = GpuResidentTensor::from_host(ctx, &self_w_k_t)
.map_err(|e| WhisperError::Inference(format!("dec self_w_k L{layer_idx}: {e}")))?;
let self_b_k = GpuResidentTensor::from_host(ctx, &block.self_attn.w_k().bias)
.map_err(|e| WhisperError::Inference(format!("dec self_b_k L{layer_idx}: {e}")))?;
let self_w_v_t = transpose_weights(&block.self_attn.w_v().weight, d_model, d_model);
let self_w_v = GpuResidentTensor::from_host(ctx, &self_w_v_t)
.map_err(|e| WhisperError::Inference(format!("dec self_w_v L{layer_idx}: {e}")))?;
let self_b_v = GpuResidentTensor::from_host(ctx, &block.self_attn.w_v().bias)
.map_err(|e| WhisperError::Inference(format!("dec self_b_v L{layer_idx}: {e}")))?;
let self_w_o_t = transpose_weights(&block.self_attn.w_o().weight, d_model, d_model);
let self_w_o = GpuResidentTensor::from_host(ctx, &self_w_o_t)
.map_err(|e| WhisperError::Inference(format!("dec self_w_o L{layer_idx}: {e}")))?;
let self_b_o = GpuResidentTensor::from_host(ctx, &block.self_attn.w_o().bias)
.map_err(|e| WhisperError::Inference(format!("dec self_b_o L{layer_idx}: {e}")))?;
// Cross-Attention weights
let ln2_gamma = GpuResidentTensor::from_host(ctx, &block.ln2.weight)
.map_err(|e| WhisperError::Inference(format!("dec ln2_gamma L{layer_idx}: {e}")))?;
let ln2_beta = GpuResidentTensor::from_host(ctx, &block.ln2.bias)
.map_err(|e| WhisperError::Inference(format!("dec ln2_beta L{layer_idx}: {e}")))?;
let cross_w_q_t = transpose_weights(&block.cross_attn.w_q().weight, d_model, d_model);
let cross_w_q = GpuResidentTensor::from_host(ctx, &cross_w_q_t)
.map_err(|e| WhisperError::Inference(format!("dec cross_w_q L{layer_idx}: {e}")))?;
let cross_b_q = GpuResidentTensor::from_host(ctx, &block.cross_attn.w_q().bias)
.map_err(|e| WhisperError::Inference(format!("dec cross_b_q L{layer_idx}: {e}")))?;
let cross_w_k_t = transpose_weights(&block.cross_attn.w_k().weight, d_model, d_model);
let cross_w_k = GpuResidentTensor::from_host(ctx, &cross_w_k_t)
.map_err(|e| WhisperError::Inference(format!("dec cross_w_k L{layer_idx}: {e}")))?;
let cross_b_k = GpuResidentTensor::from_host(ctx, &block.cross_attn.w_k().bias)
.map_err(|e| WhisperError::Inference(format!("dec cross_b_k L{layer_idx}: {e}")))?;
let cross_w_v_t = transpose_weights(&block.cross_attn.w_v().weight, d_model, d_model);
let cross_w_v = GpuResidentTensor::from_host(ctx, &cross_w_v_t)
.map_err(|e| WhisperError::Inference(format!("dec cross_w_v L{layer_idx}: {e}")))?;
let cross_b_v = GpuResidentTensor::from_host(ctx, &block.cross_attn.w_v().bias)
.map_err(|e| WhisperError::Inference(format!("dec cross_b_v L{layer_idx}: {e}")))?;
let cross_w_o_t = transpose_weights(&block.cross_attn.w_o().weight, d_model, d_model);
let cross_w_o = GpuResidentTensor::from_host(ctx, &cross_w_o_t)
.map_err(|e| WhisperError::Inference(format!("dec cross_w_o L{layer_idx}: {e}")))?;
let cross_b_o = GpuResidentTensor::from_host(ctx, &block.cross_attn.w_o().bias)
.map_err(|e| WhisperError::Inference(format!("dec cross_b_o L{layer_idx}: {e}")))?;
// FFN weights
let ln3_gamma = GpuResidentTensor::from_host(ctx, &block.ln3.weight)
.map_err(|e| WhisperError::Inference(format!("dec ln3_gamma L{layer_idx}: {e}")))?;
let ln3_beta = GpuResidentTensor::from_host(ctx, &block.ln3.bias)
.map_err(|e| WhisperError::Inference(format!("dec ln3_beta L{layer_idx}: {e}")))?;
// FFN up: [d_ff, d_model] -> transposed to [d_model, d_ff]
let ffn_up_t = transpose_weights(&block.ffn.fc1.weight, d_ff, d_model);
let ffn_up_w = GpuResidentTensor::from_host(ctx, &ffn_up_t)
.map_err(|e| WhisperError::Inference(format!("dec ffn_up_w L{layer_idx}: {e}")))?;
let ffn_up_b = GpuResidentTensor::from_host(ctx, &block.ffn.fc1.bias)
.map_err(|e| WhisperError::Inference(format!("dec ffn_up_b L{layer_idx}: {e}")))?;
// FFN down: [d_model, d_ff] -> transposed to [d_ff, d_model]
let ffn_down_t = transpose_weights(&block.ffn.fc2.weight, d_model, d_ff);
let ffn_down_w = GpuResidentTensor::from_host(ctx, &ffn_down_t).map_err(|e| {
WhisperError::Inference(format!("dec ffn_down_w L{layer_idx}: {e}"))
})?;
let ffn_down_b =
GpuResidentTensor::from_host(ctx, &block.ffn.fc2.bias).map_err(|e| {
WhisperError::Inference(format!("dec ffn_down_b L{layer_idx}: {e}"))
})?;
gpu_weights.push(GpuDecoderBlockWeights {
ln1_gamma,
ln1_beta,
self_w_q,
self_b_q,
self_w_k,
self_b_k,
self_w_v,
self_b_v,
self_w_o,
self_b_o,
ln2_gamma,
ln2_beta,
cross_w_q,
cross_b_q,
cross_w_k,
cross_b_k,
cross_w_v,
cross_b_v,
cross_w_o,
cross_b_o,
ln3_gamma,
ln3_beta,
ffn_up_w,
ffn_up_b,
ffn_down_w,
ffn_down_b,
});
}
self.gpu_decoder_weights = Some(gpu_weights);
self.gpu_decoder_config = Some(GpuDecoderConfig {
d_model: d_model as u32,
n_heads: n_heads as u32,
ffn_dim: d_ff as u32,
max_seq_len: max_seq_len as u32,
n_layers: n_layers as u32,
});
Ok(())
}
/// WAPR-PERF-013: Initialize GPU KV caches for decoder
#[cfg(feature = "cuda")]
pub fn init_gpu_decoder_kv_cache(&mut self) -> WhisperResult<()> {
if self.gpu_self_kv_cache.is_some() {
return Ok(()); // Already initialized
}
let ctx = self.executor.context();
let d_model = self.config.n_text_state as usize;
let n_layers = self.config.n_text_layer as usize;
let max_seq_len = self.config.n_text_ctx as usize;
// Self-attention KV caches (one per layer)
let mut self_kv_caches = Vec::with_capacity(n_layers);
for _layer in 0..n_layers {
let cache = GpuKvCache::new(ctx, max_seq_len, d_model)
.map_err(|e| WhisperError::Inference(format!("GPU self KV cache: {e}")))?;
self_kv_caches.push(cache);
}
// Cross-attention KV caches (one per layer, for encoder K/V)
// These are computed once from encoder output
let mut cross_kv_caches = Vec::with_capacity(n_layers);
for _layer in 0..n_layers {
// Use encoder output length (1500 for Whisper tiny)
let enc_seq_len = 1500; // Fixed for Whisper
let cache = GpuKvCache::new(ctx, enc_seq_len, d_model)
.map_err(|e| WhisperError::Inference(format!("GPU cross KV cache: {e}")))?;
cross_kv_caches.push(cache);
}
self.gpu_self_kv_cache = Some(self_kv_caches);
self.gpu_cross_kv_cache = Some(cross_kv_caches);
Ok(())
}
/// WAPR-PERF-013: Initialize head-first KV caches for GPU decoder
///
/// Creates KV caches in head-first layout [n_heads, max_seq_len, head_dim]
/// required by `incremental_attention_gpu`.
#[cfg(feature = "cuda")]
pub fn init_gpu_decoder_kv_cache_head_first(&mut self) -> WhisperResult<()> {
if self.gpu_self_k_head_first.is_some() {
return Ok(()); // Already initialized
}
let ctx = self.executor.context();
let d_model = self.config.n_text_state as usize;
let n_heads = self.config.n_text_head as usize;
let n_layers = self.config.n_text_layer as usize;
let head_dim = d_model / n_heads;
let max_seq_len = self.config.n_text_ctx as usize;
let enc_seq_len = 1500_usize; // Fixed for Whisper
// Head-first cache size: [n_heads, seq_len, head_dim]
let self_cache_size = n_heads * max_seq_len * head_dim;
let cross_cache_size = n_heads * enc_seq_len * head_dim;
let mut self_k_caches = Vec::with_capacity(n_layers);
let mut self_v_caches = Vec::with_capacity(n_layers);
let mut cross_k_caches = Vec::with_capacity(n_layers);
let mut cross_v_caches = Vec::with_capacity(n_layers);
for _layer in 0..n_layers {
// Self-attention caches
let zeros_self = vec![0.0f32; self_cache_size];
let k_self = GpuResidentTensor::from_host(ctx, &zeros_self)
.map_err(|e| WhisperError::Inference(format!("self K cache: {e}")))?;
let v_self = GpuResidentTensor::from_host(ctx, &zeros_self)
.map_err(|e| WhisperError::Inference(format!("self V cache: {e}")))?;
self_k_caches.push(k_self);
self_v_caches.push(v_self);
// Cross-attention caches
let zeros_cross = vec![0.0f32; cross_cache_size];
let k_cross = GpuResidentTensor::from_host(ctx, &zeros_cross)
.map_err(|e| WhisperError::Inference(format!("cross K cache: {e}")))?;
let v_cross = GpuResidentTensor::from_host(ctx, &zeros_cross)
.map_err(|e| WhisperError::Inference(format!("cross V cache: {e}")))?;
cross_k_caches.push(k_cross);
cross_v_caches.push(v_cross);
}
self.gpu_self_k_head_first = Some(self_k_caches);
self.gpu_self_v_head_first = Some(self_v_caches);
self.gpu_cross_k_head_first = Some(cross_k_caches);
self.gpu_cross_v_head_first = Some(cross_v_caches);
self.gpu_decoder_pos = 0;
Ok(())
}
/// WAPR-PERF-013: Reset decoder position for new sequence
#[cfg(feature = "cuda")]
pub fn reset_gpu_decoder_pos(&mut self) {
self.gpu_decoder_pos = 0;
}
/// WAPR-PERF-014: Reset GPU decoder KV caches (forces re-initialization)
///
/// Clears all head-first KV caches to force fresh allocation on next init.
/// Call this before switching between GPU path and Executor path in benchmarks.
/// Resets BOTH cache formats to ensure clean state.
#[cfg(feature = "cuda")]
pub fn reset_gpu_decoder_kv_cache(&mut self) {
// Head-first format (Executor path)
self.gpu_self_k_head_first = None;
self.gpu_self_v_head_first = None;
self.gpu_cross_k_head_first = None;
self.gpu_cross_v_head_first = None;
// Layer-major format (GPU path)
self.gpu_self_kv_cache = None;
self.gpu_cross_kv_cache = None;
}
/// WAPR-PERF-013: GPU decoder block forward pass
///
/// Processes a single token through one decoder block on GPU.
/// Uses head-first KV caches for zero-conversion attention.
///
/// # Architecture
///
/// ```text
/// x → LN1 → Q/K/V (GPU) → scatter K/V → incr_attn (GPU) → O (GPU) → residual
/// → LN2 → Q (GPU) → cross_attn (GPU) → O (GPU) → residual
/// → LN3 → FC1 (GPU) → GELU → FC2 (GPU) → residual
/// ```
///
/// # Point 149 Compliance
///
/// All GPU operations chain on implicit stream. No explicit sync inside.
/// Caller must sync only when reading final output.
///
/// # Parameters
///
/// - `encoder_output`: Optional encoder hidden states for cross-attention.
/// If None, cross-attention is skipped (useful for testing self-attention).
#[cfg(feature = "cuda")]
pub fn forward_decoder_block_gpu(
&mut self,
layer_idx: usize,
x: &[f32],
pos: usize,
encoder_output: Option<&[f32]>,
) -> WhisperResult<Vec<f32>> {
use trueno_gpu::driver::CudaStream;
let ctx = self.executor.context();
let d_model = self.config.n_text_state as usize;
let n_heads = self.config.n_text_head as usize;
let head_dim = d_model / n_heads;
let max_seq_len = self.config.n_text_ctx as usize;
// Get GPU weights for this layer
let weights = self
.gpu_decoder_weights
.as_ref()
.ok_or_else(|| WhisperError::Inference("Decoder weights not uploaded".into()))?;
let layer_weights = &weights[layer_idx];
// Get head-first KV caches
let self_k_caches = self
.gpu_self_k_head_first
.as_mut()
.ok_or_else(|| WhisperError::Inference("Self K cache not initialized".into()))?;
let self_v_caches = self
.gpu_self_v_head_first
.as_mut()
.ok_or_else(|| WhisperError::Inference("Self V cache not initialized".into()))?;
let block = &self.decoder.blocks()[layer_idx];
// === Self-Attention ===
// LN1 (CPU - simple and correct)
let normed = block.ln1.forward(x)?;
// Upload normed input to GPU
let x_gpu = GpuResidentTensor::from_host(ctx, &normed)
.map_err(|e| WhisperError::Inference(format!("x upload: {e}")))?;
// Q/K/V projections on GPU: [1, d_model] @ [d_model, d_model] = [1, d_model]
let q = x_gpu
.linear(
ctx,
&layer_weights.self_w_q,
Some(&layer_weights.self_b_q),
1,
d_model as u32,
d_model as u32,
)
.map_err(|e| WhisperError::Inference(format!("Q projection: {e}")))?;
let k = x_gpu
.linear(
ctx,
&layer_weights.self_w_k,
Some(&layer_weights.self_b_k),
1,
d_model as u32,
d_model as u32,
)
.map_err(|e| WhisperError::Inference(format!("K projection: {e}")))?;
let v = x_gpu
.linear(
ctx,
&layer_weights.self_w_v,
Some(&layer_weights.self_b_v),
1,
d_model as u32,
d_model as u32,
)
.map_err(|e| WhisperError::Inference(format!("V projection: {e}")))?;
// Scatter K/V to head-first caches
let stream =
CudaStream::new(ctx).map_err(|e| WhisperError::Inference(format!("Stream: {e}")))?;
kv_cache_scatter_gpu(
ctx,
&k,
&mut self_k_caches[layer_idx],
pos as u32,
n_heads as u32,
head_dim as u32,
max_seq_len as u32,
&stream,
)
.map_err(|e| WhisperError::Inference(format!("K scatter: {e}")))?;
kv_cache_scatter_gpu(
ctx,
&v,
&mut self_v_caches[layer_idx],
pos as u32,
n_heads as u32,
head_dim as u32,
max_seq_len as u32,
&stream,
)
.map_err(|e| WhisperError::Inference(format!("V scatter: {e}")))?;
// Incremental self-attention: Q @ cached_K^T → softmax → @ cached_V
let seq_len = (pos + 1) as u32; // Include current position
let attn_out = incremental_attention_gpu(
ctx,
&q,
&self_k_caches[layer_idx],
&self_v_caches[layer_idx],
n_heads as u32,
head_dim as u32,
seq_len,
max_seq_len as u32,
)
.map_err(|e| WhisperError::Inference(format!("Self attention: {e}")))?;
// Output projection
let mut attn_proj = attn_out
.linear(
ctx,
&layer_weights.self_w_o,
Some(&layer_weights.self_b_o),
1,
d_model as u32,
d_model as u32,
)
.map_err(|e| WhisperError::Inference(format!("O projection: {e}")))?;
// Download and add residual (sync point)
let attn_proj_host = attn_proj
.to_host()
.map_err(|e| WhisperError::Inference(format!("Attn D2H: {e}")))?;
// Residual connection
let mut residual: Vec<f32> = x
.iter()
.zip(attn_proj_host.iter())
.map(|(a, b)| a + b)
.collect();
// === Cross-Attention ===
if let Some(enc_out) = encoder_output {
let normed2 = block.ln2.forward(&residual)?;
let cross_out = block.cross_attn.forward_cross_dispatch(
&normed2, enc_out,
None, // Cross-attention K/V caching tracked in WAPR-PERF-007
)?;
for (r, c) in residual.iter_mut().zip(cross_out.iter()) {
*r += c;
}
}
// Note: When encoder_output is None, cross-attention is skipped.
// This is useful for testing self-attention in isolation.
// === FFN (CPU for now) ===
let normed3 = block.ln3.forward(&residual)?;
let ffn_out = block.ffn.forward(&normed3)?;
for (r, f) in residual.iter_mut().zip(ffn_out.iter()) {
*r += f;
}
Ok(residual)
}
/// WAPR-PERF-017: GPU decoder block with external stream (CUDA Graph capturable)
///
/// Same as `forward_decoder_block_gpu` but uses external stream for all operations.
/// Does NOT synchronize internally - caller controls when to sync.
///
/// This enables CUDA Graph capture: all operations recorded to a graph that can
/// be replayed with ~3-10µs launch overhead instead of ~20-50µs per kernel.
///
/// # Arguments
///
/// * `layer_idx` - Decoder layer index
/// * `x_gpu` - Input tensor on GPU [1, d_model]
/// * `pos` - Current position in sequence
/// * `stream` - Caller-provided CUDA stream for graph capture
/// * `enc_seq_len` - If Some, enables cross-attention using cached encoder K/V
///
/// # Returns
///
/// Output tensor on GPU [1, d_model] (still on GPU, no D2H)
#[cfg(feature = "cuda")]
pub fn forward_decoder_block_gpu_stream(
&mut self,
layer_idx: usize,
x_gpu: &GpuResidentTensor<f32>,
pos: usize,
stream: &trueno_gpu::driver::CudaStream,
enc_seq_len: Option<usize>,
) -> WhisperResult<GpuResidentTensor<f32>> {
let ctx = self.executor.context();
let d_model = self.config.n_text_state as usize;
let n_heads = self.config.n_text_head as usize;
let head_dim = d_model / n_heads;
let max_seq_len = self.config.n_text_ctx as usize;
// Get GPU weights for this layer
let weights = self
.gpu_decoder_weights
.as_ref()
.ok_or_else(|| WhisperError::Inference("Decoder weights not uploaded".into()))?;
let layer_weights = &weights[layer_idx];
// Get head-first KV caches
let self_k_caches = self
.gpu_self_k_head_first
.as_mut()
.ok_or_else(|| WhisperError::Inference("Self K cache not initialized".into()))?;
let self_v_caches = self
.gpu_self_v_head_first
.as_mut()
.ok_or_else(|| WhisperError::Inference("Self V cache not initialized".into()))?;
// === Self-Attention (all GPU, using external stream) ===
// LN1 on GPU using stream
let normed = x_gpu
.layer_norm_with_stream(
ctx,
&layer_weights.ln1_gamma,
&layer_weights.ln1_beta,
d_model as u32,
1, // batch_size = 1 for single token
stream,
)
.map_err(|e| WhisperError::Inference(format!("LN1: {e}")))?;
// Q/K/V projections (use matmul_with_stream internally via linear)
// Note: linear() creates its own stream, but we can still capture the sequence
let q = normed
.linear(
ctx,
&layer_weights.self_w_q,
Some(&layer_weights.self_b_q),
1,
d_model as u32,
d_model as u32,
)
.map_err(|e| WhisperError::Inference(format!("Q projection: {e}")))?;
let k = normed
.linear(
ctx,
&layer_weights.self_w_k,
Some(&layer_weights.self_b_k),
1,
d_model as u32,
d_model as u32,
)
.map_err(|e| WhisperError::Inference(format!("K projection: {e}")))?;
let v = normed
.linear(
ctx,
&layer_weights.self_w_v,
Some(&layer_weights.self_b_v),
1,
d_model as u32,
d_model as u32,
)
.map_err(|e| WhisperError::Inference(format!("V projection: {e}")))?;
// KV cache scatter (uses provided stream)
kv_cache_scatter_gpu(
ctx,
&k,
&mut self_k_caches[layer_idx],
pos as u32,
n_heads as u32,
head_dim as u32,
max_seq_len as u32,
stream,
)
.map_err(|e| WhisperError::Inference(format!("K scatter: {e}")))?;
kv_cache_scatter_gpu(
ctx,
&v,
&mut self_v_caches[layer_idx],
pos as u32,
n_heads as u32,
head_dim as u32,
max_seq_len as u32,
stream,
)
.map_err(|e| WhisperError::Inference(format!("V scatter: {e}")))?;
// Incremental attention using stream
let seq_len = (pos + 1) as u32;
let attn_out = incremental_attention_gpu_with_stream(
ctx,
&q,
&self_k_caches[layer_idx],
&self_v_caches[layer_idx],
n_heads as u32,
head_dim as u32,
seq_len,
max_seq_len as u32,
stream,
)
.map_err(|e| WhisperError::Inference(format!("Self attention: {e}")))?;
// Output projection
let attn_proj = attn_out
.linear(
ctx,
&layer_weights.self_w_o,
Some(&layer_weights.self_b_o),
1,
d_model as u32,
d_model as u32,
)
.map_err(|e| WhisperError::Inference(format!("O projection: {e}")))?;
// Residual connection (GPU)
let residual1 = x_gpu
.add_with_stream(ctx, &attn_proj, stream)
.map_err(|e| WhisperError::Inference(format!("Residual 1: {e}")))?;
// === Cross-Attention (GPU, if encoder output provided) ===
let residual2 =
if let Some(enc_len) = enc_seq_len {
// Get cross-attention K/V caches
let cross_k_caches = self.gpu_cross_k_head_first.as_ref().ok_or_else(|| {
WhisperError::Inference("Cross K cache not initialized".into())
})?;
let cross_v_caches = self.gpu_cross_v_head_first.as_ref().ok_or_else(|| {
WhisperError::Inference("Cross V cache not initialized".into())
})?;
// LN2 on residual1
let normed2 = residual1
.layer_norm_with_stream(
ctx,
&layer_weights.ln2_gamma,
&layer_weights.ln2_beta,
d_model as u32,
1,
stream,
)
.map_err(|e| WhisperError::Inference(format!("LN2: {e}")))?;
// Cross-attention Q from decoder hidden state
let q_cross = normed2
.linear(
ctx,
&layer_weights.cross_w_q,
Some(&layer_weights.cross_b_q),
1,
d_model as u32,
d_model as u32,
)
.map_err(|e| WhisperError::Inference(format!("Cross Q: {e}")))?;
// Cross-attention using cached encoder K/V
// Note: We use incremental_attention_gpu but with full enc_len as seq_len
let cross_attn_out = incremental_attention_gpu_with_stream(
ctx,
&q_cross,
&cross_k_caches[layer_idx],
&cross_v_caches[layer_idx],
n_heads as u32,
head_dim as u32,
enc_len as u32,
enc_len as u32, // max_seq_len = enc_len for cross-attention
stream,
)
.map_err(|e| WhisperError::Inference(format!("Cross attention: {e}")))?;
// Output projection
let cross_proj = cross_attn_out
.linear(
ctx,
&layer_weights.cross_w_o,
Some(&layer_weights.cross_b_o),
1,
d_model as u32,
d_model as u32,
)
.map_err(|e| WhisperError::Inference(format!("Cross O: {e}")))?;
// Cross-attention residual
residual1
.add_with_stream(ctx, &cross_proj, stream)
.map_err(|e| WhisperError::Inference(format!("Cross residual: {e}")))?
} else {
// No cross-attention (self-attention only path for testing)
residual1
};
// === FFN (all GPU, using external stream) ===
// LN3 on GPU
let normed3 = residual2
.layer_norm_with_stream(
ctx,
&layer_weights.ln3_gamma,
&layer_weights.ln3_beta,
d_model as u32,
1,
stream,
)
.map_err(|e| WhisperError::Inference(format!("LN3: {e}")))?;
// FFN up projection + GELU
let ffn_up = normed3
.linear(
ctx,
&layer_weights.ffn_up_w,
Some(&layer_weights.ffn_up_b),
1,
d_model as u32,
(d_model * 4) as u32,
)
.map_err(|e| WhisperError::Inference(format!("FFN up: {e}")))?;
let ffn_gelu = ffn_up
.gelu_with_stream(ctx, stream)
.map_err(|e| WhisperError::Inference(format!("GELU: {e}")))?;
// FFN down projection
let ffn_down = ffn_gelu
.linear(
ctx,
&layer_weights.ffn_down_w,
Some(&layer_weights.ffn_down_b),
1,
(d_model * 4) as u32,
d_model as u32,
)
.map_err(|e| WhisperError::Inference(format!("FFN down: {e}")))?;
// Final residual connection
let output = residual2
.add_with_stream(ctx, &ffn_down, stream)
.map_err(|e| WhisperError::Inference(format!("FFN residual: {e}")))?;
Ok(output)
}
/// WAPR-PERF-013: Full GPU decoder forward pass for single token
///
/// Runs a single token through all decoder layers on GPU.
/// Uses head-first KV caches for zero-conversion attention.
///
/// # Point 157 Compliance
///
/// - Single H2D at start (token embedding)
/// - Minimal sync points (once per token, not per layer)
/// - Target: Full transcription ≤1984ms (2x whisper.cpp @ 992ms)
///
/// # Parameters
///
/// - `token_embedding`: Embedded token vector [d_model]
/// - `pos`: Current position in sequence
/// - `encoder_output`: Encoder hidden states for cross-attention
#[cfg(feature = "cuda")]
pub fn forward_decoder_token_gpu(
&mut self,
token_embedding: &[f32],
pos: usize,
encoder_output: &[f32],
) -> WhisperResult<Vec<f32>> {
let n_layers = self.config.n_text_layer as usize;
// Ensure weights and KV caches are initialized
if self.gpu_decoder_weights.is_none() {
self.upload_decoder_weights_to_gpu()?;
}
if self.gpu_self_k_head_first.is_none() {
self.init_gpu_decoder_kv_cache_head_first()?;
}
// Process through all layers
let mut hidden = token_embedding.to_vec();
for layer_idx in 0..n_layers {
hidden =
self.forward_decoder_block_gpu(layer_idx, &hidden, pos, Some(encoder_output))?;
}
Ok(hidden)
}
/// WAPR-PERF-017: Stream-based decoder token forward pass
///
/// All-GPU implementation using external stream for CUDA graph compatibility.
/// Achieves 97x speedup when combined with graph capture.
///
/// # Parameters
///
/// - `token_embedding`: Embedded token vector [d_model]
/// - `pos`: Current position in sequence
/// - `stream`: External CUDA stream for graph capture
/// - `enc_seq_len`: If Some, enables cross-attention using cached encoder K/V
///
/// # Returns
///
/// GPU-resident output tensor (no D2H - caller handles sync and download)
#[cfg(feature = "cuda")]
pub fn forward_decoder_token_gpu_stream(
&mut self,
token_embedding: &[f32],
pos: usize,
stream: &trueno_gpu::driver::CudaStream,
enc_seq_len: Option<usize>,
) -> WhisperResult<GpuResidentTensor<f32>> {
let n_layers = self.config.n_text_layer as usize;
let profile_layers = std::env::var("WHISPER_PROFILE_DECODER_LAYERS").is_ok();
// Ensure weights and KV caches are initialized (before borrowing ctx)
if self.gpu_decoder_weights.is_none() {
self.upload_decoder_weights_to_gpu()?;
}
if self.gpu_self_k_head_first.is_none() {
self.init_gpu_decoder_kv_cache_head_first()?;
}
// Get context after mutable initialization
let ctx = self.executor.context();
// Upload embedding to GPU
let embed_start = std::time::Instant::now();
let mut hidden_gpu = GpuResidentTensor::from_host(ctx, token_embedding)
.map_err(|e| WhisperError::Inference(format!("embedding upload: {e}")))?;
if profile_layers {
stream.synchronize().ok();
eprintln!(
"[PROFILE-DEC-EMBED] pos={} embed_upload: {:.2}ms",
pos,
embed_start.elapsed().as_secs_f64() * 1000.0
);
}
// Process through all layers using stream-based path
for layer_idx in 0..n_layers {
let layer_start = std::time::Instant::now();
hidden_gpu = self.forward_decoder_block_gpu_stream(
layer_idx,
&hidden_gpu,
pos,
stream,
enc_seq_len,
)?;
if profile_layers {
stream.synchronize().ok();
eprintln!(
"[PROFILE-DEC-LAYER] pos={} layer={} time: {:.2}ms",
pos,
layer_idx,
layer_start.elapsed().as_secs_f64() * 1000.0
);
}
}
Ok(hidden_gpu)
}
/// Full GPU encoder (WAPR-PERF-004: Total Offload)
///
/// Runs the entire encoder on GPU with minimal host transfers:
/// - 1 H2D: Input mel spectrogram upload
/// - 0 transfers during forward pass (all weights pre-uploaded)
/// - 1 D2H: Final output download
///
/// Requires: `upload_encoder_weights_to_gpu()` called first.
#[cfg(feature = "cuda")]
pub fn encode_gpu_total_offload(&mut self, mel: &[f32]) -> WhisperResult<Vec<f32>> {
// Ensure weights are uploaded
if self.gpu_encoder_weights.is_none() {
self.upload_encoder_weights_to_gpu()?;
}
// WAPR-PERF-012: Upload conv weights if not already
if self.gpu_conv_weights.is_none() {
self.upload_conv_weights_to_gpu()?;
}
let ctx = self.executor.context();
let d_model = self.config.n_audio_state as usize;
let n_layers = self.config.n_audio_layer as usize;
let n_mels = self.config.n_mels as usize;
// Reset transfer counters for monitoring
reset_transfer_counters();
// WAPR-PERF-011: Detailed timing breakdown
let profile_detail = std::env::var("WHISPER_PROFILE_LAYERS").is_ok();
let total_start = std::time::Instant::now();
// WAPR-PERF-012: GPU Convolutional frontend
let conv_start = std::time::Instant::now();
let seq_len_in = mel.len() / n_mels;
// Upload mel to GPU
let mel_gpu = GpuResidentTensor::from_host(ctx, mel)
.map_err(|e| WhisperError::Inference(format!("mel upload: {e}")))?;
let conv_weights = self
.gpu_conv_weights
.as_ref()
.ok_or_else(|| WhisperError::Inference("GPU conv weights not uploaded".into()))?;
// Conv1: 80 → 384, kernel=3, stride=1, padding=1 + GELU
let conv1_out = mel_gpu
.conv1d(
ctx,
&conv_weights.conv1_weight,
Some(&conv_weights.conv1_bias),
n_mels as u32, // in_channels
d_model as u32, // out_channels
3, // kernel_size
1, // stride
1, // padding
seq_len_in as u32, // seq_len
)
.map_err(|e| WhisperError::Inference(format!("conv1 GPU: {e}")))?;
// After conv1: seq_len stays same (stride=1), channels = d_model
let seq_len_after_conv1 = seq_len_in;
// Conv2: 384 → 384, kernel=3, stride=2, padding=1 + GELU
let mut conv2_out = conv1_out
.conv1d(
ctx,
&conv_weights.conv2_weight,
Some(&conv_weights.conv2_bias),
d_model as u32, // in_channels
d_model as u32, // out_channels
3, // kernel_size
2, // stride
1, // padding
seq_len_after_conv1 as u32,
)
.map_err(|e| WhisperError::Inference(format!("conv2 GPU: {e}")))?;
let conv_time = conv_start.elapsed();
// After conv2: seq_len halved (stride=2)
let seq_len = (seq_len_after_conv1 + 2 - 3) / 2 + 1;
// Download conv output to add positional embedding (CPU - small overhead)
let pos_start = std::time::Instant::now();
let mut x = conv2_out
.to_host()
.map_err(|e| WhisperError::Inference(format!("conv output download: {e}")))?;
// Add positional embedding
let pos_emb = self.encoder.positional_embedding();
for pos in 0..seq_len {
for d in 0..d_model {
x[pos * d_model + d] += pos_emb[pos * d_model + d];
}
}
let pos_time = pos_start.elapsed();
// Upload to GPU for transformer blocks
let upload_start = std::time::Instant::now();
let mut x_gpu = GpuResidentTensor::from_host(ctx, &x)
.map_err(|e| WhisperError::Inference(format!("input upload: {e}")))?;
let upload_time = upload_start.elapsed();
if profile_detail {
eprintln!(
"[PROFILE-BREAKDOWN] Conv(GPU): {:.1}ms, PosEmb: {:.1}ms, Upload: {:.1}ms",
conv_time.as_millis(),
pos_time.as_millis(),
upload_time.as_millis()
);
}
// Step 4: Process all encoder blocks on GPU (0 transfers)
let weights = self
.gpu_encoder_weights
.as_ref()
.ok_or_else(|| WhisperError::Inference("GPU weights not uploaded".into()))?;
let config = self
.gpu_encoder_config
.as_ref()
.ok_or_else(|| WhisperError::Inference("GPU config not set".into()))?;
// Process all encoder layers on GPU
let mut layer_times: Vec<u128> = Vec::new();
for layer_idx in 0..n_layers {
let layer_start = std::time::Instant::now();
x_gpu = forward_encoder_block_gpu(ctx, &x_gpu, &weights[layer_idx], config)
.map_err(|e| WhisperError::Inference(format!("encoder block {layer_idx}: {e}")))?;
layer_times.push(layer_start.elapsed().as_micros());
}
// Step 5: Download output (1 D2H transfer)
let download_start = std::time::Instant::now();
let output = x_gpu
.to_host()
.map_err(|e| WhisperError::Inference(format!("output download: {e}")))?;
let download_time = download_start.elapsed();
// Step 6: Final layer norm (CPU - small overhead)
let result = self.encoder.ln_post().forward(&output)?;
Ok(result)
}
/// WAPR-PERF-020: Pre-compile all GPU kernels for predictable latency
///
/// Runs a complete encoder+decoder forward pass with dummy data to JIT compile
/// all PTX kernels. This moves the ~200ms compilation overhead from first
/// transcription to model initialization.
///
/// # When to Call
///
/// Call `warmup()` after `into_cuda()` to ensure all subsequent transcriptions
/// run at full speed (~10ms instead of ~200ms for first transcription).
///
/// ```rust,ignore
/// let mut cuda_model = apr.into_cuda(0)?;
/// cuda_model.warmup()?; // ~200ms kernel compilation
/// // All subsequent calls now fast:
/// let result = cuda_model.transcribe_gpu(&audio, options)?; // ~10ms
/// ```
///
/// # Returns
///
/// Time taken for warmup in milliseconds.
#[cfg(feature = "cuda")]
pub fn warmup(&mut self) -> WhisperResult<u64> {
use trueno_gpu::driver::CudaStream;
let start = std::time::Instant::now();
// Step 1: Upload all weights
self.upload_encoder_weights_to_gpu()?;
self.upload_conv_weights_to_gpu()?;
self.upload_decoder_weights_to_gpu()?;
self.init_gpu_decoder_kv_cache_head_first()?;
// Step 2: Run encoder to compile kernels - match actual transcribe_gpu path
// WAPR-PERF-020: Check which encoder path will be used in transcribe_gpu
let use_gpu_total_offload = std::env::var("WHISPER_GPU_TOTAL_OFFLOAD").is_ok();
let use_gpu_encoder = std::env::var("WHISPER_GPU_ENCODER").is_ok();
let use_gpu_decoder = std::env::var("WHISPER_GPU_DECODER_OFFLOAD").is_ok();
let n_mels = self.config.n_mels as usize;
let n_frames = 3000; // Whisper expects exactly 3000 frames for 30s audio
let d_model = self.config.n_text_state as usize;
let dummy_mel: Vec<f32> = vec![0.0; n_mels * n_frames];
let enc_output = if use_gpu_total_offload {
// Full GPU encoder path - compile all encoder kernels
self.encode_gpu_total_offload(&dummy_mel)?
} else if use_gpu_encoder {
// Partial GPU encoder path - compile attention kernels
self.encode_gpu(&dummy_mel)?
} else {
// CPU encoder path - no GPU kernels to compile for encoder
// Use a small mel to avoid wasting time on CPU encoder
let small_mel: Vec<f32> = vec![0.0; n_mels * 100]; // 100 frames
self.encoder.forward_mel(&small_mel)?
};
let enc_seq_len = enc_output.len() / d_model;
// Only warm up decoder kernels if GPU decoder will be used
if !use_gpu_decoder {
return Ok(start.elapsed().as_millis() as u64);
}
// Step 3: Get context and stream for GPU operations
let ctx = self.executor.context();
let stream = CudaStream::new(ctx)
.map_err(|e| WhisperError::Inference(format!("warmup stream: {e}")))?;
// Step 4: Upload encoder output to GPU for cross-attention warmup
let enc_gpu = GpuResidentTensor::from_host(ctx, &enc_output)
.map_err(|e| WhisperError::Inference(format!("warmup enc upload: {e}")))?;
// Step 5: Populate cross K/V to compile permute kernels
self.populate_cross_kv_caches_gpu(&enc_gpu, &stream)?;
// Step 4: Run decoder to compile decoder kernels
let dummy_embedding: Vec<f32> = vec![0.0; d_model];
let _dec_out =
self.forward_decoder_token_gpu_stream(&dummy_embedding, 0, &stream, Some(enc_seq_len))?;
stream
.synchronize()
.map_err(|e| WhisperError::Inference(format!("warmup sync: {e}")))?;
// Step 5: Reset decoder state for clean subsequent runs
self.reset_gpu_decoder_kv_cache();
self.init_gpu_decoder_kv_cache_head_first()?;
Ok(start.elapsed().as_millis() as u64)
}
/// WAPR-PERF-018: GPU-resident encoder output for graph-captured cross-attention
///
/// Same as `encode_gpu_total_offload` but returns GpuResidentTensor instead of Vec<f32>.
/// Enables decoder cross-attention to stay on GPU without D2H→H2D transfer.
///
/// # Returns
///
/// - `GpuResidentTensor<f32>` - Encoder output on GPU [seq_len, d_model]
#[cfg(feature = "cuda")]
pub fn encode_gpu_resident(&mut self, mel: &[f32]) -> WhisperResult<GpuResidentTensor<f32>> {
// Ensure weights are uploaded
if self.gpu_encoder_weights.is_none() {
self.upload_encoder_weights_to_gpu()?;
}
if self.gpu_conv_weights.is_none() {
self.upload_conv_weights_to_gpu()?;
}
let ctx = self.executor.context();
let d_model = self.config.n_audio_state as usize;
let n_layers = self.config.n_audio_layer as usize;
let n_mels = self.config.n_mels as usize;
// Step 1: Convolutional frontend on GPU
let seq_len_in = mel.len() / n_mels;
let mel_gpu = GpuResidentTensor::from_host(ctx, mel)
.map_err(|e| WhisperError::Inference(format!("mel upload: {e}")))?;
let conv_weights = self
.gpu_conv_weights
.as_ref()
.ok_or_else(|| WhisperError::Inference("Conv weights not uploaded".into()))?;
// Conv1: 80 → d_model, kernel=3, stride=1, padding=1 + GELU
let conv1_out = mel_gpu
.conv1d(
ctx,
&conv_weights.conv1_weight,
Some(&conv_weights.conv1_bias),
n_mels as u32,
d_model as u32,
3,
1,
1,
seq_len_in as u32,
)
.map_err(|e| WhisperError::Inference(format!("conv1: {e}")))?;
// Conv2: d_model → d_model, kernel=3, stride=2, padding=1 + GELU
let conv2_out = conv1_out
.conv1d(
ctx,
&conv_weights.conv2_weight,
Some(&conv_weights.conv2_bias),
d_model as u32,
d_model as u32,
3,
2,
1,
seq_len_in as u32,
)
.map_err(|e| WhisperError::Inference(format!("conv2: {e}")))?;
// After conv2: seq_len halved (stride=2)
let seq_len = (seq_len_in + 2 - 3) / 2 + 1;
let pos_emb = self.encoder.positional_embedding();
let pos_slice = &pos_emb[..seq_len * d_model];
let pos_gpu = GpuResidentTensor::from_host(ctx, pos_slice)
.map_err(|e| WhisperError::Inference(format!("pos_emb upload: {e}")))?;
let mut x_gpu = conv2_out
.add(ctx, &pos_gpu)
.map_err(|e| WhisperError::Inference(format!("pos_emb add: {e}")))?;
// Step 3: Process encoder blocks on GPU
let weights = self
.gpu_encoder_weights
.as_ref()
.ok_or_else(|| WhisperError::Inference("GPU weights not uploaded".into()))?;
let config = self
.gpu_encoder_config
.as_ref()
.ok_or_else(|| WhisperError::Inference("GPU config not set".into()))?;
for layer_idx in 0..n_layers {
x_gpu = forward_encoder_block_gpu(ctx, &x_gpu, &weights[layer_idx], config)
.map_err(|e| WhisperError::Inference(format!("encoder block {layer_idx}: {e}")))?;
}
// Step 4: Final layer norm on GPU (WAPR-PERF-019: eliminates D2H→CPU→H2D round-trip)
let ln_post_gamma = self
.gpu_enc_ln_post_gamma
.as_ref()
.ok_or_else(|| WhisperError::Inference("enc ln_post_gamma not uploaded".into()))?;
let ln_post_beta = self
.gpu_enc_ln_post_beta
.as_ref()
.ok_or_else(|| WhisperError::Inference("enc ln_post_beta not uploaded".into()))?;
let result_gpu = x_gpu
.layer_norm(
ctx,
ln_post_gamma,
ln_post_beta,
d_model as u32,
seq_len as u32,
)
.map_err(|e| WhisperError::Inference(format!("encoder ln_post: {e}")))?;
Ok(result_gpu)
}
/// WAPR-PERF-018: Populate cross-attention K/V caches from encoder output
///
/// Projects encoder output through cross-attention K/V weights and stores in
/// head-first format for GPU cross-attention. Called once per sequence.
///
/// # Arguments
///
/// * `encoder_output_gpu` - GPU-resident encoder output [enc_len, d_model]
/// * `stream` - CUDA stream for GPU operations
///
/// # Layout
///
/// Input: [enc_len, d_model] where d_model = n_heads * head_dim
/// Cache: [n_heads, enc_len, head_dim] (head-first for incremental_attention_gpu)
#[cfg(feature = "cuda")]
pub fn populate_cross_kv_caches_gpu(
&mut self,
encoder_output_gpu: &GpuResidentTensor<f32>,
stream: &trueno_gpu::driver::CudaStream,
) -> WhisperResult<()> {
// Ensure decoder weights are uploaded
if self.gpu_decoder_weights.is_none() {
self.upload_decoder_weights_to_gpu()?;
}
// Ensure KV caches are initialized
if self.gpu_cross_k_head_first.is_none() {
self.init_gpu_decoder_kv_cache_head_first()?;
}
let ctx = self.executor.context();
let d_model = self.config.n_text_state as usize;
let n_heads = self.config.n_text_head as usize;
let n_layers = self.config.n_text_layer as usize;
let head_dim = d_model / n_heads;
// Encoder sequence length from tensor size
let enc_len = encoder_output_gpu.len() / d_model;
// Get weights and caches
let weights = self
.gpu_decoder_weights
.as_ref()
.ok_or_else(|| WhisperError::Inference("Decoder weights not uploaded".into()))?;
let cross_k_caches = self
.gpu_cross_k_head_first
.as_mut()
.ok_or_else(|| WhisperError::Inference("Cross K cache not initialized".into()))?;
let cross_v_caches = self
.gpu_cross_v_head_first
.as_mut()
.ok_or_else(|| WhisperError::Inference("Cross V cache not initialized".into()))?;
// For each layer, project encoder output and reshape to head-first
for layer_idx in 0..n_layers {
let layer_weights = &weights[layer_idx];
// Project K: [enc_len, d_model] @ W_k^T -> [enc_len, d_model]
let k_proj = encoder_output_gpu
.linear(
ctx,
&layer_weights.cross_w_k,
Some(&layer_weights.cross_b_k),
enc_len as u32,
d_model as u32,
d_model as u32,
)
.map_err(|e| WhisperError::Inference(format!("cross K proj L{layer_idx}: {e}")))?;
// Project V: [enc_len, d_model] @ W_v^T -> [enc_len, d_model]
let v_proj = encoder_output_gpu
.linear(
ctx,
&layer_weights.cross_w_v,
Some(&layer_weights.cross_b_v),
enc_len as u32,
d_model as u32,
d_model as u32,
)
.map_err(|e| WhisperError::Inference(format!("cross V proj L{layer_idx}: {e}")))?;
// Reshape from [enc_len, d_model] to [n_heads, enc_len, head_dim] on GPU
// Uses InterleavedToBatchedKernel for zero-copy permute
let k_head_first = k_proj
.interleaved_to_head_first(
ctx,
enc_len as u32,
n_heads as u32,
head_dim as u32,
stream,
)
.map_err(|e| {
WhisperError::Inference(format!("cross K permute L{layer_idx}: {e}"))
})?;
let v_head_first = v_proj
.interleaved_to_head_first(
ctx,
enc_len as u32,
n_heads as u32,
head_dim as u32,
stream,
)
.map_err(|e| {
WhisperError::Inference(format!("cross V permute L{layer_idx}: {e}"))
})?;
// Store in caches (direct assignment, both are now GPU-resident)
let cache_k = &mut cross_k_caches[layer_idx];
let cache_v = &mut cross_v_caches[layer_idx];
*cache_k = k_head_first;
*cache_v = v_head_first;
}
// Sync stream to ensure all uploads complete
stream
.synchronize()
.map_err(|e| WhisperError::Inference(format!("stream sync: {e}")))?;
Ok(())
}
/// Forward pass through a single encoder block with GPU attention.
///
/// Architecture: Pre-norm with residual connections
/// x + Attention(LN(x)) then x + FFN(LN(x))
fn forward_encoder_block_gpu(
&mut self,
layer_idx: usize,
x: &[f32],
seq_len: usize,
n_heads: usize,
head_dim: usize,
) -> WhisperResult<Vec<f32>> {
// Extract block data first to avoid borrow conflicts with self.attention_via_gemm
let (_normed, q, k, v) = {
let block = &self.encoder.blocks()[layer_idx];
let normed = block.ln1.forward(x)?;
let q = block.self_attn.w_q().forward(&normed, seq_len)?;
let k = block.self_attn.w_k().forward(&normed, seq_len)?;
let v = block.self_attn.w_v().forward(&normed, seq_len)?;
(normed, q, k, v)
};
// GPU attention dispatch (WAPR-PERF-004 vs WAPR-PERF-005)
// WHISPER_GPU_RESIDENT=1: GPU-resident path with minimal transfers
// Otherwise: gemm-per-head path (higher transfer overhead)
#[cfg(feature = "cuda")]
let attn_output = {
let use_gpu_resident = std::env::var("WHISPER_GPU_RESIDENT").is_ok();
if use_gpu_resident {
// New path: GPU-resident attention with trueno-gpu (WAPR-PERF-004)
self.attention_gpu_resident(&q, &k, &v, seq_len, n_heads, head_dim)?
} else {
// Old path: gemm per head (WAPR-PERF-005)
self.attention_via_gemm(&q, &k, &v, seq_len, n_heads, head_dim)?
}
};
#[cfg(not(feature = "cuda"))]
let attn_output = self.attention_via_gemm(&q, &k, &v, seq_len, n_heads, head_dim)?;
// Output projection and residual (need block reference again)
let (attn_proj, _normed2, ffn_out) = {
let block = &self.encoder.blocks()[layer_idx];
let attn_proj = block.self_attn.w_o().forward(&attn_output, seq_len)?;
// Residual connection
let residual: Vec<f32> = x.iter().zip(attn_proj.iter()).map(|(a, b)| a + b).collect();
// Pre-norm for FFN
let normed2 = block.ln2.forward(&residual)?;
// FFN (CPU)
let ffn_out = block.ffn.forward(&normed2)?;
(attn_proj, normed2, ffn_out)
};
// Final residual (compute from x and attn_proj, then add ffn_out)
let mut residual: Vec<f32> = x.iter().zip(attn_proj.iter()).map(|(a, b)| a + b).collect();
for (r, f) in residual.iter_mut().zip(ffn_out.iter()) {
*r += f;
}
Ok(residual)
}
/// GPU attention using basic gemm primitives (WAPR-PERF-005).
///
/// This is the "dumb but working" approach that bypasses the failing
/// flash_attention_multi_head kernel. Uses basic matrix multiplication:
///
/// For each head h:
/// 1. scores = Q_h @ K_h^T (gemm: [seq, head_dim] @ [head_dim, seq] = [seq, seq])
/// 2. scores = scores / sqrt(head_dim)
/// 3. attn_weights = softmax(scores)
/// 4. output_h = attn_weights @ V_h (gemm: [seq, seq] @ [seq, head_dim] = [seq, head_dim])
///
/// Then concatenate heads.
fn attention_via_gemm(
&mut self,
q: &[f32],
k: &[f32],
v: &[f32],
seq_len: usize,
n_heads: usize,
head_dim: usize,
) -> WhisperResult<Vec<f32>> {
let d_model = n_heads * head_dim;
let scale = 1.0 / (head_dim as f32).sqrt();
// Output buffer: [seq_len, d_model]
let mut output = vec![0.0f32; seq_len * d_model];
// Process each head
for head in 0..n_heads {
let head_offset = head * head_dim;
// Extract Q_h, K_h, V_h for this head (strided access)
let mut q_head = vec![0.0f32; seq_len * head_dim];
let mut k_head = vec![0.0f32; seq_len * head_dim];
let mut v_head = vec![0.0f32; seq_len * head_dim];
for pos in 0..seq_len {
for d in 0..head_dim {
q_head[pos * head_dim + d] = q[pos * d_model + head_offset + d];
k_head[pos * head_dim + d] = k[pos * d_model + head_offset + d];
v_head[pos * head_dim + d] = v[pos * d_model + head_offset + d];
}
}
// Step 1: scores = Q_h @ K_h^T using GPU gemm
// Q_h: [seq_len, head_dim], K_h^T: [head_dim, seq_len] -> scores: [seq_len, seq_len]
let mut scores = vec![0.0f32; seq_len * seq_len];
// Transpose K for K^T
let mut k_t = vec![0.0f32; head_dim * seq_len];
for i in 0..seq_len {
for j in 0..head_dim {
k_t[j * seq_len + i] = k_head[i * head_dim + j];
}
}
// GPU gemm: scores = Q_h @ K_h^T
self.executor
.gemm(
&q_head,
&k_t,
&mut scores,
seq_len as u32, // M
seq_len as u32, // N
head_dim as u32, // K
)
.map_err(|e| WhisperError::Inference(format!("GPU gemm (Q@K^T) failed: {e}")))?;
// Step 2: Scale scores
for s in &mut scores {
*s *= scale;
}
// Step 3: Softmax (CPU - simple row-wise softmax)
for row in 0..seq_len {
let row_start = row * seq_len;
let row_slice = &mut scores[row_start..row_start + seq_len];
// Find max for numerical stability
let max_val = row_slice.iter().cloned().fold(f32::NEG_INFINITY, f32::max);
// Exp and sum
let mut sum = 0.0f32;
for val in row_slice.iter_mut() {
*val = (*val - max_val).exp();
sum += *val;
}
// Normalize
let inv_sum = if sum > 0.0 { 1.0 / sum } else { 0.0 };
for val in row_slice.iter_mut() {
*val *= inv_sum;
}
}
// Step 4: output_h = attn_weights @ V_h using GPU gemm
// scores: [seq_len, seq_len], V_h: [seq_len, head_dim] -> output_h: [seq_len, head_dim]
let mut output_head = vec![0.0f32; seq_len * head_dim];
self.executor
.gemm(
&scores,
&v_head,
&mut output_head,
seq_len as u32, // M
head_dim as u32, // N
seq_len as u32, // K
)
.map_err(|e| WhisperError::Inference(format!("GPU gemm (attn@V) failed: {e}")))?;
// Copy output_h to output buffer at correct head offset
for pos in 0..seq_len {
for d in 0..head_dim {
output[pos * d_model + head_offset + d] = output_head[pos * head_dim + d];
}
}
}
Ok(output)
}
/// GPU-resident attention (WAPR-PERF-004)
///
/// Uses trueno-gpu's `GpuResidentTensor` and `batched_multihead_attention`
/// to compute attention with ZERO intermediate host↔device transfers.
///
/// This is the high-performance path that eliminates the ~150 transfers
/// per encoder pass that plague `attention_via_gemm`.
///
/// # Performance
///
/// - Old path (attention_via_gemm): ~150 H2D/D2H transfers per forward
/// - New path (attention_gpu_resident): 3 H2D + 0 intermediate + 1 D2H
///
/// # Arguments
///
/// * `q` - Query tensor [seq_len * d_model]
/// * `k` - Key tensor [seq_len * d_model]
/// * `v` - Value tensor [seq_len * d_model]
/// * `seq_len` - Sequence length
/// * `n_heads` - Number of attention heads
/// * `head_dim` - Dimension per head
///
/// # Returns
///
/// Attention output [seq_len * d_model]
#[cfg(feature = "cuda")]
pub fn attention_gpu_resident(
&self,
q: &[f32],
k: &[f32],
v: &[f32],
seq_len: usize,
n_heads: usize,
head_dim: usize,
) -> WhisperResult<Vec<f32>> {
// Get CudaContext from executor
let ctx = self.executor.context();
// Reset transfer counters for monitoring
reset_transfer_counters();
// Upload Q, K, V to GPU (3 H2D transfers)
let q_gpu = GpuResidentTensor::from_host(ctx, q)
.map_err(|e| WhisperError::Inference(format!("Failed to upload Q: {e}")))?;
let k_gpu = GpuResidentTensor::from_host(ctx, k)
.map_err(|e| WhisperError::Inference(format!("Failed to upload K: {e}")))?;
let v_gpu = GpuResidentTensor::from_host(ctx, v)
.map_err(|e| WhisperError::Inference(format!("Failed to upload V: {e}")))?;
// GPU-resident attention: ZERO intermediate transfers!
let mut output_gpu = batched_multihead_attention(
ctx,
&q_gpu,
&k_gpu,
&v_gpu,
n_heads as u32,
head_dim as u32,
seq_len as u32,
)
.map_err(|e| WhisperError::Inference(format!("GPU attention failed: {e}")))?;
// Download result (1 D2H transfer)
let output = output_gpu.to_host().map_err(|e| {
WhisperError::Inference(format!("Failed to download attention output: {e}"))
})?;
// Log transfer stats for debugging
if std::env::var("WHISPER_DEBUG_GPU_RESIDENT").is_ok() {
let stats = TransferStats::capture();
eprintln!(
"[GPU-RESIDENT] attention: {} H2D, {} D2H (expected: 3 H2D, 1 D2H)",
stats.h2d_transfers, stats.d2h_transfers
);
}
Ok(output)
}
/// Initialize GPU KV caches for decoder self-attention.
///
/// This pre-allocates GPU memory for KV caches, enabling GPU-resident
/// incremental attention without host-device transfers per token.
///
/// # Architecture (WAPR-PERF-004)
///
/// Whisper decoder has:
/// - n_layer decoder blocks (4 for tiny)
/// - n_head attention heads (6 for tiny)
/// - head_dim = d_model / n_head (64 for tiny)
/// - max_seq_len tokens (448 for Whisper)
///
/// KV cache layout per layer: [n_head, max_len, head_dim]
fn init_gpu_kv_cache(&mut self) -> WhisperResult<()> {
if self.kv_cache_initialized {
return Ok(());
}
let n_layers = self.config.n_text_layer as usize;
let n_heads = self.config.n_text_head as usize;
let head_dim = self.config.n_text_state as usize / n_heads;
let max_len = self.config.n_text_ctx as usize; // 448 for Whisper
// Initialize GPU KV cache via realizar
self.executor
.init_kv_cache_gpu(n_layers, n_heads, n_heads, head_dim, max_len)
.map_err(|e| WhisperError::Inference(format!("Failed to init GPU KV cache: {e}")))?;
self.kv_cache_initialized = true;
Ok(())
}
/// Clear GPU KV caches for a new transcription.
///
/// This resets the cache positions without deallocating GPU memory.
pub fn clear_kv_cache(&mut self) {
self.executor.reset_kv_cache_gpu();
}
/// GPU-accelerated forward pass for a single decoder token using flash_attention_cached.
///
/// This uses realizar's `flash_attention_cached` which handles GPU buffer management
/// internally, providing GPU-accelerated attention while keeping a simple CPU-side API.
///
/// # WAPR-PERF-004: Performance
///
/// - Self-attention uses GPU KV cache via `flash_attention_cached`
/// - Cross-attention and FFN run on CPU with SIMD
/// - Output projection on CPU (workaround for gemv bug)
///
/// # Arguments
///
/// * `token` - Input token ID
/// * `encoder_output` - Encoder hidden states (for cross-attention)
/// * `position` - Current position in sequence
///
/// # Returns
///
/// Logits over vocabulary (n_vocab)
pub fn forward_one_gpu_resident(
&mut self,
token: u32,
encoder_output: &[f32],
position: usize,
) -> WhisperResult<Vec<f32>> {
// Ensure weights and KV caches are ready
if !self.weights_uploaded {
self.upload_weights()?;
}
if !self.kv_cache_initialized {
self.init_gpu_kv_cache()?;
}
let d_model = self.config.n_text_state as usize;
let n_heads = self.config.n_text_head as usize;
let head_dim = d_model / n_heads;
let n_layers = self.config.n_text_layer as usize;
// Step 1: Token embedding + positional embedding
if token as usize >= self.config.n_vocab as usize {
return Err(WhisperError::Model(format!(
"token {} out of vocabulary range {}",
token, self.config.n_vocab
)));
}
let emb_start = (token as usize) * d_model;
let mut x: Vec<f32> =
self.decoder.token_embedding()[emb_start..emb_start + d_model].to_vec();
// Add positional embedding
let pos_start = position * d_model;
for (x_elem, pos_emb) in x
.iter_mut()
.zip(&self.decoder.positional_embedding()[pos_start..pos_start + d_model])
{
*x_elem += pos_emb;
}
// Step 2: Process decoder blocks with GPU-accelerated self-attention
for layer_idx in 0..n_layers {
x = self.forward_block_gpu_flash(layer_idx, &x, encoder_output, n_heads, head_dim)?;
}
// Step 3: Final layer norm
let x_normed = self.decoder.ln_post().forward(&x)?;
// Step 4: Output projection to vocabulary (CPU path per WAPR-PERF-006)
let logits = self.decoder.project_to_vocab_debug(&x_normed);
Ok(logits)
}
/// Forward pass through a single decoder block.
///
/// Attempts GPU-accelerated self-attention via flash_attention_cached,
/// falls back to CPU attention if GPU fails.
fn forward_block_gpu_flash(
&mut self,
layer_idx: usize,
x: &[f32],
encoder_output: &[f32],
n_heads: usize,
head_dim: usize,
) -> WhisperResult<Vec<f32>> {
let d_model = n_heads * head_dim;
let block = &self.decoder.blocks()[layer_idx];
// === Self-attention ===
// Pre-norm
let normed = block.ln1.forward(x)?;
// Q/K/V projections (CPU with SIMD)
let q = block.self_attn.w_q().forward_simd(&normed, 1)?;
let k = block.self_attn.w_k().forward_simd(&normed, 1)?;
let v = block.self_attn.w_v().forward_simd(&normed, 1)?;
// Try GPU-accelerated attention, fall back to CPU if it fails
let mut attn_out = vec![0.0f32; d_model];
let gpu_result = self
.executor
.flash_attention_cached(layer_idx, &q, &k, &v, &mut attn_out);
// If GPU fails, use CPU attention (this is for robustness during development)
match gpu_result {
Ok(_seq_len) => {
if std::env::var("WHISPER_DEBUG_GPU").is_ok() && layer_idx == 0 {
eprintln!("[GPU] flash_attention_cached SUCCESS layer={}", layer_idx);
}
}
Err(e) => {
if std::env::var("WHISPER_DEBUG_GPU").is_ok() {
eprintln!(
"[GPU] flash_attention_cached failed layer={} q.len={} k.len={} v.len={} d_model={}: {}",
layer_idx, q.len(), k.len(), v.len(), d_model, e
);
}
// CPU self-attention fallback
attn_out = self.compute_self_attention(&q, &k, &v, n_heads, head_dim)?;
}
}
// Output projection + residual
let attn_proj = block.self_attn.w_o().forward_simd(&attn_out, 1)?;
let mut residual: Vec<f32> = x.iter().zip(attn_proj.iter()).map(|(a, b)| a + b).collect();
// === Cross-attention (CPU with SIMD) ===
let normed = block.ln2.forward(&residual)?;
// Cross-attention Q projection
let q_cross = block.cross_attn.w_q().forward_simd(&normed, 1)?;
// Cross-attention K/V from encoder
let enc_len = encoder_output.len() / d_model;
let k_enc = block
.cross_attn
.w_k()
.forward_simd(encoder_output, enc_len)?;
let v_enc = block
.cross_attn
.w_v()
.forward_simd(encoder_output, enc_len)?;
// Compute cross-attention (CPU)
let cross_attn_out =
self.compute_cross_attention(&q_cross, &k_enc, &v_enc, n_heads, head_dim)?;
let cross_proj = block.cross_attn.w_o().forward_simd(&cross_attn_out, 1)?;
for (r, c) in residual.iter_mut().zip(cross_proj.iter()) {
*r += c;
}
// === FFN (CPU with SIMD) ===
let normed = block.ln3.forward(&residual)?;
let fc1_out = block.ffn.fc1.forward_simd(&normed, 1)?;
// GELU activation
let gelu_out: Vec<f32> = fc1_out.iter().map(|&val| gelu(val)).collect();
let fc2_out = block.ffn.fc2.forward_simd(&gelu_out, 1)?;
for (r, f) in residual.iter_mut().zip(fc2_out.iter()) {
*r += f;
}
Ok(residual)
}
/// Compute cross-attention (query attends to encoder keys/values).
fn compute_cross_attention(
&self,
q: &[f32],
k: &[f32],
v: &[f32],
n_heads: usize,
head_dim: usize,
) -> WhisperResult<Vec<f32>> {
let d_model = n_heads * head_dim;
let enc_len = k.len() / d_model;
let mut output = vec![0.0f32; d_model];
// Multi-head attention
for h in 0..n_heads {
let q_head = &q[h * head_dim..(h + 1) * head_dim];
// Compute attention scores
let mut scores = vec![0.0f32; enc_len];
for pos in 0..enc_len {
let k_head = &k[pos * d_model + h * head_dim..pos * d_model + (h + 1) * head_dim];
scores[pos] = q_head
.iter()
.zip(k_head.iter())
.map(|(a, b)| a * b)
.sum::<f32>()
/ (head_dim as f32).sqrt();
}
// Softmax
let max_score = scores.iter().cloned().fold(f32::NEG_INFINITY, f32::max);
let exp_scores: Vec<f32> = scores.iter().map(|s| (s - max_score).exp()).collect();
let sum_exp: f32 = exp_scores.iter().sum();
let attn_weights: Vec<f32> = exp_scores.iter().map(|e| e / sum_exp).collect();
// Weighted sum of values
for pos in 0..enc_len {
let v_head = &v[pos * d_model + h * head_dim..pos * d_model + (h + 1) * head_dim];
let weight = attn_weights[pos];
for (i, &val) in v_head.iter().enumerate() {
output[h * head_dim + i] += weight * val;
}
}
}
Ok(output)
}
/// Compute self-attention (CPU fallback for incremental decoding).
///
/// This is a simplified self-attention that only attends to the current K/V.
/// In a proper incremental decoder, this would accumulate K/V history.
fn compute_self_attention(
&self,
_q: &[f32],
_k: &[f32],
v: &[f32],
n_heads: usize,
head_dim: usize,
) -> WhisperResult<Vec<f32>> {
let d_model = n_heads * head_dim;
let mut output = vec![0.0f32; d_model];
// For single-token incremental decoding with no history,
// attention to self is just the value (attention weight = 1.0)
// This is a simplified fallback - proper incremental attention
// would accumulate K/V history
for h in 0..n_heads {
let v_head = &v[h * head_dim..(h + 1) * head_dim];
for (i, &val) in v_head.iter().enumerate() {
output[h * head_dim + i] = val;
}
}
Ok(output)
}
/// Run encoder forward pass on GPU.
///
/// Uses CudaExecutor for matrix multiplications when possible.
/// Falls back to CPU for operations not yet GPU-accelerated.
///
/// # Arguments
///
/// * `mel` - Mel spectrogram features [n_mels * n_frames]
///
/// # Returns
///
/// Encoder hidden states [seq_len * n_state]
pub fn encode_cuda(&mut self, mel: &[f32]) -> WhisperResult<Vec<f32>> {
// Ensure weights are uploaded
if !self.weights_uploaded {
self.upload_weights()?;
}
// WAPR-PERF-004: GPU encoder forward pass
// The encoder processes mel spectrogram through:
// 1. Conv1d layers (CPU - complex kernel, not worth GPU overhead)
// 2. Positional embedding (CPU - element-wise addition)
// 3. Transformer blocks (GPU - heavy matmul operations)
// 4. Final layer norm (CPU - reduction operation)
//
// For now, use CPU encoder as GPU wiring requires architecture changes.
// The main bottleneck is the decoder, not the encoder.
self.encoder.forward(mel)
}
/// Run decoder forward pass on GPU.
///
/// # Arguments
///
/// * `tokens` - Input token IDs
/// * `encoder_output` - Encoder hidden states
///
/// # Returns
///
/// Logits over vocabulary [vocab_size]
pub fn decode_cuda(
&mut self,
tokens: &[u32],
encoder_output: &[f32],
) -> WhisperResult<Vec<f32>> {
// Ensure weights are uploaded
if !self.weights_uploaded {
self.upload_weights()?;
}
// GPU decoder forward tracked in WAPR-PERF-009
// CPU decoder achieves target RTF; GPU optimization deferred
self.decoder.forward(tokens, encoder_output)
}
/// Run full transcription on GPU.
///
/// # Arguments
///
/// * `mel` - Mel spectrogram features
///
/// # Returns
///
/// Transcribed token IDs
pub fn transcribe_cuda(&mut self, mel: &[f32]) -> WhisperResult<Vec<u32>> {
let encoder_output = self.encode_cuda(mel)?;
// Simple greedy decode
let mut tokens = vec![50258_u32]; // SOT token
let max_tokens = 448;
for _ in 0..max_tokens {
let logits = self.decode_cuda(&tokens, &encoder_output)?;
// Argmax for greedy decode
let next_token = logits
.iter()
.enumerate()
.max_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
.map(|(idx, _)| idx as u32)
.unwrap_or(50257); // EOT fallback
if next_token == 50257 {
// EOT
break;
}
tokens.push(next_token);
}
Ok(tokens)
}
/// Transcribe audio samples using GPU acceleration.
///
/// This is the main entry point for GPU-accelerated transcription.
/// It converts audio to mel spectrogram, runs the encoder/decoder on GPU,
/// and decodes tokens to text.
///
/// # Arguments
///
/// * `audio` - Mono audio samples at 16kHz, normalized to [-1, 1]
/// * `options` - Transcription options (language, task, strategy)
///
/// # Returns
///
/// TranscriptionResult containing the transcribed text and optional timestamps.
#[allow(unused_variables)]
pub fn transcribe(
&mut self,
audio: &[f32],
options: TranscribeOptions,
) -> WhisperResult<TranscriptionResult> {
// Whisper constants
const N_SAMPLES_30S: usize = 480_000; // 30 seconds at 16kHz
const N_FRAMES: usize = 3000; // Whisper expects exactly 3000 frames
const N_MELS: usize = 80;
// Pad/truncate audio to 30 seconds (same as WhisperApr::compute_mel)
let padded_audio = match audio.len().cmp(&N_SAMPLES_30S) {
std::cmp::Ordering::Equal => audio.to_vec(),
std::cmp::Ordering::Less => {
let mut padded = vec![0.0_f32; N_SAMPLES_30S];
padded[..audio.len()].copy_from_slice(audio);
padded
}
std::cmp::Ordering::Greater => audio[..N_SAMPLES_30S].to_vec(),
};
// Compute mel spectrogram
let mut mel = self
.mel_filters
.compute(&padded_audio)
.map_err(|e| WhisperError::Audio(e.to_string()))?;
let actual_frames = mel.len() / N_MELS;
// Ensure exactly 3000 frames (pad or truncate)
if actual_frames < N_FRAMES {
let pad_value = -1.0_f32;
let mut padded_mel = vec![pad_value; N_FRAMES * N_MELS];
padded_mel[..mel.len()].copy_from_slice(&mel);
mel = padded_mel;
} else if actual_frames > N_FRAMES {
mel.truncate(N_FRAMES * N_MELS);
}
// Run encoder - use forward_mel which handles the conv frontend
let encoder_output = self.encoder.forward_mel(&mel)?;
// Build initial tokens based on task and language using SpecialTokens
use crate::tokenizer::special_tokens::{self, SpecialTokens};
let specials = SpecialTokens::for_vocab_size(self.config.n_vocab as usize);
let mut tokens = vec![specials.sot];
// Add language token for multilingual models
if specials.is_multilingual {
let language = options.language.as_deref().unwrap_or("en");
let lang_offset = special_tokens::language_offset(language).unwrap_or(0);
tokens.push(specials.lang_base + lang_offset);
}
// Add task token
match options.task {
Task::Transcribe => tokens.push(specials.transcribe),
Task::Translate => tokens.push(special_tokens::TRANSLATE),
}
// Timestamp mode: do NOT push no_timestamps token.
// This enables the decoder to produce <|t.tt|> timestamp tokens
// which are needed for proper SRT/VTT segment timing.
// Decode loop using incremental decoding with KV cache
let max_tokens = self.config.n_text_ctx as usize;
let d_model = self.config.n_text_state as usize;
let n_layers = self.config.n_text_layer as usize;
let n_vocab = self.config.n_vocab as usize;
let eot_token = specials.eot;
// Create KV cache for incremental decoding
let mut cache = crate::model::DecoderKVCache::new(n_layers, d_model, max_tokens);
// Create token suppressor — do NOT suppress timestamps
let suppressor = crate::inference::WhisperTokenSuppressor::new()
.with_timestamp_suppression(false)
.with_vocab_size(n_vocab);
// Process initial tokens to populate cache
for &token in &tokens {
let _ = self
.decoder
.forward_one(token, &encoder_output, &mut cache)?;
}
// Generate tokens
for _ in 0..max_tokens.saturating_sub(tokens.len()) {
// Get logits for last token
let last_token = *tokens.last().unwrap_or(&specials.sot);
let mut logits = self
.decoder
.forward_one(last_token, &encoder_output, &mut cache)?;
// Apply token suppression
suppressor.apply(&mut logits);
// Get next token based on strategy
let next_token = match options.strategy {
DecodingStrategy::Greedy => logits
.iter()
.enumerate()
.max_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
.map(|(idx, _)| idx as u32)
.unwrap_or(eot_token),
DecodingStrategy::BeamSearch { .. } | DecodingStrategy::Sampling { .. } => {
// For now, fall back to greedy for beam search and sampling
logits
.iter()
.enumerate()
.max_by(|(_, a), (_, b)| {
a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal)
})
.map(|(idx, _)| idx as u32)
.unwrap_or(eot_token)
}
};
// Check for EOT
if next_token == eot_token {
break;
}
tokens.push(next_token);
}
// Decode tokens to text, skipping special tokens
let text = self.tokenizer.decode_with_options(&tokens, true)?;
// Get language from options or default to "en"
let language = options.language.clone().unwrap_or_else(|| "en".to_string());
Ok(TranscriptionResult {
text,
language,
segments: Vec::new(), // No segments for now
profiling: None,
})
}
/// Get a reference to the underlying CudaExecutor.
pub fn executor(&self) -> &CudaExecutor {
&self.executor
}
/// Get a mutable reference to the underlying CudaExecutor.
pub fn executor_mut(&mut self) -> &mut CudaExecutor {
&mut self.executor
}
/// GPU-accelerated single token forward pass.
///
/// Uses CPU for token embedding + transformer blocks, then GPU gemv for
/// the output projection (51865 × 384 matmul - the main bottleneck).
///
/// # Performance
///
/// The output projection is O(n_vocab × d_model) = O(51865 × 384) ≈ 20M FLOPs.
/// GPU gemv provides ~10-50x speedup over CPU for this operation.
///
/// # Arguments
///
/// * `token` - Input token ID
/// * `encoder_output` - Encoder hidden states
/// * `cache` - KV cache for incremental decoding
///
/// # Returns
///
/// Logits over vocabulary (n_vocab)
pub fn forward_one_gpu(
&mut self,
token: u32,
encoder_output: &[f32],
cache: &mut DecoderKVCache,
) -> WhisperResult<Vec<f32>> {
// Ensure weights are uploaded to GPU
if !self.weights_uploaded {
self.upload_weights()?;
}
// Use CPU decoder to get hidden state after ln_post
// This runs: embedding → transformer blocks → layer norm
let hidden = self
.decoder
.forward_one_hidden(token, encoder_output, cache)?;
// Debug: print CPU decoder hidden state stats
let pos = cache.seq_len().saturating_sub(1);
if pos < 5 && std::env::var("WHISPER_DEBUG_GPU").is_ok() {
let hidden_mean = hidden.iter().sum::<f32>() / hidden.len() as f32;
let hidden_std = (hidden
.iter()
.map(|v| (v - hidden_mean).powi(2))
.sum::<f32>()
/ hidden.len() as f32)
.sqrt();
eprintln!(
"[DEBUG-CPU-HIDDEN] pos={} after_ln: len={} mean={:.4} std={:.4}",
pos,
hidden.len(),
hidden_mean,
hidden_std
);
}
// Output projection on GPU using direct gemm
// FIX 1 (WAPR-PERF-004): Use executor.gemm() which works correctly,
// unlike the buggy gemv_cached (WAPR-PERF-006).
let logits = self.project_to_vocab_gpu(&hidden)?;
Ok(logits)
}
/// WAPR-PERF-013: Full GPU decoder forward pass for single token
///
/// Uses GPU self-attention with head-first KV caches for all decoder blocks.
/// This is the "Total Offload" path that minimizes host-device sync.
///
/// # Architecture
///
/// ```text
/// token → embed (CPU) → decoder blocks (GPU self-attn) → ln_post (CPU) → logits (GPU)
/// ```
///
/// # Point 157 Compliance
///
/// - GPU self-attention with head-first KV cache (no layout conversion)
/// - Cross-attention on CPU (encoder K/V not GPU-resident yet)
/// - Single sync point at the end (after all blocks)
#[cfg(feature = "cuda")]
pub fn forward_one_gpu_total_offload(
&mut self,
token: u32,
encoder_output: &[f32],
) -> WhisperResult<Vec<f32>> {
use trueno_gpu::driver::CudaStream;
let d_model = self.config.n_text_state as usize;
let n_vocab = self.config.n_vocab as usize;
// Ensure GPU decoder infrastructure is initialized
if self.gpu_decoder_weights.is_none() {
self.upload_decoder_weights_to_gpu()?;
}
if self.gpu_self_k_head_first.is_none() {
self.init_gpu_decoder_kv_cache_head_first()?;
}
let pos = self.gpu_decoder_pos;
// 1. Embed token + positional embedding (CPU - fast)
if token as usize >= n_vocab {
return Err(WhisperError::Inference(format!(
"token {} out of vocabulary range {}",
token, n_vocab
)));
}
let emb_start = (token as usize) * d_model;
let token_emb = self.decoder.token_embedding();
let pos_emb = self.decoder.positional_embedding();
let max_len = self.config.n_text_ctx as usize;
if pos >= max_len {
return Err(WhisperError::Inference(format!(
"position {} exceeds max {}",
pos, max_len
)));
}
let pos_start = pos * d_model;
let token_embedding: Vec<f32> = token_emb[emb_start..emb_start + d_model]
.iter()
.zip(&pos_emb[pos_start..pos_start + d_model])
.map(|(t, p)| t + p)
.collect();
// 2. Compute encoder sequence length for cross-attention
let enc_seq_len = encoder_output.len() / d_model;
// Debug: print encoder output stats on first token
if pos == 0 {
eprintln!(
"[DEBUG-GPU-DEC] enc_output: len={} seq_len={} d_model={}",
encoder_output.len(),
enc_seq_len,
d_model
);
let enc_mean = encoder_output.iter().sum::<f32>() / encoder_output.len() as f32;
let enc_std = (encoder_output
.iter()
.map(|x| (x - enc_mean).powi(2))
.sum::<f32>()
/ encoder_output.len() as f32)
.sqrt();
eprintln!(
"[DEBUG-GPU-DEC] enc_output stats: mean={:.4} std={:.4}",
enc_mean, enc_std
);
}
// 3. Populate cross K/V on first token (pos=0) - encoder output changes per transcription
// Cross K/V caches are pre-allocated with zeros but need actual encoder projections
if pos == 0 {
eprintln!("[DEBUG-GPU-DEC] Populating cross K/V caches (pos=0)...");
let ctx = self.executor.context();
let stream = CudaStream::new(ctx)
.map_err(|e| WhisperError::Inference(format!("stream: {e}")))?;
let enc_gpu = GpuResidentTensor::from_host(ctx, encoder_output)
.map_err(|e| WhisperError::Inference(format!("enc upload: {e}")))?;
self.populate_cross_kv_caches_gpu(&enc_gpu, &stream)?;
stream
.synchronize()
.map_err(|e| WhisperError::Inference(format!("cross kv sync: {e}")))?;
eprintln!("[DEBUG-GPU-DEC] Cross K/V populated successfully");
}
// 4. Create single stream for all decoder operations (WAPR-PERF-023)
let ctx = self.executor.context();
let stream = CudaStream::new(ctx)
.map_err(|e| WhisperError::Inference(format!("decoder stream: {e}")))?;
// 5. Run decoder using stream-based path (single stream, all GPU)
let mut hidden_gpu = self.forward_decoder_token_gpu_stream(
&token_embedding,
pos,
&stream,
Some(enc_seq_len),
)?;
// 6. Download hidden state for final layer norm
stream
.synchronize()
.map_err(|e| WhisperError::Inference(format!("stream sync: {e}")))?;
let x = hidden_gpu
.to_host()
.map_err(|e| WhisperError::Inference(format!("hidden download: {e}")))?;
// Debug: print hidden state stats
if pos < 5 && std::env::var("WHISPER_DEBUG_GPU").is_ok() {
let hidden_mean = x.iter().sum::<f32>() / x.len() as f32;
let hidden_std =
(x.iter().map(|v| (v - hidden_mean).powi(2)).sum::<f32>() / x.len() as f32).sqrt();
eprintln!(
"[DEBUG-GPU-HIDDEN] pos={} before_ln: len={} mean={:.4} std={:.4}",
pos,
x.len(),
hidden_mean,
hidden_std
);
}
// 7. Final layer norm (CPU - simple)
let hidden = self.decoder.ln_post().forward(&x)?;
// Debug: print hidden state after ln
if pos < 5 && std::env::var("WHISPER_DEBUG_GPU").is_ok() {
let hidden_mean = hidden.iter().sum::<f32>() / hidden.len() as f32;
let hidden_std = (hidden
.iter()
.map(|v| (v - hidden_mean).powi(2))
.sum::<f32>()
/ hidden.len() as f32)
.sqrt();
eprintln!(
"[DEBUG-GPU-HIDDEN] pos={} after_ln: len={} mean={:.4} std={:.4}",
pos,
hidden.len(),
hidden_mean,
hidden_std
);
}
// 8. Increment position for next token
self.gpu_decoder_pos += 1;
// 9. Output projection on GPU
self.project_to_vocab_gpu(&hidden)
}
/// WAPR-PERF-014: Executor-based single token forward pass
///
/// Uses `forward_decoder_block_executor()` which uses the executor's
/// persistent stream for GEMV operations, avoiding stream creation overhead.
#[cfg(feature = "cuda")]
pub fn forward_one_executor(
&mut self,
token: u32,
encoder_output: &[f32],
) -> WhisperResult<Vec<f32>> {
let d_model = self.config.n_text_state as usize;
let n_layers = self.config.n_text_layer as usize;
let n_vocab = self.config.n_vocab as usize;
// Ensure GPU decoder infrastructure is initialized
if self.gpu_decoder_weights.is_none() {
self.upload_decoder_weights_to_gpu()?;
}
if self.gpu_self_k_head_first.is_none() {
self.init_gpu_decoder_kv_cache_head_first()?;
}
// Also ensure executor weights are uploaded
if self.executor.cached_weight_count() == 0 {
self.upload_decoder_weights_to_executor()?;
}
let pos = self.gpu_decoder_pos;
// 1. Embed token + positional embedding (CPU - fast)
if token as usize >= n_vocab {
return Err(WhisperError::Inference(format!(
"token {} out of vocabulary range {}",
token, n_vocab
)));
}
let emb_start = (token as usize) * d_model;
let token_emb = self.decoder.token_embedding();
let pos_emb = self.decoder.positional_embedding();
let max_len = self.config.n_text_ctx as usize;
if pos >= max_len {
return Err(WhisperError::Inference(format!(
"position {} exceeds max {}",
pos, max_len
)));
}
let pos_start = pos * d_model;
let mut x: Vec<f32> = token_emb[emb_start..emb_start + d_model]
.iter()
.zip(&pos_emb[pos_start..pos_start + d_model])
.map(|(t, p)| t + p)
.collect();
// 2. Run through all decoder blocks (executor path)
for layer_idx in 0..n_layers {
x = self.forward_decoder_block_executor(layer_idx, &x, pos, Some(encoder_output))?;
}
// 3. Final layer norm (CPU - simple)
let hidden = self.decoder.ln_post().forward(&x)?;
// 4. Increment position for next token
self.gpu_decoder_pos += 1;
// 5. Output projection on GPU
self.project_to_vocab_gpu(&hidden)
}
/// GPU-accelerated output projection using direct gemm.
///
/// FIX 1 (WAPR-PERF-004): Use `executor.gemm()` directly instead of
/// the buggy `gemv_cached`. This allocates fresh GPU buffers per call
/// but produces correct results.
///
/// # Arguments
///
/// * `hidden` - Hidden state after final layer norm [d_model]
///
/// # Returns
///
/// Logits over vocabulary [n_vocab]
pub fn project_to_vocab_gpu(&mut self, hidden: &[f32]) -> WhisperResult<Vec<f32>> {
let n_vocab = self.config.n_vocab as usize;
let d_model = self.config.n_text_state as usize;
// Validate dimensions
if hidden.len() != d_model {
return Err(WhisperError::Inference(format!(
"Hidden state dimension mismatch: got {}, expected {}",
hidden.len(),
d_model
)));
}
let mut output = vec![0.0f32; n_vocab];
// WAPR-PERF-014: Try cached weights first, fall back to direct gemm
// GEMV: y[n] = W[n,k] @ x[k] where W = token_embedding [n_vocab × d_model]
let k = d_model as u32;
let n = n_vocab as u32;
if self.executor.has_weights("whisper_output_proj") {
// Fast path: use cached weights (persistent GPU buffer, no allocation)
self.executor
.gemv_cached("whisper_output_proj", hidden, &mut output, k, n)
.map_err(|e| WhisperError::Inference(format!("GPU gemv_cached failed: {e}")))?;
} else {
// Fallback: allocate per-call (GPU path before executor weights uploaded)
let weights = self.decoder.token_embedding();
let m = n_vocab as u32;
self.executor
.gemm(weights, hidden, &mut output, m, 1, k)
.map_err(|e| WhisperError::Inference(format!("GPU gemm failed: {e}")))?;
}
if std::env::var("WHISPER_DEBUG_GPU").is_ok() {
let max_val = output.iter().cloned().fold(f32::NEG_INFINITY, f32::max);
let argmax = output
.iter()
.enumerate()
.max_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
.map(|(i, _)| i)
.unwrap_or(0);
eprintln!(
"[GPU] project_to_vocab_gpu: max={:.4} argmax={}",
max_val, argmax
);
}
Ok(output)
}
/// Transcribe using GPU-accelerated decoding.
///
/// Uses SIMD-accelerated CPU decoder with proper KV caching.
/// GPU acceleration is available for weights upload but self-attention
/// runs on CPU due to CUDA kernel compatibility issues (WAPR-PERF-006).
///
/// # WAPR-PERF-004: Performance Analysis
///
/// Current approach:
/// 1. CPU encoder (SIMD-accelerated via trueno)
/// 2. CPU decoder with KV cache (proven correct)
/// 3. CPU output projection (trueno matmul)
///
/// GPU flash_attention_cached has kernel compatibility issues that need investigation.
/// The CPU path with trueno SIMD is the reliable baseline.
///
/// # APR-Style Tracing
///
/// When tracing is enabled via `enable_tracing()`, this function emits:
/// - `TraceStep::Embed`: Mel spectrogram computation
/// - `TraceStep::TransformerBlock`: Encoder forward pass
/// - `TraceStep::LmHead`: Output projection per token
/// - `TraceStep::Sample`: Token sampling per token
/// - `TraceStep::Decode`: Final detokenization
pub fn transcribe_gpu(
&mut self,
audio: &[f32],
options: TranscribeOptions,
) -> WhisperResult<TranscriptionResult> {
const N_SAMPLES_30S: usize = 480_000; // 30 seconds at 16kHz
// Use chunked processing for audio longer than 30 seconds
if audio.len() > N_SAMPLES_30S {
return self.transcribe_gpu_chunked(audio, options);
}
self.transcribe_gpu_single_chunk(audio, &options)
}
/// Transcribe a single chunk of audio (<=30 seconds) on GPU.
fn transcribe_gpu_single_chunk(
&mut self,
audio: &[f32],
options: &TranscribeOptions,
) -> WhisperResult<TranscriptionResult> {
const N_SAMPLES_30S: usize = 480_000;
const N_FRAMES: usize = 3000;
const N_MELS: usize = 80;
let profile_all = std::env::var("WHISPER_PROFILE_DECODER").is_ok();
let transcribe_start = std::time::Instant::now();
// === TRACE: Mel spectrogram (mapped to EMBED step) ===
self.tracer.start_step(TraceStep::Embed);
let mel = self.prepare_mel(audio, N_SAMPLES_30S, N_FRAMES, N_MELS, profile_all)?;
// Trace mel computation (token_count=N_FRAMES, hidden_dim=N_MELS)
self.tracer.trace_embed(N_FRAMES, N_MELS, Some(&mel));
// === TRACE: Encoder forward pass (mapped to TRANSFORMER_BLOCK) ===
self.tracer.start_step(TraceStep::TransformerBlock);
// Select and run encoder path
let encoder_output = self.run_encoder(&mel)?;
// Trace encoder output (layer_idx=0 for encoder, iteration=0 for prefill)
let d_model = self.config.n_text_state as usize;
let enc_seq_len = encoder_output.len() / d_model;
self.tracer
.trace_layer(0, 0, Some(&encoder_output), enc_seq_len, d_model);
// Build initial tokens
use crate::tokenizer::special_tokens::SpecialTokens;
let specials = SpecialTokens::for_vocab_size(self.config.n_vocab as usize);
let mut tokens = Self::build_initial_tokens(&specials, options);
// Hybrid GPU path:
// - CPU decoder blocks (GPU flash_attention_cached has kernel compatibility issues)
// - GPU output projection via executor.gemm() (FIX 1 - working!)
let max_tokens = self.config.n_text_ctx as usize;
let n_layers = self.config.n_text_layer as usize;
let n_vocab = self.config.n_vocab as usize;
let eot_token = specials.eot;
let mut cache = DecoderKVCache::new(n_layers, d_model, max_tokens);
// Token suppressor — do NOT suppress timestamps (needed for segment extraction)
let suppressor = crate::inference::WhisperTokenSuppressor::new()
.with_timestamp_suppression(false)
.with_vocab_size(n_vocab);
// Process initial tokens (prefill)
self.prefill_tokens(&tokens, &encoder_output, &mut cache)?;
// Generate tokens
let profile_decoder = std::env::var("WHISPER_PROFILE_DECODER").is_ok();
let mut decoder_token_times: Vec<u128> = Vec::new();
let decoder_start = std::time::Instant::now();
for gen_idx in 0..max_tokens.saturating_sub(tokens.len()) {
let token_start = std::time::Instant::now();
let last_token = *tokens.last().unwrap_or(&specials.sot);
// === TRACE: LM_HEAD (output projection) ===
self.tracer.start_step(TraceStep::LmHead);
let mut logits = self.forward_one_gpu(last_token, &encoder_output, &mut cache)?;
// Trace output projection
self.tracer.trace_lm_head(gen_idx, &logits, n_vocab);
Self::debug_logits(profile_decoder, gen_idx, &logits);
// === TRACE: SAMPLE (token selection) ===
self.tracer.start_step(TraceStep::Sample);
suppressor.apply(&mut logits);
// All strategies currently use greedy argmax
let next_token = crate::simd::argmax(&logits) as u32;
// Trace sampling result (temperature=0.0 for greedy, top_k=1)
self.tracer
.trace_sample(gen_idx, &logits, next_token, 0.0, 1);
// Track per-token timing
if profile_decoder {
decoder_token_times.push(token_start.elapsed().as_micros());
}
if next_token == eot_token {
break;
}
tokens.push(next_token);
}
Self::print_decoder_profile(profile_decoder, &decoder_token_times, &decoder_start);
// === TRACE: DECODE (detokenization) ===
self.tracer.start_step(TraceStep::Decode);
let text = self.tokenizer.decode_with_options(&tokens, true)?;
// Trace decode result (iteration=0 for final decode, last token, vocab_size)
let last_token = tokens.last().copied().unwrap_or(0);
self.tracer.trace_decode(0, last_token, &text, n_vocab);
let language = options.language.clone().unwrap_or_else(|| "en".to_string());
if profile_all {
eprintln!(
"[PROFILE-TRANSCRIBE] Total transcribe_gpu: {:.1}ms",
transcribe_start.elapsed().as_millis()
);
}
let segments = Self::build_segments(&tokens, &text, audio.len(), &self.tokenizer);
Ok(TranscriptionResult {
text,
language,
segments,
profiling: None,
})
}
/// Transcribe long audio using chunked streaming on GPU.
///
/// Splits audio into 30-second chunks, transcribes each independently,
/// then merges results with timestamp adjustment.
fn transcribe_gpu_chunked(
&mut self,
audio: &[f32],
options: TranscribeOptions,
) -> WhisperResult<TranscriptionResult> {
const CHUNK_SAMPLES: usize = 480_000; // 30 seconds at 16kHz
let language = options.language.clone().unwrap_or_else(|| "en".to_string());
let mut all_segments: Vec<crate::Segment> = Vec::new();
let mut all_text = String::new();
let mut offset = 0;
while offset < audio.len() {
let chunk_end = (offset + CHUNK_SAMPLES).min(audio.len());
let chunk = &audio[offset..chunk_end];
// Skip very short final chunks (less than 0.5 seconds)
if chunk.len() < crate::audio::SAMPLE_RATE as usize / 2 {
break;
}
let chunk_options = TranscribeOptions {
language: Some(language.clone()),
task: options.task,
strategy: options.strategy,
word_timestamps: options.word_timestamps,
profile: options.profile,
prompt: options.prompt.clone(),
hotwords: options.hotwords.clone(),
};
let chunk_result = self.transcribe_gpu_single_chunk(chunk, &chunk_options)?;
let time_offset = offset as f32 / crate::audio::SAMPLE_RATE as f32;
// Append text
if !chunk_result.text.trim().is_empty() {
if !all_text.is_empty() {
all_text.push(' ');
}
all_text.push_str(&chunk_result.text);
}
// Adjust segment timestamps and collect
for mut seg in chunk_result.segments {
seg.start += time_offset;
seg.end += time_offset;
all_segments.push(seg);
}
offset += CHUNK_SAMPLES;
}
// Split any remaining long segments at sentence boundaries
let final_segments = crate::timestamps::split_long_segments(&all_segments, 10.0);
Ok(TranscriptionResult {
text: all_text,
language,
segments: final_segments,
profiling: None,
})
}
/// Select and run the encoder path based on environment variables.
fn run_encoder(&mut self, mel: &[f32]) -> WhisperResult<Vec<f32>> {
#[cfg(feature = "cuda")]
{
let use_gpu_total_offload = std::env::var("WHISPER_GPU_TOTAL_OFFLOAD").is_ok();
let use_gpu_encoder = std::env::var("WHISPER_GPU_ENCODER").is_ok();
if use_gpu_total_offload {
eprintln!("[WAPR-PERF-014] Using GPU total-offload encoder...");
return self.encode_gpu_total_offload(mel);
} else if use_gpu_encoder {
eprintln!("[WAPR-PERF-005] Using GPU attention-only encoder...");
return self.encode_gpu(mel);
}
}
self.encoder.forward_mel(mel)
}
/// Run decoder prefill on initial tokens.
fn prefill_tokens(
&mut self,
tokens: &[u32],
encoder_output: &[f32],
cache: &mut DecoderKVCache,
) -> WhisperResult<()> {
let prefill_start = std::time::Instant::now();
for &token in tokens {
let _ = self.decoder.forward_one(token, encoder_output, cache)?;
}
if std::env::var("WHISPER_PROFILE_DECODER").is_ok() {
let prefill_time = prefill_start.elapsed();
eprintln!(
"[PROFILE-PREFILL] {} tokens in {:.1}ms ({:.1}ms/token)",
tokens.len(),
prefill_time.as_millis(),
prefill_time.as_millis() as f64 / tokens.len() as f64
);
}
Ok(())
}
/// Build the initial token sequence for decoder prefill.
fn build_initial_tokens(
specials: &crate::tokenizer::special_tokens::SpecialTokens,
options: &TranscribeOptions,
) -> Vec<u32> {
use crate::tokenizer::special_tokens;
let mut tokens = vec![specials.sot];
if specials.is_multilingual {
let language = options.language.as_deref().unwrap_or("en");
let lang_offset = special_tokens::language_offset(language).unwrap_or(0);
tokens.push(specials.lang_base + lang_offset);
}
match options.task {
Task::Transcribe => tokens.push(specials.transcribe),
Task::Translate => tokens.push(special_tokens::TRANSLATE),
}
// Timestamp mode: do NOT push no_timestamps token.
// This enables the decoder to produce <|t.tt|> timestamp tokens
// which are needed for proper SRT/VTT segment timing.
tokens
}
/// Prepare mel spectrogram from audio (pad/truncate + compute + frame normalization).
fn prepare_mel(
&self,
audio: &[f32],
n_samples_30s: usize,
n_frames: usize,
n_mels: usize,
profile: bool,
) -> WhisperResult<Vec<f32>> {
let mel_start = std::time::Instant::now();
let padded_audio = match audio.len().cmp(&n_samples_30s) {
std::cmp::Ordering::Equal => audio.to_vec(),
std::cmp::Ordering::Less => {
let mut padded = vec![0.0_f32; n_samples_30s];
padded[..audio.len()].copy_from_slice(audio);
padded
}
std::cmp::Ordering::Greater => audio[..n_samples_30s].to_vec(),
};
let mut mel = self
.mel_filters
.compute(&padded_audio)
.map_err(|e| WhisperError::Audio(e.to_string()))?;
if profile {
eprintln!(
"[PROFILE-MEL] Mel spectrogram: {:.1}ms",
mel_start.elapsed().as_millis()
);
}
let actual_frames = mel.len() / n_mels;
if actual_frames < n_frames {
let mut padded_mel = vec![-1.0_f32; n_frames * n_mels];
padded_mel[..mel.len()].copy_from_slice(&mel);
mel = padded_mel;
} else if actual_frames > n_frames {
mel.truncate(n_frames * n_mels);
}
Ok(mel)
}
/// Print debug logits stats for early generation steps.
fn debug_logits(enabled: bool, gen_idx: usize, logits: &[f32]) {
if enabled && gen_idx < 3 {
let logits_mean = logits.iter().sum::<f32>() / logits.len() as f32;
let logits_max = logits.iter().cloned().fold(f32::NEG_INFINITY, f32::max);
let argmax = logits
.iter()
.enumerate()
.max_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
.map(|(i, _)| i)
.unwrap_or(0);
eprintln!(
"[DEBUG-LOGITS] gen_idx={gen_idx} len={} mean={logits_mean:.4} max={logits_max:.4} argmax={argmax}",
logits.len(),
);
}
}
/// Print decoder profiling summary if enabled.
fn print_decoder_profile(
enabled: bool,
token_times: &[u128],
decoder_start: &std::time::Instant,
) {
if enabled && !token_times.is_empty() {
let decoder_total = decoder_start.elapsed();
let sum: u128 = token_times.iter().sum();
let avg = sum as f64 / token_times.len() as f64;
let max = token_times.iter().max().copied().unwrap_or(0);
let min = token_times.iter().min().copied().unwrap_or(0);
eprintln!(
"[PROFILE-DECODER] {} tokens, total {:.1}ms, avg {:.1}ms, min {:.1}ms, max {:.1}ms",
token_times.len(),
decoder_total.as_millis(),
avg / 1000.0,
min as f64 / 1000.0,
max as f64 / 1000.0
);
}
}
/// Build segments from decoded tokens (mirrors CPU path).
fn build_segments(
tokens: &[u32],
text: &str,
audio_len: usize,
tokenizer: &BpeTokenizer,
) -> Vec<crate::Segment> {
if crate::timestamps::has_timestamps(tokens) {
crate::timestamps::extract_segments(tokens, |ts| tokenizer.decode(ts).ok())
} else if !text.trim().is_empty() {
let duration = audio_len as f32 / crate::audio::SAMPLE_RATE as f32;
let single = vec![crate::Segment {
start: 0.0,
end: duration,
text: text.to_string(),
tokens: tokens.to_vec(),
}];
crate::timestamps::split_long_segments(&single, 10.0)
} else {
Vec::new()
}
}
/// Print trace summary to stderr.
///
/// Shows timing breakdown by step, total duration, and any detected anomalies.
/// Call this after `transcribe_gpu()` to see performance analysis.
pub fn print_trace_summary(&self) {
if !self.tracer.is_enabled() {
eprintln!(
"[TRACE] Tracing not enabled. Call enable_tracing(TraceConfig::enabled()) first."
);
return;
}
let events = self.tracer.events();
if events.is_empty() {
eprintln!("[TRACE] No events collected.");
return;
}
// Compute totals by step
let mut step_durations: std::collections::HashMap<&'static str, u64> =
std::collections::HashMap::new();
let mut step_counts: std::collections::HashMap<&'static str, usize> =
std::collections::HashMap::new();
for event in events {
*step_durations.entry(event.step.name()).or_insert(0) += event.duration_us;
*step_counts.entry(event.step.name()).or_insert(0) += 1;
}
let total_us: u64 = step_durations.values().sum();
let total_ms = total_us as f64 / 1000.0;
eprintln!("=== APR-Style Inference Trace Summary ===");
eprintln!("Total: {:.2}ms ({} events)", total_ms, events.len());
eprintln!();
eprintln!(
"{:20} {:>8} {:>8} {:>8}",
"STEP", "COUNT", "TIME(ms)", "PCT"
);
eprintln!("{:-<20} {:->8} {:->8} {:->8}", "", "", "", "");
// Sort by duration descending
let mut steps: Vec<_> = step_durations.iter().collect();
steps.sort_by(|a, b| b.1.cmp(a.1));
for (step, us) in steps {
let count = step_counts.get(step).unwrap_or(&0);
let ms = *us as f64 / 1000.0;
let pct = if total_us > 0 {
(*us as f64 / total_us as f64) * 100.0
} else {
0.0
};
eprintln!("{:20} {:>8} {:>8.2} {:>7.1}%", step, count, ms, pct);
}
eprintln!();
}
// ========================================================================
// WAPR-PERF-014: Stream-Optimized Decoder (CudaExecutor-based)
// ========================================================================
//
// Root cause of 10x slowdown: GpuResidentTensor creates new CUDA stream
// per operation (~40 streams per token). Fix: use CudaExecutor's persistent
// compute_stream for all operations.
/// WAPR-PERF-014: Upload decoder weights to CudaExecutor's weight_cache
///
/// Unlike `upload_decoder_weights_to_gpu()` which stores in GpuResidentTensor,
/// this uploads to executor's weight_cache for use with `gemm_cached_async()`.
///
/// # Naming Convention
///
/// Weights are cached with names: `dec.L{layer}.{component}`
/// - `dec.L0.self_w_q` - Self-attention Q projection
/// - `dec.L0.ffn_fc1` - FFN first layer
#[cfg(feature = "cuda")]
pub fn upload_decoder_weights_to_executor(&mut self) -> WhisperResult<usize> {
let d_model = self.config.n_text_state as usize;
let n_layers = self.config.n_text_layer as usize;
let d_ff = d_model * 4;
let mut total_bytes = 0;
// Helper to transpose weight matrix from [rows, cols] to [cols, rows]
fn transpose_weights(weights: &[f32], rows: usize, cols: usize) -> Vec<f32> {
let mut transposed = vec![0.0_f32; weights.len()];
for r in 0..rows {
for c in 0..cols {
transposed[c * rows + r] = weights[r * cols + c];
}
}
transposed
}
for layer_idx in 0..n_layers {
let block = &self.decoder.blocks()[layer_idx];
// Self-attention Q/K/V/O (transposed for GPU: [in, out])
let w_q_t = transpose_weights(&block.self_attn.w_q().weight, d_model, d_model);
total_bytes += self
.executor
.load_weights(&format!("dec.L{layer_idx}.self_w_q"), &w_q_t)
.map_err(|e| WhisperError::Inference(format!("dec self_w_q L{layer_idx}: {e}")))?;
total_bytes += self
.executor
.load_weights(
&format!("dec.L{layer_idx}.self_b_q"),
&block.self_attn.w_q().bias,
)
.map_err(|e| WhisperError::Inference(format!("dec self_b_q L{layer_idx}: {e}")))?;
let w_k_t = transpose_weights(&block.self_attn.w_k().weight, d_model, d_model);
total_bytes += self
.executor
.load_weights(&format!("dec.L{layer_idx}.self_w_k"), &w_k_t)
.map_err(|e| WhisperError::Inference(format!("dec self_w_k L{layer_idx}: {e}")))?;
total_bytes += self
.executor
.load_weights(
&format!("dec.L{layer_idx}.self_b_k"),
&block.self_attn.w_k().bias,
)
.map_err(|e| WhisperError::Inference(format!("dec self_b_k L{layer_idx}: {e}")))?;
let w_v_t = transpose_weights(&block.self_attn.w_v().weight, d_model, d_model);
total_bytes += self
.executor
.load_weights(&format!("dec.L{layer_idx}.self_w_v"), &w_v_t)
.map_err(|e| WhisperError::Inference(format!("dec self_w_v L{layer_idx}: {e}")))?;
total_bytes += self
.executor
.load_weights(
&format!("dec.L{layer_idx}.self_b_v"),
&block.self_attn.w_v().bias,
)
.map_err(|e| WhisperError::Inference(format!("dec self_b_v L{layer_idx}: {e}")))?;
let w_o_t = transpose_weights(&block.self_attn.w_o().weight, d_model, d_model);
total_bytes += self
.executor
.load_weights(&format!("dec.L{layer_idx}.self_w_o"), &w_o_t)
.map_err(|e| WhisperError::Inference(format!("dec self_w_o L{layer_idx}: {e}")))?;
total_bytes += self
.executor
.load_weights(
&format!("dec.L{layer_idx}.self_b_o"),
&block.self_attn.w_o().bias,
)
.map_err(|e| WhisperError::Inference(format!("dec self_b_o L{layer_idx}: {e}")))?;
// Cross-attention Q/K/V/O
let cross_w_q_t = transpose_weights(&block.cross_attn.w_q().weight, d_model, d_model);
total_bytes += self
.executor
.load_weights(&format!("dec.L{layer_idx}.cross_w_q"), &cross_w_q_t)
.map_err(|e| WhisperError::Inference(format!("dec cross_w_q L{layer_idx}: {e}")))?;
total_bytes += self
.executor
.load_weights(
&format!("dec.L{layer_idx}.cross_b_q"),
&block.cross_attn.w_q().bias,
)
.map_err(|e| WhisperError::Inference(format!("dec cross_b_q L{layer_idx}: {e}")))?;
let cross_w_k_t = transpose_weights(&block.cross_attn.w_k().weight, d_model, d_model);
total_bytes += self
.executor
.load_weights(&format!("dec.L{layer_idx}.cross_w_k"), &cross_w_k_t)
.map_err(|e| WhisperError::Inference(format!("dec cross_w_k L{layer_idx}: {e}")))?;
total_bytes += self
.executor
.load_weights(
&format!("dec.L{layer_idx}.cross_b_k"),
&block.cross_attn.w_k().bias,
)
.map_err(|e| WhisperError::Inference(format!("dec cross_b_k L{layer_idx}: {e}")))?;
let cross_w_v_t = transpose_weights(&block.cross_attn.w_v().weight, d_model, d_model);
total_bytes += self
.executor
.load_weights(&format!("dec.L{layer_idx}.cross_w_v"), &cross_w_v_t)
.map_err(|e| WhisperError::Inference(format!("dec cross_w_v L{layer_idx}: {e}")))?;
total_bytes += self
.executor
.load_weights(
&format!("dec.L{layer_idx}.cross_b_v"),
&block.cross_attn.w_v().bias,
)
.map_err(|e| WhisperError::Inference(format!("dec cross_b_v L{layer_idx}: {e}")))?;
let cross_w_o_t = transpose_weights(&block.cross_attn.w_o().weight, d_model, d_model);
total_bytes += self
.executor
.load_weights(&format!("dec.L{layer_idx}.cross_w_o"), &cross_w_o_t)
.map_err(|e| WhisperError::Inference(format!("dec cross_w_o L{layer_idx}: {e}")))?;
total_bytes += self
.executor
.load_weights(
&format!("dec.L{layer_idx}.cross_b_o"),
&block.cross_attn.w_o().bias,
)
.map_err(|e| WhisperError::Inference(format!("dec cross_b_o L{layer_idx}: {e}")))?;
// FFN weights (fc1: d_model -> d_ff, fc2: d_ff -> d_model)
let fc1_t = transpose_weights(&block.ffn.fc1.weight, d_ff, d_model);
total_bytes += self
.executor
.load_weights(&format!("dec.L{layer_idx}.ffn_fc1"), &fc1_t)
.map_err(|e| WhisperError::Inference(format!("dec ffn_fc1 L{layer_idx}: {e}")))?;
total_bytes += self
.executor
.load_weights(&format!("dec.L{layer_idx}.ffn_b1"), &block.ffn.fc1.bias)
.map_err(|e| WhisperError::Inference(format!("dec ffn_b1 L{layer_idx}: {e}")))?;
let fc2_t = transpose_weights(&block.ffn.fc2.weight, d_model, d_ff);
total_bytes += self
.executor
.load_weights(&format!("dec.L{layer_idx}.ffn_fc2"), &fc2_t)
.map_err(|e| WhisperError::Inference(format!("dec ffn_fc2 L{layer_idx}: {e}")))?;
total_bytes += self
.executor
.load_weights(&format!("dec.L{layer_idx}.ffn_b2"), &block.ffn.fc2.bias)
.map_err(|e| WhisperError::Inference(format!("dec ffn_b2 L{layer_idx}: {e}")))?;
// LayerNorm weights (gamma/beta)
total_bytes += self
.executor
.load_weights(&format!("dec.L{layer_idx}.ln1_gamma"), &block.ln1.weight)
.map_err(|e| WhisperError::Inference(format!("dec ln1_gamma L{layer_idx}: {e}")))?;
total_bytes += self
.executor
.load_weights(&format!("dec.L{layer_idx}.ln1_beta"), &block.ln1.bias)
.map_err(|e| WhisperError::Inference(format!("dec ln1_beta L{layer_idx}: {e}")))?;
total_bytes += self
.executor
.load_weights(&format!("dec.L{layer_idx}.ln2_gamma"), &block.ln2.weight)
.map_err(|e| WhisperError::Inference(format!("dec ln2_gamma L{layer_idx}: {e}")))?;
total_bytes += self
.executor
.load_weights(&format!("dec.L{layer_idx}.ln2_beta"), &block.ln2.bias)
.map_err(|e| WhisperError::Inference(format!("dec ln2_beta L{layer_idx}: {e}")))?;
total_bytes += self
.executor
.load_weights(&format!("dec.L{layer_idx}.ln3_gamma"), &block.ln3.weight)
.map_err(|e| WhisperError::Inference(format!("dec ln3_gamma L{layer_idx}: {e}")))?;
total_bytes += self
.executor
.load_weights(&format!("dec.L{layer_idx}.ln3_beta"), &block.ln3.bias)
.map_err(|e| WhisperError::Inference(format!("dec ln3_beta L{layer_idx}: {e}")))?;
}
// Output projection weights (token embedding)
// WAPR-PERF-014: Token embedding is [n_vocab, d_model] but GEMV kernel expects [k, n]
// where k=d_model (input) and n=n_vocab (output), so transpose to [d_model, n_vocab]
let n_vocab = self.config.n_vocab as usize;
let token_emb = self.decoder.token_embedding();
let token_emb_t = transpose_weights(token_emb, n_vocab, d_model);
total_bytes += self
.executor
.load_weights("dec.output_proj", &token_emb_t)
.map_err(|e| WhisperError::Inference(format!("dec output_proj: {e}")))?;
// Final layer norm
let ln_post = self.decoder.ln_post();
total_bytes += self
.executor
.load_weights("dec.ln_post_gamma", &ln_post.weight)
.map_err(|e| WhisperError::Inference(format!("dec ln_post_gamma: {e}")))?;
total_bytes += self
.executor
.load_weights("dec.ln_post_beta", &ln_post.bias)
.map_err(|e| WhisperError::Inference(format!("dec ln_post_beta: {e}")))?;
if std::env::var("WHISPER_DEBUG_GPU").is_ok() {
eprintln!(
"[WAPR-PERF-014] Uploaded {} decoder weight tensors ({:.2} MB) to executor",
self.executor.cached_weight_count(),
total_bytes as f64 / 1_048_576.0
);
}
Ok(total_bytes)
}
/// WAPR-PERF-014: Executor-based decoder block forward pass
///
/// Uses CudaExecutor's persistent stream for all GEMV operations, avoiding
/// the ~40 stream creations per token that caused 10x slowdown.
///
/// # Key Optimizations
///
/// 1. Uses `executor.gemv_cached()` with pre-uploaded weights (persistent stream)
/// 2. Minimizes H2D/D2H transfers (only input/output, not per-projection)
/// 3. Keeps LayerNorm on CPU (fast enough, avoids gamma/beta upload overhead)
///
/// # Parameters
///
/// - `layer_idx`: Decoder layer index
/// - `x`: Input hidden state [d_model]
/// - `pos`: Current position in sequence
/// - `encoder_output`: Optional encoder hidden states for cross-attention
#[cfg(feature = "cuda")]
pub fn forward_decoder_block_executor(
&mut self,
layer_idx: usize,
x: &[f32],
pos: usize,
encoder_output: Option<&[f32]>,
) -> WhisperResult<Vec<f32>> {
use trueno_gpu::driver::CudaStream;
let d_model = self.config.n_text_state as usize;
let n_heads = self.config.n_text_head as usize;
let head_dim = d_model / n_heads;
let max_seq_len = self.config.n_text_ctx as usize;
// Copy biases first (before any mutable borrows)
let block = &self.decoder.blocks()[layer_idx];
let b_q = block.self_attn.w_q().bias.clone();
let b_k = block.self_attn.w_k().bias.clone();
let b_v = block.self_attn.w_v().bias.clone();
let b_o = block.self_attn.w_o().bias.clone();
// LN1 (CPU - simple and correct)
let normed = block.ln1.forward(x)?;
// === Q/K/V projections via executor (persistent stream, no new streams!) ===
let mut q = vec![0.0f32; d_model];
let mut k = vec![0.0f32; d_model];
let mut v = vec![0.0f32; d_model];
self.executor
.gemv_cached(
&format!("dec.L{layer_idx}.self_w_q"),
&normed,
&mut q,
d_model as u32,
d_model as u32,
)
.map_err(|e| WhisperError::Inference(format!("Q projection: {e}")))?;
self.executor
.gemv_cached(
&format!("dec.L{layer_idx}.self_w_k"),
&normed,
&mut k,
d_model as u32,
d_model as u32,
)
.map_err(|e| WhisperError::Inference(format!("K projection: {e}")))?;
self.executor
.gemv_cached(
&format!("dec.L{layer_idx}.self_w_v"),
&normed,
&mut v,
d_model as u32,
d_model as u32,
)
.map_err(|e| WhisperError::Inference(format!("V projection: {e}")))?;
// Add biases (CPU - fast)
for i in 0..d_model {
q[i] += b_q[i];
k[i] += b_k[i];
v[i] += b_v[i];
}
// Now get context for GPU tensor operations (after gemv_cached calls)
let ctx = self.executor.context();
// Get head-first KV caches
let self_k_caches = self
.gpu_self_k_head_first
.as_mut()
.ok_or_else(|| WhisperError::Inference("Self K cache not initialized".into()))?;
let self_v_caches = self
.gpu_self_v_head_first
.as_mut()
.ok_or_else(|| WhisperError::Inference("Self V cache not initialized".into()))?;
// Upload Q/K/V for KV cache scatter + attention
let q_gpu = GpuResidentTensor::from_host(ctx, &q)
.map_err(|e| WhisperError::Inference(format!("Q upload: {e}")))?;
let k_gpu = GpuResidentTensor::from_host(ctx, &k)
.map_err(|e| WhisperError::Inference(format!("K upload: {e}")))?;
let v_gpu = GpuResidentTensor::from_host(ctx, &v)
.map_err(|e| WhisperError::Inference(format!("V upload: {e}")))?;
// Create stream for scatter + attention (stream pooling tracked in WAPR-PERF-010)
let stream =
CudaStream::new(ctx).map_err(|e| WhisperError::Inference(format!("Stream: {e}")))?;
kv_cache_scatter_gpu(
ctx,
&k_gpu,
&mut self_k_caches[layer_idx],
pos as u32,
n_heads as u32,
head_dim as u32,
max_seq_len as u32,
&stream,
)
.map_err(|e| WhisperError::Inference(format!("K scatter: {e}")))?;
kv_cache_scatter_gpu(
ctx,
&v_gpu,
&mut self_v_caches[layer_idx],
pos as u32,
n_heads as u32,
head_dim as u32,
max_seq_len as u32,
&stream,
)
.map_err(|e| WhisperError::Inference(format!("V scatter: {e}")))?;
// Incremental self-attention (WAPR-PERF-014: use shared stream)
let seq_len = (pos + 1) as u32;
let attn_out = incremental_attention_gpu_with_stream(
ctx,
&q_gpu,
&self_k_caches[layer_idx],
&self_v_caches[layer_idx],
n_heads as u32,
head_dim as u32,
seq_len,
max_seq_len as u32,
&stream, // Reuse stream from KV scatter (no new stream creation!)
)
.map_err(|e| WhisperError::Inference(format!("Self attention: {e}")))?;
// Sync before reading back (all kernels launched on shared stream)
stream
.synchronize()
.map_err(|e| WhisperError::Inference(format!("Stream sync: {e}")))?;
// WAPR-PERF-014: Drop ctx/stream borrows (NLL ends them) before &mut self.executor
let mut attn_out = attn_out; // Move to local
let attn_out_host = attn_out
.to_host()
.map_err(|e| WhisperError::Inference(format!("Attn D2H: {e}")))?;
let mut attn_proj = vec![0.0f32; d_model];
self.executor
.gemv_cached(
&format!("dec.L{layer_idx}.self_w_o"),
&attn_out_host,
&mut attn_proj,
d_model as u32,
d_model as u32,
)
.map_err(|e| WhisperError::Inference(format!("O projection: {e}")))?;
// Add O bias (using pre-copied b_o)
for i in 0..d_model {
attn_proj[i] += b_o[i];
}
// Residual connection
let mut residual: Vec<f32> = x.iter().zip(attn_proj.iter()).map(|(a, b)| a + b).collect();
// === Cross-Attention (re-borrow block) ===
if let Some(enc_out) = encoder_output {
let block = &self.decoder.blocks()[layer_idx];
let normed2 = block.ln2.forward(&residual)?;
let cross_out = block
.cross_attn
.forward_cross_dispatch(&normed2, enc_out, None)?;
for (r, c) in residual.iter_mut().zip(cross_out.iter()) {
*r += c;
}
}
// === FFN (CPU - already optimized with SIMD) ===
let block = &self.decoder.blocks()[layer_idx];
let normed3 = block.ln3.forward(&residual)?;
let ffn_out = block.ffn.forward(&normed3)?;
for (r, f) in residual.iter_mut().zip(ffn_out.iter()) {
*r += f;
}
Ok(residual)
}
/// WAPR-PERF-014: Full executor-based decoder forward pass
///
/// Uses `forward_decoder_block_executor` for all layers.
#[cfg(feature = "cuda")]
pub fn forward_decoder_token_executor(
&mut self,
token_embedding: &[f32],
pos: usize,
encoder_output: &[f32],
) -> WhisperResult<Vec<f32>> {
let n_layers = self.config.n_text_layer as usize;
// Ensure executor weights are uploaded
if self.executor.cached_weight_count() == 0 {
self.upload_decoder_weights_to_executor()?;
}
// Also ensure GPU weights for KV cache scatter
if self.gpu_decoder_weights.is_none() {
self.upload_decoder_weights_to_gpu()?;
}
if self.gpu_self_k_head_first.is_none() {
self.init_gpu_decoder_kv_cache_head_first()?;
}
// Process through all layers
let mut hidden = token_embedding.to_vec();
for layer_idx in 0..n_layers {
hidden =
self.forward_decoder_block_executor(layer_idx, &hidden, pos, Some(encoder_output))?;
}
Ok(hidden)
}
}