ferrotorch-core 0.6.2

Core tensor and autograd engine for ferrotorch — PyTorch in Rust
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
//! Backward functions for FFT operations.
//!
//! The key mathematical identities:
//! - `d/dx FFT(x) = FFT(grad)` (FFT is linear, so its own Jacobian)
//! - `d/dx IFFT(x) = IFFT(grad)` (same reasoning)
//!
//! More precisely, for the backward pass of `y = fft(x)`:
//!   `grad_input = ifft(grad_output) * n`  (because our ifft divides by n)
//!
//! For `y = ifft(x)`:
//!   `grad_input = fft(grad_output) / n`
//!
//! ## REQ status (per `.design/ferrotorch-core/grad_fns/fft.md`)
//!
//! As of #1294 every op honours `norm`/`dim`/`s` and reaches `0 skipped,
//! 0 failed` at `--seeds 8` (full scope). The wrappers thread the norm/dim
//! via their `*_differentiable_norm` siblings; the backward VJP uses the
//! `adjoint_norm` identity (same fft_norm_mode int, flipped direction —
//! `tools/autograd/derivatives.yaml:2960-2961`).
//!
//! | REQ | Status | Evidence |
//! |---|---|---|
//! | REQ-1 (`fft.fft`) | SHIPPED | `fft_differentiable` / `fft_differentiable_norm` + `FftBackward`; consumer: default wrapper delegates to the norm path; `[fft.fft] 64/64 passed (0 skipped, 0 failed)`. |
//! | REQ-2 (`fft.ifft`) | SHIPPED | `ifft_differentiable` / `ifft_differentiable_norm` + `IfftBackward`; `[fft.ifft] 64/64 passed (0 skipped, 0 failed)`. |
//! | REQ-3 (`fft.rfft`) | SHIPPED | `rfft_differentiable` / `rfft_differentiable_norm` + `RfftBackward`/`RfftnBackward`; `[fft.rfft] 64/64 passed (0 skipped, 0 failed)`. |
//! | REQ-4 (`fft.irfft`) | SHIPPED | `irfft_differentiable` / `irfft_differentiable_norm` + `IrfftBackward`/`IrfftnBackward`; `[fft.irfft] 64/64 passed (0 skipped, 0 failed)`. |
//! | REQ-5 (`fft.fftn`) | SHIPPED | `fftn_differentiable` / `fftn_differentiable_norm` + `FftnBackward` (closes #1296); `[fft.fftn] 72/72 passed (0 skipped, 0 failed)`. |
//! | REQ-6 (`fft.ifftn`) | SHIPPED | `ifftn_differentiable` / `ifftn_differentiable_norm` + `IfftnBackward`; `[fft.ifftn] 72/72 passed (0 skipped, 0 failed)`. |
//! | REQ-7 (`fft.rfftn`) | SHIPPED | `rfftn_differentiable` / `rfftn_differentiable_norm` + `RfftnBackward`; `[fft.rfftn] 72/72 passed (0 skipped, 0 failed)`. |
//! | REQ-8 (`fft.irfftn`) | SHIPPED | `irfftn_differentiable` / `irfftn_differentiable_norm` + `IrfftnBackward`; `[fft.irfftn] 72/72 passed (0 skipped, 0 failed)`. |
//! | REQ-9 (`fft.hfft`) | SHIPPED | `hfft_differentiable` + `HfftBackward`; parity arm calls forward `hfft_norm`; `[fft.hfft] 64/64 passed (0 skipped, 0 failed)`. |
//! | REQ-10 (`fft.ihfft`) | SHIPPED | `ihfft_differentiable` + `IhfftBackward`; parity arm calls forward `ihfft_norm`; `[fft.ihfft] 64/64 passed (0 skipped, 0 failed)`. |
//! | REQ-11 (`fft.fft2`) | SHIPPED | `fft2_differentiable` / `fft2_differentiable_norm` + `Fft2Backward`/`FftnBackward` (closes #1300); `[fft.fft2] 56/56 passed (0 skipped, 0 failed)`. |
//! | REQ-12 (`fft.ifft2`) | SHIPPED | `ifft2_differentiable` / `ifft2_differentiable_norm` + `Ifft2Backward`/`IfftnBackward` (closes #1300); `[fft.ifft2] 56/56 passed (0 skipped, 0 failed)`. |
//! | REQ-13 (`fft.rfft2`) | SHIPPED | forward `fft::rfft2`/`rfft2_norm` (closes #1299-forward); autograd wrapper follow-up; `[fft.rfft2] 56/56 passed (0 skipped, 0 failed)`. |
//! | REQ-14 (`fft.irfft2`) | SHIPPED | forward `fft::irfft2`/`irfft2_norm` (closes #1299-forward); autograd wrapper follow-up; `[fft.irfft2] 56/56 passed (0 skipped, 0 failed)`. |
//! | REQ-15 (`fft.hfft2`) | SHIPPED | forward `fft::hfft2`/`hfft2_norm` (closes #1299-forward); autograd wrapper follow-up; `[fft.hfft2] 56/56 passed (0 skipped, 0 failed)`. |
//! | REQ-16 (`fft.ihfft2`) | SHIPPED | forward `fft::ihfft2`/`ihfft2_norm` (closes #1299-forward); autograd wrapper follow-up; `[fft.ihfft2] 56/56 passed (0 skipped, 0 failed)`. |
//! | REQ-17 (`fft.hfftn`) | SHIPPED | forward `fft::hfftn`/`hfftn_norm` (closes #1299-forward); autograd wrapper follow-up; `[fft.hfftn] 72/72 passed (0 skipped, 0 failed)`. |
//! | REQ-18 (`fft.ihfftn`) | SHIPPED | forward `fft::ihfftn`/`ihfftn_norm` (closes #1299-forward); autograd wrapper follow-up; `[fft.ihfftn] 72/72 passed (0 skipped, 0 failed)`.

use std::sync::Arc;

use crate::autograd::no_grad::is_grad_enabled;
use crate::dtype::Float;
use crate::error::FerrotorchResult;
use crate::fft;
use crate::fft::FftNorm;
use crate::storage::TensorStorage;
use crate::tensor::{GradFn, Tensor};

/// Adjoint normalization mode for an FFT VJP (#1294).
///
/// PyTorch's autograd uses the SAME `fft_norm_mode` int for the backward pass
/// but flips the transform direction (`_fft_c2c(grad, dim, normalization,
/// !forward)`, `tools/autograd/derivatives.yaml:2960-2961`). The mode int is
/// direction-independent (`aten/src/ATen/native/SpectralOpsUtils.h:15-19`).
/// Expressed via ferray_fft's direction-dependent [`FftNorm`], the adjoint of
/// a forward transform with norm `X` is the inverse transform with norm
/// `adjoint_norm(X)`:
///
/// - `Backward` (fwd-scale 1) ↔ `Forward` (inv-scale 1)
/// - `Forward` (fwd-scale 1/n) ↔ `Backward` (inv-scale 1/n)
/// - `Ortho` (scale 1/√n both) → `Ortho`
///
/// This holds symmetrically for c2c forward/inverse and (combined with the
/// Hermitian-doubling correction) for the r2c / c2r VJPs.
#[inline]
fn adjoint_norm(norm: FftNorm) -> FftNorm {
    match norm {
        FftNorm::Backward => FftNorm::Forward,
        FftNorm::Forward => FftNorm::Backward,
        FftNorm::Ortho => FftNorm::Ortho,
    }
}

// ---------------------------------------------------------------------------
// FftBackward
// ---------------------------------------------------------------------------

/// Backward for `y = fft(x, n)`.
///
/// VJP: `grad_x = ifft(grad_y) * n` (un-normalized inverse).
/// Since our `ifft` already divides by n, the grad is just `ifft(grad_y) * n`,
/// but actually the correct VJP for a normalized FFT pair where
/// `fft` has no normalization and `ifft` divides by n is:
/// `grad_x = conj(fft(conj(grad_y))) / n = ifft(grad_y) * n / n = ifft(grad_y)` ... wait.
///
/// Let's be precise. Our conventions:
/// - `fft`: no normalization (forward sum without 1/n).
/// - `ifft`: divides by n.
///
/// For `y = FFT(x)` (unnormalized), the Jacobian is the DFT matrix W.
/// The VJP is `grad_x = W^H @ grad_y = n * IFFT(grad_y)`.
///
/// But our `ifft` already computes `(1/n) * W^H @ input`, so
/// `grad_x = n * ifft(grad_y)`.
#[derive(Debug)]
pub struct FftBackward<T: Float> {
    input: Tensor<T>,
    n: Option<usize>,
    /// Transform axis in the real-signal layout (`None` = last).
    dim: Option<isize>,
    /// Forward normalization mode (`torch.fft.fft`'s `norm`).
    norm: FftNorm,
}

impl<T: Float> FftBackward<T> {
    pub fn new(input: Tensor<T>, n: Option<usize>) -> Self {
        Self::new_norm(input, n, None, FftNorm::Backward)
    }

    pub fn new_norm(input: Tensor<T>, n: Option<usize>, dim: Option<isize>, norm: FftNorm) -> Self {
        Self {
            input,
            n,
            dim,
            norm,
        }
    }
}

impl<T: Float> GradFn<T> for FftBackward<T> {
    fn backward(&self, grad_output: &Tensor<T>) -> FerrotorchResult<Vec<Option<Tensor<T>>>> {
        let grad_input = if self.input.requires_grad() {
            let device = grad_output.device();
            // Adjoint of forward `fft(norm)` is `ifft(adjoint_norm(norm))`
            // (same mode int, flipped direction — derivatives.yaml:2960-2961).
            // The `n` resize is re-applied so grad shape matches the input.
            let inv = fft::ifft_norm(grad_output, self.n, self.dim, adjoint_norm(self.norm))?;
            Some(if device.is_cuda() {
                inv.to(device)?
            } else {
                inv
            })
        } else {
            None
        };
        Ok(vec![grad_input])
    }

    fn inputs(&self) -> Vec<&Tensor<T>> {
        vec![&self.input]
    }

    fn name(&self) -> &'static str {
        "FftBackward"
    }
}

// ---------------------------------------------------------------------------
// IfftBackward
// ---------------------------------------------------------------------------

/// Backward for `y = ifft(x, n)`.
///
/// Our `ifft(x)` = (1/n) * W^H @ x, so the VJP is:
/// `grad_x = (1/n) * W @ grad_y = (1/n) * fft(grad_y)`.
///
/// Since our `fft` is unnormalized: `grad_x = fft(grad_y) / n`.
#[derive(Debug)]
pub struct IfftBackward<T: Float> {
    input: Tensor<T>,
    n: Option<usize>,
    dim: Option<isize>,
    norm: FftNorm,
}

impl<T: Float> IfftBackward<T> {
    pub fn new(input: Tensor<T>, n: Option<usize>) -> Self {
        Self::new_norm(input, n, None, FftNorm::Backward)
    }

    pub fn new_norm(input: Tensor<T>, n: Option<usize>, dim: Option<isize>, norm: FftNorm) -> Self {
        Self {
            input,
            n,
            dim,
            norm,
        }
    }
}

impl<T: Float> GradFn<T> for IfftBackward<T> {
    fn backward(&self, grad_output: &Tensor<T>) -> FerrotorchResult<Vec<Option<Tensor<T>>>> {
        let grad_input = if self.input.requires_grad() {
            let device = grad_output.device();
            // Adjoint of forward `ifft(norm)` is `fft(adjoint_norm(norm))`.
            let fwd = fft::fft_norm(grad_output, self.n, self.dim, adjoint_norm(self.norm))?;
            Some(if device.is_cuda() {
                fwd.to(device)?
            } else {
                fwd
            })
        } else {
            None
        };
        Ok(vec![grad_input])
    }

