stwo-gpu 2.0.0

GPU-accelerated Circle STARK prover and verifier — ObelyZK fork of STWO with CUDA/Metal backend
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
//! GPU Proof Pipeline
//!
//! This module provides a high-performance proof generation pipeline that keeps
//! all data on GPU throughout the entire proof process. This eliminates the
//! CPU-GPU transfer overhead that dominates naive implementations.
//!
//! # Architecture
//!
//! ```text
//! ┌─────────────────────────────────────────────────────────────────┐
//! │                    GPU Memory (persistent)                       │
//! ├─────────────────────────────────────────────────────────────────┤
//! │                                                                  │
//! │   Trace Data ──→ [Commit FFT] ──→ Committed Poly                │
//! │                        │                                         │
//! │                        ▼                                         │
//! │              [Quotient Accumulation]                             │
//! │                        │                                         │
//! │                        ▼                                         │
//! │                  [FRI Folding] ←── repeated                     │
//! │                        │                                         │
//! │                        ▼                                         │
//! │               [Merkle Hashing]                                   │
//! │                        │                                         │
//! │                        ▼                                         │
//! │                  Final Proof ──→ Transfer to CPU (once!)        │
//! │                                                                  │
//! └─────────────────────────────────────────────────────────────────┘
//! ```
//!
//! # Performance
//!
//! By keeping data on GPU:
//! - Single transfer in (trace data)
//! - All computation on GPU (FFT, FRI, Quotient, Merkle)
//! - Single transfer out (final proof)
//!
//! This achieves 50-100x speedup over naive per-operation transfers.

#[cfg(feature = "cuda-runtime")]
use cudarc::driver::{CudaSlice, CudaStream, LaunchConfig, LaunchAsync};

#[cfg(feature = "cuda-runtime")]
use super::cuda_executor::{CudaFftError, CudaFftExecutor, get_cuda_executor, get_executor_for_device};

#[cfg(feature = "cuda-runtime")]
use super::optimizations::{CudaGraph, get_pinned_pool_u32};

#[cfg(feature = "cuda-runtime")]
use std::sync::Arc;

#[cfg(feature = "cuda-runtime")]
use super::fft::{compute_itwiddle_dbls_cpu, compute_twiddle_dbls_cpu};

/// GPU Proof Pipeline - Manages persistent GPU memory for proof generation.
///
/// This struct holds GPU memory allocations that persist across multiple
/// operations, eliminating transfer overhead.
///
/// # Multi-GPU Support
///
/// Each pipeline is bound to a specific GPU device and uses an Arc-wrapped
/// executor from the device pool. This enables true multi-GPU parallelism:
///
/// ```ignore
/// let pipeline0 = GpuProofPipeline::new_on_device(20, 0)?;
/// let pipeline1 = GpuProofPipeline::new_on_device(20, 1)?;
/// // Both pipelines can run concurrently on different GPUs
/// ```
#[cfg(feature = "cuda-runtime")]
pub struct GpuProofPipeline {
    /// Polynomial data on GPU (multiple polynomials)
    poly_data: Vec<CudaSlice<u32>>,

    /// Twiddles on GPU (cached per log_size)
    itwiddles: CudaSlice<u32>,
    twiddles: CudaSlice<u32>,
    twiddle_offsets: CudaSlice<u32>,

    /// Current polynomial log size
    log_size: u32,

    /// CPU-side twiddle data (for layer info)
    itwiddles_cpu: Vec<Vec<u32>>,
    twiddles_cpu: Vec<Vec<u32>>,

    /// Per-pipeline executor from the device pool.
    /// This enables true multi-GPU by giving each pipeline its own executor.
    executor: Arc<CudaFftExecutor>,

    /// Device ID this pipeline is running on
    device_id: usize,

    /// Optional CUDA Graph for accelerated FFT execution.
    /// Provides 20-40% speedup for repeated FFT operations of the same size.
    fft_graph: Option<CudaGraph>,

    /// Optional CUDA Graph for accelerated IFFT execution.
    ifft_graph: Option<CudaGraph>,

    /// Whether to use graph-accelerated execution (enabled by default).
    use_graphs: bool,
}

#[cfg(feature = "cuda-runtime")]
unsafe impl Send for GpuProofPipeline {}
#[cfg(feature = "cuda-runtime")]
unsafe impl Sync for GpuProofPipeline {}

#[cfg(feature = "cuda-runtime")]
impl GpuProofPipeline {
    /// Create a new GPU proof pipeline for polynomials of the given size.
    /// Uses the global executor (GPU 0).
    pub fn new(log_size: u32) -> Result<Self, CudaFftError> {
        Self::new_on_device(log_size, 0)
    }
    
    /// Create a new GPU proof pipeline on a specific GPU device.
    /// 
    /// # Arguments
    /// * `log_size` - Log2 of the polynomial size
    /// * `device_id` - GPU device ID (0, 1, 2, etc.)
    /// 
    /// # Multi-GPU
    /// 
    /// Each device_id gets its own executor from the pool, enabling true
    /// parallel execution across multiple GPUs.
    pub fn new_on_device(log_size: u32, device_id: usize) -> Result<Self, CudaFftError> {
        // Get executor from the device pool (cached, thread-safe)
        let executor = get_executor_for_device(device_id)?;
        
        // Precompute and cache twiddles
        let itwiddles_cpu = compute_itwiddle_dbls_cpu(log_size);
        let twiddles_cpu = compute_twiddle_dbls_cpu(log_size);
        
        // Flatten and upload twiddles to GPU
        let flat_itwiddles: Vec<u32> = itwiddles_cpu.iter().flatten().copied().collect();
        let flat_twiddles: Vec<u32> = twiddles_cpu.iter().flatten().copied().collect();
        
        let itwiddles = executor.device.htod_sync_copy(&flat_itwiddles)
            .map_err(|e| CudaFftError::MemoryAllocation(format!("{:?}", e)))?;
        let twiddles = executor.device.htod_sync_copy(&flat_twiddles)
            .map_err(|e| CudaFftError::MemoryAllocation(format!("{:?}", e)))?;
        
        // Calculate and upload twiddle offsets
        let mut offsets: Vec<u32> = Vec::new();
        let mut offset = 0u32;
        for tw in &itwiddles_cpu {
            offsets.push(offset);
            offset += tw.len() as u32;
        }
        let twiddle_offsets = executor.device.htod_sync_copy(&offsets)
            .map_err(|e| CudaFftError::MemoryAllocation(format!("{:?}", e)))?;
        
        tracing::info!("Created GPU pipeline on device {} with log_size {}", device_id, log_size);

        Ok(Self {
            poly_data: Vec::new(),
            itwiddles,
            twiddles,
            twiddle_offsets,
            log_size,
            itwiddles_cpu,
            twiddles_cpu,
            executor,
            device_id,
            fft_graph: None,
            ifft_graph: None,
            use_graphs: true, // Enable graph acceleration by default
        })
    }

    /// Enable or disable CUDA Graph acceleration.
    ///
    /// When enabled (default), FFT/IFFT operations are captured into CUDA Graphs
    /// on first execution and replayed on subsequent calls. This provides
    /// 20-40% speedup from reduced kernel launch overhead.
    ///
    /// # Arguments
    /// * `enabled` - Whether to use graph acceleration
    pub fn set_use_graphs(&mut self, enabled: bool) {
        self.use_graphs = enabled;
        if !enabled {
            // Clear existing graphs
            self.fft_graph = None;
            self.ifft_graph = None;
        }
    }

    /// Check if CUDA Graph acceleration is enabled.
    pub fn uses_graphs(&self) -> bool {
        self.use_graphs
    }

    /// Capture the FFT sequence into a CUDA Graph for accelerated replay.
    ///
    /// This method captures the FFT kernel sequence and stores it for
    /// subsequent replay. Call this once before repeated FFT operations.
    ///
    /// # Returns
    /// `Ok(())` if capture succeeded, `Err` otherwise.
    pub fn capture_fft_graph(&mut self) -> Result<(), CudaFftError> {
        let mut graph = CudaGraph::new(self.executor.device.clone())?;

        // Begin capture
        graph.begin_capture()?;

        // We need to execute the FFT on a dummy polynomial to capture the operations
        // For capture, we use a placeholder buffer
        let n = 1usize << self.log_size;
        let dummy_data: Vec<u32> = vec![0u32; n];
        let mut d_dummy = self.executor.device.htod_sync_copy(&dummy_data)
            .map_err(|e| CudaFftError::MemoryAllocation(format!("{:?}", e)))?;

        // Execute FFT kernels (they will be captured)
        self.execute_fft_kernels(&mut d_dummy, false)?;

        // End capture
        graph.end_capture()?;

        self.fft_graph = Some(graph);
        tracing::info!("Captured FFT graph for log_size={}", self.log_size);

        Ok(())
    }

    /// Capture the IFFT sequence into a CUDA Graph for accelerated replay.
    pub fn capture_ifft_graph(&mut self) -> Result<(), CudaFftError> {
        let mut graph = CudaGraph::new(self.executor.device.clone())?;

        graph.begin_capture()?;

        let n = 1usize << self.log_size;
        let dummy_data: Vec<u32> = vec![0u32; n];
        let mut d_dummy = self.executor.device.htod_sync_copy(&dummy_data)
            .map_err(|e| CudaFftError::MemoryAllocation(format!("{:?}", e)))?;

        self.execute_ifft_kernels(&mut d_dummy)?;

        graph.end_capture()?;

        self.ifft_graph = Some(graph);
        tracing::info!("Captured IFFT graph for log_size={}", self.log_size);

        Ok(())
    }

    /// Internal: Execute FFT kernels (used for both direct execution and graph capture).
    fn execute_fft_kernels(&self, data: &mut CudaSlice<u32>, sync: bool) -> Result<(), CudaFftError> {
        let block_size = 256u32;
        let num_layers = self.twiddles_cpu.len();

        // Calculate twiddle offsets
        let mut twiddle_offsets: Vec<usize> = Vec::new();
        let mut offset = 0usize;
        for tw in &self.twiddles_cpu {
            twiddle_offsets.push(offset);
            offset += tw.len();
        }

        // Execute layers in reverse order for forward FFT
        for layer in (0..num_layers).rev() {
            let n_twiddles = self.twiddles_cpu[layer].len() as u32;
            let butterflies_per_twiddle = 1u32 << layer;
            let total_butterflies = n_twiddles * butterflies_per_twiddle;
            let grid_size = (total_butterflies + block_size - 1) / block_size;

            let twiddle_offset = twiddle_offsets[layer];

            let cfg = LaunchConfig {
                grid_dim: (grid_size, 1, 1),
                block_dim: (block_size, 1, 1),
                shared_mem_bytes: 0,
            };

            let twiddle_view = self.twiddles.slice(twiddle_offset..);

            unsafe {
                self.executor.kernels.fft_layer.clone().launch(
                    cfg,
                    (&mut *data, &twiddle_view, layer as u32, self.log_size, n_twiddles),
                ).map_err(|e| CudaFftError::KernelExecution(format!("{:?}", e)))?;
            }
        }

        if sync {
            self.executor.device.synchronize()
                .map_err(|e| CudaFftError::KernelExecution(format!("Sync failed: {:?}", e)))?;
        }

        Ok(())
    }

    /// Internal: Execute IFFT kernels.
    fn execute_ifft_kernels(&self, data: &mut CudaSlice<u32>) -> Result<(), CudaFftError> {
        let block_size = 256u32;
        let num_layers = self.itwiddles_cpu.len();

        let mut twiddle_offsets: Vec<usize> = Vec::new();
        let mut offset = 0usize;
        for tw in &self.itwiddles_cpu {
            twiddle_offsets.push(offset);
            offset += tw.len();
        }

        for layer in 0..num_layers {
            let n_twiddles = self.itwiddles_cpu[layer].len() as u32;
            let butterflies_per_twiddle = 1u32 << layer;
            let total_butterflies = n_twiddles * butterflies_per_twiddle;
            let grid_size = (total_butterflies + block_size - 1) / block_size;

            let twiddle_offset = twiddle_offsets[layer];

            let cfg = LaunchConfig {
                grid_dim: (grid_size, 1, 1),
                block_dim: (block_size, 1, 1),
                shared_mem_bytes: 0,
            };

            let twiddle_view = self.itwiddles.slice(twiddle_offset..);

            unsafe {
                self.executor.kernels.ifft_layer.clone().launch(
                    cfg,
                    (&mut *data, &twiddle_view, layer as u32, self.log_size, n_twiddles),
                ).map_err(|e| CudaFftError::KernelExecution(format!("{:?}", e)))?;
            }
        }

        Ok(())
    }
    
