ferrotorch-nn 0.6.1

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

use std::any::TypeId;
use std::sync::Arc;

use ferrotorch_core::autograd::no_grad::is_grad_enabled;
use ferrotorch_core::device::Device;
use ferrotorch_core::dtype::DType;
use ferrotorch_core::gpu_dispatch::{GpuBufferHandle, gpu_backend};
use ferrotorch_core::tensor::GradFn;
use ferrotorch_core::{FerrotorchError, FerrotorchResult, Float, Tensor, TensorStorage};

use crate::init;
use crate::module::Module;
use crate::parameter::Parameter;

/// Returns `true` if `T` is `f32`.
#[inline]
fn is_f32<T: Float>() -> bool {
    TypeId::of::<T>() == TypeId::of::<f32>()
}

/// Returns `true` if `T` is `f64`.
#[inline]
fn is_f64<T: Float>() -> bool {
    TypeId::of::<T>() == TypeId::of::<f64>()
}

/// Upload a CPU `&[f32]` slice to a GPU buffer on the given device ordinal.
fn upload_f32_to_gpu(data: &[f32], ordinal: usize) -> FerrotorchResult<GpuBufferHandle> {
    let backend = gpu_backend().ok_or(FerrotorchError::DeviceUnavailable)?;
    // SAFETY: `data` is a live `&[f32]` borrow; its memory is valid for reads of
    // `data.len() * 4` bytes (every `f32` is exactly 4 bytes — `size_of::<f32>() == 4`,
    // guaranteed by the language and verified by `mem::size_of`). The cast from
    // `*const f32` to `*const u8` does not violate alignment (alignment of `u8` is 1,
    // strictly weaker than `f32`'s alignment of 4). The resulting `&[u8]` is borrowed
    // for the duration of this expression and consumed by `backend.cpu_to_gpu` before
    // `data` goes out of scope, so the lifetime never outlives the source borrow.
    // No interior mutability — `data` is a shared reference and `f32` has no padding.
    let bytes: &[u8] =
        unsafe { std::slice::from_raw_parts(data.as_ptr() as *const u8, data.len() * 4) };
    backend.cpu_to_gpu(bytes, DType::F32, ordinal)
}

/// Renormalise the rows of `weight` touched by `indices`, IN PLACE, so each
/// touched row's `norm_type`-norm is at most `max_norm`.
///
/// Faithful translation of `embedding_renorm_cpu_`
/// (`aten/src/ATen/native/Embedding.cpp:181-212`): indices are sorted and
/// de-duplicated; each unique row whose norm exceeds `max_norm` is scaled by
/// `max_norm / (norm + 1e-7)`. Rows within `max_norm`, and rows never indexed
/// this forward, are left untouched. PyTorch runs this BEFORE the gather under
/// `torch.no_grad()` (`torch/nn/functional.py:2561-2573`), mutating the
/// persisted `weight`, so the change survives across forward calls — this
/// function matches that by writing the renormed rows back via
/// [`Tensor::update_data`].
///
/// Shared by `Embedding` and `EmbeddingBag` so both layers' `max_norm`
/// semantics stay byte-identical. CUDA weights have no on-device renorm kernel
/// yet, so this returns `NotImplementedOnCuda` rather than silently skipping.
fn renorm_weight_rows_in_place<T: Float>(
    weight: &Tensor<T>,
    indices: &[usize],
    dim: usize,
    max_norm: f64,
    norm_type: f64,
    op: &'static str,
) -> FerrotorchResult<()> {
    if weight.is_cuda() {
        return Err(FerrotorchError::NotImplementedOnCuda { op });
    }

    // Sort + dedup, mirroring `std::sort` + the `sorted[i]==sorted[i-1]` skip
    // at Embedding.cpp:193-201. Visiting each unique row once is required:
    // re-scaling an already-clipped row would shrink it below max_norm.
    let mut sorted: Vec<usize> = indices.to_vec();
    sorted.sort_unstable();
    sorted.dedup();

    let weight_data = weight.data()?;
    let mut new_data: Option<Vec<T>> = None;
    for &idx in &sorted {
        let row_start = idx * dim;
        let row = &weight_data[row_start..row_start + dim];
        // `row.norm(norm_type)` = `at::norm`, which special-cases the
        // non-finite / degenerate orders rather than evaluating the generic
        // `(Σ|x|^p)^(1/p)` formula — that formula gives `inf^0 = 1` for
        // `p = +inf` and `x^0 = 1` for `p = 0`, both wrong. Mirror the kernel
        // dispatch at `aten/src/ATen/native/cpu/ReduceOpsKernel.cpp:191-203`:
        //   p == 0     -> NormZeroOps : count of nonzero elements (L0)
        //   p == +inf  -> AbsMaxOps   : max_i |x_i|  (infinity norm)
        //   p == -inf  -> AbsMinOps   : min_i |x_i|  (acc seeded +inf)
        //   else       -> NormOps     : (Σ|x|^p)^(1/p)
        // (p == 1 / p == 2 are exact under the generic formula, so they need
        // no separate arm here.)
        //
        // PRECISION (#1612): the norm is accumulated and rooted in the WEIGHT'S
        // NATIVE dtype `T`, then widened to f64 only for the `> max_norm`
        // compare and the scale. This mirrors `row.norm(norm_type).item<double>()`
        // at `Embedding.cpp:202-203` byte-for-byte: `at::norm`'s accumulator is
        // `at::opmath_type<scalar_t>` (`ReduceOpsKernel.cpp:190`), which is
        // `float` for an f32 row and `double` for an f64 row, and the result is
        // stored back as `scalar_t` (`result_data[0] = scalar_t(std::sqrt(..))`,
        // `ReduceOpsKernel.cpp:253`); `.item<double>()` widens that already-`T`-
        // rounded scalar AFTER the fact. Accumulating in f64 for an f32 weight
        // would make the clip DECISION on a value torch never sees — at the
        // boundary (f32 norm == max_norm) torch does NOT clip but an f64 norm
        // can land just above, wrongly scaling the row (#1612).
        let norm_t: T = if norm_type == 0.0 {
            // NormZeroOps (`SharedReduceOps.h:285`): count of nonzeros.
            T::from(
                row.iter()
                    .filter(|&&v| v != <T as num_traits::Zero>::zero())
                    .count(),
            )
            .unwrap_or_else(<T as num_traits::Zero>::zero)
        } else if norm_type == f64::INFINITY {
            // AbsMaxOps (`SharedReduceOps.h:216`): max_i |x_i|, in `T`.
            row.iter().fold(<T as num_traits::Zero>::zero(), |acc, &v| {
                let av = v.abs();
                if av > acc { av } else { acc }
            })
        } else if norm_type == f64::NEG_INFINITY {
            // AbsMinOps (`SharedReduceOps.h:186`): min_i |x_i|, acc seeded +inf,
            // in `T`.
            row.iter().fold(T::infinity(), |acc, &v| {
                let av = v.abs();
                if av < acc { av } else { acc }
            })
        } else if norm_type == 2.0 && is_f32::<T>() {
            // L2 FAST PATH (#1614): the default `norm_type == 2.0` over a
            // contiguous f32 row is what torch's `at::norm(2.0)` evaluates via
            // its VECTORIZED last-dim L2 kernel (`ReduceOpsKernel.cpp:222-255`):
            // a width-8 lane accumulate of `v*v` + a naive left-fold + a scalar
            // FMA tail + `sqrt`, all in an f32 (NOT f64) accumulator. A scalar
            // `Σ |v|.powf(2)` then `.powf(0.5)` (the generic arm below) lands up
            // to one ULP off that value, flipping the `norm > max_norm` boundary
            // decision (#1612 / #1614). Route the f32 L2 row through the shared
            // `ferrotorch_core::simd_reduce::l2_norm_f32_torch` primitive so the
            // renorm decision matches torch byte-for-byte (modulo the documented
            // ~3% one-ULP residual; the #1614 boundary row IS matched).
            //
            // `row: &[T]` is f32 here (guarded by `is_f32::<T>()`); collect it as
            // `&[f32]` via the exact identity `ToPrimitive::to_f32`.
            let mut row_f32: Vec<f32> = Vec::with_capacity(row.len());
            for &v in row {
                row_f32.push(num_traits::ToPrimitive::to_f32(&v).unwrap_or(0.0));
            }
            let n_f32 = ferrotorch_core::simd_reduce::l2_norm_f32_torch(&row_f32);
            // Lift the f32 norm back into `T` (== f32). The unwrap is on the
            // identity f32->f32 NumCast, which never fails for finite/inf/NaN.
            T::from(n_f32).unwrap_or_else(<T as num_traits::Zero>::zero)
        } else {
            // NormOps: generic finite p-norm `(Σ|x|^p)^(1/p)`, accumulated and
            // rooted in `T` (f32 for an f32 weight) to match `at::norm`. (Used
            // for f64 rows, and for finite p != 2; the f32 L2 case is handled
            // by the byte-exact `simd_reduce` arm above.)
            let p_t = T::from(norm_type).unwrap_or_else(<T as num_traits::One>::one);
            let mut acc = <T as num_traits::Zero>::zero();
            for &v in row {
                acc += v.abs().powf(p_t);
            }
            let inv_p = T::from(1.0 / norm_type).unwrap_or_else(<T as num_traits::One>::one);
            acc.powf(inv_p)
        };
        // Widen the native-precision norm to f64 exactly as `.item<double>()`
        // does (`Embedding.cpp:203`) — only NOW does the value become f64.
        let norm = num_traits::ToPrimitive::to_f64(&norm_t).unwrap_or(0.0);
        if norm > max_norm {
            // Lazily materialise the mutable copy only when a row needs
            // clipping, so the no-clip case never touches the buffer.
            let buf = new_data.get_or_insert_with(|| weight_data.to_vec());
            let scale = max_norm / (norm + 1e-7);
            let scale_t = T::from(scale).unwrap();
            for v in &mut buf[row_start..row_start + dim] {
                *v = *v * scale_t;
            }
        }
    }

    if let Some(buf) = new_data {
        // SAFETY: `update_data` requires exclusive access to the weight's
        // storage for the duration of the write. The renorm runs inside the
        // forward, which holds the only live borrow of `weight_data` (a
        // `&[T]` over the same Arc); that borrow ends before this call (the
        // slice is fully consumed into `buf` above). No backward node captures
        // a mutable view, and the autograd engine is not concurrently reading
        // the weight: PyTorch performs this exact mutation under
        // `torch.no_grad()` (`functional.py:2567-2572`), a grad-disabled,
        // single-threaded in-place edit of the persisted weight. `buf` has
        // exactly `num_embeddings * dim` elements, matching the tensor's numel.
        #[allow(
            clippy::undocumented_unsafe_blocks,
            reason = "SAFETY comment above documents the exclusive-access invariant; torch embedding_renorm_ mutates weight in place under no_grad (functional.py:2567-2572), matching the optimizer step()'s update_data contract"
        )]
        unsafe {
            weight.update_data(&buf)?;
        }
    }

    Ok(())
}

// ---------------------------------------------------------------------------
// EmbeddingBackward
// ---------------------------------------------------------------------------

/// Backward function for the embedding lookup.
///
/// Forward: `output[i, :] = weight[indices[i], :]`
///
/// VJP: `grad_weight = zeros(num_embeddings, embedding_dim);`
///       `for i, idx in indices: grad_weight[idx, :] += grad_output[i, :]`
///
/// This is a sparse gradient — only accessed rows are non-zero.
/// Duplicate indices accumulate their corresponding `grad_output` rows.
#[derive(Debug)]
pub struct EmbeddingBackward<T: Float> {
    /// The weight tensor (needed for graph traversal and shape).
    weight: Tensor<T>,
    /// Indices used in the forward pass.
    indices: Vec<usize>,
    /// Total number of embedding rows.
    num_embeddings: usize,
    /// Width of each embedding vector.
    embedding_dim: usize,
    /// If set, this row's gradient is always zero.
    padding_idx: Option<usize>,
    /// If `true`, divide each row's accumulated gradient by the number of
    /// times the index appeared in the forward pass — mirrors
    /// `torch/nn/functional.py:2374-2388`'s `scale_grad_by_freq=True`
    /// branch. (Closes #1445.)
    scale_grad_by_freq: bool,
}