    fn inputs(&self) -> Vec<&Tensor<T>> {
        vec![&self.input]
    }

    fn name(&self) -> &'static str {
        "IfftBackward"
    }
}

// ---------------------------------------------------------------------------
// RfftBackward
// ---------------------------------------------------------------------------

/// Backward for `y = rfft(x, n)` (real → Hermitian-truncated complex).
///
/// VJP derivation (matches PyTorch `FftR2CBackward` for `norm="backward"`):
///
/// The forward `Y = rfft(x)` with output length `K = N/2 + 1` is the linear
/// map `Y[k] = sum_{n} x[n] exp(-2π i k n / N)` for `k = 0..K-1`. For a
/// real-valued upstream cotangent `grad_Y`, the cotangent on `x` is
///
/// ```text
///   grad_x[n] = real( sum_{k=0..K-1} grad_Y[k] * exp(+2π i k n / N) )
/// ```
///
/// (i.e., the **partial** unnormalized inverse over the half-spectrum, NOT
/// the Hermitian-extended full inverse). Implementing this as `irfft(grad_Y,
/// N)` would Hermitian-extend the spectrum and **double** the interior
/// freqs, then divide by `N` — both wrong by a factor of `N` and by the
/// boundary correction.
///
/// PyTorch's reference path is equivalent to: zero-pad `grad_Y` along the
/// freq axis from `K` to `N`, run an unnormalized inverse complex FFT, take
/// the real part. We compute this by calling our normalized
/// `fft::ifft(zero_padded, N)` (which divides by `N`) and multiplying by `N`
/// to undo the normalization.
#[derive(Debug)]
pub struct RfftBackward<T: Float> {
    input: Tensor<T>,
    _n: Option<usize>,
    /// The actual FFT length used in the forward pass.
    fft_n: usize,
}

impl<T: Float> RfftBackward<T> {
    pub fn new(input: Tensor<T>, n: Option<usize>, fft_n: usize) -> Self {
        Self {
            input,
            _n: n,
            fft_n,
        }
    }
}

impl<T: Float> GradFn<T> for RfftBackward<T> {
    fn backward(&self, grad_output: &Tensor<T>) -> FerrotorchResult<Vec<Option<Tensor<T>>>> {
        let grad_input = if self.input.requires_grad() {
            let device = grad_output.device();
            // Zero-pad grad_Y from [..., K, 2] to [..., N, 2] along the freq axis.
            let go_shape = grad_output.shape();
            if go_shape.len() < 2 || go_shape[go_shape.len() - 1] != 2 {
                return Err(crate::error::FerrotorchError::InvalidArgument {
                    message: format!(
                        "RfftBackward: grad_output must have trailing complex pair, got {go_shape:?}"
                    ),
                });
            }
            let k = go_shape[go_shape.len() - 2];
            let n = self.fft_n;
            let batch_shape = &go_shape[..go_shape.len() - 2];
            let batch_size: usize = batch_shape.iter().product::<usize>().max(1);
            let go_data = grad_output.data_vec()?;

            let mut padded = vec![T::from(0.0).unwrap(); batch_size * n * 2];
            for b in 0..batch_size {
                let src_off = b * k * 2;
                let dst_off = b * n * 2;
                let copy_pairs = k.min(n);
                for kk in 0..copy_pairs {
                    padded[dst_off + kk * 2] = go_data[src_off + kk * 2];
                    padded[dst_off + kk * 2 + 1] = go_data[src_off + kk * 2 + 1];
                }
            }
            let mut padded_shape = batch_shape.to_vec();
            padded_shape.push(n);
            padded_shape.push(2);
            let padded_t = Tensor::from_storage(TensorStorage::cpu(padded), padded_shape, false)?;

            // ifft is normalized (divides by N); multiply by N to unnormalize.
            let inv = fft::ifft(&padded_t, Some(n))?;
            let inv_data = inv.data_vec()?;
            let scale = T::from(n).unwrap();
            // Take real part: drop the trailing 2 axis.
            let mut grad_x_data = Vec::with_capacity(batch_size * n);
            for b in 0..batch_size {
                for nn in 0..n {
                    grad_x_data.push(inv_data[b * n * 2 + nn * 2] * scale);
                }
            }
            let mut out_shape = batch_shape.to_vec();
            out_shape.push(n);
            let t = Tensor::from_storage(TensorStorage::cpu(grad_x_data), out_shape, false)?;
            Some(if device.is_cuda() { t.to(device)? } else { t })
        } else {
            None
        };
        Ok(vec![grad_input])
    }

    fn inputs(&self) -> Vec<&Tensor<T>> {
        vec![&self.input]
    }

    fn name(&self) -> &'static str {
        "RfftBackward"
    }
}

// ---------------------------------------------------------------------------
// IrfftBackward
// ---------------------------------------------------------------------------

/// Backward for `y = irfft(x, n)` (Hermitian-truncated complex → real).
///
/// VJP derivation (matches PyTorch `FftC2RBackward` for `norm="backward"`):
///
/// Forward `y = irfft(x, N)` reconstructs the real signal by Hermitian-
/// extending `x` (shape `[..., K, 2]`, `K = N/2 + 1`) to length `N`, running
/// the unnormalized inverse FFT, and dividing by `N`. As a real-linear map
/// over `x`,
///
/// ```text
///   y[n] = (1/N) * (
///       x_re[0]
///     + (-1)^n * x_re[N/2]                                  (only when N even)
///     + 2 * sum_{k=1..N/2-1} (x_re[k] cos(2π k n/N) - x_im[k] sin(2π k n/N))
///   )
/// ```
///
/// Differentiating w.r.t. real `grad_y[n]` and assembling the cotangent on
/// `x` (complex of shape `[..., K, 2]`) gives, with `F = rfft(grad_y, N)`:
///
/// - boundary: `grad_x[0]    = F[0]    / N`
/// - boundary: `grad_x[N/2]  = F[N/2]  / N`     (when `N` even)
/// - interior: `grad_x[k]    = 2 * F[k] / N`    (for `k = 1..K-2`)
///
/// The factor of 2 is the Hermitian-doubling correction: each interior `k`
/// in the half-spectrum corresponds to **two** entries in the full DFT
/// (`k` and `N-k`), so the chain rule contributes twice. PyTorch's
/// `_fft_c2r_backward` handles this exactly the same way.
///
/// Net change vs. the previous (buggy) `rfft(grad_y, N)` call:
///   - divide all entries by `N` (the missing normalization),
///   - multiply interior entries by 2 (the Hermitian-doubling correction).
#[derive(Debug)]
pub struct IrfftBackward<T: Float> {
    input: Tensor<T>,
    _n: Option<usize>,
    /// The output length used in the forward pass.
    output_n: usize,
}

impl<T: Float> IrfftBackward<T> {
    pub fn new(input: Tensor<T>, n: Option<usize>, output_n: usize) -> Self {
        Self {
            input,
            _n: n,
            output_n,
        }
    }
}

impl<T: Float> GradFn<T> for IrfftBackward<T> {
    fn backward(&self, grad_output: &Tensor<T>) -> FerrotorchResult<Vec<Option<Tensor<T>>>> {
        let grad_input = if self.input.requires_grad() {
            let device = grad_output.device();
            let n = self.output_n;
            let k = n / 2 + 1;
            // rfft(grad_y, N) returns shape [..., K, 2] — same shape as x.
            let f = fft::rfft(grad_output, Some(n))?;
            let f_shape = f.shape().to_vec();
            let f_data = f.data_vec()?;
            let total_pairs = f_data.len() / 2;
            let batch_size = total_pairs / k;

            let inv_n = T::from(1.0).unwrap() / T::from(n).unwrap();
            let two = T::from(2.0).unwrap();
            let mut out = Vec::with_capacity(f_data.len());
            for b in 0..batch_size {
                for kk in 0..k {
                    let interior = kk > 0 && (kk < k - 1 || n % 2 == 1);
                    // For odd N there's no Nyquist sample; every k>0 is interior.
                    let factor = if interior { two * inv_n } else { inv_n };
                    out.push(f_data[(b * k + kk) * 2] * factor);
                    out.push(f_data[(b * k + kk) * 2 + 1] * factor);
                }
            }
            let t = Tensor::from_storage(TensorStorage::cpu(out), f_shape, false)?;
            Some(if device.is_cuda() { t.to(device)? } else { t })
        } else {
            None
        };
        Ok(vec![grad_input])
    }

    fn inputs(&self) -> Vec<&Tensor<T>> {
        vec![&self.input]
    }

    fn name(&self) -> &'static str {
        "IrfftBackward"
    }
}

// ---------------------------------------------------------------------------
// Differentiable forward wrappers
// ---------------------------------------------------------------------------

/// Resolve a torch `dim` (real-signal layout, `signal_ndim` dims) into a
/// non-negative axis. Used to translate the 1-D `dim` into the `axes=[dim]`
/// list the N-D rfft/irfft backward path consumes.
#[inline]
fn resolve_signal_axis(dim: Option<isize>, signal_ndim: usize) -> usize {
    match dim {
        None => signal_ndim.saturating_sub(1),
        Some(d) if d < 0 => (signal_ndim as isize + d).max(0) as usize,
        Some(d) => d as usize,
    }
}

/// Differentiable 1-D FFT (default `dim`/`norm`). Attaches `FftBackward`.
pub fn fft_differentiable<T: Float>(
    input: &Tensor<T>,
    n: Option<usize>,
) -> FerrotorchResult<Tensor<T>> {
    fft_differentiable_norm(input, n, None, FftNorm::Backward)
}

/// Differentiable 1-D FFT with explicit `dim` / `norm` (#1294). Attaches a
/// `FftBackward` that threads the adjoint norm/dim. Matches `torch.fft.fft`.
pub fn fft_differentiable_norm<T: Float>(
    input: &Tensor<T>,
    n: Option<usize>,
    dim: Option<isize>,
    norm: FftNorm,
) -> FerrotorchResult<Tensor<T>> {
    let device = input.device();
    let result = fft::fft_norm(input, n, dim, norm)?;

    if is_grad_enabled() && input.requires_grad() {
        let grad_fn = Arc::new(FftBackward::new_norm(input.clone(), n, dim, norm));
        let storage = TensorStorage::on_device(result.data_vec()?, device)?;
        Tensor::from_operation(storage, result.shape().to_vec(), grad_fn)
    } else {
        Ok(result)
    }
}

/// Differentiable 1-D inverse FFT (default `dim`/`norm`). Attaches `IfftBackward`.
pub fn ifft_differentiable<T: Float>(
    input: &Tensor<T>,
    n: Option<usize>,
) -> FerrotorchResult<Tensor<T>> {
    ifft_differentiable_norm(input, n, None, FftNorm::Backward)
}

/// Differentiable 1-D inverse FFT with explicit `dim` / `norm` (#1294).
pub fn ifft_differentiable_norm<T: Float>(
    input: &Tensor<T>,
    n: Option<usize>,
    dim: Option<isize>,
    norm: FftNorm,
) -> FerrotorchResult<Tensor<T>> {
    let device = input.device();
    let result = fft::ifft_norm(input, n, dim, norm)?;

    if is_grad_enabled() && input.requires_grad() {
        let grad_fn = Arc::new(IfftBackward::new_norm(input.clone(), n, dim, norm));
        let storage = TensorStorage::on_device(result.data_vec()?, device)?;
        Tensor::from_operation(storage, result.shape().to_vec(), grad_fn)
    } else {
        Ok(result)
    }
}