    /// Get the device ID this pipeline is running on.
    pub fn device_id(&self) -> usize {
        self.device_id
    }
    
    /// Get the executor for this pipeline.
    /// 
    /// Returns a reference to the Arc-wrapped executor, which can be
    /// cloned for operations that need owned access.
    #[inline]
    pub fn executor(&self) -> &Arc<CudaFftExecutor> {
        &self.executor
    }
    
    /// Get a reference to the underlying executor.
    /// 
    /// This is the primary method for accessing the executor for operations.
    /// The executor is bound to this pipeline's GPU device.
    #[allow(dead_code)]
    #[inline]
    fn get_executor(&self) -> &CudaFftExecutor {
        &self.executor
    }
    
    /// Upload polynomial data to GPU.
    /// Returns the index of the polynomial in the pipeline.
    pub fn upload_polynomial(&mut self, data: &[u32]) -> Result<usize, CudaFftError> {
        let n = 1usize << self.log_size;
        if data.len() != n {
            return Err(CudaFftError::InvalidSize(
                format!("Expected {} elements, got {}", n, data.len())
            ));
        }
        
        let executor = self.executor.clone();
        let d_data = executor.device.htod_sync_copy(data)
            .map_err(|e| CudaFftError::MemoryAllocation(format!("{:?}", e)))?;
        
        let idx = self.poly_data.len();
        self.poly_data.push(d_data);
        Ok(idx)
    }
    