impl<T: Float> GradFn<T> for EmbeddingBackward<T> {
    fn backward(&self, grad_output: &Tensor<T>) -> FerrotorchResult<Vec<Option<Tensor<T>>>> {
        if !is_grad_enabled() {
            return Ok(vec![None]);
        }

        let dim = self.embedding_dim;

        // GPU fast path: scatter-add rows entirely on GPU for f32/f64 tensors.
        if grad_output.is_cuda() && (is_f32::<T>() || is_f64::<T>()) {
            let backend = gpu_backend().ok_or(FerrotorchError::DeviceUnavailable)?;
            let ordinal = match self.weight.device() {
                Device::Cuda(o) => o,
                _ => unreachable!(),
            };

            let indices_f32: Vec<f32> = self.indices.iter().map(|&i| i as f32).collect();
            let idx_handle = upload_f32_to_gpu(&indices_f32, ordinal)?;
            let go_handle = grad_output.gpu_handle()?;
            let f64_path = is_f64::<T>();
            let elem_size: usize = if f64_path { 8 } else { 4 };

            let mut gw_handle = if f64_path {
                backend.scatter_add_rows_f64(go_handle, &idx_handle, self.num_embeddings, dim)?
            } else {
                backend.scatter_add_rows_f32(go_handle, &idx_handle, self.num_embeddings, dim)?
            };

            if let Some(pad_idx) = self.padding_idx {
                let mut gw_bytes = backend.gpu_to_cpu(&gw_handle)?;
                let start_byte = pad_idx * dim * elem_size;
                let end_byte = start_byte + dim * elem_size;
                for b in &mut gw_bytes[start_byte..end_byte] {
                    *b = 0;
                }
                let gw_dtype = if f64_path { DType::F64 } else { DType::F32 };
                gw_handle = backend.cpu_to_gpu(&gw_bytes, gw_dtype, ordinal)?;
            }

            let grad_tensor = Tensor::from_storage(
                TensorStorage::gpu(gw_handle),
                vec![self.num_embeddings, dim],
                false,
            )?;
            return Ok(vec![Some(grad_tensor)]);
        }

        if grad_output.is_cuda() {
            return Err(FerrotorchError::NotImplementedOnCuda {
                op: "EmbeddingBackward",
            });
        }

        let go_data = grad_output.data()?;

        // Allocate a full-size gradient for the weight matrix, initialized to zero.
        let mut grad_weight = vec![<T as num_traits::Zero>::zero(); self.num_embeddings * dim];

        // Scatter-add: for each index position, accumulate the corresponding
        // grad_output row into the weight gradient at the accessed index.
        for (i, &idx) in self.indices.iter().enumerate() {
            let go_row = &go_data[i * dim..(i + 1) * dim];
            let gw_row = &mut grad_weight[idx * dim..(idx + 1) * dim];
            for (gw, &go) in gw_row.iter_mut().zip(go_row.iter()) {
                *gw += go;
            }
        }

        // scale_grad_by_freq: divide each touched row by its appearance
        // count in the forward pass (mirrors
        // `torch/nn/functional.py:2374-2388`). Untouched rows have grad
        // identically zero, so the divide is a no-op there.
        if self.scale_grad_by_freq {
            let mut counts: std::collections::HashMap<usize, usize> =
                std::collections::HashMap::new();
            for &idx in &self.indices {
                *counts.entry(idx).or_insert(0) += 1;
            }
            for (&idx, &cnt) in &counts {
                if cnt <= 1 {
                    continue;
                }
                let scale = T::from(1.0 / cnt as f64).unwrap();
                let row_start = idx * dim;
                for v in &mut grad_weight[row_start..row_start + dim] {
                    *v = *v * scale;
                }
            }
        }

        // If padding_idx is set, zero that row's gradient unconditionally.
        if let Some(pad_idx) = self.padding_idx {
            let start = pad_idx * dim;
            for v in &mut grad_weight[start..start + dim] {
                *v = <T as num_traits::Zero>::zero();
            }
        }

        Ok(vec![Some(Tensor::from_storage(
            TensorStorage::cpu(grad_weight),
            vec![self.num_embeddings, dim],
            false,
        )?)])
    }

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

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

// ---------------------------------------------------------------------------
// EmbeddingBagSumWeightedBackward — sum-mode bag with per_sample_weights
// ---------------------------------------------------------------------------

/// Backward function for `EmbeddingBag::forward_bag_weighted` in `sum` mode
/// with `per_sample_weights` supplied. The forward is the scaled
/// index-select-add (`aten/src/ATen/native/EmbeddingBag.cpp:537-543`):
///
/// `output[bag(i)][:] += weight[idx[i]][:] * psw[i]`  (padding samples skipped)
///
/// Gradient flows to BOTH the embedding table AND `per_sample_weights`, matching
/// torch's autograd (`per_sample_weights.requires_grad` is honored at
/// `EmbeddingBag.cpp:1248-1250`):
///
/// - `grad_weight[idx[i]][:] += grad_output[bag(i)][:] * psw[i]`
///   — the sum-mode `scale = per_sample_weights_data[..]` axpy at
///   `EmbeddingBag.cpp:1564-1582` (`scale_grad_by_freq` divides by the index
///   frequency; `mode == SUM` never divides by bag size).
/// - `grad_psw[i] = dot(grad_output[bag(i)][:], weight[idx[i]][:])`
///   — `_embedding_bag_per_sample_weights_backward_cpu_template`'s per-sample
///   `dot_impl(grad[bag], weight[idx])` at `EmbeddingBag.cpp:1716-1724`.
///
/// Padding samples (`idx[i] == padding_idx`) contribute 0 to BOTH gradients:
/// they are skipped in the weight-grad loop (`EmbeddingBag.cpp:1561`) and their
/// `grad_psw` entry stays at the zero-init (`EmbeddingBag.cpp:1671`, `:1720`).
#[derive(Debug)]
struct EmbeddingBagSumWeightedBackward<T: Float> {
    /// The embedding table (input 0; receives the scatter-add grad).
    weight: Tensor<T>,
    /// The per-sample weights (input 1; receives the per-sample dot grad).
    per_sample_weights: Tensor<T>,
    /// Flattened embedding indices, one per sample, in forward order.
    indices: Vec<usize>,
    /// Bag id for each sample (`offset2bag`): `bag_of[i]` is the output row that
    /// sample `i` accumulates into.
    bag_of: Vec<usize>,
    /// Total number of embedding rows.
    num_embeddings: usize,
    /// Width of each embedding vector.
    embedding_dim: usize,
    /// If set, samples whose index equals this contribute no gradient.
    padding_idx: Option<usize>,
    /// If `true`, each touched weight-row grad is divided by the number of
    /// times that index appeared in the forward (`EmbeddingBag.cpp:1569-1571`).
    scale_grad_by_freq: bool,
}

impl<T: Float> GradFn<T> for EmbeddingBagSumWeightedBackward<T> {
    fn backward(&self, grad_output: &Tensor<T>) -> FerrotorchResult<Vec<Option<Tensor<T>>>> {
        if !is_grad_enabled() {
            return Ok(vec![None, None]);
        }

        if grad_output.is_cuda() {
            return Err(FerrotorchError::NotImplementedOnCuda {
                op: "EmbeddingBagSumWeightedBackward",
            });
        }

        let dim = self.embedding_dim;
        let go_data = grad_output.data()?;
        let weight_data = self.weight.data()?;
        let psw_data = self.per_sample_weights.data()?;
        let n = self.indices.len();

        // grad to the embedding table: scatter-add the bag's grad row scaled by
        // the sample's per_sample_weight (EmbeddingBag.cpp:1564-1582).
        let mut grad_weight = vec![<T as num_traits::Zero>::zero(); self.num_embeddings * dim];
        // grad to per_sample_weights: dot(grad[bag], weight[idx]) per sample,
        // zero for padding samples (EmbeddingBag.cpp:1716-1724).
        let mut grad_psw = vec![<T as num_traits::Zero>::zero(); n];

        // scale_grad_by_freq divisor map (EmbeddingBag.cpp:1522,1569-1571).
        //
        // Torch's dense sum/mean backward operates on SORTED indices. It builds
        // `counts[v]` = occurrences of value `v` over the full index array
        // (`EmbeddingBag.cpp:1475-1478`), sorts the indices (`:1522`), then walks
        // the sorted array one UNIQUE index at a time with the stride
        // `i += counts[sorted[i]]` (`:1499`). For the k-th unique step (counter
        // `i` in torch's loop, here `k`) it divides that index's grad by
        // `counts[indices_data[i]]` — i.e. `counts[sorted[k]]`, NOT
        // `counts[that index's own value]` (`:1569-1571`). Because `k` indexes the
        // SORTED array, for any input that is not already sorted this divides one
        // index's grad by a *neighbouring* index's frequency. We replicate this
        // exactly: `divisor_of_index[v]` = `counts[sorted[k]]` for the unique step
        // `k` that lands on value `v`. Padding indices participate in `counts`, the
        // sort, and the unique-step counter `k` (only their grad scatter is skipped
        // at `:1561`), so they are included here too.
        let divisor_of_index: Option<std::collections::HashMap<usize, usize>> =
            if self.scale_grad_by_freq {
                let mut counts: std::collections::HashMap<usize, usize> =
                    std::collections::HashMap::new();
                for &idx in &self.indices {
                    *counts.entry(idx).or_insert(0) += 1;
                }
                let mut sorted = self.indices.clone();
                sorted.sort_unstable();
                let mut divisor: std::collections::HashMap<usize, usize> =
                    std::collections::HashMap::new();
                let mut i = 0usize; // position in the sorted array
                let mut k = 0usize; // unique-step counter (torch's loop index `i`)
                while i < sorted.len() {
                    let index = sorted[i]; // the value this unique step owns
                    // The quirk: torch divides by counts[indices_data[k]] = counts[sorted[k]].
                    let div = counts.get(&sorted[k]).copied().unwrap_or(1);
                    divisor.insert(index, div);
                    let stride = counts.get(&index).copied().unwrap_or(1).max(1);
                    i += stride;
                    k += 1;
                }
                Some(divisor)
            } else {
                None
            };

        for i in 0..n {
            let idx = self.indices[i];
            // Padding samples are excluded from BOTH grads (EmbeddingBag.cpp:1561,
            // :1720): grad_psw[i] stays 0 and no weight-row update happens.
            if self.padding_idx == Some(idx) {
                continue;
            }
            let bag = self.bag_of[i];
            let go_row = &go_data[bag * dim..(bag + 1) * dim];
            let w_row = &weight_data[idx * dim..(idx + 1) * dim];
            let psw_i = psw_data[i];

            // weight-grad scale: psw, optionally divided by torch's sorted-neighbour
            // frequency divisor (EmbeddingBag.cpp:1569-1571).
            let mut w_scale = psw_i;
            if let Some(d) = &divisor_of_index {
                if let Some(&div) = d.get(&idx) {
                    if div > 0 {
                        w_scale =
                            w_scale / T::from(div).unwrap_or_else(<T as num_traits::One>::one);
                    }
                }
            }
            let gw_row = &mut grad_weight[idx * dim..(idx + 1) * dim];
            for (gw, &go) in gw_row.iter_mut().zip(go_row.iter()) {
                *gw += go * w_scale;
            }

            // per_sample_weight grad: dot(grad[bag], weight[idx]). This is the
            // UNSCALED bag grad against the embedding row — scale_grad_by_freq
            // only weights the table grad, not the psw grad (it is absent from
            // the psw-backward kernel at EmbeddingBag.cpp:1716-1724).
            let mut dot = <T as num_traits::Zero>::zero();
            for (&go, &w) in go_row.iter().zip(w_row.iter()) {
                dot += go * w;
            }
            grad_psw[i] = dot;
        }

        let grad_weight_t = Tensor::from_storage(
            TensorStorage::cpu(grad_weight),
            vec![self.num_embeddings, dim],
            false,
        )?;
        let grad_psw_t = Tensor::from_storage(
            TensorStorage::cpu(grad_psw),
            self.per_sample_weights.shape().to_vec(),
            false,
        )?;
        Ok(vec![Some(grad_weight_t), Some(grad_psw_t)])
    }

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

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

// ---------------------------------------------------------------------------
// Embedding layer
// ---------------------------------------------------------------------------

/// A simple lookup table that stores embeddings of a fixed dictionary.
///
/// Given a 1-D tensor of integer indices (stored as float values, cast to
/// `usize`), returns a 2-D tensor `[len, embedding_dim]` by gathering the
/// corresponding rows from the weight matrix.
///
/// # Padding index
///
/// If `padding_idx` is set, the embedding vector at that index is always
/// zero and receives no gradient updates. This is commonly used to
/// represent a padding token.
///
/// # Example
///
/// ```ignore
/// let emb = Embedding::<f32>::new(1000, 64, None)?;
/// let indices = ferrotorch_core::tensor(&[1.0, 5.0, 3.0])?;
/// let output = emb.forward(&indices)?;
/// assert_eq!(output.shape(), &[3, 64]);
/// ```
#[derive(Debug)]
pub struct Embedding<T: Float> {
    /// The learnable weight matrix, shape `[num_embeddings, embedding_dim]`.
    pub weight: Parameter<T>,
    /// Number of entries in the lookup table.
    pub num_embeddings: usize,
    /// Dimensionality of each embedding vector.
    pub embedding_dim: usize,
    /// If set, this row is kept at zero and receives no gradient.
    pub padding_idx: Option<usize>,
    /// If set, every row touched by a forward call is renormalised in-place
    /// so its `norm_type`-norm is at most `max_norm`, mirroring
    /// `torch/nn/functional.py:2306-2370` (`_no_grad_embedding_renorm_`).
    /// Carried as `f64` for the upstream scalar type (kwarg is `float`).
    /// (Closes #1445.)
    pub max_norm: Option<f64>,
    /// Order of the row-norm used when `max_norm` is active. Defaults to
    /// `2.0` (Euclidean) per `torch/nn/functional.py:2316`. (Closes #1445.)
    pub norm_type: f64,
    /// If `true`, `EmbeddingBackward` divides each accumulated row gradient
    /// by the number of times that index appeared in the forward pass,
    /// matching `torch/nn/functional.py:2374-2388`. (Closes #1445.)
    pub scale_grad_by_freq: bool,
    /// Whether the module is in training mode.
    training: bool,
    /// If true, advertise a sparse gradient pattern (the only rows touched
    /// are the ones actually indexed in the most recent forward call).
    /// This is purely a flag — autograd still populates a dense grad on
    /// the weight; callers can extract a `SparseGrad` view via
    /// [`Self::sparse_grad`] to feed `optim::SparseAdam` or
    /// `SparseGrad::apply_sgd` without scanning the full dense matrix.
    /// Mirrors `torch.nn.Embedding(sparse=True)`. (#623)
    pub sparse: bool,
    /// Cached unique indices touched by the most recent forward pass. None
    /// if `sparse == false` or no forward has run yet. We dedupe here so
    /// callers don't have to coalesce the SparseGrad themselves.
    last_indices: std::sync::Mutex<Option<Vec<usize>>>,
}

impl<T: Float> Embedding<T> {
    /// Create a new embedding layer.
    ///
    /// Weight is initialized from N(0, 1). If `padding_idx` is set, that
    /// row is zeroed after initialization.
    ///
    /// # Errors
    ///
    /// Returns an error if `padding_idx >= num_embeddings`.
    pub fn new(
        num_embeddings: usize,
        embedding_dim: usize,
        padding_idx: Option<usize>,
    ) -> FerrotorchResult<Self> {
        // Validate padding_idx.
        if let Some(idx) = padding_idx {
            if idx >= num_embeddings {
                return Err(FerrotorchError::InvalidArgument {
                    message: format!(
                        "padding_idx {idx} is out of range for num_embeddings {num_embeddings}"
                    ),
                });
            }
        }

        // Initialize weight from N(0, 1).
        let mut weight = Parameter::zeros(&[num_embeddings, embedding_dim])?;
        init::normal(&mut weight, 0.0, 1.0)?;

        // Zero the padding row if requested.
        if let Some(idx) = padding_idx {
            let data = weight.data()?.to_vec();
            let mut new_data = data;
            let start = idx * embedding_dim;
            for v in &mut new_data[start..start + embedding_dim] {
                *v = <T as num_traits::Zero>::zero();
            }
            weight = Parameter::new(Tensor::from_storage(
                TensorStorage::cpu(new_data),
                vec![num_embeddings, embedding_dim],
                true,
            )?);
        }

        Ok(Self {
            weight,
            num_embeddings,
            embedding_dim,
            padding_idx,
            max_norm: None,
            norm_type: 2.0,
            scale_grad_by_freq: false,
            training: true,
            sparse: false,
            last_indices: std::sync::Mutex::new(None),
        })
    }

    /// Create an embedding layer from an existing weight tensor.
    ///
    /// The tensor must have shape `[num_embeddings, embedding_dim]`.
    pub fn from_pretrained(
        weight: Tensor<T>,
        padding_idx: Option<usize>,
    ) -> FerrotorchResult<Self> {
        if weight.ndim() != 2 {
            return Err(FerrotorchError::InvalidArgument {
                message: format!(
                    "Embedding weight must be 2-D, got shape {:?}",
                    weight.shape()
                ),
            });
        }
        let num_embeddings = weight.shape()[0];
        let embedding_dim = weight.shape()[1];

        if let Some(idx) = padding_idx {
            if idx >= num_embeddings {
                return Err(FerrotorchError::InvalidArgument {
                    message: format!(
                        "padding_idx {idx} is out of range for num_embeddings {num_embeddings}"
                    ),
                });
            }
        }

        Ok(Self {
            weight: Parameter::new(weight),
            num_embeddings,
            embedding_dim,
            padding_idx,
            max_norm: None,
            norm_type: 2.0,
            scale_grad_by_freq: false,
            training: true,
            sparse: false,
            last_indices: std::sync::Mutex::new(None),
        })
    }