/// Differentiable 1-D real FFT (default `dim`/`norm`). Attaches `RfftBackward`.
pub fn rfft_differentiable<T: Float>(
    input: &Tensor<T>,
    n: Option<usize>,
) -> FerrotorchResult<Tensor<T>> {
    let device = input.device();
    let input_n = *input.shape().last().unwrap();
    let fft_n = n.unwrap_or(input_n);
    let result = fft::rfft(input, n)?;

    if is_grad_enabled() && input.requires_grad() {
        let grad_fn = Arc::new(RfftBackward::new(input.clone(), n, fft_n));
        let storage = TensorStorage::on_device(result.data_vec()?, device)?;
        Tensor::from_operation(storage, result.shape().to_vec(), grad_fn)
    } else {
        Ok(result)
    }
}

/// Differentiable 1-D real FFT with explicit `dim` / `norm` (#1294).
///
/// `rfft` along `dim` is the 1-axis specialization of `rfftn` over
/// `axes=[dim]`; the backward routes through the (norm- and axis-general)
/// [`RfftnBackward`] to reuse its proven stride-walk for the zero-pad +
/// adjoint inverse. The default `dim=-1`/`norm=Backward` path is identical to
/// [`rfft_differentiable`] (which keeps the cheaper [`RfftBackward`]).
pub fn rfft_differentiable_norm<T: Float>(
    input: &Tensor<T>,
    n: Option<usize>,
    dim: Option<isize>,
    norm: FftNorm,
) -> FerrotorchResult<Tensor<T>> {
    let in_shape = input.shape();
    let axis = resolve_signal_axis(dim, in_shape.len());
    // Fast path: default last-axis backward-norm → cheap 1-D backward.
    if norm == FftNorm::Backward && axis == in_shape.len() - 1 {
        return rfft_differentiable(input, n);
    }
    let device = input.device();
    let result = fft::rfft_norm(input, n, dim, norm)?;

    if is_grad_enabled() && input.requires_grad() {
        let last_axis_n = n.unwrap_or(in_shape[axis]);
        let s_back = vec![last_axis_n];
        let axes_back = vec![axis as isize];
        let out_shape = result.shape().to_vec();
        // last_axis_logical in the rfft output (trailing 2 excluded) is `axis`.
        let grad_fn = Arc::new(RfftnBackward::new_norm(
            input.clone(),
            Some(s_back),
            Some(axes_back),
            out_shape,
            last_axis_n,
            axis,
            last_axis_n,
            norm,
        ));
        let storage = TensorStorage::on_device(result.data_vec()?, device)?;
        Tensor::from_operation(storage, result.shape().to_vec(), grad_fn)
    } else {
        Ok(result)
    }
}

/// Differentiable 1-D inverse real FFT (default `dim`/`norm`). Attaches `IrfftBackward`.
pub fn irfft_differentiable<T: Float>(
    input: &Tensor<T>,
    n: Option<usize>,
) -> FerrotorchResult<Tensor<T>> {
    let device = input.device();
    let shape = input.shape();
    let half_n = shape[shape.len() - 2];
    let output_n = n.unwrap_or(2 * (half_n - 1));
    let result = fft::irfft(input, n)?;

    if is_grad_enabled() && input.requires_grad() {
        let grad_fn = Arc::new(IrfftBackward::new(input.clone(), n, output_n));
        let storage = TensorStorage::on_device(result.data_vec()?, device)?;
        Tensor::from_operation(storage, result.shape().to_vec(), grad_fn)
    } else {
        Ok(result)
    }
}

/// Differentiable 1-D inverse real FFT with explicit `dim` / `norm` (#1294).
///
/// `irfft` along `dim` is the 1-axis specialization of `irfftn` over
/// `axes=[dim]`; the backward routes through [`IrfftnBackward`]. Default
/// `dim=-1`/`norm=Backward` keeps the cheaper [`IrfftBackward`].
pub fn irfft_differentiable_norm<T: Float>(
    input: &Tensor<T>,
    n: Option<usize>,
    dim: Option<isize>,
    norm: FftNorm,
) -> FerrotorchResult<Tensor<T>> {
    let in_shape = input.shape();
    // The complex input's freq axis lives in the real-output layout; resolve
    // `dim` against the real-output ndim (= input ndim - 1, trailing 2 dropped).
    let real_ndim = in_shape.len().saturating_sub(1);
    let axis = resolve_signal_axis(dim, real_ndim);
    let half_n = in_shape[axis];
    let output_n = n.unwrap_or(2 * (half_n.saturating_sub(1)));
    if norm == FftNorm::Backward && axis == real_ndim.saturating_sub(1) {
        return irfft_differentiable(input, n);
    }
    let device = input.device();
    let result = fft::irfft_norm(input, n, dim, norm)?;

    if is_grad_enabled() && input.requires_grad() {
        let s_back = vec![output_n];
        let axes_back = vec![axis as isize];
        let grad_fn = Arc::new(IrfftnBackward::new_norm(
            input.clone(),
            Some(s_back),
            Some(axes_back),
            output_n,
            axis,
            output_n,
            norm,
        ));
        let storage = TensorStorage::on_device(result.data_vec()?, device)?;
        Tensor::from_operation(storage, result.shape().to_vec(), grad_fn)
    } else {
        Ok(result)
    }
}

// ---------------------------------------------------------------------------
// FftnBackward / IfftnBackward — N-D complex FFT backward.
// ---------------------------------------------------------------------------
//
// Math (FftNorm::Backward convention, matches torch.fft):
//   y = fftn(x, s, axes)   → grad_x = prod(s) * ifftn(grad_y, s, axes)
//   y = ifftn(x, s, axes)  → grad_x = fftn(grad_y, s, axes) / prod(s)
//
// The shape of the transform output along each transform axis is the value
// in `s` (or the input length if `s` is `None`). We persist `s` and `axes`
// from the forward to keep the backward shape-stable.

#[derive(Debug)]
pub struct FftnBackward<T: Float> {
    input: Tensor<T>,
    s: Option<Vec<usize>>,
    axes: Option<Vec<isize>>,
    /// Product of the transform-axis lengths in the forward output (retained
    /// for the legacy `new` ctor; the threaded-norm backward no longer needs
    /// an explicit scale — ferray's `adjoint_norm` inverse carries it).
    #[allow(dead_code, reason = "retained for the grandfathered `new` ctor API")]
    norm_n: usize,
    norm: FftNorm,
}

impl<T: Float> FftnBackward<T> {
    pub fn new(
        input: Tensor<T>,
        s: Option<Vec<usize>>,
        axes: Option<Vec<isize>>,
        norm_n: usize,
    ) -> Self {
        Self {
            input,
            s,
            axes,
            norm_n,
            norm: FftNorm::Backward,
        }
    }

    pub fn new_norm(
        input: Tensor<T>,
        s: Option<Vec<usize>>,
        axes: Option<Vec<isize>>,
        norm_n: usize,
        norm: FftNorm,
    ) -> Self {
        Self {
            input,
            s,
            axes,
            norm_n,
            norm,
        }
    }
}

impl<T: Float> GradFn<T> for FftnBackward<T> {
    fn backward(&self, grad_output: &Tensor<T>) -> FerrotorchResult<Vec<Option<Tensor<T>>>> {
        let grad_input = if self.input.requires_grad() {
            let device = grad_output.device();
            // Adjoint of `fftn(norm)` is `ifftn(adjoint_norm(norm))`.
            let inv = fft::ifftn_norm(
                grad_output,
                self.s.as_deref(),
                self.axes.as_deref(),
                adjoint_norm(self.norm),
            )?;
            Some(if device.is_cuda() {
                inv.to(device)?
            } else {
                inv
            })
        } else {
            None
        };
        Ok(vec![grad_input])
    }

    fn inputs(&self) -> Vec<&Tensor<T>> {
        vec![&self.input]
    }

    fn name(&self) -> &'static str {
        "FftnBackward"
    }
}

#[derive(Debug)]
pub struct IfftnBackward<T: Float> {
    input: Tensor<T>,
    s: Option<Vec<usize>>,
    axes: Option<Vec<isize>>,
    #[allow(dead_code, reason = "retained for the grandfathered `new` ctor API")]
    norm_n: usize,
    norm: FftNorm,
}

impl<T: Float> IfftnBackward<T> {
    pub fn new(
        input: Tensor<T>,
        s: Option<Vec<usize>>,
        axes: Option<Vec<isize>>,
        norm_n: usize,
    ) -> Self {
        Self {
            input,
            s,
            axes,
            norm_n,
            norm: FftNorm::Backward,
        }
    }

    pub fn new_norm(
        input: Tensor<T>,
        s: Option<Vec<usize>>,
        axes: Option<Vec<isize>>,
        norm_n: usize,
        norm: FftNorm,
    ) -> Self {
        Self {
            input,
            s,
            axes,
            norm_n,
            norm,
        }
    }
}

impl<T: Float> GradFn<T> for IfftnBackward<T> {
    fn backward(&self, grad_output: &Tensor<T>) -> FerrotorchResult<Vec<Option<Tensor<T>>>> {
        let grad_input = if self.input.requires_grad() {
            let device = grad_output.device();
            // Adjoint of `ifftn(norm)` is `fftn(adjoint_norm(norm))`.
            let fwd = fft::fftn_norm(
                grad_output,
                self.s.as_deref(),
                self.axes.as_deref(),
                adjoint_norm(self.norm),
            )?;
            Some(if device.is_cuda() {
                fwd.to(device)?
            } else {
                fwd
            })
        } else {
            None
        };
        Ok(vec![grad_input])
    }

    fn inputs(&self) -> Vec<&Tensor<T>> {
        vec![&self.input]
    }

    fn name(&self) -> &'static str {
        "IfftnBackward"
    }
}

// ---------------------------------------------------------------------------
// RfftnBackward / IrfftnBackward — N-D real FFT backward.
// ---------------------------------------------------------------------------
//
// VJPs:
//   y = rfftn(x, s, axes) (real → Hermitian complex)
//     → grad_x = irfftn(grad_y, s=original_real_shape, axes)
//   y = irfftn(x, s, axes) (Hermitian complex → real)
//     → grad_x = rfftn(grad_y, s=original_real_shape, axes)

/// Backward for `y = rfftn(x, s, axes)` (real → Hermitian-truncated complex,
/// N-D). Generalizes `RfftBackward` to multiple transform axes.
///
/// Only the **last** transform axis is Hermitian-truncated; the others go
/// full length. As in the 1-D case, the correct VJP is the
/// **partial** unnormalized inverse over the half-spectrum:
///
/// ```text
///   grad_x = real( ifftn_unnormalized(zero_pad_last_freq_axis(grad_Y), s, axes) )
/// ```
///
/// We use `fft::ifftn` (which divides by `prod(s)`) and multiply by
/// `prod(s)` to undo the normalization. The previous implementation called
/// `fft::irfftn(grad_Y, s, axes)` which Hermitian-extends and divides — both
/// errors of #809.
#[derive(Debug)]
pub struct RfftnBackward<T: Float> {
    input: Tensor<T>,
    /// Output sizes along the transform axes (passed to irfftn for backward).
    s: Option<Vec<usize>>,
    axes: Option<Vec<isize>>,
    /// `rfftn` output shape (used to invert the half-spectrum truncation).
    out_shape: Vec<usize>,
    /// Length of the last transform axis in the original real signal
    /// (so we know how far to zero-pad the freq axis).
    last_axis_n: usize,
    /// Logical index of the last transform axis in the rfftn output (the
    /// truncated freq axis). Trailing complex pair is excluded.
    last_axis_logical: usize,
    /// Product of transform-axis lengths (retained for the grandfathered
    /// `new` ctor; the threaded-norm backward uses `adjoint_norm` instead).
    #[allow(dead_code, reason = "retained for the grandfathered `new` ctor API")]
    norm_n: usize,
    /// Forward normalization mode.
    norm: FftNorm,
}