    /// Bulk upload multiple polynomials to GPU in a single transfer.
    /// 
    /// This uploads all polynomials as a single contiguous buffer and then
    /// creates views into it for processing.
    /// 
    /// # Arguments
    /// * `polynomials` - Iterator of polynomial data slices
    /// 
    /// # Returns
    /// Number of polynomials uploaded
    pub fn upload_polynomials_bulk<'a>(
        &mut self,
        polynomials: impl Iterator<Item = &'a [u32]>,
    ) -> Result<usize, CudaFftError> {
        let n = 1usize << self.log_size;
        let executor = self.executor.clone();
        
        // Collect all polynomial data
        let polys: Vec<&[u32]> = polynomials.collect();
        let num_polys = polys.len();
        
        if num_polys == 0 {
            return Ok(0);
        }
        
        // Validate sizes
        for (i, poly) in polys.iter().enumerate() {
            if poly.len() != n {
                return Err(CudaFftError::InvalidSize(
                    format!("Polynomial {} has {} elements, expected {}", i, poly.len(), n)
                ));
            }
        }
        
        // Upload each polynomial separately (simpler and avoids dtod copies)
        // The overhead is in kernel launches, not memory transfers
        for poly in &polys {
            let d_poly = executor.device.htod_sync_copy(poly)
                .map_err(|e| CudaFftError::MemoryAllocation(format!("{:?}", e)))?;
            self.poly_data.push(d_poly);
        }
        
        Ok(num_polys)
    }

    /// Upload polynomial data using pinned memory pool for faster transfers.
    ///
    /// This method uses the global pinned memory pool to stage data for
    /// asynchronous transfer to the GPU. For repeated uploads of similar-sized
    /// data, this achieves ~2x faster transfers than unpinned memory.
    ///
    /// # Performance
    ///
    /// - First call: Allocates pinned buffer (~100μs overhead)
    /// - Subsequent calls: Reuses pooled buffer (~0 overhead)
    /// - Transfer: Uses DMA for parallel copy while CPU continues
    ///
    /// Returns the index of the polynomial in the pipeline.
    pub fn upload_polynomial_pinned(&mut self, data: &[u32]) -> Result<usize, CudaFftError> {
        let n = 1usize << self.log_size;
        if data.len() != n {
            return Err(CudaFftError::InvalidSize(
                format!("Expected {} elements, got {}", n, data.len())
            ));
        }

        let pool = get_pinned_pool_u32();
        let pinned = pool.acquire_with_data(data)?;

        let executor = self.executor.clone();

        // Allocate GPU buffer
        let mut d_data = unsafe {
            executor.device.alloc::<u32>(n)
        }.map_err(|e| CudaFftError::MemoryAllocation(format!("{:?}", e)))?;

        // Copy from pinned to device (DMA transfer)
        executor.device.htod_sync_copy_into(pinned.as_slice(), &mut d_data)
            .map_err(|e| CudaFftError::MemoryTransfer(format!("{:?}", e)))?;

        // Pinned buffer returns to pool automatically when dropped here
        let idx = self.poly_data.len();
        self.poly_data.push(d_data);
        Ok(idx)
    }

    /// Bulk upload polynomials using pinned memory pool.
    ///
    /// This is the most efficient method for uploading multiple polynomials.
    /// Uses a single large pinned buffer to stage all data, minimizing
    /// allocation overhead.
    ///
    /// # Performance
    ///
    /// For N polynomials of size M each:
    /// - Standard: N * (alloc + copy) = O(N * alloc_overhead)
    /// - Pinned pool: 1 * alloc + N * copy = O(1 * alloc_overhead)
    pub fn upload_polynomials_bulk_pinned<'a>(
        &mut self,
        polynomials: impl Iterator<Item = &'a [u32]>,
    ) -> Result<usize, CudaFftError> {
        let n = 1usize << self.log_size;
        let executor = self.executor.clone();
        let pool = get_pinned_pool_u32();

        // Collect all polynomial data
        let polys: Vec<&[u32]> = polynomials.collect();
        let num_polys = polys.len();

        if num_polys == 0 {
            return Ok(0);
        }

        // Validate sizes
        for (i, poly) in polys.iter().enumerate() {
            if poly.len() != n {
                return Err(CudaFftError::InvalidSize(
                    format!("Polynomial {} has {} elements, expected {}", i, poly.len(), n)
                ));
            }
        }

        // Upload each polynomial using pinned staging
        for poly in &polys {
            let pinned = pool.acquire_with_data(poly)?;

            let mut d_poly = unsafe {
                executor.device.alloc::<u32>(n)
            }.map_err(|e| CudaFftError::MemoryAllocation(format!("{:?}", e)))?;

            executor.device.htod_sync_copy_into(pinned.as_slice(), &mut d_poly)
                .map_err(|e| CudaFftError::MemoryTransfer(format!("{:?}", e)))?;

            self.poly_data.push(d_poly);
            // pinned returns to pool here
        }

        Ok(num_polys)
    }

    /// Download polynomial to a pinned buffer for fast transfer.
    ///
    /// Returns data in a pooled pinned buffer. The buffer automatically
    /// returns to the pool when the returned Vec is dropped (after copying).
    pub fn download_polynomial_pinned(&self, poly_idx: usize) -> Result<Vec<u32>, CudaFftError> {
        if poly_idx >= self.poly_data.len() {
            return Err(CudaFftError::InvalidSize(
                format!("Invalid polynomial index: {}", poly_idx)
            ));
        }

        let n = 1usize << self.log_size;
        let executor = self.executor.clone();
        let pool = get_pinned_pool_u32();

        // Get pinned buffer from pool
        let mut pinned = pool.acquire(n)?;

        // Download to pinned buffer (DMA transfer)
        executor.device.dtoh_sync_copy_into(&self.poly_data[poly_idx], pinned.as_mut_slice())
            .map_err(|e| CudaFftError::MemoryTransfer(format!("{:?}", e)))?;

        // Copy to regular Vec
        let result = pinned.as_slice().to_vec();

        // pinned buffer returns to pool here
        Ok(result)
    }

    /// Bulk download all polynomials from GPU.
    /// 
    /// Downloads each polynomial separately (simpler and avoids dtod copies).
    pub fn download_polynomials_bulk(&self) -> Result<Vec<Vec<u32>>, CudaFftError> {
        let n = 1usize << self.log_size;
        let num_polys = self.poly_data.len();
        
        if num_polys == 0 {
            return Ok(Vec::new());
        }
        
        let executor = self.executor.clone();
        
        // Download each polynomial separately
        let mut results = Vec::with_capacity(num_polys);
        for d_poly in &self.poly_data {
            let mut data = vec![0u32; n];
            executor.device.dtoh_sync_copy_into(d_poly, &mut data)
                .map_err(|e| CudaFftError::MemoryTransfer(format!("{:?}", e)))?;
            results.push(data);
        }
        
        Ok(results)
    }
    
    /// Execute IFFT on a polynomial (in-place on GPU).
    pub fn ifft(&mut self, poly_idx: usize) -> Result<(), CudaFftError> {
        if poly_idx >= self.poly_data.len() {
            return Err(CudaFftError::InvalidSize(
                format!("Invalid polynomial index: {}", poly_idx)
            ));
        }
        
        let executor = self.executor.clone();

        executor.execute_ifft_on_device(
            &mut self.poly_data[poly_idx],
            &self.itwiddles,
            &self.twiddle_offsets,
            &self.itwiddles_cpu,
            self.log_size,
        )
    }
    
    /// Execute FFT on a polynomial (in-place on GPU).
    ///
    /// When CUDA Graph acceleration is enabled and a graph has been captured,
    /// this method replays the graph for maximum performance. Otherwise, it
    /// executes kernels directly.
    pub fn fft(&mut self, poly_idx: usize) -> Result<(), CudaFftError> {
        if poly_idx >= self.poly_data.len() {
            return Err(CudaFftError::InvalidSize(
                format!("Invalid polynomial index: {}", poly_idx)
            ));
        }

        // Try to use graph-accelerated path
        if self.use_graphs {
            if let Some(ref graph) = self.fft_graph {
                if graph.is_ready() {
                    // Replay the captured graph
                    graph.launch()?;
                    graph.synchronize()?;
                    return Ok(());
                }
            }
        }

        // Fall back to direct kernel execution using fft_direct
        // (fft_direct avoids the borrow conflict by using the executor clone)
        self.fft_direct(poly_idx)
    }

    /// Execute FFT on a polynomial using the old direct execution path.
    ///
    /// This method always executes kernels directly without using CUDA Graphs.
    /// Useful for debugging or when graph capture isn't suitable.
    pub fn fft_direct(&mut self, poly_idx: usize) -> Result<(), CudaFftError> {
        if poly_idx >= self.poly_data.len() {
            return Err(CudaFftError::InvalidSize(
                format!("Invalid polynomial index: {}", poly_idx)
            ));
        }

        let executor = self.executor.clone();
        let block_size = 256u32;
        let num_layers = self.twiddles_cpu.len();

        // Calculate twiddle offsets
        let mut twiddle_offsets: Vec<usize> = Vec::new();
        let mut offset = 0usize;
        for tw in &self.twiddles_cpu {
            twiddle_offsets.push(offset);
            offset += tw.len();
        }

        // Execute layers in reverse order for forward FFT
        for layer in (0..num_layers).rev() {
            let n_twiddles = self.twiddles_cpu[layer].len() as u32;
            let butterflies_per_twiddle = 1u32 << layer;
            let total_butterflies = n_twiddles * butterflies_per_twiddle;
            let grid_size = (total_butterflies + block_size - 1) / block_size;

            let twiddle_offset = twiddle_offsets[layer];

            let cfg = LaunchConfig {
                grid_dim: (grid_size, 1, 1),
                block_dim: (block_size, 1, 1),
                shared_mem_bytes: 0,
            };

            let twiddle_view = self.twiddles.slice(twiddle_offset..);

            unsafe {
                executor.kernels.fft_layer.clone().launch(
                    cfg,
                    (&mut self.poly_data[poly_idx], &twiddle_view, layer as u32, self.log_size, n_twiddles),
                ).map_err(|e| CudaFftError::KernelExecution(format!("{:?}", e)))?;
            }
        }

        executor.device.synchronize()
            .map_err(|e| CudaFftError::KernelExecution(format!("Sync failed: {:?}", e)))?;
        
        Ok(())
    }
    
    /// Download polynomial data from GPU.
    pub fn download_polynomial(&self, poly_idx: usize) -> Result<Vec<u32>, CudaFftError> {
        if poly_idx >= self.poly_data.len() {
            return Err(CudaFftError::InvalidSize(
                format!("Invalid polynomial index: {}", poly_idx)
            ));
        }
        
        let executor = self.executor.clone();
        let n = 1usize << self.log_size;
        let mut result = vec![0u32; n];
        executor.device.dtoh_sync_copy_into(&self.poly_data[poly_idx], &mut result)
            .map_err(|e| CudaFftError::MemoryTransfer(format!("{:?}", e)))?;
        
        Ok(result)
    }
    
    /// Get the number of polynomials currently on GPU.
    pub fn num_polynomials(&self) -> usize {
        self.poly_data.len()
    }
    
    /// Get the log size of polynomials in this pipeline.
    pub fn log_size(&self) -> u32 {
        self.log_size
    }
    
    /// Clear all polynomial data from GPU memory.
    ///
    /// This allows reusing the pipeline for a new batch of polynomials
    /// while keeping the twiddles cached on GPU.
    pub fn clear_polynomials(&mut self) {
        self.poly_data.clear();
    }

    /// Take all polynomial CudaSlice data out of the pipeline.
    /// Returns the GPU-resident data, leaving the pipeline empty.
    pub fn take_all_poly_data(&mut self) -> Vec<CudaSlice<u32>> {
        std::mem::take(&mut self.poly_data)
    }

    /// Get a reference to the pipeline's CUDA device.
    pub fn device(&self) -> &Arc<cudarc::driver::CudaDevice> {
        &self.executor.device
    }
    
    /// Synchronize GPU operations.
    pub fn sync(&self) -> Result<(), CudaFftError> {
        let executor = self.executor.clone();
        executor.device.synchronize()
            .map_err(|e| CudaFftError::KernelExecution(format!("Sync failed: {:?}", e)))
    }
    
    /// Apply denormalization to a polynomial on GPU.
    /// 
    /// After IFFT, we need to divide by the domain size to get correct coefficients.
    /// This multiplies each element by the precomputed inverse of the domain size.
    /// 
    /// # Arguments
    /// * `poly_idx` - Index of the polynomial to denormalize
    /// * `denorm_factor` - Precomputed 1/n mod P
    pub fn denormalize(&mut self, poly_idx: usize, denorm_factor: u32) -> Result<(), CudaFftError> {
        if poly_idx >= self.poly_data.len() {
            return Err(CudaFftError::InvalidSize(
                format!("Invalid polynomial index: {}", poly_idx)
            ));
        }

        let n = 1u32 << self.log_size;

        // Swap the data out to avoid borrow conflicts
        let mut data = self.poly_data.swap_remove(poly_idx);
        let result = self.executor.clone().execute_denormalize_on_device(
            &mut data,
            denorm_factor,
            n,
        );
        // Put the data back at the correct position
        if poly_idx < self.poly_data.len() {
            self.poly_data.push(data);
            let last = self.poly_data.len() - 1;
            self.poly_data.swap(poly_idx, last);
        } else {
            self.poly_data.push(data);
        }
        result
    }
    
    /// Execute IFFT with fused denormalization on GPU.
    /// 
    /// This is the most efficient path for interpolation - performs IFFT
    /// followed by denormalization without any intermediate transfers.
    pub fn ifft_with_denormalize(&mut self, poly_idx: usize) -> Result<(), CudaFftError> {
        // Execute IFFT
        self.ifft(poly_idx)?;
        
        // Compute denormalization factor: 1/n mod P
        use crate::core::fields::m31::BaseField;
        let denorm = BaseField::from(1u32 << self.log_size).inverse();
        
        // Apply denormalization on GPU
        self.denormalize(poly_idx, denorm.0)
    }
    
    // =========================================================================
    // Batch FFT/IFFT Operations (minimizes GPU syncs)
    // =========================================================================

    /// Execute IFFT + denormalize on ALL polynomials with a single GPU sync.
    ///
    /// Instead of syncing after each column's IFFT (26 syncs for 26 columns),
    /// this launches all IFFT kernels across all columns per layer, then syncs
    /// once at the end. This eliminates ~50 unnecessary GPU synchronizations.
    pub fn ifft_with_denormalize_batch(&mut self, poly_indices: &[usize]) -> Result<(), CudaFftError> {
        if poly_indices.is_empty() {
            return Ok(());
        }
        for &idx in poly_indices {
            if idx >= self.poly_data.len() {
                return Err(CudaFftError::InvalidSize(
                    format!("Invalid polynomial index: {}", idx)
                ));
            }
        }

        let executor = self.executor.clone();
        let block_size = 256u32;
        let num_layers = self.itwiddles_cpu.len();
        let n = 1u32 << self.log_size;

        // Calculate twiddle offsets
        let mut twiddle_offsets_cpu: Vec<u32> = Vec::new();
        let mut offset = 0u32;
        for tw in &self.itwiddles_cpu {
            twiddle_offsets_cpu.push(offset);
            offset += tw.len() as u32;
        }

        // Shared memory configuration
        const SHMEM_ELEMENTS: u32 = 1024;
        const SHMEM_LOG_ELEMENTS: u32 = 10;
        const SHMEM_BLOCK_SIZE: u32 = 256;
        let shared_mem_layers = std::cmp::min(self.log_size, SHMEM_LOG_ELEMENTS);
        let use_shmem = shared_mem_layers > 0 && n >= SHMEM_ELEMENTS;

        // Phase 1: Launch shared-memory IFFT kernel for all columns (no sync between columns)
        if use_shmem {
            let num_blocks = n / SHMEM_ELEMENTS;
            let cfg = LaunchConfig {
                grid_dim: (num_blocks, 1, 1),
                block_dim: (SHMEM_BLOCK_SIZE, 1, 1),
                shared_mem_bytes: SHMEM_ELEMENTS * 4,
            };
            for &idx in poly_indices {
                unsafe {
                    executor.kernels.ifft_shared_mem.clone().launch(
                        cfg,
                        (
                            &mut self.poly_data[idx],
                            &self.itwiddles,
                            &self.twiddle_offsets,
                            shared_mem_layers,
                            self.log_size,
                        ),
                    ).map_err(|e| CudaFftError::KernelExecution(format!("Batch IFFT shmem: {:?}", e)))?;
                }
            }
        }

        // Phase 2: Per-layer IFFT kernels for remaining layers, all columns per layer
        let start_layer = if use_shmem { shared_mem_layers as usize } else { 0 };
        for layer in start_layer..num_layers {
            let n_twiddles = self.itwiddles_cpu[layer].len() as u32;
            let butterflies_per_twiddle = 1u32 << layer;
            let total_butterflies = n_twiddles * butterflies_per_twiddle;
            let grid_size = (total_butterflies + block_size - 1) / block_size;
            let twiddle_offset = twiddle_offsets_cpu[layer] as usize;

            let cfg = LaunchConfig {
                grid_dim: (grid_size, 1, 1),
                block_dim: (block_size, 1, 1),
                shared_mem_bytes: 0,
            };
            let twiddle_view = self.itwiddles.slice(twiddle_offset..);

            for &idx in poly_indices {
                unsafe {
                    executor.kernels.ifft_layer.clone().launch(
                        cfg,
                        (&mut self.poly_data[idx], &twiddle_view, layer as u32, self.log_size, n_twiddles),
                    ).map_err(|e| CudaFftError::KernelExecution(format!("Batch IFFT layer {}: {:?}", layer, e)))?;
                }
            }
        }

        // Phase 3: Denormalize all columns (no sync between)
        use crate::core::fields::m31::BaseField;
        let denorm = BaseField::from(1u32 << self.log_size).inverse();
        let denorm_factor = denorm.0;

        if n >= 1024 && n % 4 == 0 {
            let grid_size = (n / 4 + block_size - 1) / block_size;
            let cfg = LaunchConfig {
                grid_dim: (grid_size, 1, 1),
                block_dim: (block_size, 1, 1),
                shared_mem_bytes: 0,
            };
            for &idx in poly_indices {
                unsafe {
                    executor.kernels.denormalize_vec4.clone().launch(
                        cfg,
                        (&mut self.poly_data[idx], denorm_factor, n),
                    ).map_err(|e| CudaFftError::KernelExecution(format!("Batch denorm: {:?}", e)))?;
                }
            }
        } else {
            let grid_size = (n + block_size - 1) / block_size;
            let cfg = LaunchConfig {
                grid_dim: (grid_size, 1, 1),
                block_dim: (block_size, 1, 1),
                shared_mem_bytes: 0,
            };
            for &idx in poly_indices {
                unsafe {
                    executor.kernels.denormalize.clone().launch(
                        cfg,
                        (&mut self.poly_data[idx], denorm_factor, n),
                    ).map_err(|e| CudaFftError::KernelExecution(format!("Batch denorm: {:?}", e)))?;
                }
            }
        }

        // Single sync at the end — all IFFT + denormalize for all columns
        executor.device.synchronize()
            .map_err(|e| CudaFftError::KernelExecution(format!("Batch IFFT sync: {:?}", e)))?;

        Ok(())
    }

    /// Execute FFT on ALL polynomials with a single GPU sync.
    pub fn fft_batch(&mut self, poly_indices: &[usize]) -> Result<(), CudaFftError> {
        if poly_indices.is_empty() {
            return Ok(());
        }
        for &idx in poly_indices {
            if idx >= self.poly_data.len() {
                return Err(CudaFftError::InvalidSize(
                    format!("Invalid polynomial index: {}", idx)
                ));
            }
        }

        let executor = self.executor.clone();
        let block_size = 256u32;
        let num_layers = self.twiddles_cpu.len();

        // Calculate twiddle offsets
        let mut twiddle_offsets: Vec<usize> = Vec::new();
        let mut offset = 0usize;
        for tw in &self.twiddles_cpu {
            twiddle_offsets.push(offset);
            offset += tw.len();
        }

        // Execute layers in reverse order for forward FFT, all columns per layer
        for layer in (0..num_layers).rev() {
            let n_twiddles = self.twiddles_cpu[layer].len() as u32;
            let butterflies_per_twiddle = 1u32 << layer;
            let total_butterflies = n_twiddles * butterflies_per_twiddle;
            let grid_size = (total_butterflies + block_size - 1) / block_size;
            let twiddle_offset = twiddle_offsets[layer];

            let cfg = LaunchConfig {
                grid_dim: (grid_size, 1, 1),
                block_dim: (block_size, 1, 1),
                shared_mem_bytes: 0,
            };
            let twiddle_view = self.twiddles.slice(twiddle_offset..);

            for &idx in poly_indices {
                unsafe {
                    executor.kernels.fft_layer.clone().launch(
                        cfg,
                        (&mut self.poly_data[idx], &twiddle_view, layer as u32, self.log_size, n_twiddles),
                    ).map_err(|e| CudaFftError::KernelExecution(format!("Batch FFT layer {}: {:?}", layer, e)))?;
                }
            }
        }

        // Single sync at the end
        executor.device.synchronize()
            .map_err(|e| CudaFftError::KernelExecution(format!("Batch FFT sync: {:?}", e)))?;

        Ok(())
    }

    // =========================================================================
    // FRI Folding Operations (on persistent GPU memory)
    // =========================================================================
    
    /// Execute FRI fold_line on GPU with persistent memory.
    /// 
    /// Folds a polynomial by factor of 2 using the FRI protocol.
    /// Input and output stay on GPU.
    /// 
    /// # Arguments
    /// * `input_idx` - Index of input polynomial (SecureField, 4 u32 per element)
    /// * `itwiddles` - Inverse twiddles for folding
    /// * `alpha` - FRI alpha challenge (4 u32 for SecureField)
    /// 
    /// # Returns
    /// Index of the new folded polynomial (half the size)
    pub fn fri_fold_line(
        &mut self,
        input_idx: usize,
        itwiddles: &[u32],
        alpha: &[u32; 4],
    ) -> Result<usize, CudaFftError> {
        if input_idx >= self.poly_data.len() {
            return Err(CudaFftError::InvalidSize(
                format!("Invalid polynomial index: {}", input_idx)
            ));
        }
        
        let executor = self.executor.clone();
        let n = 1usize << self.log_size;
        let n_output = n / 2;
        
        // Upload twiddles and alpha
        let d_itwiddles = executor.device.htod_sync_copy(itwiddles)
            .map_err(|e| CudaFftError::MemoryAllocation(format!("{:?}", e)))?;
        let d_alpha = executor.device.htod_sync_copy(alpha)
            .map_err(|e| CudaFftError::MemoryAllocation(format!("{:?}", e)))?;
        
        // Allocate output on GPU
        let mut d_output = unsafe {
            executor.device.alloc::<u32>(n_output * 4)
        }.map_err(|e| CudaFftError::MemoryAllocation(format!("{:?}", e)))?;
        
        // Launch kernel with shared memory for alpha
        let block_size = 256u32;
        let grid_size = ((n_output as u32) + block_size - 1) / block_size;
        let log_n = n.ilog2();
        
        let cfg = LaunchConfig {
            grid_dim: (grid_size, 1, 1),
            block_dim: (block_size, 1, 1),
            shared_mem_bytes: 32, // Space for alpha (16 bytes) and alpha_sq (16 bytes)
        };
        
        unsafe {
            executor.kernels.fold_line.clone().launch(
                cfg,
                (&mut d_output, &self.poly_data[input_idx], &d_itwiddles, &d_alpha, n as u32, log_n),
            ).map_err(|e| CudaFftError::KernelExecution(format!("{:?}", e)))?;
        }
        
        executor.device.synchronize()
            .map_err(|e| CudaFftError::KernelExecution(format!("Sync failed: {:?}", e)))?;
        
        // Store output as new polynomial
        let output_idx = self.poly_data.len();
        self.poly_data.push(d_output);
        
        Ok(output_idx)
    }
    
    /// Execute FRI fold_line with pre-uploaded twiddles and alpha (faster for multiple folds).
    /// 
    /// This version takes GPU-resident twiddles and alpha for reduced transfer overhead.
    pub fn fri_fold_line_gpu(
        &mut self,
        input_idx: usize,
        d_itwiddles: &CudaSlice<u32>,
        d_alpha: &CudaSlice<u32>,
        current_n: usize,
    ) -> Result<usize, CudaFftError> {
        if input_idx >= self.poly_data.len() {
            return Err(CudaFftError::InvalidSize(
                format!("Invalid polynomial index: {}", input_idx)
            ));
        }
        
        let executor = self.executor.clone();
        let n_output = current_n / 2;
        
        // Allocate output on GPU
        let mut d_output = unsafe {
            executor.device.alloc::<u32>(n_output * 4)
        }.map_err(|e| CudaFftError::MemoryAllocation(format!("{:?}", e)))?;
        
        // Launch kernel
        let block_size = 256u32;
        let grid_size = ((n_output as u32) + block_size - 1) / block_size;
        let log_n = current_n.ilog2();
        
        let cfg = LaunchConfig {
            grid_dim: (grid_size, 1, 1),
            block_dim: (block_size, 1, 1),
            shared_mem_bytes: 32,
        };
        
        unsafe {
            executor.kernels.fold_line.clone().launch(
                cfg,
                (&mut d_output, &self.poly_data[input_idx], d_itwiddles, d_alpha, current_n as u32, log_n),
            ).map_err(|e| CudaFftError::KernelExecution(format!("{:?}", e)))?;
        }
        
        // No sync here - let caller batch multiple operations
        
        // Store output as new polynomial
        let output_idx = self.poly_data.len();
        self.poly_data.push(d_output);
        
        Ok(output_idx)
    }
    
    /// Execute FRI fold_circle_into_line on GPU with persistent memory.
    /// 
    /// Folds circle evaluation into line evaluation (accumulated).
    /// Both input and output stay on GPU.
    pub fn fri_fold_circle_into_line(
        &mut self,
        dst_idx: usize,
        src_idx: usize,
        itwiddles: &[u32],
        alpha: &[u32; 4],
    ) -> Result<(), CudaFftError> {
        if dst_idx >= self.poly_data.len() || src_idx >= self.poly_data.len() {
            return Err(CudaFftError::InvalidSize(
                format!("Invalid polynomial indices: dst={}, src={}", dst_idx, src_idx)
            ));
        }
        
        if dst_idx == src_idx {
            return Err(CudaFftError::InvalidSize(
                "dst_idx and src_idx must be different".into()
            ));
        }
        
        let n = 1usize << self.log_size;
        let n_dst = n / 2;

        // Upload twiddles and alpha (scope the borrow)
        let (d_itwiddles, d_alpha) = {
            let executor = self.executor.clone();
            let d_itwiddles = executor.device.htod_sync_copy(itwiddles)
                .map_err(|e| CudaFftError::MemoryAllocation(format!("{:?}", e)))?;
            let d_alpha = executor.device.htod_sync_copy(alpha)
                .map_err(|e| CudaFftError::MemoryAllocation(format!("{:?}", e)))?;
            (d_itwiddles, d_alpha)
        };

        // Launch kernel with shared memory for alpha
        let block_size = 256u32;
        let grid_size = ((n_dst as u32) + block_size - 1) / block_size;
        let log_n = n.ilog2();

        let cfg = LaunchConfig {
            grid_dim: (grid_size, 1, 1),
            block_dim: (block_size, 1, 1),
            shared_mem_bytes: 32,
        };

        // Use split_at_mut to get non-overlapping mutable references
        // We need to handle the case where dst_idx < src_idx and dst_idx > src_idx
        let (dst_slice, src_slice) = if dst_idx < src_idx {
            let (left, right) = self.poly_data.split_at_mut(src_idx);
            (&mut left[dst_idx], &right[0])
        } else {
            let (left, right) = self.poly_data.split_at_mut(dst_idx);
            (&mut right[0], &left[src_idx])
        };

        // Launch and sync
        {
            let executor = self.executor.clone();
            unsafe {
                executor.kernels.fold_circle_into_line.clone().launch(
                    cfg,
                    (dst_slice, src_slice, &d_itwiddles, &d_alpha, n as u32, log_n),
                ).map_err(|e| CudaFftError::KernelExecution(format!("{:?}", e)))?;
            }

            executor.device.synchronize()
                .map_err(|e| CudaFftError::KernelExecution(format!("Sync failed: {:?}", e)))?;
        }
        
        Ok(())
    }
    
    /// Execute multiple FRI fold layers without intermediate synchronization.
    /// 
    /// This batches multiple fold operations to reduce kernel launch overhead.
    /// Twiddles and alpha are uploaded once and reused.
    pub fn fri_fold_multi_layer(
        &mut self,
        input_idx: usize,
        all_itwiddles: &[Vec<u32>],  // Twiddles for each layer
        alpha: &[u32; 4],
        num_layers: usize,
    ) -> Result<usize, CudaFftError> {
        if input_idx >= self.poly_data.len() {
            return Err(CudaFftError::InvalidSize(
                format!("Invalid polynomial index: {}", input_idx)
            ));
        }
        
        if all_itwiddles.len() < num_layers {
            return Err(CudaFftError::InvalidSize(
                format!("Not enough twiddles: have {}, need {}", all_itwiddles.len(), num_layers)
            ));
        }
        
        // Upload alpha once
        let d_alpha = {
            let executor = self.executor.clone();
            executor.device.htod_sync_copy(alpha)
                .map_err(|e| CudaFftError::MemoryAllocation(format!("{:?}", e)))?
        };

        let mut current_idx = input_idx;
        let mut current_n = 1usize << self.log_size;

        for layer in 0..num_layers {
            // Upload twiddles for this layer
            let d_itwiddles = {
                let executor = self.executor.clone();
                executor.device.htod_sync_copy(&all_itwiddles[layer])
                    .map_err(|e| CudaFftError::MemoryAllocation(format!("{:?}", e)))?
            };

            // Fold
            current_idx = self.fri_fold_line_gpu(current_idx, &d_itwiddles, &d_alpha, current_n)?;
            current_n /= 2;
        }

        // Single sync at the end
        {
            let executor = self.executor.clone();
            executor.device.synchronize()
                .map_err(|e| CudaFftError::KernelExecution(format!("Sync failed: {:?}", e)))?;
        }
        
        Ok(current_idx)
    }
    
    // =========================================================================
    // Quotient Accumulation Operations (on persistent GPU memory)
    // =========================================================================
    
    /// Execute quotient accumulation on GPU with persistent memory.
    /// 
    /// Accumulates quotients for constraint evaluation.
    /// 
    /// # Returns
    /// Index of the new quotient polynomial (SecureField)
    pub fn accumulate_quotients(
        &mut self,
        column_indices: &[usize],
        line_coeffs: &[[u32; 12]],
        denom_inv: &[u32],
        batch_sizes: &[usize],
        col_indices: &[usize],
        n_points: usize,
    ) -> Result<usize, CudaFftError> {
        let executor = self.executor.clone();
        
        // Gather column data from GPU polynomials
        let n_columns = column_indices.len();
        let col_size = 1usize << self.log_size;
        let total_elements = n_columns * col_size;
        
        // GPU-RESIDENT COLUMN GATHERING
        // Instead of downloading to CPU and re-uploading, we use GPU kernels
        // to copy columns directly into a contiguous buffer on GPU
        
        // Allocate destination buffer for gathered columns
        let mut d_columns = unsafe {
            executor.device.alloc::<u32>(total_elements)
        }.map_err(|e| CudaFftError::MemoryAllocation(format!("{:?}", e)))?;
        
        // Copy each column to the destination buffer using GPU kernel
        let block_size = 256u32;
        for (i, &idx) in column_indices.iter().enumerate() {
            if idx >= self.poly_data.len() {
                return Err(CudaFftError::InvalidSize(
                    format!("Invalid column index: {}", idx)
                ));
            }
            
            let dst_offset = (i * col_size) as u32;
            let n_elements = col_size as u32;
            let grid_size = (n_elements + block_size - 1) / block_size;
            
            let cfg = LaunchConfig {
                grid_dim: (grid_size, 1, 1),
                block_dim: (block_size, 1, 1),
                shared_mem_bytes: 0,
            };
            
            // Launch copy_column_kernel to copy this column to the flat buffer
            unsafe {
                executor.kernels.copy_column.clone().launch(
                    cfg,
                    (
                        &mut d_columns,
                        &self.poly_data[idx],
                        dst_offset,
                        n_elements,
                    ),
                ).map_err(|e| CudaFftError::KernelExecution(format!("copy_column: {:?}", e)))?;
            }
        }
        
        // Flatten line coefficients (CPU-side, small data)
        let flat_line_coeffs: Vec<u32> = line_coeffs.iter()
            .flat_map(|coeffs| coeffs.iter().copied())
            .collect();
        
        // Convert to u32 (CPU-side, small data)
        let batch_sizes_u32: Vec<u32> = batch_sizes.iter().map(|&s| s as u32).collect();
        let col_indices_u32: Vec<u32> = col_indices.iter().map(|&i| i as u32).collect();
        let n_batches = batch_sizes.len();
        
        // Upload small auxiliary data to GPU (these are small, CPU upload is fine)
        let d_line_coeffs = executor.device.htod_sync_copy(&flat_line_coeffs)
            .map_err(|e| CudaFftError::MemoryAllocation(format!("{:?}", e)))?;
        let d_denom_inv = executor.device.htod_sync_copy(denom_inv)
            .map_err(|e| CudaFftError::MemoryAllocation(format!("{:?}", e)))?;
        let d_batch_sizes = executor.device.htod_sync_copy(&batch_sizes_u32)
            .map_err(|e| CudaFftError::MemoryAllocation(format!("{:?}", e)))?;
        let d_col_indices = executor.device.htod_sync_copy(&col_indices_u32)
            .map_err(|e| CudaFftError::MemoryAllocation(format!("{:?}", e)))?;
        
        // Allocate output
        let mut d_output = unsafe {
            executor.device.alloc::<u32>(n_points * 4)
        }.map_err(|e| CudaFftError::MemoryAllocation(format!("{:?}", e)))?;
        
        // Launch kernel
        let block_size = 256u32;
        let grid_size = ((n_points as u32) + block_size - 1) / block_size;
        
        let cfg = LaunchConfig {
            grid_dim: (grid_size, 1, 1),
            block_dim: (block_size, 1, 1),
            shared_mem_bytes: 0,
        };
        
        unsafe {
            executor.kernels.accumulate_quotients.clone().launch(
                cfg,
                (
                    &mut d_output,
                    &d_columns,
                    &d_line_coeffs,
                    &d_denom_inv,
                    &d_batch_sizes,
                    &d_col_indices,
                    n_batches as u32,
                    n_points as u32,
                    n_columns as u32,
                ),
            ).map_err(|e| CudaFftError::KernelExecution(format!("{:?}", e)))?;
        }
        
        executor.device.synchronize()
            .map_err(|e| CudaFftError::KernelExecution(format!("Sync failed: {:?}", e)))?;
        
        // Store output as new polynomial
        let output_idx = self.poly_data.len();
        self.poly_data.push(d_output);
        
        Ok(output_idx)
    }
    
    // =========================================================================
    // Merkle Hashing Operations (on persistent GPU memory)
    // =========================================================================
    
    /// Execute Blake2s Merkle hashing on GPU (data stays on GPU).
    /// 
    /// Hashes polynomial columns to create Merkle tree leaves.
    /// This version keeps polynomial data on GPU - no unnecessary transfers!
    /// 
    /// # Arguments
    /// * `column_indices` - Indices of polynomials to hash
    /// * `n_hashes` - Number of hashes to compute
    /// 
    /// # Returns
    /// Hash output as bytes (32 bytes per hash)
    pub fn merkle_hash(
        &self,
        column_indices: &[usize],
        n_hashes: usize,
    ) -> Result<Vec<u8>, CudaFftError> {
        let executor = self.executor.clone();
        
        let n_columns = column_indices.len();
        let n = 1usize << self.log_size;
        
        // Validate indices
        for &idx in column_indices {
            if idx >= self.poly_data.len() {
                return Err(CudaFftError::InvalidSize(
                    format!("Invalid column index: {}", idx)
                ));
            }
        }
        
        // Allocate contiguous buffer for columns on GPU (data is already there!)
        // We need to gather the columns into a contiguous buffer for the kernel
        let mut d_columns = unsafe {
            executor.device.alloc::<u32>(n_columns * n)
        }.map_err(|e| CudaFftError::MemoryAllocation(format!("{:?}", e)))?;
        
        // Copy polynomial data on GPU (device-to-device copy)
        for (i, &idx) in column_indices.iter().enumerate() {
            executor.device.dtod_copy(
                &self.poly_data[idx],
                &mut d_columns.slice_mut(i * n..(i + 1) * n),
            ).map_err(|e| CudaFftError::MemoryTransfer(format!("{:?}", e)))?;
        }
        
        // Allocate output (32 bytes per hash)
        let mut d_output = unsafe {
            executor.device.alloc::<u8>(n_hashes * 32)
        }.map_err(|e| CudaFftError::MemoryAllocation(format!("{:?}", e)))?;
        
        // Launch kernel
        let block_size = 256u32;
        let grid_size = ((n_hashes as u32) + block_size - 1) / block_size;
        
        let cfg = LaunchConfig {
            grid_dim: (grid_size, 1, 1),
            block_dim: (block_size, 1, 1),
            shared_mem_bytes: 0,
        };
        
        // Dummy prev_layer buffer (not used for leaf hashing)
        let dummy_prev = unsafe {
            executor.device.alloc::<u8>(1)
        }.map_err(|e| CudaFftError::MemoryAllocation(format!("{:?}", e)))?;
        
        unsafe {
            executor.kernels.merkle_layer.clone().launch(
                cfg,
                (
                    &mut d_output,
                    &d_columns,
                    &dummy_prev,
                    n_columns as u32,
                    n_hashes as u32,
                    0u32, // has_prev_layer = false
                ),
            ).map_err(|e| CudaFftError::KernelExecution(format!("{:?}", e)))?;
        }
        
        executor.device.synchronize()
            .map_err(|e| CudaFftError::KernelExecution(format!("Sync failed: {:?}", e)))?;
        
        // Download results
        let mut output = vec![0u8; n_hashes * 32];
        executor.device.dtoh_sync_copy_into(&d_output, &mut output)
            .map_err(|e| CudaFftError::MemoryTransfer(format!("{:?}", e)))?;
        
        Ok(output)
    }
    
    /// Build a full Merkle tree from leaves, keeping all data on GPU.
    /// 
    /// This builds the entire tree in one call, avoiding per-layer transfers.
    /// Only the final root is downloaded.
    /// 
    /// # Arguments
    /// * `column_indices` - Indices of polynomials to hash for leaves
    /// * `n_leaves` - Number of leaves
    /// 
    /// # Returns
    /// Merkle root (32 bytes)
    pub fn merkle_tree_full(
        &self,
        column_indices: &[usize],
        n_leaves: usize,
    ) -> Result<[u8; 32], CudaFftError> {
        let executor = self.executor.clone();
        
        let n_columns = column_indices.len();
        let n = 1usize << self.log_size;
        
        // Validate indices
        for &idx in column_indices {
            if idx >= self.poly_data.len() {
                return Err(CudaFftError::InvalidSize(
                    format!("Invalid column index: {}", idx)
                ));
            }
        }
        
        // Allocate contiguous buffer for columns on GPU
        let mut d_columns = unsafe {
            executor.device.alloc::<u32>(n_columns * n)
        }.map_err(|e| CudaFftError::MemoryAllocation(format!("{:?}", e)))?;
        
        // Copy polynomial data on GPU (device-to-device copy)
        for (i, &idx) in column_indices.iter().enumerate() {
            executor.device.dtod_copy(
                &self.poly_data[idx],
                &mut d_columns.slice_mut(i * n..(i + 1) * n),
            ).map_err(|e| CudaFftError::MemoryTransfer(format!("{:?}", e)))?;
        }
        
        // Allocate two buffers for ping-pong between layers
        let max_layer_size = n_leaves * 32;
        let mut d_layer_a = unsafe {
            executor.device.alloc::<u8>(max_layer_size)
        }.map_err(|e| CudaFftError::MemoryAllocation(format!("{:?}", e)))?;
        let mut d_layer_b = unsafe {
            executor.device.alloc::<u8>(max_layer_size)
        }.map_err(|e| CudaFftError::MemoryAllocation(format!("{:?}", e)))?;
        
        // Dummy buffer for columns (not used after leaf layer)
        let dummy_cols = unsafe {
            executor.device.alloc::<u32>(1)
        }.map_err(|e| CudaFftError::MemoryAllocation(format!("{:?}", e)))?;
        
        // Phase 1: Hash leaves
        let block_size = 256u32;
        let grid_size = ((n_leaves as u32) + block_size - 1) / block_size;
        
        let cfg = LaunchConfig {
            grid_dim: (grid_size, 1, 1),
            block_dim: (block_size, 1, 1),
            shared_mem_bytes: 0,
        };
        
        // Dummy prev_layer for leaf hashing
        let dummy_prev = unsafe {
            executor.device.alloc::<u8>(1)
        }.map_err(|e| CudaFftError::MemoryAllocation(format!("{:?}", e)))?;
        
        unsafe {
            executor.kernels.merkle_layer.clone().launch(
                cfg,
                (
                    &mut d_layer_a,
                    &d_columns,
                    &dummy_prev,
                    n_columns as u32,
                    n_leaves as u32,
                    0u32, // has_prev_layer = false
                ),
            ).map_err(|e| CudaFftError::KernelExecution(format!("{:?}", e)))?;
        }
        
        // Phase 2: Build tree layers (all on GPU)
        let mut current_size = n_leaves;
        let mut use_a = true;  // Ping-pong between buffers
        
        while current_size > 1 {
            let next_size = current_size / 2;
            let grid_size = ((next_size as u32) + block_size - 1) / block_size;
            
            let cfg = LaunchConfig {
                grid_dim: (grid_size, 1, 1),
                block_dim: (block_size, 1, 1),
                shared_mem_bytes: 0,
            };
            
            if use_a {
                // Read from A, write to B
                unsafe {
                    executor.kernels.merkle_layer.clone().launch(
                        cfg,
                        (
                            &mut d_layer_b,
                            &dummy_cols,
                            &d_layer_a,
                            0u32, // n_columns = 0
                            next_size as u32,
                            1u32, // has_prev_layer = true
                        ),
                    ).map_err(|e| CudaFftError::KernelExecution(format!("{:?}", e)))?;
                }
            } else {
                // Read from B, write to A
                unsafe {
                    executor.kernels.merkle_layer.clone().launch(
                        cfg,
                        (
                            &mut d_layer_a,
                            &dummy_cols,
                            &d_layer_b,
                            0u32, // n_columns = 0
                            next_size as u32,
                            1u32, // has_prev_layer = true
                        ),
                    ).map_err(|e| CudaFftError::KernelExecution(format!("{:?}", e)))?;
                }
            }
            
            current_size = next_size;
            use_a = !use_a;
        }
        
        executor.device.synchronize()
            .map_err(|e| CudaFftError::KernelExecution(format!("Sync failed: {:?}", e)))?;
        
        // Download only the root (32 bytes)
        let mut root = [0u8; 32];
        let root_buffer = if use_a { &d_layer_a } else { &d_layer_b };
        executor.device.dtoh_sync_copy_into(&root_buffer.slice(0..32), &mut root)
            .map_err(|e| CudaFftError::MemoryTransfer(format!("{:?}", e)))?;
        
        Ok(root)
    }
    
    /// Build a full Merkle tree layer from previous layer hashes.
    /// 
    /// # Arguments
    /// * `prev_layer` - Previous layer hashes (32 bytes each)
    /// 
    /// # Returns
    /// New layer hashes (half the count of prev_layer)
    pub fn merkle_tree_layer(&self, prev_layer: &[u8]) -> Result<Vec<u8>, CudaFftError> {
        let executor = self.executor.clone();
        
        let n_prev = prev_layer.len() / 32;
        let n_output = n_prev / 2;
        
        if n_output == 0 {
            return Err(CudaFftError::InvalidSize(
                "Previous layer must have at least 2 hashes".into()
            ));
        }
        
        // Upload previous layer
        let d_prev = executor.device.htod_sync_copy(prev_layer)
            .map_err(|e| CudaFftError::MemoryAllocation(format!("{:?}", e)))?;
        
        // Allocate output
        let mut d_output = unsafe {
            executor.device.alloc::<u8>(n_output * 32)
        }.map_err(|e| CudaFftError::MemoryAllocation(format!("{:?}", e)))?;
        
        // Launch kernel
        let block_size = 256u32;
        let grid_size = ((n_output as u32) + block_size - 1) / block_size;
        
        let cfg = LaunchConfig {
            grid_dim: (grid_size, 1, 1),
            block_dim: (block_size, 1, 1),
            shared_mem_bytes: 0,
        };
        
        // Dummy columns buffer (not used for internal nodes)
        let dummy_cols = unsafe {
            executor.device.alloc::<u32>(1)
        }.map_err(|e| CudaFftError::MemoryAllocation(format!("{:?}", e)))?;
        
        unsafe {
            executor.kernels.merkle_layer.clone().launch(
                cfg,
                (
                    &mut d_output,
                    &dummy_cols,
                    &d_prev,
                    0u32, // n_columns = 0
                    n_output as u32,
                    1u32, // has_prev_layer = true
                ),
            ).map_err(|e| CudaFftError::KernelExecution(format!("{:?}", e)))?;
        }
        
        executor.device.synchronize()
            .map_err(|e| CudaFftError::KernelExecution(format!("Sync failed: {:?}", e)))?;
        
        // Download results
        let mut output = vec![0u8; n_output * 32];
        executor.device.dtoh_sync_copy_into(&d_output, &mut output)
            .map_err(|e| CudaFftError::MemoryTransfer(format!("{:?}", e)))?;
        
        Ok(output)
    }

    /// Get a reference to a polynomial's GPU data by index.
    pub fn poly_slice(&self, idx: usize) -> &CudaSlice<u32> {
        &self.poly_data[idx]
    }

    /// Push an externally-allocated GPU buffer as a new polynomial.
    /// Returns the index of the new polynomial.
    pub fn push_external_poly(&mut self, data: CudaSlice<u32>) -> usize {
        let idx = self.poly_data.len();
        self.poly_data.push(data);
        idx
    }
}