    /// Builder: set the maximum row norm. After every forward pass, rows
    /// of `weight` touched by the input have their `norm_type`-norm clipped
    /// to `max_norm` via in-place renormalisation, matching
    /// `torch.nn.Embedding(max_norm=...)`. Closes #1445.
    pub fn with_max_norm(mut self, max_norm: f64) -> Self {
        self.max_norm = Some(max_norm);
        self
    }

    /// Builder: set the order of the row-norm used by `max_norm` (default
    /// `2.0`). Closes #1445.
    pub fn with_norm_type(mut self, norm_type: f64) -> Self {
        self.norm_type = norm_type;
        self
    }

    /// Builder: if `true`, `EmbeddingBackward` divides each touched row's
    /// gradient by the number of times the index appeared in the forward
    /// (`torch.nn.Embedding(scale_grad_by_freq=True)`). Closes #1445.
    pub fn with_scale_grad_by_freq(mut self, scale: bool) -> Self {
        self.scale_grad_by_freq = scale;
        self
    }

    /// Renormalise the rows of `self.weight` that `indices` touched, IN
    /// PLACE, so each touched row's `norm_type`-norm is at most `max_norm`.
    ///
    /// This is a faithful translation of the aten kernel
    /// `embedding_renorm_cpu_` (`aten/src/ATen/native/Embedding.cpp:181-212`):
    /// the touched indices are sorted and de-duplicated, and for each unique
    /// row whose current norm exceeds `max_norm` the row is scaled by
    /// `max_norm / (norm + 1e-7)`. Rows already within `max_norm` are left
    /// untouched, and rows never indexed in this forward are not visited.
    ///
    /// PyTorch's `F.embedding` (`torch/nn/functional.py:2561-2573`) runs this
    /// renorm BEFORE the gather, under `torch.no_grad()`, mutating the
    /// persisted `weight` tensor — so the change survives across forward
    /// calls. We match that by writing the renormed rows back into
    /// `self.weight` via [`Tensor::update_data`], the same in-place storage
    /// mutation the optimizer `step()` uses. The write is performed only when
    /// at least one row actually exceeded `max_norm`, keeping the common
    /// "nothing to clip" path allocation-free on the weight buffer.
    ///
    /// Returns `Ok(())` when `max_norm` is unset (no-op) or after the
    /// in-place mutation completes.
    fn renorm_weight_in_place(&self, indices: &[usize]) -> FerrotorchResult<()> {
        let Some(max_norm) = self.max_norm else {
            return Ok(());
        };
        renorm_weight_rows_in_place(
            self.weight.tensor(),
            indices,
            self.embedding_dim,
            max_norm,
            self.norm_type,
            "Embedding(max_norm) weight renorm",
        )
    }

    /// Toggle the sparse-grad mode. When enabled, [`Self::sparse_grad`]
    /// returns a `SparseGrad<T>` populated only with the rows actually
    /// touched by the most recent forward pass. Off by default. Returns
    /// `&mut self` for chaining.
    pub fn with_sparse(mut self, sparse: bool) -> Self {
        self.sparse = sparse;
        self
    }

    /// Record the unique row indices touched by the most recent forward pass.
    /// No-op when sparse mode is off — keeps the hot path zero-overhead for
    /// the common dense-grad case.
    fn cache_touched_rows(&self, indices: &[usize]) {
        if !self.sparse {
            return;
        }
        // Dedupe (sorted) so callers don't have to coalesce later.
        let mut uniq: Vec<usize> = indices.to_vec();
        uniq.sort_unstable();
        uniq.dedup();
        if let Ok(mut g) = self.last_indices.lock() {
            *g = Some(uniq);
        }
    }

    /// Materialize a [`SparseGrad`] from the current dense weight gradient,
    /// keyed on the indices touched by the most recent forward pass.
    ///
    /// Returns `None` when sparse mode is off, no forward has been run yet,
    /// or the parameter has no gradient (e.g. before the first backward
    /// call). The returned grad is already coalesced (each touched row
    /// appears once with its full gradient slab) — feed it directly into
    /// [`SparseGrad::apply_sgd`] or `optim::SparseAdam`.
    ///
    /// Mirrors PyTorch's `embedding_bag(..., sparse=True)` → `SparseAdam`
    /// flow. The dense grad is unchanged; `sparse_grad` just provides a
    /// compact view for optimizers that benefit from skipping zero rows.
    pub fn sparse_grad(&self) -> FerrotorchResult<Option<ferrotorch_core::SparseGrad<T>>> {
        if !self.sparse {
            return Ok(None);
        }
        let last = match self.last_indices.lock() {
            Ok(g) => g,
            Err(_) => return Ok(None),
        };
        let indices = match last.as_ref() {
            Some(v) => v.clone(),
            None => return Ok(None),
        };
        let grad = match self.weight.tensor().grad()? {
            Some(g) => g,
            None => return Ok(None),
        };
        let grad_data = grad.data_vec()?;
        let dim = self.embedding_dim;
        let mut values = Vec::with_capacity(indices.len() * dim);
        for &idx in &indices {
            let row_start = idx * dim;
            let row_end = row_start + dim;
            values.extend_from_slice(&grad_data[row_start..row_end]);
        }
        let sg = ferrotorch_core::SparseGrad::new(indices, values, vec![dim])?;
        Ok(Some(sg))
    }
}

impl<T: Float> Module<T> for Embedding<T> {
    /// Forward pass: look up embedding vectors for the given indices.
    ///
    /// `input` is an index tensor of ANY shape whose values are non-negative
    /// integers stored as floats. Each value is cast to `usize` and used to
    /// index into the weight matrix. The lookup operates on the flattened
    /// indices (row-major), exactly mirroring upstream `embedding_symint`
    /// (`aten/src/ATen/native/Embedding.cpp:43-53`):
    /// `weight.index_select(0, indices.reshape(-1)).view_symint(size)` where
    /// `size = (*indices.sizes(), weight.size(1))`.
    ///
    /// Returns a tensor of shape `(*input.shape(), embedding_dim)`. A 1-D
    /// index of length `n` therefore yields `[n, embedding_dim]`, and a 2-D
    /// index `[a, b]` yields `[a, b, embedding_dim]`, matching `F.embedding`.
    fn forward(&self, input: &Tensor<T>) -> FerrotorchResult<Tensor<T>> {
        let dim = self.embedding_dim;

        // Output shape is the index shape with `embedding_dim` appended, per
        // upstream `embedding_symint` (`Embedding.cpp:48-53`): the gather runs
        // over the flattened indices and the result is viewed back to
        // `(*indices.sizes(), weight.size(1))`. A 1-D input keeps the existing
        // `[n, dim]` behavior (the empty-prefix special-case is implicit).
        let mut output_shape: Vec<usize> = input.shape().to_vec();
        output_shape.push(dim);

        // GPU fast path for f32/f64 embeddings: gather rows entirely on GPU.
        if self.weight.tensor().is_cuda() && (is_f32::<T>() || is_f64::<T>()) {
            let backend = gpu_backend().ok_or(FerrotorchError::DeviceUnavailable)?;
            let device = self.weight.tensor().device();
            let ordinal = match device {
                Device::Cuda(o) => o,
                _ => unreachable!(),
            };

            let input_data = input.data_vec()?;
            let n = input_data.len();

            let mut indices = Vec::with_capacity(n);
            let mut indices_f32 = Vec::with_capacity(n);
            for (i, &val) in input_data.iter().enumerate() {
                let idx = num_traits::ToPrimitive::to_usize(&val).ok_or_else(|| {
                    FerrotorchError::InvalidArgument {
                        message: format!(
                            "Embedding index at position {i} cannot be converted to usize: {val:?}"
                        ),
                    }
                })?;
                if idx >= self.num_embeddings {
                    return Err(FerrotorchError::IndexOutOfBounds {
                        index: idx,
                        axis: 0,
                        size: self.num_embeddings,
                    });
                }
                indices.push(idx);
                indices_f32.push(idx as f32);
            }

            self.cache_touched_rows(&indices);

            // max_norm with a CUDA weight has no on-device renorm kernel yet;
            // surface that explicitly rather than silently returning
            // un-renormed rows (which would diverge from torch's in-place
            // mutation at functional.py:2561-2573). No-op when max_norm unset.
            self.renorm_weight_in_place(&indices)?;

            let idx_handle = upload_f32_to_gpu(&indices_f32, ordinal)?;
            let weight_handle = self.weight.tensor().gpu_handle()?;

            let output_handle = if is_f64::<T>() {
                backend.embed_lookup_batch_f64(&idx_handle, weight_handle, n, dim)?
            } else {
                backend.embed_lookup_batch_f32(&idx_handle, weight_handle, n, dim)?
            };

            // Padding index: if set, zero the corresponding output rows on GPU.
            // For padding_idx, the weight row should already be zero, so output
            // rows at padding positions should already be zero. Be defensive
            // only if padding_idx is actually referenced.
            // (The weight is zeroed at init, so we skip extra GPU work here.)

            let storage = TensorStorage::gpu(output_handle);

            if self.weight.requires_grad() && is_grad_enabled() {
                let grad_fn = Arc::new(EmbeddingBackward {
                    weight: self.weight.tensor().clone(),
                    indices,
                    num_embeddings: self.num_embeddings,
                    embedding_dim: dim,
                    padding_idx: self.padding_idx,
                    scale_grad_by_freq: self.scale_grad_by_freq,
                });
                return Tensor::from_operation(storage, output_shape, grad_fn);
            } else {
                return Tensor::from_storage(storage, output_shape, false);
            }
        }

        // CPU path — non-f32 GPU tensors have no GPU kernel, error out.
        if self.weight.tensor().is_cuda() {
            return Err(FerrotorchError::NotImplementedOnCuda { op: "Embedding" });
        }
        let input_data = input.data_vec()?;
        let n = input_data.len();

        // Convert float indices to usize and validate bounds.
        let mut indices = Vec::with_capacity(n);
        for (i, &val) in input_data.iter().enumerate() {
            let idx = num_traits::ToPrimitive::to_usize(&val).ok_or_else(|| {
                FerrotorchError::InvalidArgument {
                    message: format!(
                        "Embedding index at position {i} cannot be converted to usize: {val:?}"
                    ),
                }
            })?;
            if idx >= self.num_embeddings {
                return Err(FerrotorchError::IndexOutOfBounds {
                    index: idx,
                    axis: 0,
                    size: self.num_embeddings,
                });
            }
            indices.push(idx);
        }

        self.cache_touched_rows(&indices);

        // max_norm: renormalise the touched rows of the PERSISTED weight
        // IN PLACE, BEFORE the gather. This mirrors
        // `torch/nn/functional.py:2561-2573`, where `F.embedding` calls
        // `_no_grad_embedding_renorm_(weight, ...)` (which mutates `weight`
        // via `torch.embedding_renorm_`) and only THEN does the lookup.
        // The mutation persists across forward calls — a second forward with
        // the same indices is a no-op because the rows now satisfy max_norm.
        // Closes #1445 (CPU path).
        self.renorm_weight_in_place(&indices)?;

        // Re-read the (possibly mutated) weight buffer for the gather.
        let cpu_weight = self.weight.tensor().clone();
        let weight_data = cpu_weight.data()?;

        // Gather rows from weight.
        let mut output_data = Vec::with_capacity(n * dim);
        for &idx in &indices {
            let row_start = idx * dim;
            output_data.extend_from_slice(&weight_data[row_start..row_start + dim]);
        }

        // If padding_idx is set, ensure those rows are zeros in the output
        // (they should already be zero in the weight, but be defensive).
        if let Some(pad_idx) = self.padding_idx {
            for (i, &idx) in indices.iter().enumerate() {
                if idx == pad_idx {
                    let start = i * dim;
                    for v in &mut output_data[start..start + dim] {
                        *v = <T as num_traits::Zero>::zero();
                    }
                }
            }
        }

        // Output device matches the weight's device (GPU if model is on GPU).
        let device = self.weight.tensor().device();

        // Build storage on the target device first, then attach grad_fn.
        // This avoids to() stripping the grad_fn by creating a leaf tensor.
        let storage = if device.is_cuda() {
            let backend = gpu_backend().ok_or(FerrotorchError::DeviceUnavailable)?;
            let ordinal = match device {
                Device::Cuda(o) => o,
                _ => unreachable!(),
            };
            // SAFETY: `output_data` is a live owned `Vec<T>` whose contents we borrow
            // shared for the duration of this expression. Its underlying buffer is valid
            // for reads of `output_data.len() * size_of::<T>()` bytes — `T: Float`
            // is one of f32/f64/bf16/f16, none of which have padding bytes (no struct
            // wrappers, no niches), so the byte-length calculation is exact. The cast
            // `*const T` -> `*const u8` does not violate alignment because `u8`'s
            // alignment (1) is at most `T`'s alignment. The resulting `&[u8]` is
            // consumed by `backend.cpu_to_gpu` before `output_data` is moved into
            // `TensorStorage::cpu` on the else branch (mutually exclusive paths) or
            // dropped here, so the borrow never outlives the source.
            let bytes: &[u8] = unsafe {
                std::slice::from_raw_parts(
                    output_data.as_ptr() as *const u8,
                    output_data.len() * std::mem::size_of::<T>(),
                )
            };
            let handle = backend.cpu_to_gpu(bytes, T::dtype(), ordinal)?;
            TensorStorage::gpu(handle)
        } else {
            TensorStorage::cpu(output_data)
        };

        if self.weight.requires_grad() && is_grad_enabled() {
            let grad_fn = Arc::new(EmbeddingBackward {
                weight: self.weight.tensor().clone(),
                indices,
                num_embeddings: self.num_embeddings,
                embedding_dim: dim,
                padding_idx: self.padding_idx,
                scale_grad_by_freq: self.scale_grad_by_freq,
            });
            Tensor::from_operation(storage, output_shape, grad_fn)
        } else {
            Tensor::from_storage(storage, output_shape, false)
        }
    }

    fn parameters(&self) -> Vec<&Parameter<T>> {
        vec![&self.weight]
    }

    fn parameters_mut(&mut self) -> Vec<&mut Parameter<T>> {
        vec![&mut self.weight]
    }

    fn named_parameters(&self) -> Vec<(String, &Parameter<T>)> {
        vec![("weight".to_string(), &self.weight)]
    }

    fn train(&mut self) {
        self.training = true;
    }

    fn eval(&mut self) {
        self.training = false;
    }