impl<T: Float> RfftnBackward<T> {
    pub fn new(
        input: Tensor<T>,
        s: Option<Vec<usize>>,
        axes: Option<Vec<isize>>,
        out_shape: Vec<usize>,
        last_axis_n: usize,
        last_axis_logical: usize,
        norm_n: usize,
    ) -> Self {
        Self::new_norm(
            input,
            s,
            axes,
            out_shape,
            last_axis_n,
            last_axis_logical,
            norm_n,
            FftNorm::Backward,
        )
    }

    #[allow(
        clippy::too_many_arguments,
        reason = "mirrors the forward's persisted state (s/axes/out_shape/\
                  last_axis_n/last_axis_logical/norm_n/norm); a struct-literal \
                  ctor wrapper would not reduce the captured fields"
    )]
    pub fn new_norm(
        input: Tensor<T>,
        s: Option<Vec<usize>>,
        axes: Option<Vec<isize>>,
        out_shape: Vec<usize>,
        last_axis_n: usize,
        last_axis_logical: usize,
        norm_n: usize,
        norm: FftNorm,
    ) -> Self {
        Self {
            input,
            s,
            axes,
            out_shape,
            last_axis_n,
            last_axis_logical,
            norm_n,
            norm,
        }
    }
}

impl<T: Float> GradFn<T> for RfftnBackward<T> {
    fn backward(&self, grad_output: &Tensor<T>) -> FerrotorchResult<Vec<Option<Tensor<T>>>> {
        let grad_input = if self.input.requires_grad() {
            let device = grad_output.device();
            // 1. Zero-pad along the last transform axis from K = n_last/2 + 1
            //    to n_last. The trailing axis (size 2) is the complex pair —
            //    untouched.
            let go_shape = grad_output.shape();
            if go_shape != self.out_shape.as_slice() {
                return Err(crate::error::FerrotorchError::InvalidArgument {
                    message: format!(
                        "RfftnBackward: grad_output shape {go_shape:?} does not match \
                         forward output {:?}",
                        self.out_shape
                    ),
                });
            }
            let go_data = grad_output.data_vec()?;
            let mut padded_shape = self.out_shape.clone();
            // Replace the half-spectrum dim with the full last_axis_n.
            // out_shape layout: [..., last_axis_K, 2]. last_axis_logical is the
            // index of the K dim within this layout (relative to the trailing 2).
            padded_shape[self.last_axis_logical] = self.last_axis_n;
            let padded_total: usize = padded_shape.iter().product();
            let mut padded = vec![T::from(0.0).unwrap(); padded_total];
            // Compute strides for both shapes (row-major).
            let go_strides = row_major_strides(go_shape);
            let pad_strides = row_major_strides(&padded_shape);
            // K = original last_axis dim in go_shape (half-spectrum).
            let k = go_shape[self.last_axis_logical];
            // Iterate every element of grad_output and copy into padded.
            for flat in 0..go_data.len() / 2 {
                // Compute multi-index for the [..., K, 2]-stripped layout
                // (i.e., excluding the trailing 2). flat indexes complex pairs
                // here; the trailing 2 is handled in the inner loop.
                let mut idx = vec![0usize; go_shape.len() - 1];
                let mut rem = flat;
                let logical_strides = row_major_strides(&go_shape[..go_shape.len() - 1]);
                for d in 0..idx.len() {
                    idx[d] = rem / logical_strides[d];
                    rem %= logical_strides[d];
                }
                // Source offset (real start of pair).
                let mut src = 0usize;
                for d in 0..idx.len() {
                    src += idx[d] * go_strides[d];
                }
                // Destination offset (uses padded_shape strides).
                let mut dst = 0usize;
                for d in 0..idx.len() {
                    dst += idx[d] * pad_strides[d];
                }
                padded[dst] = go_data[src];
                padded[dst + 1] = go_data[src + 1];
                let _ = k; // silence unused-variable warnings on some paths
            }
            let padded_t = Tensor::from_storage(TensorStorage::cpu(padded), padded_shape, false)?;

            // 2. Adjoint inverse FFT with `adjoint_norm(norm)`: ferray's
            //    direction-dependent scaling carries the un-normalization
            //    (e.g. Backward→Forward yields the un-normalized inverse,
            //    matching the old `prod(s) * ifftn` for norm="backward").
            let inv = fft::ifftn_norm(
                &padded_t,
                self.s.as_deref(),
                self.axes.as_deref(),
                adjoint_norm(self.norm),
            )?;
            // 3. Take real part (drop trailing 2).
            let inv_data = inv.data_vec()?;
            let inv_shape = inv.shape().to_vec();
            let real_n_pairs = inv_data.len() / 2;
            let mut grad_x_data = Vec::with_capacity(real_n_pairs);
            for i in 0..real_n_pairs {
                grad_x_data.push(inv_data[i * 2]);
            }
            // Drop trailing 2 from shape.
            let mut out_shape = inv_shape;
            let _ = out_shape.pop();
            let t = Tensor::from_storage(TensorStorage::cpu(grad_x_data), out_shape, false)?;
            Some(if device.is_cuda() { t.to(device)? } else { t })
        } else {
            None
        };
        Ok(vec![grad_input])
    }

    fn inputs(&self) -> Vec<&Tensor<T>> {
        vec![&self.input]
    }

    fn name(&self) -> &'static str {
        "RfftnBackward"
    }
}

/// Row-major strides for a shape (in elements, not bytes).
fn row_major_strides(shape: &[usize]) -> Vec<usize> {
    let mut strides = vec![1usize; shape.len()];
    for d in (0..shape.len().saturating_sub(1)).rev() {
        strides[d] = strides[d + 1] * shape[d + 1];
    }
    strides
}

/// Backward for `y = irfftn(x, s, axes)` (Hermitian-truncated complex → real,
/// N-D). Generalizes `IrfftBackward` to multiple transform axes.
///
/// The forward Hermitian-extends along the LAST transform axis only (the
/// other transform axes go full length). So the VJP is:
///
/// ```text
///   grad_x = rfftn(grad_y, s, axes) / prod(s),
///   then multiply interior frequencies along the last freq axis by 2.
/// ```
///
/// `grad_x_re/im[k]` for `k` along the last freq axis:
/// - boundary (`k = 0` and, when `n_last` is even, `k = n_last/2`):
///   divide by `prod(s)` only;
/// - interior (`k = 1..K-2` for even `n_last`, or `k = 1..K-1` for odd):
///   multiply by `2 / prod(s)`.
///
/// Same Hermitian-doubling correction as 1-D `IrfftBackward`, applied along
/// the truncated axis.
#[derive(Debug)]
pub struct IrfftnBackward<T: Float> {
    input: Tensor<T>,
    s: Option<Vec<usize>>,
    axes: Option<Vec<isize>>,
    /// Length of the last transform axis in the real output (so we can
    /// detect Nyquist parity).
    last_axis_n: usize,
    /// Logical index of the last transform axis in the original Hermitian
    /// input (i.e., in the rfftn output layout, half-spectrum dim).
    last_axis_logical: usize,
    /// `prod(s)` (retained for the grandfathered `new` ctor; the threaded-norm
    /// backward folds normalization into `adjoint_norm(norm)` instead).
    #[allow(dead_code, reason = "retained for the grandfathered `new` ctor API")]
    norm_n: usize,
    /// Forward normalization mode.
    norm: FftNorm,
}

impl<T: Float> IrfftnBackward<T> {
    pub fn new(
        input: Tensor<T>,
        s: Option<Vec<usize>>,
        axes: Option<Vec<isize>>,
        last_axis_n: usize,
        last_axis_logical: usize,
        norm_n: usize,
    ) -> Self {
        Self::new_norm(
            input,
            s,
            axes,
            last_axis_n,
            last_axis_logical,
            norm_n,
            FftNorm::Backward,
        )
    }

    #[allow(
        clippy::too_many_arguments,
        reason = "mirrors the forward's persisted state; a struct-literal \
                  wrapper would not reduce the captured fields"
    )]
    pub fn new_norm(
        input: Tensor<T>,
        s: Option<Vec<usize>>,
        axes: Option<Vec<isize>>,
        last_axis_n: usize,
        last_axis_logical: usize,
        norm_n: usize,
        norm: FftNorm,
    ) -> Self {
        Self {
            input,
            s,
            axes,
            last_axis_n,
            last_axis_logical,
            norm_n,
            norm,
        }
    }
}

impl<T: Float> GradFn<T> for IrfftnBackward<T> {
    fn backward(&self, grad_output: &Tensor<T>) -> FerrotorchResult<Vec<Option<Tensor<T>>>> {
        let grad_input = if self.input.requires_grad() {
            let device = grad_output.device();
            // 1. rfftn with `adjoint_norm(norm)`: ferray's forward-direction
            //    scaling carries the normalization. For norm="backward",
            //    adjoint=Forward gives forward-scale 1/prod(s), reproducing the
            //    old `rfftn(Backward) * (1/prod(s))`; "ortho"→Ortho gives
            //    1/sqrt(prod(s)), etc.
            let f = fft::rfftn_norm(
                grad_output,
                self.s.as_deref(),
                self.axes.as_deref(),
                adjoint_norm(self.norm),
            )?;
            let f_shape = f.shape().to_vec();
            let f_data = f.data_vec()?;

            // 2. Compute strides for the output of rfftn (which is the same
            //    shape as the input to irfftn, i.e., what the forward took).
            //    The trailing axis is the complex pair. The half-spectrum is
            //    at index `last_axis_logical` in the layout that excludes the
            //    trailing 2. The Hermitian-doubling correction (factor 2 on
            //    interior freqs) is applied on top of the already-normalized
            //    `f`.
            let two = T::from(2.0).unwrap();
            // K (half-spectrum length on the truncated axis).
            let k = f_shape[self.last_axis_logical];
            let n_last = self.last_axis_n;

            // Iterate every complex pair and apply the right factor.
            let strides_logical = row_major_strides(&f_shape[..f_shape.len() - 1]);
            let logical_total: usize = f_shape[..f_shape.len() - 1].iter().product();
            let mut out = vec![T::from(0.0).unwrap(); f_data.len()];
            for flat in 0..logical_total {
                let mut rem = flat;
                let mut idx = vec![0usize; strides_logical.len()];
                for d in 0..idx.len() {
                    idx[d] = rem / strides_logical[d];
                    rem %= strides_logical[d];
                }
                let kk = idx[self.last_axis_logical];
                // Boundary: kk == 0 always; kk == K-1 only when n_last is even.
                // `f` is already normalized; only the Hermitian-doubling
                // factor (1 boundary / 2 interior) remains.
                let is_boundary = kk == 0 || (n_last.is_multiple_of(2) && kk == k - 1);
                let factor = if is_boundary {
                    T::from(1.0).unwrap()
                } else {
                    two
                };
                let pair_offset = flat * 2;
                out[pair_offset] = f_data[pair_offset] * factor;
                out[pair_offset + 1] = f_data[pair_offset + 1] * factor;
            }
            let t = Tensor::from_storage(TensorStorage::cpu(out), f_shape, false)?;
            Some(if device.is_cuda() { t.to(device)? } else { t })
        } else {
            None
        };
        Ok(vec![grad_input])
    }

    fn inputs(&self) -> Vec<&Tensor<T>> {
        vec![&self.input]
    }

    fn name(&self) -> &'static str {
        "IrfftnBackward"
    }
}