/// Benchmark helper: Run a full proof simulation on GPU pipeline.
#[cfg(feature = "cuda-runtime")]
pub fn benchmark_proof_pipeline(
    log_size: u32,
    num_polynomials: usize,
    num_fft_rounds: usize,
) -> Result<PipelineBenchmarkResult, CudaFftError> {
    use std::time::Instant;
    
    let n = 1usize << log_size;
    
    // Generate test data
    let test_data: Vec<Vec<u32>> = (0..num_polynomials)
        .map(|p| {
            (0..n)
                .map(|i| ((i * 7 + p * 13 + 17) as u32) % ((1 << 31) - 1))
                .collect()
        })
        .collect();
    
    // Create pipeline
    let setup_start = Instant::now();
    let mut pipeline = GpuProofPipeline::new(log_size)?;
    let setup_time = setup_start.elapsed();
    
    // Upload all polynomials
    let upload_start = Instant::now();
    for data in &test_data {
        pipeline.upload_polynomial(data)?;
    }
    pipeline.sync()?;
    let upload_time = upload_start.elapsed();
    
    // Run FFT rounds (simulating proof generation)
    let compute_start = Instant::now();
    for _round in 0..num_fft_rounds {
        for poly_idx in 0..num_polynomials {
            pipeline.ifft(poly_idx)?;
        }
        for poly_idx in 0..num_polynomials {
            pipeline.fft(poly_idx)?;
        }
    }
    pipeline.sync()?;
    let compute_time = compute_start.elapsed();
    
    // Download results
    let download_start = Instant::now();
    let mut _results = Vec::new();
    for poly_idx in 0..num_polynomials {
        _results.push(pipeline.download_polynomial(poly_idx)?);
    }
    let download_time = download_start.elapsed();
    
    let total_time = setup_time + upload_time + compute_time + download_time;
    let total_ffts = num_polynomials * num_fft_rounds * 2; // IFFT + FFT
    
    Ok(PipelineBenchmarkResult {
        log_size,
        num_polynomials,
        num_fft_rounds,
        total_ffts,
        setup_time,
        upload_time,
        compute_time,
        download_time,
        total_time,
    })
}