    fn is_training(&self) -> bool {
        self.training
    }
}

// ---------------------------------------------------------------------------
// EmbeddingBag — fused lookup + reduce
// ---------------------------------------------------------------------------

/// Reduction mode for [`EmbeddingBag`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EmbeddingBagMode {
    /// Sum all embeddings in each bag.
    Sum,
    /// Mean of all embeddings in each bag.
    Mean,
    /// Element-wise max across embeddings in each bag.
    Max,
}

/// Computes sums or means of bags of embeddings without instantiating the
/// full intermediate embeddings. This is more efficient than `Embedding`
/// followed by a reduction for variable-length sequences.
///
/// # Input format
///
/// - `input`: 1-D tensor of indices [total_indices]
/// - `offsets`: 1-D tensor [num_bags] giving the start index of each bag
///   in `input`. Must be sorted and non-negative. Example: if `input` has
///   indices for 3 bags with lengths [2, 3, 1], then `offsets = [0, 2, 5]`.
///
/// # Modes
///
/// - `Sum`: output[b] = sum of weight[input[offsets[b]:offsets[b+1]]]
/// - `Mean`: output[b] = mean of weight[input[offsets[b]:offsets[b+1]]]
/// - `Max`: output[b] = element-wise max of weight[input[offsets[b]:offsets[b+1]]]
#[derive(Debug)]
#[allow(
    clippy::struct_excessive_bools,
    reason = "scale_grad_by_freq/sparse/include_last_offset/training each mirror a distinct torch.nn.EmbeddingBag kwarg (sparse.py:376-380) — R-DEV-2 requires matching the upstream Python API surface field-for-field, so collapsing them into a flags enum would diverge from the user-facing kwarg contract"
)]
pub struct EmbeddingBag<T: Float> {
    weight: Parameter<T>,
    num_embeddings: usize,
    embedding_dim: usize,
    mode: EmbeddingBagMode,
    training: bool,
    /// If set, each touched weight row is renormalised in place to at most
    /// `max_norm` under the `norm_type`-norm before the bag reduction,
    /// mirroring `torch.nn.EmbeddingBag(max_norm=...)`
    /// (`torch/nn/modules/sparse.py:374`, `functional.py:2766-2771`).
    pub max_norm: Option<f64>,
    /// Order of the row-norm used when `max_norm` is active. Defaults to
    /// `2.0` per `torch/nn/modules/sparse.py:375`.
    pub norm_type: f64,
    /// If `true`, future gradient accumulation scales each row by the inverse
    /// frequency of its index in the mini-batch. Carried to mirror the
    /// upstream kwarg (`sparse.py:376`); `max` mode forbids it
    /// (`functional.py:2755-2758`).
    pub scale_grad_by_freq: bool,
    /// Advertises a sparse-gradient pattern, mirroring
    /// `torch.nn.EmbeddingBag(sparse=True)` (`sparse.py:378`). `max` mode
    /// forbids it (`functional.py:2760-2761`).
    pub sparse: bool,
    /// When `true`, `offsets` has `num_bags + 1` entries and its last entry
    /// is the total index count (CSR-style), mirroring
    /// `torch.nn.EmbeddingBag(include_last_offset=True)` (`sparse.py:380`,
    /// `functional.py:2621-2624`).
    pub include_last_offset: bool,
    /// If set, indices equal to `padding_idx` are excluded from each bag's
    /// reduction (and the mean divisor), and the corresponding weight row is
    /// zeroed at construction — matching `torch.nn.EmbeddingBag(padding_idx)`
    /// (`sparse.py:381`, `aten/src/ATen/native/EmbeddingBag.cpp:140-156`).
    pub padding_idx: Option<usize>,
}

impl<T: Float> EmbeddingBag<T> {
    /// Create a new EmbeddingBag with default kwargs (no `max_norm`,
    /// `norm_type = 2.0`, `scale_grad_by_freq = false`, `sparse = false`,
    /// `include_last_offset = false`, no `padding_idx`), matching the
    /// `torch.nn.EmbeddingBag(num_embeddings, embedding_dim, mode=...)`
    /// defaults at `torch/nn/modules/sparse.py:370-381`.
    pub fn new(
        num_embeddings: usize,
        embedding_dim: usize,
        mode: EmbeddingBagMode,
    ) -> FerrotorchResult<Self> {
        Self::new_with(num_embeddings, embedding_dim, mode, None)
    }

    /// Create a new EmbeddingBag, optionally with a `padding_idx`.
    ///
    /// Mirrors the `padding_idx` validation + zero-fill in
    /// `torch.nn.EmbeddingBag.__init__` / `_fill_padding_idx_with_zero`
    /// (`torch/nn/modules/sparse.py:392-423`): `padding_idx` must be within
    /// `num_embeddings`, and that weight row is zeroed after init.
    ///
    /// # Errors
    ///
    /// Returns an error if `padding_idx >= num_embeddings`.
    pub fn new_with(
        num_embeddings: usize,
        embedding_dim: usize,
        mode: EmbeddingBagMode,
        padding_idx: Option<usize>,
    ) -> FerrotorchResult<Self> {
        if let Some(idx) = padding_idx {
            if idx >= num_embeddings {
                return Err(FerrotorchError::InvalidArgument {
                    message: format!(
                        "padding_idx {idx} must be within num_embeddings {num_embeddings}"
                    ),
                });
            }
        }

        let mut weight = Parameter::zeros(&[num_embeddings, embedding_dim])?;
        init::normal(&mut weight, 0.0, 1.0)?;

        // Zero the padding row if requested (mirrors
        // `_fill_padding_idx_with_zero`, sparse.py:420-423).
        if let Some(idx) = padding_idx {
            let data = weight.data()?.to_vec();
            let mut new_data = data;
            let start = idx * embedding_dim;
            for v in &mut new_data[start..start + embedding_dim] {
                *v = <T as num_traits::Zero>::zero();
            }
            weight = Parameter::new(Tensor::from_storage(
                TensorStorage::cpu(new_data),
                vec![num_embeddings, embedding_dim],
                true,
            )?);
        }

        Ok(Self {
            weight,
            num_embeddings,
            embedding_dim,
            mode,
            training: true,
            max_norm: None,
            norm_type: 2.0,
            scale_grad_by_freq: false,
            sparse: false,
            include_last_offset: false,
            padding_idx,
        })
    }

    /// Builder: set the maximum row norm. Touched rows of `weight` have their
    /// `norm_type`-norm clipped to `max_norm` in place before each bag
    /// reduction, mirroring `torch.nn.EmbeddingBag(max_norm=...)`. Closes #1445.
    pub fn with_max_norm(mut self, max_norm: f64) -> Self {
        self.max_norm = Some(max_norm);
        self
    }

    /// Builder: set the order of the row-norm used by `max_norm` (default
    /// `2.0`, `sparse.py:375`). Closes #1445.
    pub fn with_norm_type(mut self, norm_type: f64) -> Self {
        self.norm_type = norm_type;
        self
    }

    /// Builder: set `scale_grad_by_freq` (`sparse.py:376`). Rejected for
    /// `max` mode by [`Self::forward_bag`], matching `functional.py:2755-2758`.
    /// Closes #1445.
    pub fn with_scale_grad_by_freq(mut self, scale: bool) -> Self {
        self.scale_grad_by_freq = scale;
        self
    }

    /// Builder: set `sparse` (`sparse.py:378`). Rejected for `max` mode by
    /// [`Self::forward_bag`], matching `functional.py:2760-2761`. Closes #1445.
    pub fn with_sparse(mut self, sparse: bool) -> Self {
        self.sparse = sparse;
        self
    }

    /// Builder: set `include_last_offset` (`sparse.py:380`). When `true`,
    /// `offsets` carries `num_bags + 1` entries with the last being the total
    /// index count, matching the CSR convention in `functional.py:2621-2624`.
    /// Closes #1445.
    pub fn with_include_last_offset(mut self, include_last_offset: bool) -> Self {
        self.include_last_offset = include_last_offset;
        self
    }

    /// Forward pass: compute bag-reduced embeddings.
    ///
    /// `input`: 1-D tensor of indices `[total_indices]`.
    /// `offsets`: bag start offsets. When `include_last_offset == false`,
    /// this has `num_bags` entries (bag `b` spans `offsets[b]..offsets[b+1]`,
    /// the last bag running to the end of `input`). When
    /// `include_last_offset == true`, it has `num_bags + 1` entries with the
    /// final entry being the total index count (CSR style), matching
    /// `torch/nn/functional.py:2621-2624`.
    ///
    /// Honors `max_norm` (in-place weight renorm before the reduction,
    /// mirroring `functional.py:2766-2771`) and `padding_idx` (indices equal
    /// to it are excluded from both the reduction and the mean divisor,
    /// mirroring `aten/src/ATen/native/EmbeddingBag.cpp:140-156`). `max` mode
    /// rejects `scale_grad_by_freq` / `sparse`
    /// (`functional.py:2755-2761`).
    ///
    /// This is the unweighted path (`per_sample_weights = None`); it delegates
    /// to [`Self::forward_bag_weighted`] so the two share a single reduction
    /// body. The unweighted forward returns a non-grad-tracked tensor (per-bag
    /// backward for the plain reductions is tracked separately).
    pub fn forward_bag(&self, input: &Tensor<T>, offsets: &[usize]) -> FerrotorchResult<Tensor<T>> {
        self.forward_bag_weighted(input, offsets, None)
    }

    /// Forward pass with optional `per_sample_weights`, mirroring
    /// `F.embedding_bag(input, weight, offsets, ..., per_sample_weights=...)`
    /// (`torch/nn/functional.py:2576-2791`).
    ///
    /// When `per_sample_weights` is `Some(psw)`:
    /// - It is ONLY valid for `mode == Sum`; any other mode returns torch's
    ///   exact `NotImplementedError` text (`functional.py:2773-2778`).
    /// - `psw` must have the same shape as `input` (`functional.py:2698-2702`).
    /// - Each gathered embedding row is scaled by its sample weight BEFORE the
    ///   sum reduction (`output[bag][:] += weight[idx][:] * psw[i]`,
    ///   `EmbeddingBag.cpp:537-543`).
    /// - The output is grad-tracked: gradient flows to BOTH `weight` and
    ///   `psw` via [`EmbeddingBagSumWeightedBackward`], matching torch's
    ///   autograd. `padding_idx` samples contribute 0 to the reduction and to
    ///   both gradients.
    ///
    /// When `per_sample_weights` is `None` this is the plain unweighted
    /// reduction (sum / mean / max) and returns a non-grad tensor — identical
    /// to the historical [`Self::forward_bag`] behavior.
    pub fn forward_bag_weighted(
        &self,
        input: &Tensor<T>,
        offsets: &[usize],
        per_sample_weights: Option<&Tensor<T>>,
    ) -> FerrotorchResult<Tensor<T>> {
        if input.ndim() != 1 {
            return Err(FerrotorchError::InvalidArgument {
                message: format!("EmbeddingBag input must be 1-D, got {:?}", input.shape()),
            });
        }

        // per_sample_weights is only supported for mode='sum' — torch raises a
        // NotImplementedError with this exact text (functional.py:2773-2778).
        // Validate this BEFORE the shape check matches torch's ordering only
        // loosely, but both are user-facing errors; we surface the mode error
        // first since it is the dominant constraint for this feature.
        if let Some(psw) = per_sample_weights {
            if self.mode != EmbeddingBagMode::Sum {
                let mode_str = match self.mode {
                    EmbeddingBagMode::Sum => "sum",
                    EmbeddingBagMode::Mean => "mean",
                    EmbeddingBagMode::Max => "max",
                };
                return Err(FerrotorchError::InvalidArgument {
                    message: format!(
                        "embedding_bag: per_sample_weights was not None. per_sample_weights is \
                         only supported for mode='sum' (got mode='{mode_str}'). Please open a \
                         feature request on GitHub."
                    ),
                });
            }
            // psw must have exactly the same shape as input (functional.py:2698).
            if psw.shape() != input.shape() {
                return Err(FerrotorchError::InvalidArgument {
                    message: format!(
                        "embedding_bag: If per_sample_weights ({:?}) is not None, then it must \
                         have the same shape as the input ({:?})",
                        psw.shape(),
                        input.shape()
                    ),
                });
            }
        }

        // mode='max' forbids scale_grad_by_freq and sparse, matching
        // functional.py:2755-2761.
        if self.mode == EmbeddingBagMode::Max {
            if self.scale_grad_by_freq {
                return Err(FerrotorchError::InvalidArgument {
                    message: "max mode does not support scaling the gradient by the frequency"
                        .into(),
                });
            }
            if self.sparse {
                return Err(FerrotorchError::InvalidArgument {
                    message: "max mode does not support sparse weights".into(),
                });
            }
        }

        let input_data = input.data_vec()?;
        let dim = self.embedding_dim;
        let total = input_data.len();

        // Materialise the bag boundaries from `offsets`, honoring
        // include_last_offset (CSR layout: trailing entry == total count).
        let num_bags = if self.include_last_offset {
            offsets.len().saturating_sub(1)
        } else {
            offsets.len()
        };

        // Validate + collect indices.
        let mut indices = Vec::with_capacity(total);
        for (i, &val) in input_data.iter().enumerate() {
            let idx = num_traits::ToPrimitive::to_usize(&val).ok_or_else(|| {
                FerrotorchError::InvalidArgument {
                    message: format!("EmbeddingBag index {i} invalid: {val:?}"),
                }
            })?;
            if idx >= self.num_embeddings {
                return Err(FerrotorchError::IndexOutOfBounds {
                    index: idx,
                    axis: 0,
                    size: self.num_embeddings,
                });
            }
            indices.push(idx);
        }

        // max_norm: renormalise the touched rows of the persisted weight IN
        // PLACE before the reduction (functional.py:2766-2771 runs the renorm
        // before torch.embedding_bag). No-op when max_norm unset.
        if let Some(max_norm) = self.max_norm {
            renorm_weight_rows_in_place(
                self.weight.tensor(),
                &indices,
                dim,
                max_norm,
                self.norm_type,
                "EmbeddingBag(max_norm) weight renorm",
            )?;
        }

        // per_sample_weights data + a `bag_of` map (offset2bag) — both only
        // materialised when psw is present (psw is sum-mode-only, validated
        // above). `bag_of[i]` is the output row sample `i` accumulates into,
        // mirroring torch's `offset2bag` (`EmbeddingBag.cpp:1563`).
        let psw_data: Option<Vec<T>> = match per_sample_weights {
            Some(psw) => Some(psw.data_vec()?),
            None => None,
        };
        let mut bag_of: Vec<usize> = vec![0; total];

        // Re-read the (possibly renormed) weight for the reduction.
        let weight_data = self.weight.tensor().data()?;

        let mut output = vec![<T as num_traits::Zero>::zero(); num_bags * dim];

        for b in 0..num_bags {
            let start = offsets[b];
            // With include_last_offset, every bag (including the last) reads
            // its end from offsets[b+1]; otherwise the final bag runs to total.
            let end = if self.include_last_offset || b + 1 < num_bags {
                offsets[b + 1]
            } else {
                total
            };

            // Record offset2bag for every sample in this bag so the weighted
            // backward (when psw is present) can map sample -> bag grad row.
            for s in bag_of.iter_mut().take(end).skip(start) {
                *s = b;
            }

            match self.mode {
                EmbeddingBagMode::Sum | EmbeddingBagMode::Mean => {
                    // Count of non-padding entries; the mean divides by this,
                    // mirroring the bag_size decrement at EmbeddingBag.cpp:151-156.
                    let mut count: usize = 0;
                    let out_start = b * dim;
                    for s in start..end {
                        let idx = indices[s];
                        // padding_idx entries are excluded from the reduction
                        // (EmbeddingBag.cpp:147 `if (idx != padding_idx)`).
                        if self.padding_idx == Some(idx) {
                            continue;
                        }
                        let row_start = idx * dim;
                        // per_sample_weights scale (sum-mode only): each gathered
                        // row is multiplied by its sample weight BEFORE the sum
                        // (EmbeddingBag.cpp:540-543). `None` => scale of 1.
                        match &psw_data {
                            Some(pw) => {
                                let scale = pw[s];
                                for d in 0..dim {
                                    output[out_start + d] += weight_data[row_start + d] * scale;
                                }
                            }
                            None => {
                                for d in 0..dim {
                                    output[out_start + d] += weight_data[row_start + d];
                                }
                            }
                        }
                        count += 1;
                    }
                    if self.mode == EmbeddingBagMode::Mean && count > 0 {
                        let scale = T::from(count).unwrap();
                        for d in 0..dim {
                            output[out_start + d] = output[out_start + d] / scale;
                        }
                    }
                }
                EmbeddingBagMode::Max => {
                    let out_start = b * dim;
                    // Initialize with -inf; an all-padding (or empty) bag stays
                    // at zero (torch leaves max-mode empty bags at zero too).
                    let mut any = false;
                    for d in 0..dim {
                        output[out_start + d] = T::neg_infinity();
                    }
                    for &idx in &indices[start..end] {
                        if self.padding_idx == Some(idx) {
                            continue;
                        }
                        any = true;
                        let row_start = idx * dim;
                        for d in 0..dim {
                            let val = weight_data[row_start + d];
                            if val > output[out_start + d] {
                                output[out_start + d] = val;
                            }
                        }
                    }
                    if !any {
                        for d in 0..dim {
                            output[out_start + d] = <T as num_traits::Zero>::zero();
                        }
                    }
                }
            }
        }

        let storage = TensorStorage::cpu(output);
        let out_shape = vec![num_bags, dim];

        // When per_sample_weights is supplied (sum mode, validated above) and
        // either the weight or the psw requires grad, attach the weighted
        // backward so gradient flows to BOTH inputs (EmbeddingBag.cpp:1248-1250
        // honors per_sample_weights.requires_grad).
        if let Some(psw) = per_sample_weights {
            let weight_t = self.weight.tensor();
            let needs_grad = is_grad_enabled() && (weight_t.requires_grad() || psw.requires_grad());
            if needs_grad {
                let grad_fn = Arc::new(EmbeddingBagSumWeightedBackward {
                    weight: weight_t.clone(),
                    per_sample_weights: psw.clone(),
                    indices,
                    bag_of,
                    num_embeddings: self.num_embeddings,
                    embedding_dim: dim,
                    padding_idx: self.padding_idx,
                    scale_grad_by_freq: self.scale_grad_by_freq,
                });
                return Tensor::from_operation(storage, out_shape, grad_fn);
            }
        }

        // Unweighted path (or grad disabled): non-grad tensor, matching the
        // historical forward_bag behavior.
        Tensor::from_storage(storage, out_shape, false)
    }