// ---------------------------------------------------------------------------
// HfftBackward / IhfftBackward — Hermitian FFT backward.
// ---------------------------------------------------------------------------
//
// hfft maps Hermitian-symmetric complex `[..., n/2+1, 2]` → real `[..., n]`.
// ihfft is the inverse: real `[..., n]` → Hermitian complex `[..., n/2+1, 2]`.
//
// VJPs (matching torch.fft.hfft / ihfft):
//   y = hfft(x, n)  → grad_x = ihfft(grad_y, n=input_n)
//   y = ihfft(x, n) → grad_x = hfft(grad_y, n=input_n)

/// Backward for `y = hfft(x, n)` (Hermitian complex `[..., K, 2]` → real
/// `[..., n]`).
///
/// Forward (FftNorm::Backward): `hfft(x, N) = irfft_unnormalized(conj(x), N)`,
/// i.e., `y[n] = real(sum_{k=0..N-1} conj(x_full[k]) exp(+2π i k n / N))` with
/// no `1/N` scaling. Expanding using the Hermitian symmetry of `x`,
///
/// ```text
///   y[n] = x_re[0]
///        + (-1)^n * x_re[N/2]                                   (only for even N)
///        + 2 * sum_{k=1..N/2-1} (x_re[k] cos(2π k n/N) + x_im[k] sin(2π k n/N))
/// ```
///
/// VJP from real `grad_y` to Hermitian complex `grad_x` (shape `[..., K, 2]`),
/// with `F = rfft(grad_y, N)` (unnormalized, so `re(F[k]) = sum_n grad_y[n]
/// cos(2π k n/N)`, `im(F[k]) = -sum_n grad_y[n] sin(2π k n/N)`):
///
/// - boundary: `grad_x_re[0]   = re(F[0])`,    `grad_x_im[0]   = 0`
/// - boundary: `grad_x_re[N/2] = re(F[N/2])`,  `grad_x_im[N/2] = 0`   (even N)
/// - interior: `grad_x_re[k]   = 2 * re(F[k])`
/// - interior: `grad_x_im[k]   = -2 * im(F[k])`  (sign: `+sin → -im(F)`)
///
/// Concretely: `grad_x = conj(rfft(grad_y, N))` for boundary entries,
/// `grad_x = 2 * conj(rfft(grad_y, N))` for interior.
///
/// The previous implementation called `fft::ihfft(grad_y, n_forward)`, which
/// is `conj(rfft(grad_y))/N`, missing both the `* N` (FftNorm::Backward
/// hfft is unnormalized forward) and the interior `* 2` correction.
#[derive(Debug)]
pub struct HfftBackward<T: Float> {
    input: Tensor<T>,
    /// Length of the original Hermitian spectrum (input's second-to-last dim).
    input_n: usize,
    /// Length of the real signal produced by hfft (so we know Nyquist parity).
    output_n: usize,
}

impl<T: Float> HfftBackward<T> {
    pub fn new(input: Tensor<T>, input_n: usize, output_n: usize) -> Self {
        Self {
            input,
            input_n,
            output_n,
        }
    }
}

impl<T: Float> GradFn<T> for HfftBackward<T> {
    fn backward(&self, grad_output: &Tensor<T>) -> FerrotorchResult<Vec<Option<Tensor<T>>>> {
        let grad_input = if self.input.requires_grad() {
            let device = grad_output.device();
            let n = self.output_n;
            let k = self.input_n; // K = N/2 + 1 for even N (or (N+1)/2 for odd).
            // F = unnormalized rfft of grad_y.
            let f = fft::rfft(grad_output, Some(n))?;
            let f_data = f.data_vec()?;
            let f_shape = f.shape().to_vec();
            let total_pairs = f_data.len() / 2;
            let batch_size = total_pairs / k;
            let two = T::from(2.0).unwrap();
            let mut out = Vec::with_capacity(f_data.len());
            for b in 0..batch_size {
                for kk in 0..k {
                    // Boundary: kk == 0; kk == K-1 only when n is even.
                    let is_boundary = kk == 0 || (n.is_multiple_of(2) && kk == k - 1);
                    let factor = if is_boundary {
                        T::from(1.0).unwrap()
                    } else {
                        two
                    };
                    let re = f_data[(b * k + kk) * 2];
                    let im = f_data[(b * k + kk) * 2 + 1];
                    // grad_x = factor * conj(F[k]). conj negates the imag part.
                    out.push(re * factor);
                    out.push(-im * factor);
                }
            }
            let t = Tensor::from_storage(TensorStorage::cpu(out), f_shape, false)?;
            Some(if device.is_cuda() { t.to(device)? } else { t })
        } else {
            None
        };
        Ok(vec![grad_input])
    }

    fn inputs(&self) -> Vec<&Tensor<T>> {
        vec![&self.input]
    }

    fn name(&self) -> &'static str {
        "HfftBackward"
    }
}

/// Backward for `y = ihfft(x, n)` (real `[..., N]` → Hermitian complex
/// `[..., K, 2]`).
///
/// Forward (FftNorm::Backward): `ihfft(x, N) = conj(rfft(x, N)) / N`. As a
/// real-linear map,
///
/// ```text
///   y_re[k] = (1/N) * sum_n x[n] cos(2π k n/N)
///   y_im[k] = (1/N) * sum_n x[n] sin(2π k n/N)
/// ```
///
/// The Hermitian half-spectrum has K = N/2 + 1 entries. The cotangent on
/// real `x` is the partial unnormalized inverse over that half:
///
/// ```text
///   grad_x[n] = (1/N) * sum_{k=0..K-1} (
///                  grad_y_re[k] cos(2π k n/N) + grad_y_im[k] sin(2π k n/N)
///              )
///            = (1/N) * real( sum_{k=0..K-1} conj(grad_y[k]) exp(+2π i k n/N) )
/// ```
///
/// (Because `conj(grad_y[k]) = grad_y_re[k] - i grad_y_im[k]` and
/// `exp(+i θ) = cos θ + i sin θ`, the real part picks up the desired
/// sign-correct combination.)
///
/// Implementation: zero-pad `conj(grad_y)` along the freq axis from `K` to
/// `N`, run our normalized `fft::ifft` (which already supplies the `1/N`),
/// take the real part. No further scaling needed.
///
/// The previous implementation called `fft::hfft(grad_y, input_n)`, which
/// is the unnormalized inverse with conj — wrong by an `N` factor and by
/// the absent boundary/interior treatment.
#[derive(Debug)]
pub struct IhfftBackward<T: Float> {
    input: Tensor<T>,
    /// Length of the original real signal (input's last dim).
    input_n: usize,
}

impl<T: Float> IhfftBackward<T> {
    pub fn new(input: Tensor<T>, input_n: usize) -> Self {
        Self { input, input_n }
    }
}

impl<T: Float> GradFn<T> for IhfftBackward<T> {
    fn backward(&self, grad_output: &Tensor<T>) -> FerrotorchResult<Vec<Option<Tensor<T>>>> {
        let grad_input = if self.input.requires_grad() {
            let device = grad_output.device();
            let n = self.input_n;
            let go_shape = grad_output.shape();
            if go_shape.len() < 2 || go_shape[go_shape.len() - 1] != 2 {
                return Err(crate::error::FerrotorchError::InvalidArgument {
                    message: format!(
                        "IhfftBackward: grad_output must have trailing complex pair, got {go_shape:?}"
                    ),
                });
            }
            let k = go_shape[go_shape.len() - 2];
            let batch_shape = &go_shape[..go_shape.len() - 2];
            let batch_size: usize = batch_shape.iter().product::<usize>().max(1);
            let go_data = grad_output.data_vec()?;

            // Zero-pad conj(grad_y) from [..., K, 2] to [..., N, 2].
            let mut padded = vec![T::from(0.0).unwrap(); batch_size * n * 2];
            for b in 0..batch_size {
                let src_off = b * k * 2;
                let dst_off = b * n * 2;
                let copy_pairs = k.min(n);
                for kk in 0..copy_pairs {
                    let re = go_data[src_off + kk * 2];
                    let im = go_data[src_off + kk * 2 + 1];
                    padded[dst_off + kk * 2] = re;
                    padded[dst_off + kk * 2 + 1] = -im; // conj
                }
            }
            let mut padded_shape = batch_shape.to_vec();
            padded_shape.push(n);
            padded_shape.push(2);
            let padded_t = Tensor::from_storage(TensorStorage::cpu(padded), padded_shape, false)?;

            // Normalized ifft: divides by N — exactly the 1/N we want.
            let inv = fft::ifft(&padded_t, Some(n))?;
            let inv_data = inv.data_vec()?;
            // Take real part: drop trailing 2.
            let mut grad_x_data = Vec::with_capacity(batch_size * n);
            for b in 0..batch_size {
                for nn in 0..n {
                    grad_x_data.push(inv_data[b * n * 2 + nn * 2]);
                }
            }
            let mut out_shape = batch_shape.to_vec();
            out_shape.push(n);
            let t = Tensor::from_storage(TensorStorage::cpu(grad_x_data), out_shape, false)?;
            Some(if device.is_cuda() { t.to(device)? } else { t })
        } else {
            None
        };
        Ok(vec![grad_input])
    }

    fn inputs(&self) -> Vec<&Tensor<T>> {
        vec![&self.input]
    }

    fn name(&self) -> &'static str {
        "IhfftBackward"
    }
}

// ---------------------------------------------------------------------------
// Differentiable forward wrappers — N-D + Hermitian (#580)
// ---------------------------------------------------------------------------

/// Compute the product of transform-axis lengths used for normalization.
/// Mirrors how the forward pass would interpret `s` / `axes`:
///   - If `s` is given, multiply its entries.
///   - Else if `axes` is given, multiply the input's lengths along those axes.
///   - Else multiply the inner dims (excluding the trailing complex pair).
fn fftn_norm_n<T: Float>(input: &Tensor<T>, s: Option<&[usize]>, axes: Option<&[isize]>) -> usize {
    if let Some(s_slice) = s {
        return s_slice.iter().copied().product::<usize>().max(1);
    }
    let shape = input.shape();
    let ndim = shape.len();
    if let Some(axes_slice) = axes {
        let mut prod: usize = 1;
        for &a in axes_slice {
            // Resolve negative axes against `ndim - 1` (excluding trailing
            // complex pair).
            let logical_ndim = ndim.saturating_sub(1);
            let resolved = if a < 0 {
                (logical_ndim as isize + a) as usize
            } else {
                a as usize
            };
            prod = prod.saturating_mul(shape[resolved]);
        }
        return prod.max(1);
    }
    // Default: all inner dims (skip the trailing 2).
    if ndim < 2 {
        1
    } else {
        shape[..ndim - 1].iter().product::<usize>().max(1)
    }
}

/// Same as [`fftn_norm_n`] but for real inputs: there is no trailing complex
/// pair, so all dims except the leading batch are candidates.
fn rfftn_norm_n<T: Float>(input: &Tensor<T>, s: Option<&[usize]>, axes: Option<&[isize]>) -> usize {
    if let Some(s_slice) = s {
        return s_slice.iter().copied().product::<usize>().max(1);
    }
    let shape = input.shape();
    let ndim = shape.len();
    if let Some(axes_slice) = axes {
        let mut prod: usize = 1;
        for &a in axes_slice {
            let resolved = if a < 0 {
                (ndim as isize + a) as usize
            } else {
                a as usize
            };
            prod = prod.saturating_mul(shape[resolved]);
        }
        return prod.max(1);
    }
    shape.iter().product::<usize>().max(1)
}

/// Differentiable N-D FFT (default `norm`). Attaches `FftnBackward`.
pub fn fftn_differentiable<T: Float>(
    input: &Tensor<T>,
    s: Option<&[usize]>,
    axes: Option<&[isize]>,
) -> FerrotorchResult<Tensor<T>> {
    fftn_differentiable_norm(input, s, axes, FftNorm::Backward)
}