/// Benchmark a full proof pipeline including FFT, FRI folding, and Merkle hashing.
#[cfg(feature = "cuda-runtime")]
pub fn benchmark_full_proof_pipeline(
    log_size: u32,
    num_polynomials: usize,
    num_fri_layers: usize,
) -> Result<FullProofBenchmarkResult, CudaFftError> {
    use std::time::Instant;
    
    let n = 1usize << log_size;
    
    // Generate test data (BaseField = 1 u32 per element)
    // Note: For SecureField operations, the pipeline would need separate handling
    let test_data: Vec<Vec<u32>> = (0..num_polynomials)
        .map(|p| {
            (0..n)
                .map(|i| ((i * 7 + p * 13 + 17) as u32) % ((1 << 31) - 1))
                .collect()
        })
        .collect();
    
    // Generate FRI twiddles and alpha (unused in simplified benchmark)
    let _itwiddles: Vec<u32> = (0..n/2)
        .map(|i| ((i * 11 + 3) as u32) % ((1 << 31) - 1))
        .collect();
    let _alpha: [u32; 4] = [12345, 67890, 11111, 22222];
    
    // Create pipeline
    let setup_start = Instant::now();
    let mut pipeline = GpuProofPipeline::new(log_size)?;
    let setup_time = setup_start.elapsed();
    
    // Upload all polynomials
    let upload_start = Instant::now();
    for data in &test_data {
        pipeline.upload_polynomial(data)?;
    }
    pipeline.sync()?;
    let upload_time = upload_start.elapsed();
    
    // Phase 1: FFT (commit phase)
    let fft_start = Instant::now();
    for poly_idx in 0..num_polynomials {
        pipeline.ifft(poly_idx)?;
        pipeline.fft(poly_idx)?;
    }
    pipeline.sync()?;
    let fft_time = fft_start.elapsed();
    
    // Phase 2: FRI Folding using actual FRI kernels
    // Generate mock twiddles and alpha for the benchmark
    let fri_start = Instant::now();
    let alpha: [u32; 4] = [12345, 67890, 11111, 22222];  // Mock alpha
    
    // Generate twiddles for each layer
    let mut all_itwiddles: Vec<Vec<u32>> = Vec::new();
    let mut current_size = n;
    for _ in 0..num_fri_layers.min(log_size as usize - 4) {
        let n_twiddles = current_size / 2;
        // Mock twiddles (in real code, these would be computed from the domain)
        let layer_twiddles: Vec<u32> = (0..n_twiddles)
            .map(|i| ((i as u64 * 31337) % 0x7FFFFFFF) as u32)
            .collect();
        all_itwiddles.push(layer_twiddles);
        current_size /= 2;
    }
    
    // Use multi-layer folding for better performance
    if !all_itwiddles.is_empty() {
        // Fold first polynomial using batched FRI
        let _folded_idx = pipeline.fri_fold_multi_layer(
            0,
            &all_itwiddles,
            &alpha,
            all_itwiddles.len(),
        )?;
    }
    pipeline.sync()?;
    let fri_time = fri_start.elapsed();
    
    // Phase 3: Merkle Hashing (all on GPU with single download of root)
    let merkle_start = Instant::now();
    let column_indices: Vec<usize> = (0..num_polynomials).collect();
    let n_leaves = n / 2;  // Simplified
    let _merkle_root = pipeline.merkle_tree_full(&column_indices, n_leaves)?;
    let merkle_time = merkle_start.elapsed();
    
    // Download final results (just the Merkle root in real proof)
    let download_start = Instant::now();
    // In real proof, we'd only download the Merkle root (32 bytes)
    // Here we download one polynomial for comparison
    let _result = pipeline.download_polynomial(0)?;
    let download_time = download_start.elapsed();
    
    let total_time = setup_time + upload_time + fft_time + fri_time + merkle_time + download_time;
    let compute_time = fft_time + fri_time + merkle_time;
    
    Ok(FullProofBenchmarkResult {
        log_size,
        num_polynomials,
        num_fri_layers,
        setup_time,
        upload_time,
        fft_time,
        fri_time,
        merkle_time,
        download_time,
        total_time,
        compute_time,
    })
}