    /// Number of embeddings in the table.
    pub fn num_embeddings(&self) -> usize {
        self.num_embeddings
    }

    /// Dimension of each embedding vector.
    pub fn embedding_dim(&self) -> usize {
        self.embedding_dim
    }

    /// The reduction mode.
    pub fn mode(&self) -> EmbeddingBagMode {
        self.mode
    }

    /// The `padding_idx`, if set. Indices equal to it are excluded from each
    /// bag's reduction. Mirrors `torch.nn.EmbeddingBag.padding_idx`.
    pub fn padding_idx(&self) -> Option<usize> {
        self.padding_idx
    }
}

impl<T: Float> Module<T> for EmbeddingBag<T> {
    /// Forward pass using the input as both indices and offsets.
    ///
    /// If input is 2-D [num_bags, bag_size], each row is a bag.
    /// If input is 1-D, treats the entire input as a single bag.
    fn forward(&self, input: &Tensor<T>) -> FerrotorchResult<Tensor<T>> {
        if input.ndim() == 2 {
            // 2D input: [num_bags, bag_size] — each row is a fixed-length bag.
            // torch forces include_last_offset=False for 2D input
            // (functional.py:2735); we build offsets in whichever convention
            // `forward_bag` will read, so a configured `include_last_offset`
            // flag stays consistent here too.
            let shape = input.shape();
            let num_bags = shape[0];
            let bag_size = shape[1];
            let mut offsets: Vec<usize> = (0..num_bags).map(|b| b * bag_size).collect();
            if self.include_last_offset {
                // CSR layout: trailing entry is the total index count.
                offsets.push(num_bags * bag_size);
            }
            let flat = input.view_reshape(vec![num_bags * bag_size])?;
            self.forward_bag(&flat, &offsets)
        } else if input.ndim() == 1 {
            // 1D input: single bag. With include_last_offset the CSR boundary
            // is [0, total]; otherwise a single [0] start offset.
            if self.include_last_offset {
                let total = input.shape()[0];
                self.forward_bag(input, &[0, total])
            } else {
                self.forward_bag(input, &[0])
            }
        } else {
            Err(FerrotorchError::InvalidArgument {
                message: format!(
                    "EmbeddingBag input must be 1-D or 2-D, got {:?}",
                    input.shape()
                ),
            })
        }
    }

    fn parameters(&self) -> Vec<&Parameter<T>> {
        vec![&self.weight]
    }

    fn parameters_mut(&mut self) -> Vec<&mut Parameter<T>> {
        vec![&mut self.weight]
    }

    fn named_parameters(&self) -> Vec<(String, &Parameter<T>)> {
        vec![("weight".to_string(), &self.weight)]
    }

    fn train(&mut self) {
        self.training = true;
    }

    fn eval(&mut self) {
        self.training = false;
    }

    fn is_training(&self) -> bool {
        self.training
    }
}

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

#[cfg(test)]
mod tests {
    use super::*;
    use ferrotorch_core::autograd::graph::backward;
    use ferrotorch_core::storage::TensorStorage;

    /// Helper: create a 1-D tensor of float indices.
    fn index_tensor(indices: &[f32]) -> Tensor<f32> {
        Tensor::from_storage(
            TensorStorage::cpu(indices.to_vec()),
            vec![indices.len()],
            false,
        )
        .unwrap()
    }

    // --- Forward tests ---

    #[test]
    fn test_forward_shape() {
        let emb = Embedding::<f32>::new(10, 4, None).unwrap();
        let indices = index_tensor(&[0.0, 3.0, 7.0]);
        let output = emb.forward(&indices).unwrap();
        assert_eq!(output.shape(), &[3, 4]);
    }

    #[test]
    fn test_forward_correct_values() {
        // Build an embedding with known weights.
        let weight_data: Vec<f32> = (0..12).map(|i| i as f32).collect();
        let weight =
            Tensor::from_storage(TensorStorage::cpu(weight_data), vec![4, 3], true).unwrap();
        let emb = Embedding::from_pretrained(weight, None).unwrap();

        // Look up rows 2 and 0.
        let indices = index_tensor(&[2.0, 0.0]);
        let output = emb.forward(&indices).unwrap();
        let data = output.data().unwrap();

        // Row 2 = [6, 7, 8], Row 0 = [0, 1, 2]
        assert_eq!(data.len(), 6);
        assert!((data[0] - 6.0).abs() < 1e-6);
        assert!((data[1] - 7.0).abs() < 1e-6);
        assert!((data[2] - 8.0).abs() < 1e-6);
        assert!((data[3] - 0.0).abs() < 1e-6);
        assert!((data[4] - 1.0).abs() < 1e-6);
        assert!((data[5] - 2.0).abs() < 1e-6);
    }

    #[test]
    fn test_forward_single_index() {
        let emb = Embedding::<f32>::new(5, 8, None).unwrap();
        let indices = index_tensor(&[3.0]);
        let output = emb.forward(&indices).unwrap();
        assert_eq!(output.shape(), &[1, 8]);
    }

    // --- Padding index tests ---

    #[test]
    #[allow(clippy::needless_range_loop)]
    fn test_padding_idx_zeros() {
        let emb = Embedding::<f32>::new(5, 3, Some(2)).unwrap();

        // The padding row in the weight should be zero.
        let w_data = emb.weight.data().unwrap();
        let pad_start = 2 * 3;
        for i in 0..3 {
            assert!(
                (w_data[pad_start + i] - 0.0).abs() < 1e-6,
                "padding row weight[2][{i}] should be 0, got {}",
                w_data[pad_start + i]
            );
        }

        // Forward with the padding index should return zeros.
        let indices = index_tensor(&[2.0]);
        let output = emb.forward(&indices).unwrap();
        let data = output.data().unwrap();
        for i in 0..3 {
            assert!(
                (data[i] - 0.0).abs() < 1e-6,
                "padding output[0][{i}] should be 0, got {}",
                data[i]
            );
        }
    }

    #[test]
    fn test_padding_idx_mixed() {
        // Build known weights, set padding_idx=1.
        let weight_data: Vec<f32> = vec![
            1.0, 2.0, // row 0
            0.0, 0.0, // row 1 (padding — will be zeroed)
            5.0, 6.0, // row 2
        ];
        let weight =
            Tensor::from_storage(TensorStorage::cpu(weight_data), vec![3, 2], true).unwrap();
        let emb = Embedding::from_pretrained(weight, Some(1)).unwrap();

        let indices = index_tensor(&[0.0, 1.0, 2.0]);
        let output = emb.forward(&indices).unwrap();
        let data = output.data().unwrap();

        // Row 0: [1, 2]
        assert!((data[0] - 1.0).abs() < 1e-6);
        assert!((data[1] - 2.0).abs() < 1e-6);
        // Row 1 (padding): [0, 0]
        assert!((data[2] - 0.0).abs() < 1e-6);
        assert!((data[3] - 0.0).abs() < 1e-6);
        // Row 2: [5, 6]
        assert!((data[4] - 5.0).abs() < 1e-6);
        assert!((data[5] - 6.0).abs() < 1e-6);
    }

    #[test]
    fn test_padding_idx_out_of_range() {
        let result = Embedding::<f32>::new(5, 3, Some(10));
        assert!(result.is_err());
    }

    // --- Out-of-bounds error ---

    #[test]
    fn test_out_of_bounds_error() {
        let emb = Embedding::<f32>::new(5, 3, None).unwrap();
        let indices = index_tensor(&[0.0, 5.0]); // 5 is out of bounds for num_embeddings=5
        let result = emb.forward(&indices);
        assert!(result.is_err());
    }

    #[test]
    fn test_negative_index_error() {
        let emb = Embedding::<f32>::new(5, 3, None).unwrap();
        let indices = index_tensor(&[-1.0]); // Negative cannot convert to usize
        let result = emb.forward(&indices);
        assert!(result.is_err());
    }

    // --- N-D index input (matches upstream F.embedding) ---

    #[test]
    fn test_2d_index_input_shape() {
        // Upstream `embedding_symint` (aten/src/ATen/native/Embedding.cpp:48-53)
        // accepts ANY index shape and returns `(*indices.sizes(), embedding_dim)`.
        // A [2,2] index against a [5,3] weight => output shape [2,2,3].
        let emb = Embedding::<f32>::new(5, 3, None).unwrap();
        let input = Tensor::from_storage(
            TensorStorage::cpu(vec![0.0f32, 1.0, 2.0, 3.0]),
            vec![2, 2],
            false,
        )
        .unwrap();
        let output = emb.forward(&input).unwrap();
        assert_eq!(output.shape(), &[2, 2, 3]);
    }

    // --- Backward tests ---

    #[test]
    fn test_backward_simple() {
        // weight shape [3, 2], look up indices [0, 2]
        // output shape [2, 2]
        // grad_output = [[1, 1], [1, 1]]
        // grad_weight = [[1, 1], [0, 0], [1, 1]]
        let weight_data: Vec<f32> = vec![
            10.0, 20.0, // row 0
            30.0, 40.0, // row 1
            50.0, 60.0, // row 2
        ];
        let weight =
            Tensor::from_storage(TensorStorage::cpu(weight_data), vec![3, 2], true).unwrap();
        let emb = Embedding::from_pretrained(weight, None).unwrap();

        let indices = index_tensor(&[0.0, 2.0]);
        let output = emb.forward(&indices).unwrap();

        assert!(output.requires_grad());
        assert_eq!(output.grad_fn().unwrap().name(), "EmbeddingBackward");

        // Manually call backward on the grad_fn.
        let grad_output =
            Tensor::from_storage(TensorStorage::cpu(vec![1.0f32; 4]), vec![2, 2], false).unwrap();

        let grad_fn = output.grad_fn().unwrap();
        let grads = grad_fn.backward(&grad_output).unwrap();

        let grad_weight = grads[0].as_ref().unwrap();
        assert_eq!(grad_weight.shape(), &[3, 2]);
        let gd = grad_weight.data().unwrap();

        // Row 0: accessed once -> [1, 1]
        assert!((gd[0] - 1.0).abs() < 1e-6);
        assert!((gd[1] - 1.0).abs() < 1e-6);
        // Row 1: not accessed -> [0, 0]
        assert!((gd[2] - 0.0).abs() < 1e-6);
        assert!((gd[3] - 0.0).abs() < 1e-6);
        // Row 2: accessed once -> [1, 1]
        assert!((gd[4] - 1.0).abs() < 1e-6);
        assert!((gd[5] - 1.0).abs() < 1e-6);
    }

    #[test]
    fn test_backward_duplicate_indices() {
        // weight shape [3, 2], look up indices [1, 1, 0, 1]
        // output shape [4, 2]
        //
        // grad_output = [[1, 2], [3, 4], [5, 6], [7, 8]]
        //
        // grad_weight[0] = grad_output[2] = [5, 6]       (index 0 appears once, at position 2)
        // grad_weight[1] = grad_output[0] + grad_output[1] + grad_output[3]
        //                = [1, 2] + [3, 4] + [7, 8] = [11, 14]
        // grad_weight[2] = [0, 0]                          (index 2 never accessed)
        let weight_data: Vec<f32> = vec![
            10.0, 20.0, // row 0
            30.0, 40.0, // row 1
            50.0, 60.0, // row 2
        ];
        let weight =
            Tensor::from_storage(TensorStorage::cpu(weight_data), vec![3, 2], true).unwrap();
        let emb = Embedding::from_pretrained(weight, None).unwrap();

        let indices = index_tensor(&[1.0, 1.0, 0.0, 1.0]);
        let output = emb.forward(&indices).unwrap();

        let grad_output = Tensor::from_storage(
            TensorStorage::cpu(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]),
            vec![4, 2],
            false,
        )
        .unwrap();

        let grad_fn = output.grad_fn().unwrap();
        let grads = grad_fn.backward(&grad_output).unwrap();

        let grad_weight = grads[0].as_ref().unwrap();
        let gd = grad_weight.data().unwrap();