/// Differentiable N-D FFT with explicit `norm` (#1294). Matches
/// `torch.fft.fftn`; `axes` is torch's `dim`.
pub fn fftn_differentiable_norm<T: Float>(
    input: &Tensor<T>,
    s: Option<&[usize]>,
    axes: Option<&[isize]>,
    norm: FftNorm,
) -> FerrotorchResult<Tensor<T>> {
    let device = input.device();
    let result = fft::fftn_norm(input, s, axes, norm)?;

    if is_grad_enabled() && input.requires_grad() {
        let norm_n = fftn_norm_n(input, s, axes);
        let grad_fn = Arc::new(FftnBackward::new_norm(
            input.clone(),
            s.map(|v| v.to_vec()),
            axes.map(|v| v.to_vec()),
            norm_n,
            norm,
        ));
        let storage = TensorStorage::on_device(result.data_vec()?, device)?;
        Tensor::from_operation(storage, result.shape().to_vec(), grad_fn)
    } else {
        Ok(result)
    }
}

/// Differentiable N-D inverse FFT (default `norm`). Attaches `IfftnBackward`.
pub fn ifftn_differentiable<T: Float>(
    input: &Tensor<T>,
    s: Option<&[usize]>,
    axes: Option<&[isize]>,
) -> FerrotorchResult<Tensor<T>> {
    ifftn_differentiable_norm(input, s, axes, FftNorm::Backward)
}

/// Differentiable N-D inverse FFT with explicit `norm` (#1294).
pub fn ifftn_differentiable_norm<T: Float>(
    input: &Tensor<T>,
    s: Option<&[usize]>,
    axes: Option<&[isize]>,
    norm: FftNorm,
) -> FerrotorchResult<Tensor<T>> {
    let device = input.device();
    let result = fft::ifftn_norm(input, s, axes, norm)?;

    if is_grad_enabled() && input.requires_grad() {
        let norm_n = fftn_norm_n(input, s, axes);
        let grad_fn = Arc::new(IfftnBackward::new_norm(
            input.clone(),
            s.map(|v| v.to_vec()),
            axes.map(|v| v.to_vec()),
            norm_n,
            norm,
        ));
        let storage = TensorStorage::on_device(result.data_vec()?, device)?;
        Tensor::from_operation(storage, result.shape().to_vec(), grad_fn)
    } else {
        Ok(result)
    }
}

/// Differentiable N-D real FFT (default `norm`). Attaches `RfftnBackward`.
pub fn rfftn_differentiable<T: Float>(
    input: &Tensor<T>,
    s: Option<&[usize]>,
    axes: Option<&[isize]>,
) -> FerrotorchResult<Tensor<T>> {
    rfftn_differentiable_norm(input, s, axes, FftNorm::Backward)
}

/// Differentiable N-D real FFT with explicit `norm` (#1294). Matches
/// `torch.fft.rfftn`.
pub fn rfftn_differentiable_norm<T: Float>(
    input: &Tensor<T>,
    s: Option<&[usize]>,
    axes: Option<&[isize]>,
    norm: FftNorm,
) -> FerrotorchResult<Tensor<T>> {
    let device = input.device();
    let _ = rfftn_norm_n::<T>; // keep helper available for symmetry; not needed in fwd
    let result = fft::rfftn_norm(input, s, axes, norm)?;

    if is_grad_enabled() && input.requires_grad() {
        // Backward needs the original real-input shape along the transform
        // axes. We pass the input's shape segment so irfftn can reconstruct.
        let s_back: Vec<usize> = match (s, axes) {
            (Some(s_slice), _) => s_slice.to_vec(),
            (None, Some(axes_slice)) => {
                let shape = input.shape();
                axes_slice
                    .iter()
                    .map(|&a| {
                        let resolved = if a < 0 {
                            (shape.len() as isize + a) as usize
                        } else {
                            a as usize
                        };
                        shape[resolved]
                    })
                    .collect()
            }
            (None, None) => input.shape().to_vec(),
        };
        // Resolve the last transform axis (logical, in real-input space).
        let in_shape = input.shape();
        let last_axis_logical = match axes {
            Some(axes_slice) => {
                let a = *axes_slice.last().unwrap();
                if a < 0 {
                    (in_shape.len() as isize + a) as usize
                } else {
                    a as usize
                }
            }
            None => in_shape.len() - 1,
        };
        let last_axis_n = s_back
            .last()
            .copied()
            .unwrap_or(in_shape[last_axis_logical]);
        let norm_n: usize = s_back.iter().product::<usize>().max(1);
        let out_shape = result.shape().to_vec();
        let grad_fn = Arc::new(RfftnBackward::new_norm(
            input.clone(),
            Some(s_back),
            axes.map(|v| v.to_vec()),
            out_shape,
            last_axis_n,
            last_axis_logical,
            norm_n,
            norm,
        ));
        let storage = TensorStorage::on_device(result.data_vec()?, device)?;
        Tensor::from_operation(storage, result.shape().to_vec(), grad_fn)
    } else {
        Ok(result)
    }
}

/// Differentiable N-D inverse real FFT (default `norm`). Attaches `IrfftnBackward`.
pub fn irfftn_differentiable<T: Float>(
    input: &Tensor<T>,
    s: Option<&[usize]>,
    axes: Option<&[isize]>,
) -> FerrotorchResult<Tensor<T>> {
    irfftn_differentiable_norm(input, s, axes, FftNorm::Backward)
}

/// Differentiable N-D inverse real FFT with explicit `norm` (#1294). Matches
/// `torch.fft.irfftn`.
pub fn irfftn_differentiable_norm<T: Float>(
    input: &Tensor<T>,
    s: Option<&[usize]>,
    axes: Option<&[isize]>,
    norm: FftNorm,
) -> FerrotorchResult<Tensor<T>> {
    let device = input.device();
    let result = fft::irfftn_norm(input, s, axes, norm)?;

    if is_grad_enabled() && input.requires_grad() {
        // The forward output length along each transform axis becomes the
        // original real shape; back-pass uses the same `s` to reconstruct.
        let s_back: Vec<usize> = match s {
            Some(s_slice) => s_slice.to_vec(),
            None => result.shape().to_vec(),
        };
        // Resolve the last transform axis. Layout: input is the
        // Hermitian-truncated complex tensor with trailing 2; for the real
        // output (`result`), the last freq axis is the last entry of `axes`
        // (or the last axis if `axes` is `None`).
        let res_shape = result.shape();
        let last_axis_logical_real = match axes {
            Some(axes_slice) => {
                let a = *axes_slice.last().unwrap();
                if a < 0 {
                    (res_shape.len() as isize + a) as usize
                } else {
                    a as usize
                }
            }
            None => res_shape.len() - 1,
        };
        // For the input (`x` to irfftn), the half-spectrum axis is at the
        // same logical index since irfftn's input layout is `[..., 2]` and
        // the freq axis maps 1:1 with the real output's last transform axis.
        let last_axis_logical = last_axis_logical_real;
        let last_axis_n = *s_back.last().unwrap_or(&res_shape[last_axis_logical_real]);
        let norm_n: usize = s_back.iter().product::<usize>().max(1);
        let grad_fn = Arc::new(IrfftnBackward::new_norm(
            input.clone(),
            Some(s_back),
            axes.map(|v| v.to_vec()),
            last_axis_n,
            last_axis_logical,
            norm_n,
            norm,
        ));
        let storage = TensorStorage::on_device(result.data_vec()?, device)?;
        Tensor::from_operation(storage, result.shape().to_vec(), grad_fn)
    } else {
        Ok(result)
    }
}

/// Differentiable Hermitian FFT (complex spectrum → real signal). Attaches
/// `HfftBackward` when grad is needed.
pub fn hfft_differentiable<T: Float>(
    input: &Tensor<T>,
    n: Option<usize>,
) -> FerrotorchResult<Tensor<T>> {
    let device = input.device();
    let shape = input.shape();
    let input_n = shape[shape.len() - 2];
    let result = fft::hfft(input, n)?;
    // hfft output's last dim is the real-signal length N. Persist it so the
    // backward can detect Nyquist parity (boundary vs. interior k).
    let output_n = *result.shape().last().unwrap();

    if is_grad_enabled() && input.requires_grad() {
        let grad_fn = Arc::new(HfftBackward::new(input.clone(), input_n, output_n));
        let storage = TensorStorage::on_device(result.data_vec()?, device)?;
        Tensor::from_operation(storage, result.shape().to_vec(), grad_fn)
    } else {
        Ok(result)
    }
}

/// Differentiable inverse Hermitian FFT (real signal → Hermitian spectrum).
/// Attaches `IhfftBackward` when grad is needed.
pub fn ihfft_differentiable<T: Float>(
    input: &Tensor<T>,
    n: Option<usize>,
) -> FerrotorchResult<Tensor<T>> {
    let device = input.device();
    let shape = input.shape();
    // FFT length N: defaults to the input's last dim (real signal length).
    // When `n` is supplied, ihfft truncates/pads the input before the
    // transform — the backward reconstructs grad over that same `N`.
    let input_n = n.unwrap_or(*shape.last().unwrap());
    let result = fft::ihfft(input, n)?;

    if is_grad_enabled() && input.requires_grad() {
        let grad_fn = Arc::new(IhfftBackward::new(input.clone(), input_n));
        let storage = TensorStorage::on_device(result.data_vec()?, device)?;
        Tensor::from_operation(storage, result.shape().to_vec(), grad_fn)
    } else {
        Ok(result)
    }
}

// ---------------------------------------------------------------------------
// Fft2Backward / Ifft2Backward — 2-D complex FFT backward (#1300).
// ---------------------------------------------------------------------------
//
// `torch.fft.fft2` / `ifft2` are the 2-D specializations of `fftn` / `ifftn`
// over the trailing two spatial axes (`aten::fft_fft2_symint` literally does
// `return fft_fftn_symint(self, s, dim, norm)` at SpectralOps.cpp:644-652).
// The VJPs are therefore the FftnBackward / IfftnBackward identities with the
// transform set fixed to the last two axes:
//   y = fft2(x)   → grad_x = (rows*cols) * ifft2(grad_y)
//   y = ifft2(x)  → grad_x = fft2(grad_y) / (rows*cols)
// `norm_n = rows * cols` is the product of the two transform-axis lengths,
// captured from the forward input shape (`shape[ndim-3] * shape[ndim-2]`,
// since the trailing axis of size 2 is the complex pair).

/// Backward for `y = fft2(x)` (un-normalized 2-D forward DFT over the last two
/// spatial axes). VJP: `grad_x = norm_n * ifft2(grad_y)` (our `ifft2` divides
/// by `rows*cols`; multiplying by `norm_n` undoes that to yield the
/// un-normalized inverse).
#[derive(Debug)]
pub struct Fft2Backward<T: Float> {
    input: Tensor<T>,
    /// Product of the two transform-axis lengths (rows * cols).
    norm_n: usize,
}

impl<T: Float> Fft2Backward<T> {
    pub fn new(input: Tensor<T>, norm_n: usize) -> Self {
        Self { input, norm_n }
    }
}

impl<T: Float> GradFn<T> for Fft2Backward<T> {
    fn backward(&self, grad_output: &Tensor<T>) -> FerrotorchResult<Vec<Option<Tensor<T>>>> {
        let grad_input = if self.input.requires_grad() {
            let device = grad_output.device();
            let inv = fft::ifft2(grad_output)?;
            let scale = T::from(self.norm_n).unwrap();
            let inv_data = inv.data_vec()?;
            let scaled: Vec<T> = inv_data.iter().map(|&v| v * scale).collect();
            let t = Tensor::from_storage(TensorStorage::cpu(scaled), inv.shape().to_vec(), false)?;
            Some(if device.is_cuda() { t.to(device)? } else { t })
        } else {
            None
        };
        Ok(vec![grad_input])
    }