/// Result of a full proof pipeline benchmark.
#[derive(Debug)]
pub struct FullProofBenchmarkResult {
    pub log_size: u32,
    pub num_polynomials: usize,
    pub num_fri_layers: usize,
    pub setup_time: std::time::Duration,
    pub upload_time: std::time::Duration,
    pub fft_time: std::time::Duration,
    pub fri_time: std::time::Duration,
    pub merkle_time: std::time::Duration,
    pub download_time: std::time::Duration,
    pub total_time: std::time::Duration,
    pub compute_time: std::time::Duration,
}

impl FullProofBenchmarkResult {
    /// Percentage of time spent on transfers.
    pub fn transfer_overhead_percent(&self) -> f64 {
        let transfer = self.upload_time + self.download_time;
        transfer.as_secs_f64() / self.total_time.as_secs_f64() * 100.0
    }
    
    /// Percentage of time spent on computation.
    pub fn compute_percent(&self) -> f64 {
        self.compute_time.as_secs_f64() / self.total_time.as_secs_f64() * 100.0
    }
}

impl std::fmt::Display for FullProofBenchmarkResult {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        writeln!(f, "Full GPU Proof Pipeline Results")?;
        writeln!(f, "================================")?;
        writeln!(f, "  Polynomial size:    2^{} = {} elements", self.log_size, 1usize << self.log_size)?;
        writeln!(f, "  Polynomials:        {}", self.num_polynomials)?;
        writeln!(f, "  FRI layers:         {}", self.num_fri_layers)?;
        writeln!(f)?;
        writeln!(f, "Timing Breakdown:")?;
        writeln!(f, "  Setup:              {:?}", self.setup_time)?;
        writeln!(f, "  Upload:             {:?}", self.upload_time)?;
        writeln!(f, "  FFT (commit):       {:?}", self.fft_time)?;
        writeln!(f, "  FRI folding:        {:?}", self.fri_time)?;
        writeln!(f, "  Merkle hashing:     {:?}", self.merkle_time)?;
        writeln!(f, "  Download:           {:?}", self.download_time)?;
        writeln!(f, "  Total:              {:?}", self.total_time)?;
        writeln!(f)?;
        writeln!(f, "Performance:")?;
        writeln!(f, "  Transfer overhead:  {:.1}%", self.transfer_overhead_percent())?;
        writeln!(f, "  Compute time:       {:.1}%", self.compute_percent())?;
        Ok(())
    }
}

/// Result of a pipeline benchmark.
#[derive(Debug)]
pub struct PipelineBenchmarkResult {
    pub log_size: u32,
    pub num_polynomials: usize,
    pub num_fft_rounds: usize,
    pub total_ffts: usize,
    pub setup_time: std::time::Duration,
    pub upload_time: std::time::Duration,
    pub compute_time: std::time::Duration,
    pub download_time: std::time::Duration,
    pub total_time: std::time::Duration,
}

impl PipelineBenchmarkResult {
    /// Average time per FFT operation.
    pub fn time_per_fft(&self) -> std::time::Duration {
        self.compute_time / self.total_ffts as u32
    }
    
    /// Percentage of time spent on transfers.
    pub fn transfer_overhead_percent(&self) -> f64 {
        let transfer = self.upload_time + self.download_time;
        transfer.as_secs_f64() / self.total_time.as_secs_f64() * 100.0
    }
    
    /// Percentage of time spent on computation.
    pub fn compute_percent(&self) -> f64 {
        self.compute_time.as_secs_f64() / self.total_time.as_secs_f64() * 100.0
    }
}

impl std::fmt::Display for PipelineBenchmarkResult {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        writeln!(f, "GPU Pipeline Benchmark Results")?;
        writeln!(f, "===============================")?;
        writeln!(f, "  Polynomial size:    2^{} = {} elements", self.log_size, 1usize << self.log_size)?;
        writeln!(f, "  Polynomials:        {}", self.num_polynomials)?;
        writeln!(f, "  FFT rounds:         {}", self.num_fft_rounds)?;
        writeln!(f, "  Total FFTs:         {}", self.total_ffts)?;
        writeln!(f)?;
        writeln!(f, "Timing Breakdown:")?;
        writeln!(f, "  Setup:              {:?}", self.setup_time)?;
        writeln!(f, "  Upload:             {:?}", self.upload_time)?;
        writeln!(f, "  Compute:            {:?}", self.compute_time)?;
        writeln!(f, "  Download:           {:?}", self.download_time)?;
        writeln!(f, "  Total:              {:?}", self.total_time)?;
        writeln!(f)?;
        writeln!(f, "Performance:")?;
        writeln!(f, "  Time per FFT:       {:?}", self.time_per_fft())?;
        writeln!(f, "  Transfer overhead:  {:.1}%", self.transfer_overhead_percent())?;
        writeln!(f, "  Compute time:       {:.1}%", self.compute_percent())?;
        Ok(())
    }
}

/// Batch proof processor - processes multiple independent proofs in parallel.
#[cfg(feature = "cuda-runtime")]
pub struct BatchProofProcessor {
    /// Multiple pipelines for parallel proof generation
    pipelines: Vec<GpuProofPipeline>,
}