        // Row 0: [5, 6]
        assert!((gd[0] - 5.0).abs() < 1e-6, "gd[0] = {}, expected 5", gd[0]);
        assert!((gd[1] - 6.0).abs() < 1e-6, "gd[1] = {}, expected 6", gd[1]);
        // Row 1: [1+3+7, 2+4+8] = [11, 14]
        assert!(
            (gd[2] - 11.0).abs() < 1e-6,
            "gd[2] = {}, expected 11",
            gd[2]
        );
        assert!(
            (gd[3] - 14.0).abs() < 1e-6,
            "gd[3] = {}, expected 14",
            gd[3]
        );
        // Row 2: [0, 0]
        assert!((gd[4] - 0.0).abs() < 1e-6, "gd[4] = {}, expected 0", gd[4]);
        assert!((gd[5] - 0.0).abs() < 1e-6, "gd[5] = {}, expected 0", gd[5]);
    }

    #[test]
    fn test_backward_padding_idx_zeroed() {
        // Even if padding_idx is accessed, its gradient should be zero.
        let weight_data: Vec<f32> = vec![
            1.0, 2.0, // row 0
            0.0, 0.0, // row 1 (padding)
            5.0, 6.0, // row 2
        ];
        let weight =
            Tensor::from_storage(TensorStorage::cpu(weight_data), vec![3, 2], true).unwrap();
        let emb = Embedding::from_pretrained(weight, Some(1)).unwrap();

        let indices = index_tensor(&[0.0, 1.0, 2.0]);
        let output = emb.forward(&indices).unwrap();

        let grad_output =
            Tensor::from_storage(TensorStorage::cpu(vec![1.0f32; 6]), vec![3, 2], false).unwrap();

        let grad_fn = output.grad_fn().unwrap();
        let grads = grad_fn.backward(&grad_output).unwrap();

        let grad_weight = grads[0].as_ref().unwrap();
        let gd = grad_weight.data().unwrap();

        // Row 0: [1, 1]
        assert!((gd[0] - 1.0).abs() < 1e-6);
        assert!((gd[1] - 1.0).abs() < 1e-6);
        // Row 1 (padding): must be [0, 0] even though it was accessed
        assert!((gd[2] - 0.0).abs() < 1e-6, "padding grad[1][0] should be 0");
        assert!((gd[3] - 0.0).abs() < 1e-6, "padding grad[1][1] should be 0");
        // Row 2: [1, 1]
        assert!((gd[4] - 1.0).abs() < 1e-6);
        assert!((gd[5] - 1.0).abs() < 1e-6);
    }

    #[test]
    fn test_backward_end_to_end() {
        // End-to-end test: use the autograd engine to verify gradients
        // flow all the way to the weight parameter.
        let weight_data: Vec<f32> = vec![
            1.0, 2.0, // row 0
            3.0, 4.0, // row 1
            5.0, 6.0, // row 2
        ];
        let weight =
            Tensor::from_storage(TensorStorage::cpu(weight_data), vec![3, 2], true).unwrap();
        let emb = Embedding::from_pretrained(weight, None).unwrap();

        let indices = index_tensor(&[1.0, 0.0]);
        let output = emb.forward(&indices).unwrap();
        // output = [[3, 4], [1, 2]], shape [2, 2]

        // Sum all elements to get a scalar for backward.
        let out_data = output.data().unwrap();
        let total: f32 = out_data.iter().sum();

        // Build a SumBackward that broadcasts scalar grad to output shape.
        #[derive(Debug)]
        struct SumBackward<T: Float> {
            input: Tensor<T>,
        }
        impl<T: Float> GradFn<T> for SumBackward<T> {
            fn backward(
                &self,
                grad_output: &Tensor<T>,
            ) -> FerrotorchResult<Vec<Option<Tensor<T>>>> {
                let go_val = grad_output.data()?[0];
                let grad = vec![go_val; self.input.numel()];
                let t = Tensor::from_storage(
                    TensorStorage::cpu(grad),
                    self.input.shape().to_vec(),
                    false,
                )?;
                Ok(vec![Some(t)])
            }
            fn inputs(&self) -> Vec<&Tensor<T>> {
                vec![&self.input]
            }
            fn name(&self) -> &'static str {
                "SumBackward"
            }
        }

        let loss = Tensor::from_operation(
            TensorStorage::cpu(vec![total]),
            vec![],
            Arc::new(SumBackward {
                input: output.clone(),
            }),
        )
        .unwrap();

        backward(&loss).unwrap();

        // The weight should now have a gradient.
        let grad = emb.weight.tensor().grad().unwrap().unwrap();
        let gd = grad.data().unwrap();
        assert_eq!(gd.len(), 6);

        // Row 0 accessed once (position 1): grad = [1, 1]
        assert!((gd[0] - 1.0).abs() < 1e-6, "grad[0][0] = {}", gd[0]);
        assert!((gd[1] - 1.0).abs() < 1e-6, "grad[0][1] = {}", gd[1]);
        // Row 1 accessed once (position 0): grad = [1, 1]
        assert!((gd[2] - 1.0).abs() < 1e-6, "grad[1][0] = {}", gd[2]);
        assert!((gd[3] - 1.0).abs() < 1e-6, "grad[1][1] = {}", gd[3]);
        // Row 2 not accessed: grad = [0, 0]
        assert!((gd[4] - 0.0).abs() < 1e-6, "grad[2][0] = {}", gd[4]);
        assert!((gd[5] - 0.0).abs() < 1e-6, "grad[2][1] = {}", gd[5]);
    }

    // --- Module trait tests ---

    #[test]
    fn test_module_parameters() {
        let emb = Embedding::<f32>::new(10, 4, None).unwrap();
        assert_eq!(emb.parameters().len(), 1);
        assert_eq!(emb.parameters()[0].shape(), &[10, 4]);
    }

    #[test]
    fn test_module_named_parameters() {
        let emb = Embedding::<f32>::new(5, 3, None).unwrap();
        let named = emb.named_parameters();
        assert_eq!(named.len(), 1);
        assert_eq!(named[0].0, "weight");
    }

    #[test]
    fn test_module_train_eval() {
        let mut emb = Embedding::<f32>::new(5, 3, None).unwrap();
        assert!(emb.is_training());
        emb.eval();
        assert!(!emb.is_training());
        emb.train();
        assert!(emb.is_training());
    }

    #[test]
    fn test_embedding_is_send_sync() {
        fn assert_send_sync<T: Send + Sync>() {}
        assert_send_sync::<Embedding<f32>>();
        assert_send_sync::<Embedding<f64>>();
    }

    #[test]
    fn test_f64_embedding() {
        let emb = Embedding::<f64>::new(5, 3, None).unwrap();
        let indices =
            Tensor::from_storage(TensorStorage::cpu(vec![0.0f64, 2.0, 4.0]), vec![3], false)
                .unwrap();
        let output = emb.forward(&indices).unwrap();
        assert_eq!(output.shape(), &[3, 3]);
    }

    // -------------------------------------------------------------------
    // SparseGrad integration (#623)
    // -------------------------------------------------------------------

    #[test]
    fn sparse_grad_returns_none_when_sparse_off() {
        let emb = Embedding::<f32>::new(8, 4, None).unwrap();
        // Default ctor leaves sparse off.
        assert!(!emb.sparse);
        let idx =
            Tensor::from_storage(TensorStorage::cpu(vec![0.0f32, 1.0]), vec![2], false).unwrap();
        let _ = emb.forward(&idx).unwrap();
        assert!(emb.sparse_grad().unwrap().is_none());
    }

    #[test]
    fn sparse_grad_returns_none_before_first_forward() {
        let emb = Embedding::<f32>::new(8, 4, None).unwrap().with_sparse(true);
        // No forward run yet -> no last_indices recorded.
        assert!(emb.sparse_grad().unwrap().is_none());
    }

    #[test]
    fn sparse_grad_emits_only_touched_rows() {
        // Vocabulary 8, dim 4. Touch only rows 1, 3, 5.
        let emb = Embedding::<f32>::new(8, 4, None).unwrap().with_sparse(true);
        let idx = Tensor::from_storage(
            TensorStorage::cpu(vec![1.0f32, 3.0, 5.0, 1.0]),
            vec![4],
            false,
        )
        .unwrap();
        let _out = emb.forward(&idx).unwrap();

        // Manually attach a synthetic dense gradient to weight, simulating
        // post-backward state. The gradient has known per-row values so we
        // can verify slab extraction.
        let grad_data: Vec<f32> = (0..8 * 4).map(|i| i as f32).collect();
        let grad_tensor =
            Tensor::from_storage(TensorStorage::cpu(grad_data), vec![8, 4], false).unwrap();
        emb.weight.tensor().set_grad(Some(grad_tensor)).unwrap();

        let sg = emb.sparse_grad().unwrap().expect("sparse grad");
        // Touched rows are deduped + sorted: {1, 3, 5}.
        assert_eq!(sg.indices(), &[1, 3, 5]);
        assert_eq!(sg.slab_shape(), &[4]);
        // Row 1 of grad: indices 4..8 -> values [4,5,6,7]
        // Row 3 -> [12,13,14,15]
        // Row 5 -> [20,21,22,23]
        assert_eq!(
            sg.values(),
            &[
                4.0, 5.0, 6.0, 7.0, 12.0, 13.0, 14.0, 15.0, 20.0, 21.0, 22.0, 23.0
            ]
        );
    }

    #[test]
    fn sparse_grad_apply_sgd_updates_only_touched_rows() {
        // End-to-end: forward → set synthetic grad → sparse_grad → apply_sgd.
        // Verifies that untouched rows stay at their original values.
        let mut emb = Embedding::<f32>::new(4, 2, None).unwrap().with_sparse(true);
        // Pin weight to known values for a tractable assertion.
        let init: Vec<f32> = (0..4 * 2).map(|i| i as f32 * 10.0).collect();
        emb.weight = Parameter::new(
            Tensor::from_storage(TensorStorage::cpu(init.clone()), vec![4, 2], true).unwrap(),
        );

        let idx =
            Tensor::from_storage(TensorStorage::cpu(vec![0.0f32, 2.0]), vec![2], false).unwrap();
        let _ = emb.forward(&idx).unwrap();

        // Synthetic gradient: each row is its index repeated.
        let grad_vec: Vec<f32> = (0..4_usize)
            .flat_map(|r| vec![r as f32, r as f32])
            .collect();
        let grad_tensor =
            Tensor::from_storage(TensorStorage::cpu(grad_vec), vec![4, 2], false).unwrap();
        emb.weight.tensor().set_grad(Some(grad_tensor)).unwrap();

        let sg = emb.sparse_grad().unwrap().unwrap();
        let mut weight = emb.weight.tensor().clone();
        sg.apply_sgd(&mut weight, 0.5_f32).unwrap();

        // init pattern is `i*10` row-major over [4, 2] → rows
        //   r0=[0, 10], r1=[20, 30], r2=[40, 50], r3=[60, 70].
        // Touched rows: {0, 2} (deduped). Synthetic per-row grad slabs:
        //   r0=[0,0], r1=[1,1], r2=[2,2], r3=[3,3].
        // SparseGrad pulls only touched rows -> {0: [0,0], 2: [2,2]}.
        // apply_sgd(lr=0.5):
        //   r0 -= 0.5 * [0, 0]  → [0, 10]      (no change, grad zero)
        //   r1                  → [20, 30]     (untouched, no update)
        //   r2 -= 0.5 * [2, 2]  → [40-1, 50-1] = [39, 49]
        //   r3                  → [60, 70]     (untouched)
        let updated = weight.data().unwrap().to_vec();
        assert_eq!(updated, vec![0.0, 10.0, 20.0, 30.0, 39.0, 49.0, 60.0, 70.0]);
    }

    // -------------------------------------------------------------------
    // #1445 — max_norm persisted in-place weight renorm (Embedding)
    // -------------------------------------------------------------------
    //
    // Oracle (live torch 2.11.0):
    //   W = [[3,4],[0,0.5],[6,8],[1,1]]
    //   F.embedding(torch.tensor([0,2]), w, max_norm=5.0, norm_type=2.0)
    //   -> w mutated to [[3,4],[0,0.5],[3,4],[1,1]]
    //   row0 norm == 5.0 == max_norm (NOT > so untouched)
    //   row2 norm == 10 > 5 -> scale 5/(10+1e-7) ≈ 0.5 -> [3,4]
    //   2nd forward leaves w unchanged (rows now satisfy max_norm).
    //   See torch/nn/functional.py:2561-2573 (renorm before gather),
    //   aten/src/ATen/native/Embedding.cpp:181-212 (embedding_renorm_cpu_).

    fn pretrained_embedding(rows: &[[f32; 2]]) -> Embedding<f32> {
        let mut data = Vec::with_capacity(rows.len() * 2);
        for r in rows {
            data.extend_from_slice(r);
        }
        let w = Tensor::from_storage(TensorStorage::cpu(data), vec![rows.len(), 2], true).unwrap();
        Embedding::from_pretrained(w, None).unwrap()
    }

    #[test]
    fn test_max_norm_mutates_persisted_weight() {
        let emb = pretrained_embedding(&[[3.0, 4.0], [0.0, 0.5], [6.0, 8.0], [1.0, 1.0]])
            .with_max_norm(5.0)
            .with_norm_type(2.0);

        // Look up rows 0 and 2.
        let idx = index_tensor(&[0.0, 2.0]);
        let out = emb.forward(&idx).unwrap();
        let od = out.data().unwrap();
        // Row 0 untouched ([3,4], norm exactly 5); row 2 clipped to [3,4].
        assert!((od[0] - 3.0).abs() < 1e-4, "out r0[0]={}", od[0]);
        assert!((od[1] - 4.0).abs() < 1e-4, "out r0[1]={}", od[1]);
        assert!((od[2] - 3.0).abs() < 1e-4, "out r2[0]={}", od[2]);
        assert!((od[3] - 4.0).abs() < 1e-4, "out r2[1]={}", od[3]);

        // The PERSISTED weight must be mutated in place, not just the output.
        let w_after = emb.weight.data().unwrap().to_vec();
        // [[3,4],[0,0.5],[3,4],[1,1]] — only the touched, over-norm row 2 moved.
        assert!((w_after[0] - 3.0).abs() < 1e-4); // row0 untouched
        assert!((w_after[1] - 4.0).abs() < 1e-4);
        assert!((w_after[2] - 0.0).abs() < 1e-4); // row1 not indexed, untouched
        assert!((w_after[3] - 0.5).abs() < 1e-4);
        assert!(
            (w_after[4] - 3.0).abs() < 1e-4,
            "row2[0] persisted={}",
            w_after[4]
        );
        assert!(
            (w_after[5] - 4.0).abs() < 1e-4,
            "row2[1] persisted={}",
            w_after[5]
        );
        assert!((w_after[6] - 1.0).abs() < 1e-4); // row3 not indexed
        assert!((w_after[7] - 1.0).abs() < 1e-4);
    }

    #[test]
    fn test_max_norm_second_forward_is_stable() {
        // Forward twice: the first call clips the over-norm row in place; the
        // second call sees the already-clipped weight and is a no-op on it.
        let emb = pretrained_embedding(&[[3.0, 4.0], [0.0, 0.5], [6.0, 8.0], [1.0, 1.0]])
            .with_max_norm(5.0)
            .with_norm_type(2.0);
        let idx = index_tensor(&[0.0, 2.0]);

        let _ = emb.forward(&idx).unwrap();
        let w_after_first = emb.weight.data().unwrap().to_vec();

        let _ = emb.forward(&idx).unwrap();
        let w_after_second = emb.weight.data().unwrap().to_vec();

        // Stable: a second renorm of already-clipped rows changes nothing.
        for (a, b) in w_after_first.iter().zip(w_after_second.iter()) {
            assert!((a - b).abs() < 1e-7, "weight drifted: {a} vs {b}");
        }
        // And the clipped row really did change relative to the original [6,8].
        assert!((w_after_first[4] - 3.0).abs() < 1e-4);
        assert!((w_after_first[5] - 4.0).abs() < 1e-4);
    }

    #[test]
    fn test_max_norm_untouched_rows_not_renormed() {
        // Row 2 ([6,8], norm 10) exceeds max_norm but is NOT indexed this
        // forward; only row 0 is looked up. The persisted weight's row 2 must
        // stay at its original over-norm value (renorm visits only touched
        // rows — Embedding.cpp:198-202).
        let emb = pretrained_embedding(&[[3.0, 4.0], [0.0, 0.5], [6.0, 8.0], [1.0, 1.0]])
            .with_max_norm(5.0);
        let idx = index_tensor(&[0.0]);
        let _ = emb.forward(&idx).unwrap();
        let w = emb.weight.data().unwrap().to_vec();
        assert!(
            (w[4] - 6.0).abs() < 1e-6,
            "row2 should be untouched: {}",
            w[4]
        );
        assert!(
            (w[5] - 8.0).abs() < 1e-6,
            "row2 should be untouched: {}",
            w[5]
        );
    }

    #[test]
    fn test_max_norm_f32_vs_f64_norm_boundary_unchanged() {
        // #1612: the renorm clip DECISION must be made in the weight's native
        // dtype (f32 here), matching torch's `row.norm(norm_type).item<double>()`
        // (Embedding.cpp:202-203) — `at::norm` accumulates in `opmath_type<f32>`
        // == f32, stores back as f32, and only THEN widens to double.
        //
        // #1614 NOTE: the f32 L2 norm is now computed via
        // `ferrotorch_core::simd_reduce::l2_norm_f32_torch` (torch's vectorized
        // last-dim L2 kernel model), not the old scalar `Σ powf(|v|,2)`. The
        // row below was re-selected (live torch 2.11.0+cu130, 2026-05-28) so
        // that BOTH torch AND the SIMD primitive give the exact same f32 norm
        // 151.10968017578125 (bits 0x43171c14), preserving this test's intent
        // (f32-boundary, row unchanged) on a row where ferrotorch matches torch
        // byte-for-byte. (The previous row `[-5.0920777, -9.034002, -99.06734,
        // -8.838612]` — torch f32 norm == 100.0 — is a known ~3% one-ULP
        // residual under the SIMD primitive: torch gives 0x42c80000 but the
        // portable model gives 0x42c80001; that residual is documented in
        // `simd_reduce.rs` / `.design/ferrotorch-core/simd_reduce.md`. Re-rowing
        // here keeps this test pinning the f32-vs-f64 decision, not the residual.)
        //
        // Oracle: torch f32 norm of this row is 151.10968017578125 (== max_norm
        // below), so `F.embedding([0], w, max_norm=151.10968017578125,
        // norm_type=2.0)` leaves the row UNCHANGED (norm > max_norm is false,
        // verified live). Its f64 norm is 151.10968198544464 > the f32 norm,
        // which the OLD f64-accumulate path treated as "exceeds" and wrongly
        // scaled the row down — exactly the #1612 distinction this test pins.
        let row: [f32; 4] = [-92.500_87, -13.270_86, -86.028_92, -81.857_4];
        let emb = {
            let mut data = row.to_vec();
            data.extend_from_slice(&[0.1f32, 0.2, 0.3, 0.4]);
            let w = Tensor::from_storage(TensorStorage::cpu(data), vec![2, 4], true).unwrap();
            Embedding::from_pretrained(w, None)
                .unwrap()
                .with_max_norm(151.109_680_175_781_25)
                .with_norm_type(2.0)
        };

        let idx = index_tensor(&[0.0]);
        let _ = emb.forward(&idx).unwrap();

        // Persisted weight row 0 must be byte-identical to the input — torch's
        // f32 norm == max_norm so it does NOT clip.
        let w = emb.weight.data().unwrap().to_vec();
        for (i, &orig) in row.iter().enumerate() {
            assert_eq!(
                w[i], orig,
                "row[{i}] must stay byte-for-byte unchanged at the f32-norm==max_norm \
                 boundary (torch F.embedding leaves it intact); got {} expected {orig}",
                w[i]
            );
        }
    }

    // -------------------------------------------------------------------
    // #1445 — scale_grad_by_freq divides duplicate-index grad rows
    // -------------------------------------------------------------------
    //
    // Oracle (live torch 2.11.0): indices [1,1,0], grad_output ones[3,2].
    //   scale_grad_by_freq=True  -> grad rows: r0=[1,1], r1=[1,1], r2=[0,0]
    //   scale_grad_by_freq=False -> grad rows: r0=[1,1], r1=[2,2], r2=[0,0]
    //   torch/nn/functional.py:2499-2500 + aten embedding_dense_backward.

    #[test]
    fn test_scale_grad_by_freq_divides_duplicates() {
        let weight =
            Tensor::from_storage(TensorStorage::cpu(vec![0.0f32; 6]), vec![3, 2], true).unwrap();
        let emb = Embedding::from_pretrained(weight, None)
            .unwrap()
            .with_scale_grad_by_freq(true);

        // Index 1 appears twice, index 0 once.
        let idx = index_tensor(&[1.0, 1.0, 0.0]);
        let out = emb.forward(&idx).unwrap();

        let grad_output =
            Tensor::from_storage(TensorStorage::cpu(vec![1.0f32; 6]), vec![3, 2], false).unwrap();
        let grads = out.grad_fn().unwrap().backward(&grad_output).unwrap();
        let gd = grads[0].as_ref().unwrap().data().unwrap();

        // Row 0 (1 occurrence): [1,1]; row 1 (2 occurrences /2): [1,1]; row 2: [0,0].
        assert!((gd[0] - 1.0).abs() < 1e-6, "r0[0]={}", gd[0]);
        assert!((gd[1] - 1.0).abs() < 1e-6);
        assert!(
            (gd[2] - 1.0).abs() < 1e-6,
            "r1[0]={} (should be 1, scaled)",
            gd[2]
        );
        assert!((gd[3] - 1.0).abs() < 1e-6);
        assert!((gd[4] - 0.0).abs() < 1e-6);
        assert!((gd[5] - 0.0).abs() < 1e-6);
    }

    #[test]
    fn test_scale_grad_by_freq_off_accumulates() {
        // Same indices, flag OFF: row 1's grad accumulates to [2,2].
        let weight =
            Tensor::from_storage(TensorStorage::cpu(vec![0.0f32; 6]), vec![3, 2], true).unwrap();
        let emb = Embedding::from_pretrained(weight, None).unwrap();
        let idx = index_tensor(&[1.0, 1.0, 0.0]);
        let out = emb.forward(&idx).unwrap();
        let grad_output =
            Tensor::from_storage(TensorStorage::cpu(vec![1.0f32; 6]), vec![3, 2], false).unwrap();
        let grads = out.grad_fn().unwrap().backward(&grad_output).unwrap();
        let gd = grads[0].as_ref().unwrap().data().unwrap();
        assert!(
            (gd[2] - 2.0).abs() < 1e-6,
            "r1[0]={} (should be 2, unscaled)",
            gd[2]
        );
        assert!((gd[3] - 2.0).abs() < 1e-6);
    }

    // -------------------------------------------------------------------
    // #1445 — EmbeddingBag kwargs (max_norm / padding_idx / include_last_offset)
    // -------------------------------------------------------------------

    fn pretrained_bag(rows: &[Vec<f32>], mode: EmbeddingBagMode) -> EmbeddingBag<f32> {
        let dim = rows[0].len();
        let mut data = Vec::new();
        for r in rows {
            data.extend_from_slice(r);
        }
        let mut bag = EmbeddingBag::<f32>::new(rows.len(), dim, mode).unwrap();
        bag.weight = Parameter::new(
            Tensor::from_storage(TensorStorage::cpu(data), vec![rows.len(), dim], true).unwrap(),
        );
        bag
    }

    #[test]
    fn test_bag_modes_match_torch() {
        // Oracle (torch 2.11.0): W=[[1,2,3],[4,5,6],[7,8,9],[10,11,12]],
        // input [0,1,2,3], offsets [0,2] -> bag0=rows{0,1}, bag1=rows{2,3}.
        //   sum:  [[5,7,9],[17,19,21]]
        //   mean: [[2.5,3.5,4.5],[8.5,9.5,10.5]]
        //   max:  [[4,5,6],[10,11,12]]
        let rows = vec![
            vec![1.0, 2.0, 3.0],
            vec![4.0, 5.0, 6.0],
            vec![7.0, 8.0, 9.0],
            vec![10.0, 11.0, 12.0],
        ];
        let inp = index_tensor(&[0.0, 1.0, 2.0, 3.0]);
        let offs = [0usize, 2];

        let sum = pretrained_bag(&rows, EmbeddingBagMode::Sum)
            .forward_bag(&inp, &offs)
            .unwrap();
        assert_eq!(sum.data().unwrap(), &[5.0, 7.0, 9.0, 17.0, 19.0, 21.0]);

        let mean = pretrained_bag(&rows, EmbeddingBagMode::Mean)
            .forward_bag(&inp, &offs)
            .unwrap();
        assert_eq!(mean.data().unwrap(), &[2.5, 3.5, 4.5, 8.5, 9.5, 10.5]);

        let max = pretrained_bag(&rows, EmbeddingBagMode::Max)
            .forward_bag(&inp, &offs)
            .unwrap();
        assert_eq!(max.data().unwrap(), &[4.0, 5.0, 6.0, 10.0, 11.0, 12.0]);
    }

    #[test]
    fn test_bag_max_norm_mutates_weight() {
        // Oracle (torch 2.11.0): W=[[1,2,3],[4,5,6],[7,8,9],[10,11,12]],
        // input [0,1,2,3], offsets [0,2], mode=sum, max_norm=5.0.
        // row0 norm sqrt(14)≈3.74 < 5 untouched; rows 1,2,3 over-norm scaled.
        // Persisted weight row1 -> ~[2.279212, 2.849014, 3.418817].
        let rows = vec![
            vec![1.0, 2.0, 3.0],
            vec![4.0, 5.0, 6.0],
            vec![7.0, 8.0, 9.0],
            vec![10.0, 11.0, 12.0],
        ];
        let bag = pretrained_bag(&rows, EmbeddingBagMode::Sum)
            .with_max_norm(5.0)
            .with_norm_type(2.0);
        let inp = index_tensor(&[0.0, 1.0, 2.0, 3.0]);
        let offs = [0usize, 2];
        let out = bag.forward_bag(&inp, &offs).unwrap();
        // bag0 = renormed row0 + renormed row1 = [1,2,3] + [2.279212,2.849014,3.418817]
        let od = out.data().unwrap();
        assert!((od[0] - 3.279212).abs() < 1e-4, "bag0[0]={}", od[0]);
        assert!((od[1] - 4.849014).abs() < 1e-4, "bag0[1]={}", od[1]);
        assert!((od[2] - 6.418818).abs() < 1e-4, "bag0[2]={}", od[2]);

        // Persisted weight row0 untouched (under norm), row1 renormed.
        let w = bag.weight.data().unwrap().to_vec();
        assert!((w[0] - 1.0).abs() < 1e-6, "row0 untouched");
        assert!(
            (w[3] - 2.279212).abs() < 1e-4,
            "row1 persisted renorm: {}",
            w[3]
        );
        assert!((w[4] - 2.849014).abs() < 1e-4);
        assert!((w[5] - 3.418817).abs() < 1e-4);
    }

    #[test]
    fn test_bag_padding_idx_excluded_from_reduction() {
        // Oracle (torch 2.11.0): W=[[1,1],[2,2],[4,4],[8,8]], padding_idx=1,
        // single bag input [0,1,2]. idx 1 is padding -> excluded.
        //   mean: ([1,1]+[4,4])/2 = [2.5,2.5]   (divides by non-pad count 2)
        //   sum:  [1,1]+[4,4]      = [5,5]
        let rows = vec![
            vec![1.0, 1.0],
            vec![2.0, 2.0],
            vec![4.0, 4.0],
            vec![8.0, 8.0],
        ];
        let inp = index_tensor(&[0.0, 1.0, 2.0]);
        let offs = [0usize];

        let mut mean = pretrained_bag(&rows, EmbeddingBagMode::Mean);
        mean.padding_idx = Some(1);
        let mo = mean.forward_bag(&inp, &offs).unwrap();
        assert_eq!(mo.data().unwrap(), &[2.5, 2.5]);

        let mut sum = pretrained_bag(&rows, EmbeddingBagMode::Sum);
        sum.padding_idx = Some(1);
        let so = sum.forward_bag(&inp, &offs).unwrap();
        assert_eq!(so.data().unwrap(), &[5.0, 5.0]);
    }

    #[test]
    fn test_bag_include_last_offset() {
        // Oracle (torch 2.11.0): W=[[1,2],[3,4],[5,6],[7,8]], input [0,1,2,3],
        // offsets [0,2,4], include_last_offset=True, mode=sum.
        //   bag0 = row0+row1 = [4,6]; bag1 = row2+row3 = [12,14].
        let rows = vec![
            vec![1.0, 2.0],
            vec![3.0, 4.0],
            vec![5.0, 6.0],
            vec![7.0, 8.0],
        ];
        let bag = pretrained_bag(&rows, EmbeddingBagMode::Sum).with_include_last_offset(true);
        let inp = index_tensor(&[0.0, 1.0, 2.0, 3.0]);
        let offs = [0usize, 2, 4];
        let out = bag.forward_bag(&inp, &offs).unwrap();
        assert_eq!(out.shape(), &[2, 2]);
        assert_eq!(out.data().unwrap(), &[4.0, 6.0, 12.0, 14.0]);
    }

    #[test]
    fn test_bag_max_mode_rejects_sparse_and_scale_grad() {
        let rows = vec![vec![1.0, 2.0], vec![3.0, 4.0]];
        let inp = index_tensor(&[0.0, 1.0]);
        let offs = [0usize];

        let scaled = pretrained_bag(&rows, EmbeddingBagMode::Max).with_scale_grad_by_freq(true);
        assert!(scaled.forward_bag(&inp, &offs).is_err());

        let sparse = pretrained_bag(&rows, EmbeddingBagMode::Max).with_sparse(true);
        assert!(sparse.forward_bag(&inp, &offs).is_err());
    }

    #[test]
    fn test_bag_padding_idx_validated_and_zeroed() {
        // padding_idx out of range rejected; in range -> that row zeroed.
        assert!(EmbeddingBag::<f32>::new_with(3, 2, EmbeddingBagMode::Sum, Some(5)).is_err());

        let bag = EmbeddingBag::<f32>::new_with(4, 3, EmbeddingBagMode::Sum, Some(2)).unwrap();
        let w = bag.weight.data().unwrap();
        let pad_start = 2 * 3;
        for i in 0..3 {
            assert!(
                w[pad_start + i].abs() < 1e-6,
                "padding row not zeroed at {i}: {}",
                w[pad_start + i]
            );
        }
        assert_eq!(bag.padding_idx(), Some(2));
    }

    // -------------------------------------------------------------------
    // #1610 — EmbeddingBag per_sample_weights (sum-mode-only scaling +
    // gradient to BOTH the embedding table AND per_sample_weights).
    // -------------------------------------------------------------------
    //
    // All oracle values constructed from live torch 2.11.0+cu130
    // (2026-05-28) via `torch.nn.functional.embedding_bag(...,
    // per_sample_weights=...)` with `.backward()`:
    //   torch/nn/functional.py:2576-2791 (psw handling + mode='sum'-only
    //   check at :2773-2778; shape check at :2698-2702);
    //   aten/src/ATen/native/EmbeddingBag.cpp:537-543 (forward scale),
    //   :1564-1582 (grad to weight = grad[bag]*psw), :1716-1724
    //   (grad to psw = dot(grad[bag], weight[idx])).

    /// Helper: build a `per_sample_weights` tensor with `requires_grad`.
    fn psw_tensor(w: &[f32]) -> Tensor<f32> {
        Tensor::from_storage(TensorStorage::cpu(w.to_vec()), vec![w.len()], true).unwrap()
    }

    #[test]
    fn test_bag_psw_sum_forward_single_bag() {
        // Oracle (torch 2.11.0): W=[[1,2],[3,4],[5,6]], input [0,1,2],
        // offsets [0], mode=sum, per_sample_weights=[0.5,2.0,1.0].
        //   out = 0.5*[1,2] + 2*[3,4] + 1*[5,6] = [11.5, 15.0]
        let rows = vec![vec![1.0, 2.0], vec![3.0, 4.0], vec![5.0, 6.0]];
        let bag = pretrained_bag(&rows, EmbeddingBagMode::Sum);
        let inp = index_tensor(&[0.0, 1.0, 2.0]);
        let offs = [0usize];
        let psw = psw_tensor(&[0.5, 2.0, 1.0]);
        let out = bag.forward_bag_weighted(&inp, &offs, Some(&psw)).unwrap();
        let od = out.data().unwrap();
        assert!((od[0] - 11.5).abs() < 1e-5, "out[0]={}", od[0]);
        assert!((od[1] - 15.0).abs() < 1e-5, "out[1]={}", od[1]);
    }

    #[test]
    fn test_bag_psw_sum_grad_to_weight_and_psw() {
        // Same setup as the forward test; grad_output = ones[1,2].
        // Oracle (torch 2.11.0):
        //   grad_W   = [[0.5,0.5],[2,2],[1,1]]   (= grad[bag]*psw per row)
        //   grad_psw = [3.0, 7.0, 11.0]          (= dot(grad[bag], weight[idx]))
        let rows = vec![vec![1.0, 2.0], vec![3.0, 4.0], vec![5.0, 6.0]];
        let bag = pretrained_bag(&rows, EmbeddingBagMode::Sum);
        let inp = index_tensor(&[0.0, 1.0, 2.0]);
        let offs = [0usize];
        let psw = psw_tensor(&[0.5, 2.0, 1.0]);
        let out = bag.forward_bag_weighted(&inp, &offs, Some(&psw)).unwrap();

        assert!(out.requires_grad());
        assert_eq!(
            out.grad_fn().unwrap().name(),
            "EmbeddingBagSumWeightedBackward"
        );

        let grad_output =
            Tensor::from_storage(TensorStorage::cpu(vec![1.0f32, 1.0]), vec![1, 2], false).unwrap();
        let grads = out.grad_fn().unwrap().backward(&grad_output).unwrap();

        // grads[0] -> weight (input 0), grads[1] -> psw (input 1).
        let gw = grads[0].as_ref().unwrap().data().unwrap();
        assert_eq!(grads[0].as_ref().unwrap().shape(), &[3, 2]);
        let expect_w = [0.5, 0.5, 2.0, 2.0, 1.0, 1.0];
        for (i, &e) in expect_w.iter().enumerate() {
            assert!((gw[i] - e).abs() < 1e-5, "grad_W[{i}]={} exp {e}", gw[i]);
        }

        let gp = grads[1].as_ref().unwrap().data().unwrap();
        assert_eq!(grads[1].as_ref().unwrap().shape(), &[3]);
        let expect_psw = [3.0, 7.0, 11.0];
        for (i, &e) in expect_psw.iter().enumerate() {
            assert!((gp[i] - e).abs() < 1e-5, "grad_psw[{i}]={} exp {e}", gp[i]);
        }
    }

    #[test]
    fn test_bag_psw_sum_two_bags_offsets() {
        // Oracle (torch 2.11.0): W=[[1,2,3],[4,5,6],[7,8,9],[10,11,12]],
        // input [0,1,2,3], offsets [0,2], mode=sum, psw=[2,0.5,1.5,3].
        //   bag0 = 2*[1,2,3] + 0.5*[4,5,6]   = [4, 6.5, 9]
        //   bag1 = 1.5*[7,8,9] + 3*[10,11,12] = [40.5, 45, 49.5]
        // grad_output = [[1,1,1],[2,2,2]]:
        //   grad_W   = [[2,2,2],[0.5,0.5,0.5],[3,3,3],[6,6,6]]
        //   grad_psw = [6, 15, 48, 66]
        let rows = vec![
            vec![1.0, 2.0, 3.0],
            vec![4.0, 5.0, 6.0],
            vec![7.0, 8.0, 9.0],
            vec![10.0, 11.0, 12.0],
        ];
        let bag = pretrained_bag(&rows, EmbeddingBagMode::Sum);
        let inp = index_tensor(&[0.0, 1.0, 2.0, 3.0]);
        let offs = [0usize, 2];
        let psw = psw_tensor(&[2.0, 0.5, 1.5, 3.0]);
        let out = bag.forward_bag_weighted(&inp, &offs, Some(&psw)).unwrap();
        let od = out.data().unwrap();
        let expect_out = [4.0, 6.5, 9.0, 40.5, 45.0, 49.5];
        for (i, &e) in expect_out.iter().enumerate() {
            assert!((od[i] - e).abs() < 1e-4, "out[{i}]={} exp {e}", od[i]);
        }

        let grad_output = Tensor::from_storage(
            TensorStorage::cpu(vec![1.0f32, 1.0, 1.0, 2.0, 2.0, 2.0]),
            vec![2, 3],
            false,
        )
        .unwrap();
        let grads = out.grad_fn().unwrap().backward(&grad_output).unwrap();
        let gw = grads[0].as_ref().unwrap().data().unwrap();
        let expect_w = [2.0, 2.0, 2.0, 0.5, 0.5, 0.5, 3.0, 3.0, 3.0, 6.0, 6.0, 6.0];
        for (i, &e) in expect_w.iter().enumerate() {
            assert!((gw[i] - e).abs() < 1e-4, "grad_W[{i}]={} exp {e}", gw[i]);
        }
        let gp = grads[1].as_ref().unwrap().data().unwrap();
        let expect_psw = [6.0, 15.0, 48.0, 66.0];
        for (i, &e) in expect_psw.iter().enumerate() {
            assert!((gp[i] - e).abs() < 1e-4, "grad_psw[{i}]={} exp {e}", gp[i]);
        }
    }

    #[test]
    fn test_bag_psw_with_padding_idx() {
        // Oracle (torch 2.11.0): W=[[1,1],[2,2],[4,4],[8,8]], padding_idx=1,
        // single bag input [0,1,2], mode=sum, psw=[2,5,3].
        // idx 1 is padding -> excluded from the bag AND from both grads.
        //   out = 2*[1,1] + 3*[4,4] = [14, 14]
        //   grad_W   (g=ones) = [[2,2],[0,0],[3,3],[0,0]]
        //   grad_psw           = [2.0, 0.0, 8.0]   (padding sample's psw grad 0)
        let rows = vec![
            vec![1.0, 1.0],
            vec![2.0, 2.0],
            vec![4.0, 4.0],
            vec![8.0, 8.0],
        ];
        let mut bag = pretrained_bag(&rows, EmbeddingBagMode::Sum);
        bag.padding_idx = Some(1);
        let inp = index_tensor(&[0.0, 1.0, 2.0]);
        let offs = [0usize];
        let psw = psw_tensor(&[2.0, 5.0, 3.0]);
        let out = bag.forward_bag_weighted(&inp, &offs, Some(&psw)).unwrap();
        let od = out.data().unwrap();
        assert!((od[0] - 14.0).abs() < 1e-5, "out[0]={}", od[0]);
        assert!((od[1] - 14.0).abs() < 1e-5, "out[1]={}", od[1]);

        let grad_output =
            Tensor::from_storage(TensorStorage::cpu(vec![1.0f32, 1.0]), vec![1, 2], false).unwrap();
        let grads = out.grad_fn().unwrap().backward(&grad_output).unwrap();
        let gw = grads[0].as_ref().unwrap().data().unwrap();
        let expect_w = [2.0, 2.0, 0.0, 0.0, 3.0, 3.0, 0.0, 0.0];
        for (i, &e) in expect_w.iter().enumerate() {
            assert!((gw[i] - e).abs() < 1e-5, "grad_W[{i}]={} exp {e}", gw[i]);
        }
        let gp = grads[1].as_ref().unwrap().data().unwrap();
        let expect_psw = [2.0, 0.0, 8.0];
        for (i, &e) in expect_psw.iter().enumerate() {
            assert!((gp[i] - e).abs() < 1e-5, "grad_psw[{i}]={} exp {e}", gp[i]);
        }
    }

    #[test]
    fn test_bag_psw_end_to_end_autograd() {
        // End-to-end via the autograd engine: a scalar loss = sum(out) should
        // populate grads on BOTH the weight parameter and the psw leaf.
        // Reuses the single-bag oracle (grad_output = ones).
        let rows = vec![vec![1.0, 2.0], vec![3.0, 4.0], vec![5.0, 6.0]];
        let bag = pretrained_bag(&rows, EmbeddingBagMode::Sum);
        let inp = index_tensor(&[0.0, 1.0, 2.0]);
        let offs = [0usize];
        let psw = psw_tensor(&[0.5, 2.0, 1.0]);
        let out = bag.forward_bag_weighted(&inp, &offs, Some(&psw)).unwrap();

        // loss = sum(out); SumBackward broadcasts the scalar grad to ones.
        let out_data = out.data().unwrap();
        let total: f32 = out_data.iter().sum();
        #[derive(Debug)]
        struct SumBackward<T: Float> {
            input: Tensor<T>,
        }
        impl<T: Float> GradFn<T> for SumBackward<T> {
            fn backward(
                &self,
                grad_output: &Tensor<T>,
            ) -> FerrotorchResult<Vec<Option<Tensor<T>>>> {
                let go_val = grad_output.data()?[0];
                let grad = vec![go_val; self.input.numel()];
                Ok(vec![Some(Tensor::from_storage(
                    TensorStorage::cpu(grad),
                    self.input.shape().to_vec(),
                    false,
                )?)])
            }
            fn inputs(&self) -> Vec<&Tensor<T>> {
                vec![&self.input]
            }
            fn name(&self) -> &'static str {
                "SumBackward"
            }
        }
        let loss = Tensor::from_operation(
            TensorStorage::cpu(vec![total]),
            vec![],
            Arc::new(SumBackward { input: out.clone() }),
        )
        .unwrap();
        backward(&loss).unwrap();

        // Weight grad = [[0.5,0.5],[2,2],[1,1]].
        let wg = bag.weight.tensor().grad().unwrap().unwrap();
        let wgd = wg.data().unwrap();
        let expect_w = [0.5, 0.5, 2.0, 2.0, 1.0, 1.0];
        for (i, &e) in expect_w.iter().enumerate() {
            assert!((wgd[i] - e).abs() < 1e-5, "W.grad[{i}]={} exp {e}", wgd[i]);
        }
        // psw grad = [3,7,11].
        let pg = psw.grad().unwrap().unwrap();
        let pgd = pg.data().unwrap();
        let expect_psw = [3.0, 7.0, 11.0];
        for (i, &e) in expect_psw.iter().enumerate() {
            assert!(
                (pgd[i] - e).abs() < 1e-5,
                "psw.grad[{i}]={} exp {e}",
                pgd[i]
            );
        }
    }

    #[test]
    fn test_bag_psw_rejects_mean_and_max_modes() {
        // torch raises NotImplementedError with this exact text for non-sum
        // modes (functional.py:2773-2778). ferrotorch returns Err with the
        // byte-identical message.
        let rows = vec![vec![1.0, 2.0], vec![3.0, 4.0]];
        let inp = index_tensor(&[0.0, 1.0]);
        let offs = [0usize];
        let psw = psw_tensor(&[1.0, 1.0]);

        for (mode, mode_str) in [
            (EmbeddingBagMode::Mean, "mean"),
            (EmbeddingBagMode::Max, "max"),
        ] {
            let bag = pretrained_bag(&rows, mode);
            let err = bag
                .forward_bag_weighted(&inp, &offs, Some(&psw))
                .unwrap_err();
            let msg = err.to_string();
            let expected = format!(
                "embedding_bag: per_sample_weights was not None. per_sample_weights is only \
                 supported for mode='sum' (got mode='{mode_str}'). Please open a feature request \
                 on GitHub."
            );
            assert!(
                msg.contains(&expected),
                "mode={mode_str}: error message must contain torch's exact text.\n got: {msg}\n want: {expected}"
            );
        }
    }

    #[test]
    fn test_bag_psw_rejects_shape_mismatch() {
        // psw must have the same shape as input (functional.py:2698-2702).
        let rows = vec![vec![1.0, 2.0], vec![3.0, 4.0]];
        let bag = pretrained_bag(&rows, EmbeddingBagMode::Sum);
        let inp = index_tensor(&[0.0, 1.0]);
        let offs = [0usize];
        let psw = psw_tensor(&[1.0]); // wrong length
        assert!(bag.forward_bag_weighted(&inp, &offs, Some(&psw)).is_err());
    }

    #[test]
    fn test_bag_forward_bag_unweighted_unchanged() {
        // The 2-arg forward_bag (psw=None delegate) must produce the SAME
        // unweighted sum as before, with NO grad attached.
        let rows = vec![vec![1.0, 2.0], vec![3.0, 4.0], vec![5.0, 6.0]];
        let bag = pretrained_bag(&rows, EmbeddingBagMode::Sum);
        let inp = index_tensor(&[0.0, 1.0, 2.0]);
        let offs = [0usize];
        let out = bag.forward_bag(&inp, &offs).unwrap();
        // sum = [1+3+5, 2+4+6] = [9, 12]
        assert_eq!(out.data().unwrap(), &[9.0, 12.0]);
        assert!(!out.requires_grad());
    }
}