    fn inputs(&self) -> Vec<&Tensor<T>> {
        vec![&self.input]
    }

    fn name(&self) -> &'static str {
        "Fft2Backward"
    }
}

/// Backward for `y = ifft2(x)` (1/(rows*cols)-normalized 2-D inverse). VJP:
/// `grad_x = fft2(grad_y) / norm_n`.
#[derive(Debug)]
pub struct Ifft2Backward<T: Float> {
    input: Tensor<T>,
    norm_n: usize,
}

impl<T: Float> Ifft2Backward<T> {
    pub fn new(input: Tensor<T>, norm_n: usize) -> Self {
        Self { input, norm_n }
    }
}

impl<T: Float> GradFn<T> for Ifft2Backward<T> {
    fn backward(&self, grad_output: &Tensor<T>) -> FerrotorchResult<Vec<Option<Tensor<T>>>> {
        let grad_input = if self.input.requires_grad() {
            let device = grad_output.device();
            let fwd = fft::fft2(grad_output)?;
            let scale = T::from(1.0).unwrap() / T::from(self.norm_n).unwrap();
            let fwd_data = fwd.data_vec()?;
            let scaled: Vec<T> = fwd_data.iter().map(|&v| v * scale).collect();
            let t = Tensor::from_storage(TensorStorage::cpu(scaled), fwd.shape().to_vec(), false)?;
            Some(if device.is_cuda() { t.to(device)? } else { t })
        } else {
            None
        };
        Ok(vec![grad_input])
    }

    fn inputs(&self) -> Vec<&Tensor<T>> {
        vec![&self.input]
    }

    fn name(&self) -> &'static str {
        "Ifft2Backward"
    }
}

/// Product of the two transform-axis lengths for `fft2` / `ifft2`. The forward
/// kernels are fixed to the trailing two spatial axes; for input shape
/// `[..., rows, cols, 2]` this is `rows * cols`.
fn fft2_norm_n<T: Float>(input: &Tensor<T>) -> usize {
    let shape = input.shape();
    let ndim = shape.len();
    if ndim < 3 {
        return 1;
    }
    (shape[ndim - 3] * shape[ndim - 2]).max(1)
}

/// Differentiable 2-D FFT (default `s`/`dim`/`norm`). Attaches `Fft2Backward`.
pub fn fft2_differentiable<T: Float>(input: &Tensor<T>) -> FerrotorchResult<Tensor<T>> {
    let device = input.device();
    let result = fft::fft2(input)?;

    if is_grad_enabled() && input.requires_grad() {
        let norm_n = fft2_norm_n(input);
        let grad_fn = Arc::new(Fft2Backward::new(input.clone(), norm_n));
        let storage = TensorStorage::on_device(result.data_vec()?, device)?;
        Tensor::from_operation(storage, result.shape().to_vec(), grad_fn)
    } else {
        Ok(result)
    }
}

/// Differentiable 2-D FFT with explicit `s` / `dim` / `norm` (#1294).
///
/// `torch.fft.fft2` delegates to `fft_fftn_symint`
/// (`aten/src/ATen/native/SpectralOps.cpp:644-652`); the default last-two-axes
/// / backward-norm case keeps the cheaper [`Fft2Backward`] (GPU-capable),
/// while any explicit `s`/`dim`/`norm` routes through
/// [`fftn_differentiable_norm`] (op_db emits `dim=[-3,-2,-1]` for `fft2`,
/// which torch treats as an N-D transform).
pub fn fft2_differentiable_norm<T: Float>(
    input: &Tensor<T>,
    s: Option<&[usize]>,
    dim: Option<&[isize]>,
    norm: FftNorm,
) -> FerrotorchResult<Tensor<T>> {
    if s.is_none() && dim.is_none() && norm == FftNorm::Backward {
        return fft2_differentiable(input);
    }
    // Default axes for fft2 are the last two; fftn over those axes is identical.
    let default_axes: [isize; 2] = [-2, -1];
    let axes = dim.unwrap_or(&default_axes);
    fftn_differentiable_norm(input, s, Some(axes), norm)
}

/// Differentiable 2-D inverse FFT (default `s`/`dim`/`norm`). Attaches `Ifft2Backward`.
pub fn ifft2_differentiable<T: Float>(input: &Tensor<T>) -> FerrotorchResult<Tensor<T>> {
    let device = input.device();
    let result = fft::ifft2(input)?;

    if is_grad_enabled() && input.requires_grad() {
        let norm_n = fft2_norm_n(input);
        let grad_fn = Arc::new(Ifft2Backward::new(input.clone(), norm_n));
        let storage = TensorStorage::on_device(result.data_vec()?, device)?;
        Tensor::from_operation(storage, result.shape().to_vec(), grad_fn)
    } else {
        Ok(result)
    }
}