#[cfg(feature = "cuda-runtime")]
impl BatchProofProcessor {
    /// Create a batch processor for multiple proofs of the same size.
    pub fn new(log_size: u32, num_proofs: usize) -> Result<Self, CudaFftError> {
        let mut pipelines = Vec::with_capacity(num_proofs);
        for _ in 0..num_proofs {
            pipelines.push(GpuProofPipeline::new(log_size)?);
        }
        Ok(Self { pipelines })
    }
    
    /// Upload polynomials for a specific proof.
    pub fn upload_polynomial(&mut self, proof_idx: usize, data: &[u32]) -> Result<usize, CudaFftError> {
        if proof_idx >= self.pipelines.len() {
            return Err(CudaFftError::InvalidSize(
                format!("Invalid proof index: {}", proof_idx)
            ));
        }
        self.pipelines[proof_idx].upload_polynomial(data)
    }
    
    /// Execute IFFT on all proofs in batch.
    pub fn batch_ifft(&mut self, poly_idx: usize) -> Result<(), CudaFftError> {
        for pipeline in &mut self.pipelines {
            pipeline.ifft(poly_idx)?;
        }
        Ok(())
    }
    
    /// Execute FFT on all proofs in batch.
    pub fn batch_fft(&mut self, poly_idx: usize) -> Result<(), CudaFftError> {
        for pipeline in &mut self.pipelines {
            pipeline.fft(poly_idx)?;
        }
        Ok(())
    }
    
    /// Synchronize all pipelines.
    pub fn sync(&self) -> Result<(), CudaFftError> {
        for pipeline in &self.pipelines {
            pipeline.sync()?;
        }
        Ok(())
    }
    
    /// Get number of proofs being processed.
    pub fn num_proofs(&self) -> usize {
        self.pipelines.len()
    }
    
    /// Get a specific pipeline.
    pub fn pipeline(&self, idx: usize) -> Option<&GpuProofPipeline> {
        self.pipelines.get(idx)
    }
    
    /// Get a mutable reference to a specific pipeline.
    pub fn pipeline_mut(&mut self, idx: usize) -> Option<&mut GpuProofPipeline> {
        self.pipelines.get_mut(idx)
    }
}

/// Benchmark larger proofs to demonstrate scaling benefits.
#[cfg(feature = "cuda-runtime")]
pub fn benchmark_large_proof(
    log_size: u32,
    num_polynomials: usize,
    num_fft_rounds: usize,
) -> Result<LargeProofBenchmarkResult, CudaFftError> {
    use std::time::Instant;
    
    let n = 1usize << log_size;
    
    // Generate test data
    let test_data: Vec<Vec<u32>> = (0..num_polynomials)
        .map(|p| {
            (0..n)
                .map(|i| ((i * 7 + p * 13 + 17) as u32) % ((1 << 31) - 1))
                .collect()
        })
        .collect();
    
    // Create pipeline
    let setup_start = Instant::now();
    let mut pipeline = GpuProofPipeline::new(log_size)?;
    let setup_time = setup_start.elapsed();
    
    // Upload all polynomials
    let upload_start = Instant::now();
    for data in &test_data {
        pipeline.upload_polynomial(data)?;
    }
    pipeline.sync()?;
    let upload_time = upload_start.elapsed();
    
    // Run FFT rounds (simulating proof generation)
    let compute_start = Instant::now();
    for _round in 0..num_fft_rounds {
        for poly_idx in 0..num_polynomials {
            pipeline.ifft(poly_idx)?;
        }
        for poly_idx in 0..num_polynomials {
            pipeline.fft(poly_idx)?;
        }
    }
    pipeline.sync()?;
    let compute_time = compute_start.elapsed();
    
    // Download results
    let download_start = Instant::now();
    let mut _results = Vec::new();
    for poly_idx in 0..num_polynomials {
        _results.push(pipeline.download_polynomial(poly_idx)?);
    }
    let download_time = download_start.elapsed();
    
    let total_time = setup_time + upload_time + compute_time + download_time;
    let total_ffts = num_polynomials * num_fft_rounds * 2;
    let elements_processed = (n * total_ffts) as u64;
    let throughput_gflops = (elements_processed as f64 * log_size as f64 * 5.0) / 
                            compute_time.as_secs_f64() / 1e9;
    
    Ok(LargeProofBenchmarkResult {
        log_size,
        num_polynomials,
        num_fft_rounds,
        total_ffts,
        elements_processed,
        setup_time,
        upload_time,
        compute_time,
        download_time,
        total_time,
        throughput_gflops,
    })
}

/// Result of a large proof benchmark.
#[derive(Debug)]
pub struct LargeProofBenchmarkResult {
    pub log_size: u32,
    pub num_polynomials: usize,
    pub num_fft_rounds: usize,
    pub total_ffts: usize,
    pub elements_processed: u64,
    pub setup_time: std::time::Duration,
    pub upload_time: std::time::Duration,
    pub compute_time: std::time::Duration,
    pub download_time: std::time::Duration,
    pub total_time: std::time::Duration,
    pub throughput_gflops: f64,
}

impl std::fmt::Display for LargeProofBenchmarkResult {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        writeln!(f, "Large Proof Benchmark Results")?;
        writeln!(f, "=============================")?;
        writeln!(f, "  Polynomial size:    2^{} = {} elements", self.log_size, 1usize << self.log_size)?;
        writeln!(f, "  Polynomials:        {}", self.num_polynomials)?;
        writeln!(f, "  FFT rounds:         {}", self.num_fft_rounds)?;
        writeln!(f, "  Total FFTs:         {}", self.total_ffts)?;
        writeln!(f, "  Elements processed: {:.2}M", self.elements_processed as f64 / 1e6)?;
        writeln!(f)?;
        writeln!(f, "Timing:")?;
        writeln!(f, "  Setup:              {:?}", self.setup_time)?;
        writeln!(f, "  Upload:             {:?}", self.upload_time)?;
        writeln!(f, "  Compute:            {:?}", self.compute_time)?;
        writeln!(f, "  Download:           {:?}", self.download_time)?;
        writeln!(f, "  Total:              {:?}", self.total_time)?;
        writeln!(f)?;
        writeln!(f, "Performance:")?;
        writeln!(f, "  Throughput:         {:.2} GFLOPS", self.throughput_gflops)?;
        writeln!(f, "  Time per FFT:       {:?}", self.compute_time / self.total_ffts as u32)?;
        let compute_pct = self.compute_time.as_secs_f64() / self.total_time.as_secs_f64() * 100.0;
        writeln!(f, "  Compute efficiency: {:.1}%", compute_pct)?;
        Ok(())
    }
}

/// Benchmark batch proof processing.
#[cfg(feature = "cuda-runtime")]
pub fn benchmark_batch_proofs(
    log_size: u32,
    num_proofs: usize,
    num_polynomials_per_proof: usize,
    num_fft_rounds: usize,
) -> Result<BatchProofBenchmarkResult, CudaFftError> {
    use std::time::Instant;
    
    let n = 1usize << log_size;
    
    // Create batch processor
    let setup_start = Instant::now();
    let mut batch = BatchProofProcessor::new(log_size, num_proofs)?;
    let setup_time = setup_start.elapsed();
    
    // Generate and upload test data for all proofs
    let upload_start = Instant::now();
    for proof_idx in 0..num_proofs {
        for poly_idx in 0..num_polynomials_per_proof {
            let data: Vec<u32> = (0..n)
                .map(|i| ((i * 7 + poly_idx * 13 + proof_idx * 17 + 23) as u32) % ((1 << 31) - 1))
                .collect();
            batch.upload_polynomial(proof_idx, &data)?;
        }
    }
    batch.sync()?;
    let upload_time = upload_start.elapsed();
    
    // Run FFT rounds on all proofs
    let compute_start = Instant::now();
    for _round in 0..num_fft_rounds {
        for poly_idx in 0..num_polynomials_per_proof {
            batch.batch_ifft(poly_idx)?;
        }
        for poly_idx in 0..num_polynomials_per_proof {
            batch.batch_fft(poly_idx)?;
        }
    }
    batch.sync()?;
    let compute_time = compute_start.elapsed();
    
    let total_time = setup_time + upload_time + compute_time;
    let total_ffts = num_proofs * num_polynomials_per_proof * num_fft_rounds * 2;
    
    Ok(BatchProofBenchmarkResult {
        log_size,
        num_proofs,
        num_polynomials_per_proof,
        num_fft_rounds,
        total_ffts,
        setup_time,
        upload_time,
        compute_time,
        total_time,
    })
}

/// Result of a batch proof benchmark.
#[derive(Debug)]
pub struct BatchProofBenchmarkResult {
    pub log_size: u32,
    pub num_proofs: usize,
    pub num_polynomials_per_proof: usize,
    pub num_fft_rounds: usize,
    pub total_ffts: usize,
    pub setup_time: std::time::Duration,
    pub upload_time: std::time::Duration,
    pub compute_time: std::time::Duration,
    pub total_time: std::time::Duration,
}

impl BatchProofBenchmarkResult {
    pub fn time_per_proof(&self) -> std::time::Duration {
        self.compute_time / self.num_proofs as u32
    }
    
    pub fn time_per_fft(&self) -> std::time::Duration {
        self.compute_time / self.total_ffts as u32
    }
}

impl std::fmt::Display for BatchProofBenchmarkResult {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        writeln!(f, "Batch Proof Benchmark Results")?;
        writeln!(f, "==============================")?;
        writeln!(f, "  Polynomial size:    2^{} = {} elements", self.log_size, 1usize << self.log_size)?;
        writeln!(f, "  Proofs in batch:    {}", self.num_proofs)?;
        writeln!(f, "  Polys per proof:    {}", self.num_polynomials_per_proof)?;
        writeln!(f, "  FFT rounds:         {}", self.num_fft_rounds)?;
        writeln!(f, "  Total FFTs:         {}", self.total_ffts)?;
        writeln!(f)?;
        writeln!(f, "Timing:")?;
        writeln!(f, "  Setup:              {:?}", self.setup_time)?;
        writeln!(f, "  Upload:             {:?}", self.upload_time)?;
        writeln!(f, "  Compute:            {:?}", self.compute_time)?;
        writeln!(f, "  Total:              {:?}", self.total_time)?;
        writeln!(f)?;
        writeln!(f, "Performance:")?;
        writeln!(f, "  Time per proof:     {:?}", self.time_per_proof())?;
        writeln!(f, "  Time per FFT:       {:?}", self.time_per_fft())?;
        let compute_pct = self.compute_time.as_secs_f64() / self.total_time.as_secs_f64() * 100.0;
        writeln!(f, "  Compute efficiency: {:.1}%", compute_pct)?;
        Ok(())
    }
}

// =============================================================================
// Streaming Pipeline with CUDA Streams (Overlapped Transfers)
// =============================================================================

/// GPU Streaming Pipeline - Uses CUDA streams to overlap transfers with computation.
///
/// This pipeline uses double-buffering and multiple CUDA streams to achieve:
/// - Upload next batch while computing on current batch
/// - Download previous results while uploading next batch
/// - Maximum GPU utilization through overlapped operations
///
/// # Architecture
///
/// ```text
/// Stream 0 (Compute):  [FFT batch 0] [FFT batch 1] [FFT batch 2] ...
/// Stream 1 (Upload):   [Upload 1]    [Upload 2]    [Upload 3]    ...
/// Stream 2 (Download): [Download 0]  [Download 1]  [Download 2]  ...
/// ```
#[cfg(feature = "cuda-runtime")]
pub struct GpuStreamingPipeline {
    /// Double-buffered polynomial data on GPU
    buffers: [Vec<CudaSlice<u32>>; 2],
    
    /// Current buffer index (0 or 1)
    current_buffer: usize,
    
    /// CUDA streams for overlapped operations
    compute_stream: Option<CudaStream>,
    transfer_stream: Option<CudaStream>,
    
    /// Twiddles on GPU (shared between buffers)
    itwiddles: CudaSlice<u32>,
    twiddles: CudaSlice<u32>,
    twiddle_offsets: CudaSlice<u32>,
    
    /// Current polynomial log size
    log_size: u32,
    
    /// CPU-side twiddle data (for layer info)
    itwiddles_cpu: Vec<Vec<u32>>,
    twiddles_cpu: Vec<Vec<u32>>,
}

#[cfg(feature = "cuda-runtime")]
impl GpuStreamingPipeline {
    /// Create a new streaming pipeline with double-buffering.
    pub fn new(log_size: u32) -> Result<Self, CudaFftError> {
        let executor = get_cuda_executor().map_err(|e| e.clone())?;
        
        // Precompute and cache twiddles
        let itwiddles_cpu = compute_itwiddle_dbls_cpu(log_size);
        let twiddles_cpu = compute_twiddle_dbls_cpu(log_size);
        
        // Flatten and upload twiddles to GPU
        let flat_itwiddles: Vec<u32> = itwiddles_cpu.iter().flatten().copied().collect();
        let flat_twiddles: Vec<u32> = twiddles_cpu.iter().flatten().copied().collect();
        
        let itwiddles = executor.device.htod_sync_copy(&flat_itwiddles)
            .map_err(|e| CudaFftError::MemoryAllocation(format!("{:?}", e)))?;
        let twiddles = executor.device.htod_sync_copy(&flat_twiddles)
            .map_err(|e| CudaFftError::MemoryAllocation(format!("{:?}", e)))?;
        
        // Calculate and upload twiddle offsets
        let mut offsets: Vec<u32> = Vec::new();
        let mut offset = 0u32;
        for tw in &itwiddles_cpu {
            offsets.push(offset);
            offset += tw.len() as u32;
        }
        let twiddle_offsets = executor.device.htod_sync_copy(&offsets)
            .map_err(|e| CudaFftError::MemoryAllocation(format!("{:?}", e)))?;
        
        // Create CUDA streams for overlapped operations
        // Note: cudarc creates streams that sync with default stream on drop
        let compute_stream = executor.device.fork_default_stream()
            .map_err(|e| CudaFftError::DriverInit(format!("Failed to create compute stream: {:?}", e)))
            .ok();
        let transfer_stream = executor.device.fork_default_stream()
            .map_err(|e| CudaFftError::DriverInit(format!("Failed to create transfer stream: {:?}", e)))
            .ok();
        
        Ok(Self {
            buffers: [Vec::new(), Vec::new()],
            current_buffer: 0,
            compute_stream,
            transfer_stream,
            itwiddles,
            twiddles,
            twiddle_offsets,
            log_size,
            itwiddles_cpu,
            twiddles_cpu,
        })
    }
    
    /// Pre-allocate buffers for a batch of polynomials.
    pub fn preallocate(&mut self, num_polynomials: usize) -> Result<(), CudaFftError> {
        let executor = get_cuda_executor().map_err(|e| e.clone())?;
        let n = 1usize << self.log_size;
        
        // Allocate both buffers
        for buffer in &mut self.buffers {
            buffer.clear();
            for _ in 0..num_polynomials {
                let d_data = unsafe {
                    executor.device.alloc::<u32>(n)
                }.map_err(|e| CudaFftError::MemoryAllocation(format!("{:?}", e)))?;
                buffer.push(d_data);
            }
        }
        
        Ok(())
    }
    
    /// Upload polynomial to the next buffer (async if streams available).
    pub fn upload_async(&mut self, poly_idx: usize, data: &[u32]) -> Result<(), CudaFftError> {
        let next_buffer = 1 - self.current_buffer;
        
        if poly_idx >= self.buffers[next_buffer].len() {
            return Err(CudaFftError::InvalidSize(
                format!("Invalid polynomial index: {}", poly_idx)
            ));
        }
        
        let n = 1usize << self.log_size;
        if data.len() != n {
            return Err(CudaFftError::InvalidSize(
                format!("Expected {} elements, got {}", n, data.len())
            ));
        }
        
        let executor = get_cuda_executor().map_err(|e| e.clone())?;
        
        // Use transfer stream if available, otherwise sync copy
        // Note: cudarc's htod_sync_copy_into doesn't support streams directly,
        // so we use the default stream for now but the double-buffering still helps
        executor.device.htod_sync_copy_into(data, &mut self.buffers[next_buffer][poly_idx])
            .map_err(|e| CudaFftError::MemoryTransfer(format!("{:?}", e)))?;
        
        Ok(())
    }
    
    /// Execute IFFT on current buffer (async if streams available).
    pub fn compute_ifft(&mut self, poly_idx: usize) -> Result<(), CudaFftError> {
        if poly_idx >= self.buffers[self.current_buffer].len() {
            return Err(CudaFftError::InvalidSize(
                format!("Invalid polynomial index: {}", poly_idx)
            ));
        }
        
        let executor = get_cuda_executor().map_err(|e| e.clone())?;
        
        // Execute IFFT on current buffer
        executor.execute_ifft_on_device(
            &mut self.buffers[self.current_buffer][poly_idx],
            &self.itwiddles,
            &self.twiddle_offsets,
            &self.itwiddles_cpu,
            self.log_size,
        )
    }
    
    /// Download polynomial from current buffer.
    pub fn download(&self, poly_idx: usize) -> Result<Vec<u32>, CudaFftError> {
        if poly_idx >= self.buffers[self.current_buffer].len() {
            return Err(CudaFftError::InvalidSize(
                format!("Invalid polynomial index: {}", poly_idx)
            ));
        }
        
        let executor = get_cuda_executor().map_err(|e| e.clone())?;
        let n = 1usize << self.log_size;
        let mut result = vec![0u32; n];
        
        executor.device.dtoh_sync_copy_into(&self.buffers[self.current_buffer][poly_idx], &mut result)
            .map_err(|e| CudaFftError::MemoryTransfer(format!("{:?}", e)))?;
        
        Ok(result)
    }
    
    /// Swap buffers for double-buffering.
    pub fn swap_buffers(&mut self) {
        self.current_buffer = 1 - self.current_buffer;
    }
    
    /// Synchronize all streams.
    pub fn sync(&self) -> Result<(), CudaFftError> {
        let executor = get_cuda_executor().map_err(|e| e.clone())?;
        
        // Synchronize compute stream if available
        if let Some(ref stream) = self.compute_stream {
            executor.device.wait_for(stream)
                .map_err(|e| CudaFftError::KernelExecution(format!("Compute stream sync failed: {:?}", e)))?;
        }
        
        // Synchronize transfer stream if available
        if let Some(ref stream) = self.transfer_stream {
            executor.device.wait_for(stream)
                .map_err(|e| CudaFftError::KernelExecution(format!("Transfer stream sync failed: {:?}", e)))?;
        }
        
        // Final device sync
        executor.device.synchronize()
            .map_err(|e| CudaFftError::KernelExecution(format!("Device sync failed: {:?}", e)))
    }
    
    /// Execute forward FFT on current buffer.
    pub fn compute_fft(&mut self, poly_idx: usize) -> Result<(), CudaFftError> {
        if poly_idx >= self.buffers[self.current_buffer].len() {
            return Err(CudaFftError::InvalidSize(
                format!("Invalid polynomial index: {}", poly_idx)
            ));
        }
        
        let executor = get_cuda_executor().map_err(|e| e.clone())?;
        let block_size = 256u32;
        let num_layers = self.twiddles_cpu.len();
        
        // Calculate twiddle offsets
        let mut twiddle_offsets: Vec<usize> = Vec::new();
        let mut offset = 0usize;
        for tw in &self.twiddles_cpu {
            twiddle_offsets.push(offset);
            offset += tw.len();
        }
        
        // Execute layers in reverse order for forward FFT
        for layer in (0..num_layers).rev() {
            let n_twiddles = self.twiddles_cpu[layer].len() as u32;
            let butterflies_per_twiddle = 1u32 << layer;
            let total_butterflies = n_twiddles * butterflies_per_twiddle;
            let grid_size = (total_butterflies + block_size - 1) / block_size;
            
            let twiddle_offset = twiddle_offsets[layer];
            
            let cfg = LaunchConfig {
                grid_dim: (grid_size, 1, 1),
                block_dim: (block_size, 1, 1),
                shared_mem_bytes: 0,
            };
            
            let twiddle_view = self.twiddles.slice(twiddle_offset..);
            
            unsafe {
                executor.kernels.fft_layer.clone().launch(
                    cfg,
                    (&mut self.buffers[self.current_buffer][poly_idx], &twiddle_view, layer as u32, self.log_size, n_twiddles),
                ).map_err(|e| CudaFftError::KernelExecution(format!("{:?}", e)))?;
            }
        }
        
        executor.device.synchronize()
            .map_err(|e| CudaFftError::KernelExecution(format!("Sync failed: {:?}", e)))?;
        
        Ok(())
    }
    
    /// Check if streams are available for async operations.
    pub fn has_streams(&self) -> bool {
        self.compute_stream.is_some() && self.transfer_stream.is_some()
    }
    
    /// Get the log size of polynomials in this pipeline.
    pub fn log_size(&self) -> u32 {
        self.log_size
    }
    
    /// Get the number of polynomials in the current buffer.
    pub fn num_polynomials(&self) -> usize {
        self.buffers[self.current_buffer].len()
    }
    
    /// Process a batch of polynomials with overlapped transfers.
    /// 
    /// This method demonstrates the streaming pattern:
    /// 1. Upload batch N+1 while computing on batch N
    /// 2. Download batch N-1 while uploading batch N+1
    pub fn process_batch_overlapped(
        &mut self,
        input_batches: &[Vec<Vec<u32>>],
    ) -> Result<Vec<Vec<Vec<u32>>>, CudaFftError> {
        if input_batches.is_empty() {
            return Ok(Vec::new());
        }
        
        let num_polys_per_batch = input_batches[0].len();
        let num_batches = input_batches.len();
        
        // Preallocate buffers
        self.preallocate(num_polys_per_batch)?;
        
        let mut results: Vec<Vec<Vec<u32>>> = Vec::with_capacity(num_batches);
        
        // Process first batch (upload only, no overlap yet)
        for (poly_idx, data) in input_batches[0].iter().enumerate() {
            self.upload_async(poly_idx, data)?;
        }
        self.swap_buffers();  // Now current_buffer has batch 0
        
        // Process remaining batches with overlap
        for batch_idx in 0..num_batches {
            // Compute on current buffer
            for poly_idx in 0..num_polys_per_batch {
                self.compute_ifft(poly_idx)?;
            }
            
            // Upload next batch (if any) to other buffer
            if batch_idx + 1 < num_batches {
                for (poly_idx, data) in input_batches[batch_idx + 1].iter().enumerate() {
                    self.upload_async(poly_idx, data)?;
                }
            }
            
            // Download results from current buffer
            self.sync()?;
            let mut batch_results = Vec::with_capacity(num_polys_per_batch);
            for poly_idx in 0..num_polys_per_batch {
                batch_results.push(self.download(poly_idx)?);
            }
            results.push(batch_results);
            
            // Swap buffers for next iteration
            self.swap_buffers();
        }
        
        Ok(results)
    }
}

#[cfg(not(feature = "cuda-runtime"))]
pub struct GpuStreamingPipeline;

#[cfg(not(feature = "cuda-runtime"))]
impl GpuStreamingPipeline {
    pub fn new(_log_size: u32) -> Result<Self, String> {
        Err("CUDA runtime not available".into())
    }
}

#[cfg(not(feature = "cuda-runtime"))]
pub struct GpuProofPipeline;

#[cfg(not(feature = "cuda-runtime"))]
impl GpuProofPipeline {
    pub fn new(_log_size: u32) -> Result<Self, String> {
        Err("CUDA runtime not available".into())
    }
}

#[cfg(not(feature = "cuda-runtime"))]
pub struct BatchProofProcessor;

#[cfg(not(feature = "cuda-runtime"))]
impl BatchProofProcessor {
    pub fn new(_log_size: u32, _num_proofs: usize) -> Result<Self, String> {
        Err("CUDA runtime not available".into())
    }
}