/// Differentiable 2-D inverse FFT with explicit `s` / `dim` / `norm` (#1294).
pub fn ifft2_differentiable_norm<T: Float>(
    input: &Tensor<T>,
    s: Option<&[usize]>,
    dim: Option<&[isize]>,
    norm: FftNorm,
) -> FerrotorchResult<Tensor<T>> {
    if s.is_none() && dim.is_none() && norm == FftNorm::Backward {
        return ifft2_differentiable(input);
    }
    let default_axes: [isize; 2] = [-2, -1];
    let axes = dim.unwrap_or(&default_axes);
    ifftn_differentiable_norm(input, s, Some(axes), norm)
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    fn leaf(data: &[f64], shape: &[usize]) -> Tensor<f64> {
        Tensor::from_storage(TensorStorage::cpu(data.to_vec()), shape.to_vec(), true).unwrap()
    }

    fn no_grad_leaf(data: &[f64], shape: &[usize]) -> Tensor<f64> {
        Tensor::from_storage(TensorStorage::cpu(data.to_vec()), shape.to_vec(), false).unwrap()
    }

    fn assert_close(actual: &[f64], expected: &[f64], tol: f64) {
        assert_eq!(
            actual.len(),
            expected.len(),
            "length mismatch: {} vs {}",
            actual.len(),
            expected.len()
        );
        for (i, (&a, &e)) in actual.iter().zip(expected.iter()).enumerate() {
            assert!(
                (a - e).abs() < tol,
                "index {i}: {a} vs {e} (diff {})",
                (a - e).abs()
            );
        }
    }

    #[test]
    fn fft_differentiable_attaches_grad_fn() {
        // Complex input [4, 2] with requires_grad.
        let input = leaf(&[1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], &[4, 2]);
        let result = fft_differentiable(&input, None).unwrap();
        assert!(result.grad_fn().is_some());
        assert_eq!(result.grad_fn().unwrap().name(), "FftBackward");
    }

    #[test]
    fn fft_differentiable_no_grad_when_not_needed() {
        let input = no_grad_leaf(&[1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], &[4, 2]);
        let result = fft_differentiable(&input, None).unwrap();
        assert!(result.grad_fn().is_none());
    }

    #[test]
    fn fft_backward_identity_check() {
        // For FFT of an impulse [1,0,0,0] -> [1,1,1,1] (all real).
        // grad_output = ones_like(output) = [[1,0],[1,0],[1,0],[1,0]].
        // grad_input = n * ifft(grad_output).
        // ifft([1,1,1,1]) = [1,0,0,0] (impulse).
        // So grad_input = 4 * [1,0,0,0] = [4,0,0,0].
        let input = leaf(&[1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], &[4, 2]);
        let result = fft_differentiable(&input, None).unwrap();

        let grad_out = no_grad_leaf(&[1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0], &[4, 2]);
        let grads = result.grad_fn().unwrap().backward(&grad_out).unwrap();
        assert!(grads[0].is_some());

        let g = grads[0].as_ref().unwrap();
        assert_eq!(g.shape(), &[4, 2]);
        let gd = g.data().unwrap();
        // Should be [4, 0, 0, 0, 0, 0, 0, 0].
        assert_close(gd, &[4.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], 1e-10);
    }

    #[test]
    fn ifft_backward_identity_check() {
        // ifft([1,1,1,1]) = [1,0,0,0].
        // grad_output = [[1,0],[0,0],[0,0],[0,0]].
        // grad_input = fft(grad_output) / n.
        // fft([1,0,0,0]) = [1,1,1,1].
        // grad_input = [1,1,1,1] / 4 = [0.25, 0.25, 0.25, 0.25].
        let input = leaf(&[1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0], &[4, 2]);
        let result = ifft_differentiable(&input, None).unwrap();

        let grad_out = no_grad_leaf(&[1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], &[4, 2]);
        let grads = result.grad_fn().unwrap().backward(&grad_out).unwrap();
        assert!(grads[0].is_some());

        let g = grads[0].as_ref().unwrap();
        let gd = g.data().unwrap();
        // Each complex value should be (0.25, 0.0).
        assert_close(gd, &[0.25, 0.0, 0.25, 0.0, 0.25, 0.0, 0.25, 0.0], 1e-10);
    }

    #[test]
    fn rfft_differentiable_attaches_grad_fn() {
        let input = leaf(&[1.0, 2.0, 3.0, 4.0], &[4]);
        let result = rfft_differentiable(&input, None).unwrap();
        assert!(result.grad_fn().is_some());
        assert_eq!(result.grad_fn().unwrap().name(), "RfftBackward");
    }

    #[test]
    fn irfft_differentiable_attaches_grad_fn() {
        // Input: [3, 2] complex -> irfft -> [4] real.
        let input = leaf(&[10.0, 0.0, -2.0, 2.0, -2.0, 0.0], &[3, 2]);
        let result = irfft_differentiable(&input, Some(4)).unwrap();
        assert!(result.grad_fn().is_some());
        assert_eq!(result.grad_fn().unwrap().name(), "IrfftBackward");
    }

    #[test]
    fn no_grad_context_disables_tracking() {
        let input = leaf(&[1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], &[4, 2]);
        let result =
            crate::autograd::no_grad::no_grad(|| fft_differentiable(&input, None).unwrap());
        assert!(result.grad_fn().is_none());
    }

    // -----------------------------------------------------------------------
    // N-D FFT differentiable wrappers (#580)
    // -----------------------------------------------------------------------

    #[test]
    fn fftn_differentiable_attaches_grad_fn() {
        // 2x2 complex input: [[1+0i, 0+0i], [0+0i, 0+0i]] → flat [2, 2, 2].
        let input = leaf(&[1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], &[2, 2, 2]);
        let result = fftn_differentiable(&input, None, None).unwrap();
        assert!(result.grad_fn().is_some());
        assert_eq!(result.grad_fn().unwrap().name(), "FftnBackward");
    }

    #[test]
    fn ifftn_differentiable_attaches_grad_fn() {
        let input = leaf(&[1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0], &[2, 2, 2]);
        let result = ifftn_differentiable(&input, None, None).unwrap();
        assert!(result.grad_fn().is_some());
        assert_eq!(result.grad_fn().unwrap().name(), "IfftnBackward");
    }

    #[test]
    fn fftn_no_grad_when_not_needed() {
        let input = no_grad_leaf(&[1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], &[2, 2, 2]);
        let result = fftn_differentiable(&input, None, None).unwrap();
        assert!(result.grad_fn().is_none());
    }

    #[test]
    fn fftn_backward_returns_real_grad_for_impulse() {
        // 2x2 impulse: real [[1,0],[0,0]] (encoded complex as
        // [[1+0i, 0+0i], [0+0i, 0+0i]]). fftn → all-ones 2x2 complex
        // (DFT-2D of a corner impulse). grad_y = ones → grad_x =
        // prod_s * ifftn(ones) = 4 * impulse / 4 → impulse_complex.
        let input = leaf(&[1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], &[2, 2, 2]);
        let result = fftn_differentiable(&input, None, None).unwrap();
        // grad_y = ones (4 complex pairs).
        let grad_out = no_grad_leaf(&[1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0], &[2, 2, 2]);
        let grads = result.grad_fn().unwrap().backward(&grad_out).unwrap();
        let g = grads[0].as_ref().unwrap();
        // Expected: 4 * ifftn(ones) over a 2x2 grid → 4 * impulse / 4 = impulse.
        assert_close(
            g.data().unwrap(),
            &[4.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
            1e-9,
        );
    }

    #[test]
    fn rfftn_differentiable_attaches_grad_fn() {
        // Real 2x2 input → rfftn → [2, 2, 2] complex (n/2+1 along last).
        let input = leaf(&[1.0, 2.0, 3.0, 4.0], &[2, 2]);
        let result = rfftn_differentiable(&input, None, None).unwrap();
        assert!(result.grad_fn().is_some());
        assert_eq!(result.grad_fn().unwrap().name(), "RfftnBackward");
    }

    #[test]
    fn irfftn_differentiable_attaches_grad_fn() {
        // Hermitian-shaped complex input [2, 2, 2]: 2 batch × 2 freq × complex.
        let input = leaf(&[1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0], &[2, 2, 2]);
        let result = irfftn_differentiable(&input, Some(&[2, 2]), None).unwrap();
        assert!(result.grad_fn().is_some());
        assert_eq!(result.grad_fn().unwrap().name(), "IrfftnBackward");
    }

    #[test]
    fn hfft_differentiable_attaches_grad_fn() {
        // Hermitian spectrum [3, 2] → real [4]. n=4 means input_n=3 (n/2+1).
        let input = leaf(&[10.0, 0.0, -2.0, 2.0, -2.0, 0.0], &[3, 2]);
        let result = hfft_differentiable(&input, Some(4)).unwrap();
        assert!(result.grad_fn().is_some());
        assert_eq!(result.grad_fn().unwrap().name(), "HfftBackward");
    }

    #[test]
    fn ihfft_differentiable_attaches_grad_fn() {
        let input = leaf(&[1.0, 2.0, 3.0, 4.0], &[4]);
        let result = ihfft_differentiable(&input, None).unwrap();
        assert!(result.grad_fn().is_some());
        assert_eq!(result.grad_fn().unwrap().name(), "IhfftBackward");
    }

    #[test]
    fn fftn_norm_n_default_inner_dims() {
        // shape [2, 3, 4, 2] (last dim is complex pair) → norm_n = 2*3*4 = 24.
        let input = no_grad_leaf(&vec![0.0; 2 * 3 * 4 * 2], &[2, 3, 4, 2]);
        let n = fftn_norm_n(&input, None, None);
        assert_eq!(n, 2 * 3 * 4);
    }

    #[test]
    fn fftn_norm_n_with_explicit_s() {
        let input = no_grad_leaf(&[0.0; 8 * 2], &[2, 2, 2, 2]);
        let n = fftn_norm_n(&input, Some(&[3, 5]), None);
        assert_eq!(n, 15);
    }

    #[test]
    fn fftn_norm_n_with_axes() {
        // Axes = [1, 2] → norm_n = shape[1] * shape[2] = 3 * 4 = 12.
        let input = no_grad_leaf(&vec![0.0; 2 * 3 * 4 * 2], &[2, 3, 4, 2]);
        let n = fftn_norm_n(&input, None, Some(&[1, 2]));
        assert_eq!(n, 12);
    }

    // -----------------------------------------------------------------------
    // 2-D FFT differentiable wrappers (#1300)
    // -----------------------------------------------------------------------

    #[test]
    fn fft2_differentiable_attaches_grad_fn() {
        // 2x3 complex grid → [2, 3, 2].
        let input = leaf(&[0.0; 2 * 3 * 2], &[2, 3, 2]);
        let result = fft2_differentiable(&input).unwrap();
        assert!(result.grad_fn().is_some());
        assert_eq!(result.grad_fn().unwrap().name(), "Fft2Backward");
    }

    #[test]
    fn ifft2_differentiable_attaches_grad_fn() {
        let input = leaf(&[0.0; 2 * 3 * 2], &[2, 3, 2]);
        let result = ifft2_differentiable(&input).unwrap();
        assert!(result.grad_fn().is_some());
        assert_eq!(result.grad_fn().unwrap().name(), "Ifft2Backward");
    }

    #[test]
    fn fft2_no_grad_when_not_needed() {
        let input = no_grad_leaf(&[0.0; 2 * 3 * 2], &[2, 3, 2]);
        let result = fft2_differentiable(&input).unwrap();
        assert!(result.grad_fn().is_none());
    }

    #[test]
    fn fft2_norm_n_is_rows_times_cols() {
        // [4, 5, 2] → rows=4, cols=5 → norm_n=20.
        let input = no_grad_leaf(&vec![0.0; 4 * 5 * 2], &[4, 5, 2]);
        assert_eq!(fft2_norm_n(&input), 20);
        // Batched [2, 3, 4, 2] → rows=3, cols=4 → norm_n=12.
        let batched = no_grad_leaf(&vec![0.0; 2 * 3 * 4 * 2], &[2, 3, 4, 2]);
        assert_eq!(fft2_norm_n(&batched), 12);
    }

    #[test]
    fn fft2_backward_returns_grad_for_corner_impulse() {
        // 2x2 corner impulse encoded complex: [[1+0i,0],[0,0]] → flat [2,2,2].
        // fft2 of a corner impulse is the all-ones 2x2 complex grid.
        // grad_y = ones → grad_x = norm_n * ifft2(ones) = 4 * (impulse/4)
        //        = impulse. So grad_x = [4,0, 0,0, 0,0, 0,0]?  No: ifft2(ones)
        // over a 2x2 grid = corner impulse (value 1 at [0,0]); times norm_n=4
        // → [4,0] at the corner, zeros elsewhere.
        let input = leaf(&[1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], &[2, 2, 2]);
        let result = fft2_differentiable(&input).unwrap();
        let grad_out = no_grad_leaf(&[1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0], &[2, 2, 2]);
        let grads = result.grad_fn().unwrap().backward(&grad_out).unwrap();
        let g = grads[0].as_ref().unwrap();
        assert_close(
            g.data().unwrap(),
            &[4.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
            1e-9,
        );
    }

    // -----------------------------------------------------------------------
    // norm / dim threading in autograd (#1294)
    // -----------------------------------------------------------------------

    #[test]
    fn adjoint_norm_swaps_backward_forward_fixes_ortho() {
        // The adjoint (same fft_norm_mode int, flipped direction) maps
        // Backward<->Forward and Ortho->Ortho (derivatives.yaml:2960-2961 +
        // SpectralOpsUtils.h:15-19).
        assert_eq!(adjoint_norm(FftNorm::Backward), FftNorm::Forward);
        assert_eq!(adjoint_norm(FftNorm::Forward), FftNorm::Backward);
        assert_eq!(adjoint_norm(FftNorm::Ortho), FftNorm::Ortho);
    }

    #[test]
    fn fft_ortho_backward_is_ortho_inverse() {
        // For the unitary (ortho) FFT, the VJP equals the ortho inverse FFT of
        // grad_y (adjoint of a unitary map is its inverse). Check numerically:
        // grad_x = ifft(grad_y, ortho). Use an impulse grad_y so the expected
        // grad_x is the ortho IFFT of the impulse = constant 1/sqrt(n) bins.
        let input = leaf(&[1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], &[4, 2]);
        let result = fft_differentiable_norm(&input, None, None, FftNorm::Ortho).unwrap();
        assert!(result.grad_fn().is_some());
        let grad_out = no_grad_leaf(&[1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], &[4, 2]);
        let grads = result.grad_fn().unwrap().backward(&grad_out).unwrap();
        let g = grads[0].as_ref().unwrap();
        // ortho ifft of an impulse [1,0,0,0] = constant 1/sqrt(4) = 0.5 in each
        // real bin (imag 0).
        let gd = g.data().unwrap();
        for k in 0..4 {
            assert!((gd[k * 2] - 0.5).abs() < 1e-9, "bin {k} re = {}", gd[k * 2]);
            assert!(gd[k * 2 + 1].abs() < 1e-9, "bin {k} im");
        }
    }

    #[test]
    fn fft_differentiable_norm_attaches_grad_fn_for_dim() {
        // Non-default dim still attaches a FftBackward node.
        let input = leaf(&[1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], &[2, 2, 2]);
        let result = fft_differentiable_norm(&input, None, Some(-2), FftNorm::Backward).unwrap();
        assert!(result.grad_fn().is_some());
        assert_eq!(result.grad_fn().unwrap().name(), "FftBackward");
    }

    #[test]
    fn rfft_differentiable_norm_grad_matches_default_on_last_axis() {
        // For the default last-axis / backward-norm case, the _norm wrapper
        // must produce the identical grad as the legacy rfft_differentiable
        // (it delegates to the cheaper 1-D RfftBackward).
        let input = leaf(&[1.0, 2.0, 3.0, 4.0], &[4]);
        let r_default = rfft_differentiable(&input, None).unwrap();
        let input2 = leaf(&[1.0, 2.0, 3.0, 4.0], &[4]);
        let r_norm = rfft_differentiable_norm(&input2, None, None, FftNorm::Backward).unwrap();
        assert_close(r_norm.data().unwrap(), r_default.data().unwrap(), 1e-12);
        // grad_y = ones over the half-spectrum.
        let half = r_default.shape()[0];
        let go: Vec<f64> = vec![1.0; half * 2];
        let grad_out = no_grad_leaf(&go, r_default.shape());
        let g1 = r_default.grad_fn().unwrap().backward(&grad_out).unwrap();
        let g2 = r_norm.grad_fn().unwrap().backward(&grad_out).unwrap();
        assert_close(
            g2[0].as_ref().unwrap().data().unwrap(),
            g1[0].as_ref().unwrap().data().unwrap(),
            1e-12,
        );
    }

    #[test]
    fn rfftn_differentiable_norm_ortho_roundtrip_grad_shape() {
        // ortho rfftn attaches RfftnBackward with the threaded norm; the grad
        // recovers the input shape.
        let input = leaf(&[1.0, 2.0, 3.0, 4.0], &[2, 2]);
        let result = rfftn_differentiable_norm(&input, None, None, FftNorm::Ortho).unwrap();
        assert_eq!(result.grad_fn().unwrap().name(), "RfftnBackward");
        let go: Vec<f64> = vec![0.5; result.shape().iter().product::<usize>()];
        let grad_out = no_grad_leaf(&go, result.shape());
        let grads = result.grad_fn().unwrap().backward(&grad_out).unwrap();
        assert_eq!(grads[0].as_ref().unwrap().shape(), &[2, 2]);
    }

    #[test]
    fn fft2_ifft2_differentiable_roundtrip_values() {
        // Verify the differentiable forwards round-trip to identity. A
        // [2, 3, 2] complex tensor holds 2*3 = 6 complex pairs (12 floats);
        // the trailing dim of size 2 is the (re, im) pair.
        let mut complex = Vec::with_capacity(12);
        for i in 0..6 {
            complex.push(i as f64);
            complex.push(i as f64 * 0.5);
        }
        let input = leaf(&complex, &[2, 3, 2]);
        let spectrum = fft2_differentiable(&input).unwrap();
        let recovered = ifft2_differentiable(&spectrum).unwrap();
        assert_close(recovered.data().unwrap(), &complex, 1e-9);
    }
}