shrew-core 0.1.0

Core tensor, dtype, shape, layout, backend traits, and autograd for Shrew
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
use std::sync::{Arc, RwLock};

use crate::backend::{Backend, BinaryOp, CmpOp, ReduceOp, UnaryOp};
use crate::dtype::DType;
use crate::error::{Error, Result};
use crate::layout::Layout;
use crate::op::{Op, TensorId};
use crate::shape::Shape;

// Tensor — The fundamental data structure
//
// A Tensor is an n-dimensional array of numbers, the building block of all
// neural network computations. Like in PyTorch, our Tensor:
//
//   1. Holds data on a specific device (CPU, GPU)
//   2. Has a shape (e.g., [batch, channels, height, width])
//   3. Has a dtype (f32, f64, etc.)
//   4. Optionally tracks the operation that created it (for autograd)
//
// ARCHITECTURE:
//
//   Tensor<B: Backend> is generic over the backend. This means:
//     - Tensor<CpuBackend> holds data in CPU memory
//     - Tensor<CudaBackend> holds data in GPU memory
//     - Operations are dispatched via the Backend trait
//
// MEMORY MODEL:
//
//   The inner data is wrapped in Arc (atomic reference counting).
//   This means cloning a Tensor is cheap (just increments a counter).
//   Multiple tensors can share the same underlying storage (views).
//
//   Storage is behind Arc<RwLock<Storage>> so that:
//   - Multiple tensors can read concurrently
//   - In-place ops can write when there's only one reference
//
// WHY Arc + inner struct?
//
//   We separate Tensor (the handle) from TensorInner (the data) so that:
//   - Cloning Tensor is O(1) — just copies the Arc pointer
//   - The autograd graph can hold TensorIds without owning data
//   - Views (transpose, narrow) share the same storage via Arc<RwLock<>>

/// Inner data of a tensor, shared via Arc.
struct TensorInner<B: Backend> {
    /// Unique identifier for this tensor (used in autograd graph).
    id: TensorId,
    /// The raw data stored on the backend's device.
    storage: Arc<RwLock<B::Storage>>,
    /// Memory layout: shape + strides + offset.
    layout: Layout,
    /// Data type of the elements.
    dtype: DType,
    /// The device this tensor lives on.
    device: B::Device,
    /// The operation that created this tensor (for autograd).
    /// None for leaf tensors (inputs, parameters).
    op: Op<B>,
    /// Whether this tensor is a trainable variable.
    /// Only variables accumulate gradients during backward().
    is_variable: bool,
}

/// An n-dimensional array of numbers on a specific backend.
///
/// Tensors are the fundamental data type in Shrew. All neural network
/// operations accept and return tensors.
///
/// # Type Parameter
/// - `B: Backend` — the compute backend (e.g., `CpuBackend`, `CudaBackend`)
///
/// # Example
/// ```ignore
/// use shrew_core::Tensor;
/// use shrew_cpu::CpuBackend;
///
/// let a = Tensor::<CpuBackend>::from_slice(&[1.0, 2.0, 3.0, 4.0], (2, 2))?;
/// let b = Tensor::<CpuBackend>::ones((2, 2), DType::F32, &CpuDevice)?;
/// let c = a.add(&b)?;
/// ```
pub struct Tensor<B: Backend> {
    inner: Arc<TensorInner<B>>,
}

// Manual Clone: Arc::clone is cheap (just increment refcount).
impl<B: Backend> Clone for Tensor<B> {
    fn clone(&self) -> Self {
        Tensor {
            inner: Arc::clone(&self.inner),
        }
    }
}

impl<B: Backend> std::fmt::Debug for Tensor<B> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "Tensor(id={:?}, shape={}, dtype={}, device={:?})",
            self.inner.id,
            self.inner.layout.shape(),
            self.inner.dtype,
            self.inner.device,
        )
    }
}

impl<B: Backend> Tensor<B> {
    // Internal constructors

    /// Create a tensor from existing storage and layout.
    pub(crate) fn from_storage(
        storage: B::Storage,
        layout: Layout,
        dtype: DType,
        device: B::Device,
        op: Op<B>,
    ) -> Self {
        Tensor {
            inner: Arc::new(TensorInner {
                id: TensorId::new(),
                storage: Arc::new(RwLock::new(storage)),
                layout,
                dtype,
                device,
                op,
                is_variable: false,
            }),
        }
    }

    /// Create a view tensor sharing the same storage but with a different layout.
    fn view_with_layout(&self, layout: Layout, op: Op<B>) -> Self {
        Tensor {
            inner: Arc::new(TensorInner {
                id: TensorId::new(),
                storage: Arc::clone(&self.inner.storage),
                layout,
                dtype: self.inner.dtype,
                device: self.inner.device.clone(),
                op,
                is_variable: false,
            }),
        }
    }

    // Accessors

    /// Unique tensor ID.
    pub fn id(&self) -> TensorId {
        self.inner.id
    }

    /// The shape of this tensor.
    pub fn shape(&self) -> &Shape {
        self.inner.layout.shape()
    }

    /// The dimensions as a slice (shortcut for shape().dims()).
    pub fn dims(&self) -> &[usize] {
        self.inner.layout.dims()
    }

    /// Number of dimensions (rank).
    pub fn rank(&self) -> usize {
        self.inner.layout.rank()
    }

    /// Total number of elements.
    pub fn elem_count(&self) -> usize {
        self.inner.layout.elem_count()
    }

    /// Data type of the elements.
    pub fn dtype(&self) -> DType {
        self.inner.dtype
    }

    /// The device this tensor is on.
    pub fn device(&self) -> &B::Device {
        &self.inner.device
    }

    /// The memory layout (shape + strides + offset).
    pub fn layout(&self) -> &Layout {
        &self.inner.layout
    }

    /// Whether this tensor is contiguous in memory.
    pub fn is_contiguous(&self) -> bool {
        self.inner.layout.is_contiguous()
    }

    /// Whether this tensor tracks gradients.
    pub fn is_variable(&self) -> bool {
        self.inner.is_variable
    }

    /// Access the underlying storage (read lock).
    pub fn storage(&self) -> std::sync::RwLockReadGuard<'_, B::Storage> {
        self.inner.storage.read().expect("storage lock poisoned")
    }

    /// Try to acquire a read lock on storage, returning an error instead of panicking.
    fn read_storage(&self) -> Result<std::sync::RwLockReadGuard<'_, B::Storage>> {
        self.inner
            .storage
            .read()
            .map_err(|_| Error::msg("storage lock poisoned"))
    }

    /// Try to acquire a write lock on storage, returning an error instead of panicking.
    fn write_storage(&self) -> Result<std::sync::RwLockWriteGuard<'_, B::Storage>> {
        self.inner
            .storage
            .write()
            .map_err(|_| Error::msg("storage lock poisoned"))
    }

    /// The op that created this tensor.
    pub fn op(&self) -> &Op<B> {
        &self.inner.op
    }

    // In-place mutation

    /// Update the underlying storage data in place.
    ///
    /// This writes `new_data` directly into the existing `Arc<RwLock<Storage>>`,
    /// so any other tensor sharing this storage (e.g., a clone held by a Module)
    /// will also see the updated values.
    ///
    /// This is the mechanism that makes optimizer parameter updates visible to
    /// model layers without needing to re-assign parameters.
    ///
    /// # Safety (logical)
    /// The new data must have the same number of elements and dtype as the
    /// current storage. The shape is not changed.
    pub fn update_data_inplace(&self, new_data: &[f64]) -> Result<()> {
        let expected = self.elem_count();
        if new_data.len() != expected {
            return Err(Error::msg(format!(
                "update_data_inplace: expected {} elements, got {}",
                expected,
                new_data.len()
            )));
        }
        let new_storage = B::from_f64_slice(new_data, self.dtype(), self.device())?;
        let mut guard = self.write_storage()?;
        *guard = new_storage;
        Ok(())
    }

    // Creation methods

    /// Create a tensor filled with zeros.
    pub fn zeros(shape: impl Into<Shape>, dtype: DType, device: &B::Device) -> Result<Self> {
        let shape = shape.into();
        let layout = Layout::contiguous(shape.clone());
        let storage = B::zeros(&shape, dtype, device)?;
        Ok(Self::from_storage(
            storage,
            layout,
            dtype,
            device.clone(),
            Op::None,
        ))
    }

    /// Create a tensor filled with ones.
    pub fn ones(shape: impl Into<Shape>, dtype: DType, device: &B::Device) -> Result<Self> {
        let shape = shape.into();
        let layout = Layout::contiguous(shape.clone());
        let storage = B::ones(&shape, dtype, device)?;
        Ok(Self::from_storage(
            storage,
            layout,
            dtype,
            device.clone(),
            Op::None,
        ))
    }

    /// Create a tensor filled with a constant value.
    pub fn full(
        shape: impl Into<Shape>,
        val: f64,
        dtype: DType,
        device: &B::Device,
    ) -> Result<Self> {
        let shape = shape.into();
        let layout = Layout::contiguous(shape.clone());
        let storage = B::full(&shape, val, dtype, device)?;
        Ok(Self::from_storage(
            storage,
            layout,
            dtype,
            device.clone(),
            Op::None,
        ))
    }

    /// Create a tensor from a flat slice of f64 values.
    /// The data is converted to the specified dtype.
    pub fn from_f64_slice(
        data: &[f64],
        shape: impl Into<Shape>,
        dtype: DType,
        device: &B::Device,
    ) -> Result<Self> {
        let shape = shape.into();
        if data.len() != shape.elem_count() {
            return Err(Error::ElementCountMismatch {
                shape: shape.clone(),
                expected: shape.elem_count(),
                got: data.len(),
            });
        }
        let layout = Layout::contiguous(shape);
        let storage = B::from_f64_slice(data, dtype, device)?;
        Ok(Self::from_storage(
            storage,
            layout,
            dtype,
            device.clone(),
            Op::None,
        ))
    }

    /// Create a tensor with random uniform values in [0, 1).
    pub fn rand(shape: impl Into<Shape>, dtype: DType, device: &B::Device) -> Result<Self> {
        let shape = shape.into();
        let layout = Layout::contiguous(shape.clone());
        let storage = B::rand_uniform(&shape, dtype, device)?;
        Ok(Self::from_storage(
            storage,
            layout,
            dtype,
            device.clone(),
            Op::None,
        ))
    }

    /// Create a tensor with random normal values (mean=0, std=1).
    pub fn randn(shape: impl Into<Shape>, dtype: DType, device: &B::Device) -> Result<Self> {
        let shape = shape.into();
        let layout = Layout::contiguous(shape.clone());
        let storage = B::rand_normal(&shape, dtype, device)?;
        Ok(Self::from_storage(
            storage,
            layout,
            dtype,
            device.clone(),
            Op::None,
        ))
    }

    /// Create a 1-D tensor with `steps` evenly spaced values from `start` to `end` (inclusive).
    ///
    /// ```ignore
    /// let t = Tensor::linspace(0.0, 1.0, 5, DType::F64, &dev)?;
    /// // => [0.0, 0.25, 0.5, 0.75, 1.0]
    /// ```
    pub fn linspace(
        start: f64,
        end: f64,
        steps: usize,
        dtype: DType,
        device: &B::Device,
    ) -> Result<Self> {
        if steps == 0 {
            return Err(Error::msg("linspace requires steps >= 1"));
        }
        if steps == 1 {
            return Self::from_f64_slice(&[start], 1, dtype, device);
        }
        let step = (end - start) / (steps as f64 - 1.0);
        let data: Vec<f64> = (0..steps).map(|i| start + step * i as f64).collect();
        Self::from_f64_slice(&data, steps, dtype, device)
    }

    /// Create an identity matrix of size `n × n`.
    ///
    /// ```ignore
    /// let I = Tensor::eye(3, DType::F64, &dev)?;
    /// // [[1, 0, 0],
    /// //  [0, 1, 0],
    /// //  [0, 0, 1]]
    /// ```
    pub fn eye(n: usize, dtype: DType, device: &B::Device) -> Result<Self> {
        let mut data = vec![0.0f64; n * n];
        for i in 0..n {
            data[i * n + i] = 1.0;
        }
        Self::from_f64_slice(&data, (n, n), dtype, device)
    }

    /// Create a tensor of zeros with the same shape, dtype, and device as `other`.
    pub fn zeros_like(other: &Self) -> Result<Self> {
        Self::zeros(other.shape().clone(), other.dtype(), other.device())
    }

    /// Create a tensor of ones with the same shape, dtype, and device as `other`.
    pub fn ones_like(other: &Self) -> Result<Self> {
        Self::ones(other.shape().clone(), other.dtype(), other.device())
    }

    /// Create a tensor filled with `val`, with the same shape, dtype, and device as `other`.
    pub fn full_like(other: &Self, val: f64) -> Result<Self> {
        Self::full(other.shape().clone(), val, other.dtype(), other.device())
    }

    /// Mark this tensor as a variable (trainable parameter).
    /// Variables accumulate gradients during backward().
    pub fn set_variable(self) -> Self {
        Tensor {
            inner: Arc::new(TensorInner {
                id: self.inner.id,
                storage: Arc::clone(&self.inner.storage),
                layout: self.inner.layout.clone(),
                dtype: self.inner.dtype,
                device: self.inner.device.clone(),
                op: self.inner.op.clone(),
                is_variable: true,
            }),
        }
    }

    // Shape manipulation (these create views, no data copy)

    /// Transpose two dimensions (no data copy).
    pub fn transpose(&self, dim0: usize, dim1: usize) -> Result<Self> {
        let new_layout = self.inner.layout.transpose(dim0, dim1)?;
        let op = Op::Transpose {
            input: self.clone(),
            dim0,
            dim1,
        };
        Ok(self.view_with_layout(new_layout, op))
    }

    /// Transpose a 2D matrix (shorthand for transpose(0, 1)).
    pub fn t(&self) -> Result<Self> {
        if self.rank() != 2 {
            return Err(Error::RankMismatch {
                expected: 2,
                got: self.rank(),
            });
        }
        self.transpose(0, 1)
    }

    /// Narrow (slice) along a dimension.
    pub fn narrow(&self, dim: usize, start: usize, len: usize) -> Result<Self> {
        let new_layout = self.inner.layout.narrow(dim, start, len)?;
        let op = Op::Narrow {
            input: self.clone(),
            dim,
            start,
            len,
        };
        Ok(self.view_with_layout(new_layout, op))
    }

    /// Reshape to a new shape. The new shape must have the same total elements.
    /// If the tensor is not contiguous, it will be made contiguous first.
    pub fn reshape(&self, new_shape: impl Into<Shape>) -> Result<Self> {
        let new_shape = new_shape.into();
        let current_count = self.elem_count();
        let new_count = new_shape.elem_count();
        if current_count != new_count {
            return Err(Error::ReshapeElementMismatch {
                src: current_count,
                dst: new_count,
                dst_shape: new_shape,
            });
        }
        // If not contiguous, make a contiguous copy first
        let tensor = if self.is_contiguous() {
            self.clone()
        } else {
            self.contiguous()?
        };
        let src_shape = tensor.shape().clone();
        let new_layout = Layout::contiguous(new_shape);
        let op = Op::Reshape {
            input: tensor.clone(),
            src_shape,
        };
        Ok(tensor.view_with_layout(new_layout, op))
    }

    /// Ensure the tensor is contiguous in memory.
    /// If already contiguous, returns a clone (cheap Arc copy).
    /// Otherwise, copies the data into a new contiguous storage.
    pub fn contiguous(&self) -> Result<Self> {
        if self.is_contiguous() {
            return Ok(self.clone());
        }
        let storage = self.read_storage()?;
        let new_storage = B::to_contiguous(&storage, &self.inner.layout)?;
        let new_layout = Layout::contiguous(self.shape().clone());
        Ok(Self::from_storage(
            new_storage,
            new_layout,
            self.inner.dtype,
            self.inner.device.clone(),
            Op::Contiguous {
                input: self.clone(),
            },
        ))
    }

    /// Add a dimension of size 1 at the given position.
    /// unsqueeze(0) on [3, 4] → [1, 3, 4]
    /// unsqueeze(2) on [3, 4] → [3, 4, 1]
    pub fn unsqueeze(&self, dim: usize) -> Result<Self> {
        let rank = self.rank();
        if dim > rank {
            return Err(Error::DimOutOfRange {
                dim,
                rank: rank + 1,
            });
        }
        let mut new_dims = self.dims().to_vec();
        let mut new_strides = self.layout().strides().to_vec();
        // The stride for a size-1 dim doesn't matter (you never move along it),
        // but convention is to use the stride of the next dimension (or 1 if last).
        let stride_val = if dim < rank { new_strides[dim] } else { 1 };
        new_dims.insert(dim, 1);
        new_strides.insert(dim, stride_val);
        let new_layout = Layout::new(Shape::new(new_dims), new_strides, self.layout().offset());
        let op = Op::Reshape {
            input: self.clone(),
            src_shape: self.shape().clone(),
        };
        Ok(self.view_with_layout(new_layout, op))
    }

    /// Remove dimensions of size 1.
    /// squeeze on [1, 3, 1, 4] → [3, 4]
    pub fn squeeze_all(&self) -> Self {
        let new_dims: Vec<usize> = self.dims().iter().copied().filter(|&d| d != 1).collect();
        let new_strides: Vec<usize> = self
            .dims()
            .iter()
            .zip(self.layout().strides().iter())
            .filter(|(&d, _)| d != 1)
            .map(|(_, &s)| s)
            .collect();
        let new_layout = Layout::new(
            Shape::new(if new_dims.is_empty() {
                vec![]
            } else {
                new_dims
            }),
            new_strides,
            self.layout().offset(),
        );
        let op = Op::Reshape {
            input: self.clone(),
            src_shape: self.shape().clone(),
        };
        self.view_with_layout(new_layout, op)
    }

    /// Remove a specific dimension of size 1.
    ///
    /// squeeze(1) on [3, 1, 4] → [3, 4]
    ///
    /// Returns an error if the specified dimension is not size 1.
    pub fn squeeze(&self, dim: usize) -> Result<Self> {
        let rank = self.rank();
        if dim >= rank {
            return Err(Error::DimOutOfRange { dim, rank });
        }
        if self.dims()[dim] != 1 {
            return Err(Error::msg(format!(
                "squeeze: dimension {} has size {}, expected 1",
                dim,
                self.dims()[dim]
            )));
        }
        let mut new_dims = self.dims().to_vec();
        let mut new_strides = self.layout().strides().to_vec();
        new_dims.remove(dim);
        new_strides.remove(dim);
        let new_layout = Layout::new(
            Shape::new(if new_dims.is_empty() {
                vec![]
            } else {
                new_dims
            }),
            new_strides,
            self.layout().offset(),
        );
        let op = Op::Reshape {
            input: self.clone(),
            src_shape: self.shape().clone(),
        };
        Ok(self.view_with_layout(new_layout, op))
    }

    /// Permute the dimensions of this tensor.
    ///
    /// permute(&[2, 0, 1]) on [A, B, C] → [C, A, B]
    ///
    /// This is a generalization of transpose to arbitrary dimension orderings.
    /// No data copy — just changes strides.
    pub fn permute(&self, dims: &[usize]) -> Result<Self> {
        let rank = self.rank();
        if dims.len() != rank {
            return Err(Error::msg(format!(
                "permute: expected {} dimensions, got {}",
                rank,
                dims.len()
            )));
        }
        // Check for duplicates and out-of-range
        let mut seen = vec![false; rank];
        for &d in dims {
            if d >= rank {
                return Err(Error::DimOutOfRange { dim: d, rank });
            }
            if seen[d] {
                return Err(Error::msg(format!("permute: duplicate dimension {}", d)));
            }
            seen[d] = true;
        }

        let old_dims = self.dims();
        let old_strides = self.layout().strides();
        let new_dims: Vec<usize> = dims.iter().map(|&d| old_dims[d]).collect();
        let new_strides: Vec<usize> = dims.iter().map(|&d| old_strides[d]).collect();
        let new_layout = Layout::new(Shape::new(new_dims), new_strides, self.layout().offset());
        // Use a chain of transposes conceptually, but represent as reshape
        // for backward compatibility. The backward pass handles reshapes correctly.
        let op = Op::Reshape {
            input: self.clone(),
            src_shape: self.shape().clone(),
        };
        Ok(self.view_with_layout(new_layout, op))
    }

    /// Cumulative sum along dimension `dim`.
    ///
    /// ```ignore
    /// // [1, 2, 3] → [1, 3, 6]
    /// let y = x.cumsum(0)?;
    /// ```
    pub fn cumsum(&self, dim: usize) -> Result<Self> {
        let rank = self.rank();
        if dim >= rank {
            return Err(Error::DimOutOfRange { dim, rank });
        }
        let t = self.contiguous()?;
        let data = t.to_f64_vec()?;
        let shape = t.shape().clone();
        let dims = shape.dims();
        let mut out = data.clone();

        // Compute strides for iteration
        let inner: usize = dims[dim + 1..].iter().product();
        let outer: usize = dims[..dim].iter().product();
        let dim_size = dims[dim];

        for o in 0..outer {
            for i in 0..inner {
                for d in 1..dim_size {
                    let idx = (o * dim_size + d) * inner + i;
                    let prev = (o * dim_size + d - 1) * inner + i;
                    out[idx] += out[prev];
                }
            }
        }

        Self::from_f64_slice(&out, shape, t.dtype(), t.device())
    }

    /// Sort along a dimension. Returns `(sorted_values, sorted_indices)`.
    ///
    /// ```ignore
    /// let (vals, idxs) = x.sort(0, false)?; // ascending along dim 0
    /// ```
    pub fn sort(&self, dim: usize, descending: bool) -> Result<(Self, Self)> {
        let rank = self.rank();
        if dim >= rank {
            return Err(Error::DimOutOfRange { dim, rank });
        }
        let t = self.contiguous()?;
        let data = t.to_f64_vec()?;
        let shape = t.shape().clone();
        let dims = shape.dims();
        let dim_size = dims[dim];
        let inner: usize = dims[dim + 1..].iter().product();
        let outer: usize = dims[..dim].iter().product();

        let mut sorted_data = data.clone();
        let mut indices = vec![0.0f64; data.len()];

        for o in 0..outer {
            for i in 0..inner {
                // Extract the slice along dim
                let mut slice: Vec<(f64, usize)> = (0..dim_size)
                    .map(|d| {
                        let idx = (o * dim_size + d) * inner + i;
                        (data[idx], d)
                    })
                    .collect();

                if descending {
                    slice
                        .sort_by(|a, b| b.0.partial_cmp(&a.0).unwrap_or(std::cmp::Ordering::Equal));
                } else {
                    slice
                        .sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap_or(std::cmp::Ordering::Equal));
                }

                for (d, (val, orig_idx)) in slice.into_iter().enumerate() {
                    let idx = (o * dim_size + d) * inner + i;
                    sorted_data[idx] = val;
                    indices[idx] = orig_idx as f64;
                }
            }
        }

        let vals = Self::from_f64_slice(&sorted_data, shape.clone(), t.dtype(), t.device())?;
        let idxs = Self::from_f64_slice(&indices, shape, t.dtype(), t.device())?;
        Ok((vals, idxs))
    }

    /// Argsort: returns indices that would sort the tensor along `dim`.
    ///
    /// ```ignore
    /// let indices = x.argsort(0, false)?; // ascending
    /// ```
    pub fn argsort(&self, dim: usize, descending: bool) -> Result<Self> {
        let (_, indices) = self.sort(dim, descending)?;
        Ok(indices)
    }

    // Arithmetic operations

    /// Element-wise addition: self + rhs.
    pub fn add(&self, rhs: &Self) -> Result<Self> {
        self.binary_op(rhs, BinaryOp::Add)
    }

    /// Element-wise subtraction: self - rhs.
    pub fn sub(&self, rhs: &Self) -> Result<Self> {
        self.binary_op(rhs, BinaryOp::Sub)
    }

    /// Element-wise multiplication: self * rhs.
    pub fn mul(&self, rhs: &Self) -> Result<Self> {
        self.binary_op(rhs, BinaryOp::Mul)
    }

    /// Element-wise division: self / rhs.
    pub fn div(&self, rhs: &Self) -> Result<Self> {
        self.binary_op(rhs, BinaryOp::Div)
    }

    /// Generic binary operation dispatch.
    fn binary_op(&self, rhs: &Self, op: BinaryOp) -> Result<Self> {
        if self.dtype() != rhs.dtype() {
            return Err(Error::DTypeMismatch {
                expected: self.dtype(),
                got: rhs.dtype(),
            });
        }
        let storage_lhs = self.read_storage()?;
        let storage_rhs = rhs.read_storage()?;
        let result = B::binary_op(
            op,
            &storage_lhs,
            &self.inner.layout,
            &storage_rhs,
            &rhs.inner.layout,
        )?;
        // Compute broadcast output shape
        let result_shape = Shape::broadcast_shape(self.shape(), rhs.shape())?;
        let result_layout = Layout::contiguous(result_shape);
        let result_op = Op::Binary {
            lhs: self.clone(),
            rhs: rhs.clone(),
            op,
        };
        Ok(Self::from_storage(
            result,
            result_layout,
            self.inner.dtype,
            self.inner.device.clone(),
            result_op,
        ))
    }

    // Comparison operations

    /// Element-wise equal: self == rhs. Returns a U8 tensor (0 or 1).
    pub fn eq(&self, rhs: &Self) -> Result<Self> {
        self.cmp_op(rhs, CmpOp::Eq)
    }

    /// Element-wise not-equal: self != rhs. Returns a U8 tensor (0 or 1).
    pub fn ne(&self, rhs: &Self) -> Result<Self> {
        self.cmp_op(rhs, CmpOp::Ne)
    }

    /// Element-wise greater-than: self > rhs. Returns a U8 tensor (0 or 1).
    pub fn gt(&self, rhs: &Self) -> Result<Self> {
        self.cmp_op(rhs, CmpOp::Gt)
    }

    /// Element-wise greater-or-equal: self >= rhs. Returns a U8 tensor (0 or 1).
    pub fn ge(&self, rhs: &Self) -> Result<Self> {
        self.cmp_op(rhs, CmpOp::Ge)
    }

    /// Element-wise less-than: self < rhs. Returns a U8 tensor (0 or 1).
    pub fn lt(&self, rhs: &Self) -> Result<Self> {
        self.cmp_op(rhs, CmpOp::Lt)
    }

    /// Element-wise less-or-equal: self <= rhs. Returns a U8 tensor (0 or 1).
    pub fn le(&self, rhs: &Self) -> Result<Self> {
        self.cmp_op(rhs, CmpOp::Le)
    }

    /// Generic comparison operation dispatch.
    /// Produces a U8-dtype tensor (non-differentiable, Op::None).
    fn cmp_op(&self, rhs: &Self, op: CmpOp) -> Result<Self> {
        let storage_lhs = self.read_storage()?;
        let storage_rhs = rhs.read_storage()?;
        let result = B::cmp_op(
            op,
            &storage_lhs,
            &self.inner.layout,
            &storage_rhs,
            &rhs.inner.layout,
        )?;
        let result_shape = Shape::broadcast_shape(self.shape(), rhs.shape())?;
        let result_layout = Layout::contiguous(result_shape);
        // Comparisons are non-differentiable — no autograd tracking
        Ok(Self::from_storage(
            result,
            result_layout,
            DType::U8,
            self.inner.device.clone(),
            Op::None,
        ))
    }

    // Unary operations

    /// Element-wise negation: -self.
    pub fn neg(&self) -> Result<Self> {
        self.unary_op(UnaryOp::Neg)
    }

    /// Element-wise absolute value.
    pub fn abs(&self) -> Result<Self> {
        self.unary_op(UnaryOp::Abs)
    }

    /// Element-wise exponential: e^x.
    pub fn exp(&self) -> Result<Self> {
        self.unary_op(UnaryOp::Exp)
    }

    /// Element-wise natural logarithm.
    pub fn log(&self) -> Result<Self> {
        self.unary_op(UnaryOp::Log)
    }

    /// Element-wise square root.
    pub fn sqrt(&self) -> Result<Self> {
        self.unary_op(UnaryOp::Sqrt)
    }

    /// Element-wise square: x².
    pub fn square(&self) -> Result<Self> {
        self.unary_op(UnaryOp::Square)
    }

    /// ReLU activation: max(0, x).
    pub fn relu(&self) -> Result<Self> {
        self.unary_op(UnaryOp::Relu)
    }

    /// Sigmoid activation: 1 / (1 + e^(-x)).
    pub fn sigmoid(&self) -> Result<Self> {
        self.unary_op(UnaryOp::Sigmoid)
    }

    /// Tanh activation.
    pub fn tanh(&self) -> Result<Self> {
        self.unary_op(UnaryOp::Tanh)
    }

    /// GELU activation (Gaussian Error Linear Unit).
    pub fn gelu(&self) -> Result<Self> {
        self.unary_op(UnaryOp::Gelu)
    }

    /// SiLU / Swish activation: x * sigmoid(x).
    pub fn silu(&self) -> Result<Self> {
        self.unary_op(UnaryOp::Silu)
    }

    /// Element-wise sine.
    pub fn sin(&self) -> Result<Self> {
        self.unary_op(UnaryOp::Sin)
    }

    /// Element-wise cosine.
    pub fn cos(&self) -> Result<Self> {
        self.unary_op(UnaryOp::Cos)
    }

    /// Element-wise floor: largest integer ≤ x.
    pub fn floor(&self) -> Result<Self> {
        self.unary_op(UnaryOp::Floor)
    }

    /// Element-wise ceiling: smallest integer ≥ x.
    pub fn ceil(&self) -> Result<Self> {
        self.unary_op(UnaryOp::Ceil)
    }

    /// Element-wise round to nearest integer.
    pub fn round(&self) -> Result<Self> {
        self.unary_op(UnaryOp::Round)
    }

    /// Element-wise power: self^exponent.
    pub fn powf(&self, exponent: f64) -> Result<Self> {
        let storage = self.read_storage()?;
        let result = B::powf(&storage, &self.inner.layout, exponent)?;
        let result_layout = Layout::contiguous(self.shape().clone());
        let result_op = Op::Powf {
            input: self.clone(),
            exponent,
        };
        Ok(Self::from_storage(
            result,
            result_layout,
            self.inner.dtype,
            self.inner.device.clone(),
            result_op,
        ))
    }

    /// Element-wise clamp to [min, max].
    pub fn clamp(&self, min: f64, max: f64) -> Result<Self> {
        let storage = self.read_storage()?;
        let result = B::clamp(&storage, &self.inner.layout, min, max)?;
        let result_layout = Layout::contiguous(self.shape().clone());
        let result_op = Op::Clamp {
            input: self.clone(),
            min,
            max,
        };
        Ok(Self::from_storage(
            result,
            result_layout,
            self.inner.dtype,
            self.inner.device.clone(),
            result_op,
        ))
    }

    /// Conditional select: result[i] = if mask[i] != 0 { on_true[i] } else { on_false[i] }.
    ///
    /// `mask` is typically a U8 tensor from comparison ops.
    /// `on_true` and `on_false` must have the same shape and dtype.
    pub fn where_cond(mask: &Self, on_true: &Self, on_false: &Self) -> Result<Self> {
        let mask_s = mask.read_storage()?;
        let true_s = on_true.read_storage()?;
        let false_s = on_false.read_storage()?;
        let result = B::where_cond(
            &mask_s,
            &mask.inner.layout,
            &true_s,
            &on_true.inner.layout,
            &false_s,
            &on_false.inner.layout,
        )?;
        let result_layout = Layout::contiguous(on_true.shape().clone());
        let result_op = Op::WhereCond {
            mask: mask.clone(),
            on_true: on_true.clone(),
            on_false: on_false.clone(),
        };
        Ok(Self::from_storage(
            result,
            result_layout,
            on_true.inner.dtype,
            on_true.inner.device.clone(),
            result_op,
        ))
    }

    /// Gather elements along `dim` using an index tensor.
    ///
    /// `output[i][j][k] = input[index[i][j][k]][j][k]`  (when dim=0)
    ///
    /// The index tensor must have the same number of dimensions as self.
    /// The output has the same shape as the index tensor.
    pub fn gather(&self, dim: usize, index: &Self) -> Result<Self> {
        let input_s = self.read_storage()?;
        let index_s = index.read_storage()?;
        let result = B::gather(
            &input_s,
            &self.inner.layout,
            &index_s,
            &index.inner.layout,
            dim,
        )?;
        let result_layout = Layout::contiguous(index.shape().clone());
        let result_op = Op::Gather {
            input: self.clone(),
            index: index.clone(),
            dim,
        };
        Ok(Self::from_storage(
            result,
            result_layout,
            self.inner.dtype,
            self.inner.device.clone(),
            result_op,
        ))
    }

    /// Fill elements where `mask != 0` with `value`, keeping other elements.
    ///
    /// `result[i] = if mask[i] != 0 { value } else { self[i] }`
    ///
    /// This is implemented via `where_cond` so autograd is automatic.
    pub fn masked_fill(&self, mask: &Self, value: f64) -> Result<Self> {
        let fill = Self::full(self.shape().clone(), value, self.dtype(), self.device())?;
        Self::where_cond(mask, &fill, self)
    }

    /// Pad the last N dimensions with constant `value`.
    ///
    /// `padding` is a list of `[before, after]` pairs, one per dimension,
    /// applied to the **last** dimensions of the tensor.
    ///
    /// Example: `pad(&[[1, 1], [2, 2]], 0.0)` pads the last 2 dims.
    pub fn pad(&self, padding: &[[usize; 2]], value: f64) -> Result<Self> {
        let rank = self.rank();
        if padding.len() > rank {
            return Err(Error::msg(format!(
                "pad: {} padding pairs but tensor rank is {}",
                padding.len(),
                rank
            )));
        }

        // Build full-rank padding: leading dims get [0,0]
        let mut full_pad = vec![[0usize; 2]; rank];
        let offset = rank - padding.len();
        for (i, p) in padding.iter().enumerate() {
            full_pad[offset + i] = *p;
        }

        // Compute output shape
        let in_dims = self.dims();
        let out_dims: Vec<usize> = in_dims
            .iter()
            .zip(full_pad.iter())
            .map(|(&d, &[b, a])| d + b + a)
            .collect();

        // If no padding at all, just return a clone
        if full_pad.iter().all(|&[b, a]| b == 0 && a == 0) {
            return Ok(self.clone());
        }

        // Build result by concatenating pad tensors along each dimension
        let mut current = self.clone();
        for d in (0..rank).rev() {
            let [before, after] = full_pad[d];
            if before == 0 && after == 0 {
                continue;
            }
            let mut cur_dims = current.dims().to_vec();
            let mut parts: Vec<Self> = Vec::new();

            if before > 0 {
                cur_dims[d] = before;
                let pad_before = Self::full(
                    Shape::new(cur_dims.clone()),
                    value,
                    self.dtype(),
                    self.device(),
                )?;
                cur_dims[d] = current.dims()[d]; // restore
                parts.push(pad_before);
            }
            parts.push(current);
            if after > 0 {
                cur_dims[d] = after;
                let pad_after = Self::full(
                    Shape::new(cur_dims.clone()),
                    value,
                    self.dtype(),
                    self.device(),
                )?;
                parts.push(pad_after);
            }
            current = Self::cat(&parts, d)?;
        }

        // Wrap in Op::Pad for clean backward
        let result_layout = Layout::contiguous(Shape::new(out_dims));
        let pad_op = Op::Pad {
            input: self.clone(),
            padding: full_pad,
        };

        // Re-wrap with the Pad op so backward can narrow back
        let storage = current.read_storage()?;
        Ok(Self::from_storage(
            storage.clone(),
            result_layout,
            self.inner.dtype,
            self.inner.device.clone(),
            pad_op,
        ))
    }

    /// Return the `k` largest elements along `dim`.
    ///
    /// Returns `(values, indices)` where both have the same shape as self
    /// except dimension `dim` has size `k`.
    ///
    /// Non-differentiable (returns detached values).
    #[allow(clippy::needless_range_loop)]
    pub fn topk(&self, k: usize, dim: usize) -> Result<(Self, Vec<usize>)> {
        if dim >= self.rank() {
            return Err(Error::DimOutOfRange {
                dim,
                rank: self.rank(),
            });
        }
        let dims = self.dims();
        let dim_size = dims[dim];
        if k > dim_size {
            return Err(Error::msg(format!(
                "topk: k={} exceeds dim {} size {}",
                k, dim, dim_size
            )));
        }

        let data = self.contiguous()?.to_f64_vec()?;

        // Output shape: same as input but dim has size k
        let mut out_dims = dims.to_vec();
        out_dims[dim] = k;
        let out_size: usize = out_dims.iter().product();
        let mut out_values = vec![0.0f64; out_size];
        let mut out_indices = vec![0usize; out_size];

        // Number of "slices" along dim
        let outer: usize = dims[..dim].iter().product();
        let inner: usize = dims[dim + 1..].iter().product();

        for o in 0..outer {
            for i in 0..inner {
                // Collect all elements along this dim-slice
                let mut slice: Vec<(f64, usize)> = (0..dim_size)
                    .map(|d| {
                        let flat = o * (dim_size * inner) + d * inner + i;
                        (data[flat], d)
                    })
                    .collect();
                // Sort descending
                slice.sort_by(|a, b| b.0.partial_cmp(&a.0).unwrap_or(std::cmp::Ordering::Equal));

                // Write top-k
                for j in 0..k {
                    let out_flat = o * (k * inner) + j * inner + i;
                    out_values[out_flat] = slice[j].0;
                    out_indices[out_flat] = slice[j].1;
                }
            }
        }

        let values = Self::from_f64_slice(
            &out_values,
            Shape::new(out_dims),
            self.dtype(),
            self.device(),
        )?;
        Ok((values, out_indices))
    }

    /// Generic unary operation dispatch.
    fn unary_op(&self, op: UnaryOp) -> Result<Self> {
        let storage = self.read_storage()?;
        let result = B::unary_op(op, &storage, &self.inner.layout)?;
        let result_layout = Layout::contiguous(self.shape().clone());
        let result_op = Op::Unary {
            input: self.clone(),
            op,
        };
        Ok(Self::from_storage(
            result,
            result_layout,
            self.inner.dtype,
            self.inner.device.clone(),
            result_op,
        ))
    }

    // Reductions

    /// Sum all elements, returning a scalar tensor.
    pub fn sum_all(&self) -> Result<Self> {
        self.reduce_op(ReduceOp::Sum, &[], false)
    }

    /// Sum along a specific dimension.
    pub fn sum(&self, dim: usize, keep_dim: bool) -> Result<Self> {
        self.reduce_op(ReduceOp::Sum, &[dim], keep_dim)
    }

    /// Mean of all elements, returning a scalar tensor.
    pub fn mean_all(&self) -> Result<Self> {
        self.reduce_op(ReduceOp::Mean, &[], false)
    }

    /// Mean along a specific dimension.
    pub fn mean(&self, dim: usize, keep_dim: bool) -> Result<Self> {
        self.reduce_op(ReduceOp::Mean, &[dim], keep_dim)
    }

    /// Max along a specific dimension.
    pub fn max(&self, dim: usize, keep_dim: bool) -> Result<Self> {
        self.reduce_op(ReduceOp::Max, &[dim], keep_dim)
    }

    /// Min along a specific dimension.
    pub fn min(&self, dim: usize, keep_dim: bool) -> Result<Self> {
        self.reduce_op(ReduceOp::Min, &[dim], keep_dim)
    }

    /// ArgMax along a specific dimension (returns i64 indices).
    pub fn argmax(&self, dim: usize, keep_dim: bool) -> Result<Self> {
        self.reduce_op(ReduceOp::ArgMax, &[dim], keep_dim)
    }

    /// ArgMin along a specific dimension (returns i64 indices).
    pub fn argmin(&self, dim: usize, keep_dim: bool) -> Result<Self> {
        self.reduce_op(ReduceOp::ArgMin, &[dim], keep_dim)
    }

    /// Generic reduction dispatch.
    fn reduce_op(&self, op: ReduceOp, dims: &[usize], keep_dim: bool) -> Result<Self> {
        // Validate dimensions
        for &d in dims {
            if d >= self.rank() {
                return Err(Error::DimOutOfRange {
                    dim: d,
                    rank: self.rank(),
                });
            }
        }
        let storage = self.read_storage()?;
        let result = B::reduce_op(op, &storage, &self.inner.layout, dims, keep_dim)?;

        // Compute result shape
        let result_shape = if dims.is_empty() {
            // Reduce all → scalar
            Shape::from(())
        } else if keep_dim {
            let mut new_dims = self.dims().to_vec();
            for &d in dims {
                new_dims[d] = 1;
            }
            Shape::new(new_dims)
        } else {
            let new_dims: Vec<usize> = self
                .dims()
                .iter()
                .enumerate()
                .filter(|(i, _)| !dims.contains(i))
                .map(|(_, &d)| d)
                .collect();
            if new_dims.is_empty() {
                Shape::from(())
            } else {
                Shape::new(new_dims)
            }
        };

        let result_layout = Layout::contiguous(result_shape);
        let result_dtype = match op {
            ReduceOp::ArgMax | ReduceOp::ArgMin => DType::I64,
            _ => self.inner.dtype,
        };
        let result_op = Op::Reduce {
            input: self.clone(),
            op,
            dims: dims.to_vec(),
            keep_dim,
        };
        Ok(Self::from_storage(
            result,
            result_layout,
            result_dtype,
            self.inner.device.clone(),
            result_op,
        ))
    }

    // Composite operations (built from primitives)

    /// Softmax along a dimension: softmax(x)_i = exp(x_i) / sum(exp(x_j))
    ///
    /// Uses the numerically stable trick: subtract max before exp.
    /// This is built from existing differentiable ops (exp, sum, div, sub)
    /// so gradients flow through automatically.
    pub fn softmax(&self, dim: usize) -> Result<Self> {
        // max(x, dim, keep_dim=true) — used as a constant for stability
        let max_val = self.max(dim, true)?;
        // We detach max so it's treated as a constant in backward
        let max_detached = max_val.detach();
        let shifted = self.sub(&max_detached)?; // x - max(x)
        let exp_x = shifted.exp()?;
        let sum_exp = exp_x.sum(dim, true)?;
        exp_x.div(&sum_exp)
    }

    /// Log-softmax along a dimension: log(softmax(x)) but numerically stable.
    ///
    /// log_softmax(x)_i = x_i - max(x) - log(sum(exp(x - max(x))))
    pub fn log_softmax(&self, dim: usize) -> Result<Self> {
        let max_val = self.max(dim, true)?.detach();
        let shifted = self.sub(&max_val)?;
        let exp_x = shifted.exp()?;
        let sum_exp = exp_x.sum(dim, true)?;
        let log_sum_exp = sum_exp.log()?;
        shifted.sub(&log_sum_exp)
    }

    /// Variance along a dimension: var(x) = mean((x - mean(x))²)
    pub fn var(&self, dim: usize, keep_dim: bool) -> Result<Self> {
        let mu = self.mean(dim, true)?;
        let centered = self.sub(&mu)?;
        let sq = centered.square()?;
        sq.mean(dim, keep_dim)
    }

    /// Concatenate tensors along a dimension.
    ///
    /// All tensors must have the same shape except in the concatenation dimension.
    /// This creates a new tensor by copying data from all inputs.
    pub fn cat(tensors: &[Self], dim: usize) -> Result<Self> {
        if tensors.is_empty() {
            return Err(Error::msg("cat: empty tensor list"));
        }
        if tensors.len() == 1 {
            return Ok(tensors[0].clone());
        }

        let first = &tensors[0];
        let rank = first.rank();
        if dim >= rank {
            return Err(Error::DimOutOfRange { dim, rank });
        }

        // Validate shapes: all dims must match except `dim`
        for (i, t) in tensors.iter().enumerate().skip(1) {
            if t.rank() != rank {
                return Err(Error::msg(format!(
                    "cat: tensor {} has rank {} but expected {}",
                    i,
                    t.rank(),
                    rank
                )));
            }
            if t.dtype() != first.dtype() {
                return Err(Error::DTypeMismatch {
                    expected: first.dtype(),
                    got: t.dtype(),
                });
            }
            for d in 0..rank {
                if d != dim && t.dims()[d] != first.dims()[d] {
                    return Err(Error::msg(format!(
                        "cat: tensor {} has size {} at dim {} but expected {}",
                        i,
                        t.dims()[d],
                        d,
                        first.dims()[d]
                    )));
                }
            }
        }

        // Compute output shape
        let cat_size: usize = tensors.iter().map(|t| t.dims()[dim]).sum();
        let mut out_dims = first.dims().to_vec();
        out_dims[dim] = cat_size;
        let out_shape = Shape::new(out_dims.clone());

        // Record per-input sizes along the cat dim for backward
        let sizes: Vec<usize> = tensors.iter().map(|t| t.dims()[dim]).collect();

        // Collect (storage, layout) pairs for Backend::cat
        let inner_guards: Vec<_> = tensors
            .iter()
            .map(|t| t.inner.storage.read().unwrap())
            .collect();
        let pairs: Vec<(&B::Storage, &Layout)> = tensors
            .iter()
            .enumerate()
            .map(|(i, t)| (&*inner_guards[i], &t.inner.layout))
            .collect();

        let storage = B::cat(&pairs, &out_shape, dim)?;
        let layout = Layout::contiguous(out_shape);
        let op = Op::Cat {
            inputs: tensors.to_vec(),
            dim,
            sizes,
        };
        Ok(Self::from_storage(
            storage,
            layout,
            first.dtype(),
            first.device().clone(),
            op,
        ))
    }

    /// Split a tensor into `n` equal chunks along a dimension.
    /// If the dimension size is not evenly divisible, the last chunk is smaller.
    pub fn chunk(&self, n: usize, dim: usize) -> Result<Vec<Self>> {
        if dim >= self.rank() {
            return Err(Error::DimOutOfRange {
                dim,
                rank: self.rank(),
            });
        }
        let dim_size = self.dims()[dim];
        let chunk_size = dim_size.div_ceil(n);
        let mut chunks = Vec::new();
        let mut start = 0;
        while start < dim_size {
            let len = chunk_size.min(dim_size - start);
            chunks.push(self.narrow(dim, start, len)?);
            start += len;
        }
        Ok(chunks)
    }

    /// Expand a tensor to a larger shape by repeating data along size-1 dims.
    /// Only dims that are currently size 1 can be expanded.
    /// A size of -1 (usize::MAX) means don't change that dim.
    pub fn expand(&self, target_shape: impl Into<Shape>) -> Result<Self> {
        let target = target_shape.into();
        let self_dims = self.dims();
        let target_dims = target.dims();

        if self_dims.len() != target_dims.len() {
            return Err(Error::msg(format!(
                "expand: rank mismatch — self {:?} vs target {:?}",
                self_dims, target_dims
            )));
        }

        for (i, (&sd, &td)) in self_dims.iter().zip(target_dims.iter()).enumerate() {
            if sd != td && sd != 1 {
                return Err(Error::msg(format!(
                    "expand: can only expand size-1 dims, but dim {} has size {}",
                    i, sd
                )));
            }
        }

        // Zero-copy expand via stride tricks:
        // For dims where self_dim == 1 and target_dim > 1, set stride to 0
        // (the single element is "repeated" without copying data).
        let self_strides = self.inner.layout.strides();
        let mut new_strides = self_strides.to_vec();
        for d in 0..target_dims.len() {
            if self_dims[d] == 1 && target_dims[d] > 1 {
                new_strides[d] = 0;
            }
        }

        let new_layout = Layout::new(target, new_strides, self.inner.layout.offset());

        // Share the same storage — no copy!
        Ok(Tensor {
            inner: Arc::new(TensorInner {
                id: TensorId::new(),
                storage: Arc::clone(&self.inner.storage),
                layout: new_layout,
                dtype: self.inner.dtype,
                device: self.inner.device.clone(),
                op: Op::None,
                is_variable: false,
            }),
        })
    }

    // Stack — concatenate with a new dimension

    /// Stack tensors along a new dimension.
    ///
    /// All tensors must have the same shape. Inserts a new dimension at `dim`.
    /// `stack([a, b], dim=0)` where a,b are shape [2,3] → [2, 2, 3].
    pub fn stack(tensors: &[Self], dim: usize) -> Result<Self> {
        if tensors.is_empty() {
            return Err(Error::msg("stack: empty tensor list"));
        }
        let first_shape = tensors[0].shape().clone();
        let rank = first_shape.dims().len();
        if dim > rank {
            return Err(Error::DimOutOfRange {
                dim,
                rank: rank + 1,
            });
        }
        // Validate all shapes match
        for (i, t) in tensors.iter().enumerate().skip(1) {
            if t.shape() != &first_shape {
                return Err(Error::msg(format!(
                    "stack: tensor {} has shape {:?} but expected {:?}",
                    i,
                    t.dims(),
                    first_shape.dims()
                )));
            }
        }
        // Unsqueeze each tensor at `dim`, then cat
        let unsqueezed: Vec<Self> = tensors
            .iter()
            .map(|t| t.unsqueeze(dim))
            .collect::<Result<Vec<_>>>()?;
        Self::cat(&unsqueezed, dim)
    }

    // Arange — generate a sequence

    /// Create a 1-D tensor with values [0, 1, ..., n-1].
    pub fn arange(n: usize, dtype: DType, device: &B::Device) -> Result<Self> {
        let data: Vec<f64> = (0..n).map(|i| i as f64).collect();
        Self::from_f64_slice(&data, n, dtype, device)
    }

    /// Create a 1-D tensor with values [start, start+step, ..., <end).
    pub fn arange_step(
        start: f64,
        end: f64,
        step: f64,
        dtype: DType,
        device: &B::Device,
    ) -> Result<Self> {
        if step == 0.0 {
            return Err(Error::msg("arange_step: step cannot be zero"));
        }
        let mut data = Vec::new();
        let mut v = start;
        if step > 0.0 {
            while v < end {
                data.push(v);
                v += step;
            }
        } else {
            while v > end {
                data.push(v);
                v += step;
            }
        }
        let len = data.len();
        Self::from_f64_slice(&data, len, dtype, device)
    }

    // Triangular masks — triu / tril

    /// Upper triangular mask: returns a 2-D tensor of shape [n, m] where
    /// elements on and above the `diagonal`-th diagonal are 1.0, rest 0.0.
    ///
    /// `diagonal = 0` → main diagonal. `diagonal > 0` → above. `diagonal < 0` → below.
    pub fn triu(
        n: usize,
        m: usize,
        diagonal: i64,
        dtype: DType,
        device: &B::Device,
    ) -> Result<Self> {
        let mut data = vec![0.0f64; n * m];
        for i in 0..n {
            for j in 0..m {
                if (j as i64) >= (i as i64) + diagonal {
                    data[i * m + j] = 1.0;
                }
            }
        }
        Self::from_f64_slice(&data, (n, m), dtype, device)
    }

    /// Lower triangular mask: returns a 2-D tensor of shape [n, m] where
    /// elements on and below the `diagonal`-th diagonal are 1.0, rest 0.0.
    pub fn tril(
        n: usize,
        m: usize,
        diagonal: i64,
        dtype: DType,
        device: &B::Device,
    ) -> Result<Self> {
        let mut data = vec![0.0f64; n * m];
        for i in 0..n {
            for j in 0..m {
                if (j as i64) <= (i as i64) + diagonal {
                    data[i * m + j] = 1.0;
                }
            }
        }
        Self::from_f64_slice(&data, (n, m), dtype, device)
    }

    // Matrix multiplication

    /// Matrix multiplication: self @ rhs.
    ///
    /// - [m, k] @ [k, n] → [m, n]
    /// - Batched: [b, m, k] @ [b, k, n] → [b, m, n]
    pub fn matmul(&self, rhs: &Self) -> Result<Self> {
        if self.dtype() != rhs.dtype() {
            return Err(Error::DTypeMismatch {
                expected: self.dtype(),
                got: rhs.dtype(),
            });
        }
        // Validate shapes for matmul
        if self.rank() < 2 || rhs.rank() < 2 {
            return Err(Error::RankMismatch {
                expected: 2,
                got: self.rank().min(rhs.rank()),
            });
        }
        let lhs_dims = self.dims();
        let rhs_dims = rhs.dims();
        let k1 = lhs_dims[lhs_dims.len() - 1];
        let k2 = rhs_dims[rhs_dims.len() - 2];
        if k1 != k2 {
            let m = lhs_dims[lhs_dims.len() - 2];
            let n = rhs_dims[rhs_dims.len() - 1];
            return Err(Error::MatmulShapeMismatch { m, k1, k2, n });
        }

        let storage_lhs = self.read_storage()?;
        let storage_rhs = rhs.read_storage()?;
        let result = B::matmul(
            &storage_lhs,
            &self.inner.layout,
            &storage_rhs,
            &rhs.inner.layout,
        )?;

        // Result shape: [..., m, n]
        let m = lhs_dims[lhs_dims.len() - 2];
        let n = rhs_dims[rhs_dims.len() - 1];
        let mut result_dims: Vec<usize> = lhs_dims[..lhs_dims.len() - 2].to_vec();
        result_dims.push(m);
        result_dims.push(n);
        let result_layout = Layout::contiguous(Shape::new(result_dims));
        let result_op = Op::Matmul {
            lhs: self.clone(),
            rhs: rhs.clone(),
        };
        Ok(Self::from_storage(
            result,
            result_layout,
            self.inner.dtype,
            self.inner.device.clone(),
            result_op,
        ))
    }

    // 2D Convolution

    /// 2D convolution: applies convolution filters to a 4D input tensor.
    ///
    /// - `self` (input): `[N, C_in, H, W]`
    /// - `weight`:       `[C_out, C_in, kH, kW]`
    /// - `bias`:         optional `[C_out]`
    /// - `stride`:       `[sH, sW]`
    /// - `padding`:      `[pH, pW]`
    ///
    /// Returns tensor of shape `[N, C_out, H_out, W_out]` where
    /// `H_out = (H + 2*pH - kH) / sH + 1`.
    #[allow(clippy::needless_range_loop)]
    pub fn conv2d(
        &self,
        weight: &Self,
        bias: Option<&Self>,
        stride: [usize; 2],
        padding: [usize; 2],
    ) -> Result<Self> {
        // Validate ranks
        if self.rank() != 4 {
            return Err(Error::msg(format!(
                "conv2d input must be 4D [N,C,H,W], got rank {}",
                self.rank()
            )));
        }
        if weight.rank() != 4 {
            return Err(Error::msg(format!(
                "conv2d weight must be 4D [C_out,C_in,kH,kW], got rank {}",
                weight.rank()
            )));
        }

        let in_dims = self.dims();
        let w_dims = weight.dims();
        let (n, c_in, h, w) = (in_dims[0], in_dims[1], in_dims[2], in_dims[3]);
        let (c_out, wc_in, kh, kw) = (w_dims[0], w_dims[1], w_dims[2], w_dims[3]);

        if c_in != wc_in {
            return Err(Error::msg(format!(
                "conv2d: input channels {} != weight channels {}",
                c_in, wc_in
            )));
        }

        let [sh, sw] = stride;
        let [ph, pw] = padding;

        if h + 2 * ph < kh || w + 2 * pw < kw {
            return Err(Error::msg("conv2d: kernel larger than padded input"));
        }

        let h_out = (h + 2 * ph - kh) / sh + 1;
        let w_out = (w + 2 * pw - kw) / sw + 1;

        // Get contiguous data
        let input_data = self.contiguous()?.to_f64_vec()?;
        let weight_data = weight.contiguous()?.to_f64_vec()?;
        let bias_data = match bias {
            Some(b) => Some(b.contiguous()?.to_f64_vec()?),
            None => None,
        };

        let out_size = n * c_out * h_out * w_out;
        let mut output = vec![0.0f64; out_size];

        // im2col + GEMM approach:
        // For each batch sample:
        //   1. im2col: unroll input patches → columns [c_in*kh*kw, h_out*w_out]
        //   2. GEMM: weight [c_out, c_in*kh*kw] × columns → out [c_out, h_out*w_out]
        let col_rows = c_in * kh * kw;
        let col_cols = h_out * w_out;
        let mut columns = vec![0.0f64; col_rows * col_cols];
        let sample_size = c_in * h * w;

        for ni in 0..n {
            // im2col for this sample
            let in_offset = ni * sample_size;
            im2col(
                &input_data[in_offset..in_offset + sample_size],
                c_in,
                h,
                w,
                kh,
                kw,
                sh,
                sw,
                ph,
                pw,
                h_out,
                w_out,
                &mut columns,
            );

            // GEMM: output[ni] = weight × columns + bias
            let out_offset = ni * c_out * h_out * w_out;
            gemm(
                &weight_data,
                &columns,
                &mut output[out_offset..out_offset + c_out * col_cols],
                c_out,
                col_cols,
                col_rows,
            );

            // Add bias
            if let Some(ref bd) = bias_data {
                for co in 0..c_out {
                    let row_start = out_offset + co * col_cols;
                    for j in 0..col_cols {
                        output[row_start + j] += bd[co];
                    }
                }
            }
        }

        let result_shape = Shape::new(vec![n, c_out, h_out, w_out]);
        let result_op = Op::Conv2d {
            input: self.clone(),
            weight: weight.clone(),
            bias: bias.cloned(),
            stride,
            padding,
        };
        Self::from_f64_slice(&output, result_shape.clone(), self.dtype(), self.device()).map(|t| {
            Self::from_storage(
                {
                    let s = t.inner.storage.read().expect("storage lock poisoned");
                    s.clone()
                },
                Layout::contiguous(result_shape),
                self.inner.dtype,
                self.inner.device.clone(),
                result_op,
            )
        })
    }

    // 2D Max Pooling

    /// 2D max pooling on a 4D input tensor `[N, C, H, W]`.
    ///
    /// Returns `(output, indices)` where `indices` stores argmax positions
    /// (flat indices into the input) for backward.
    pub fn max_pool2d(
        &self,
        kernel_size: [usize; 2],
        stride: [usize; 2],
        padding: [usize; 2],
    ) -> Result<Self> {
        if self.rank() != 4 {
            return Err(Error::msg(format!(
                "max_pool2d input must be 4D [N,C,H,W], got rank {}",
                self.rank()
            )));
        }

        let dims = self.dims();
        let (n, c, h, w) = (dims[0], dims[1], dims[2], dims[3]);
        let [kh, kw] = kernel_size;
        let [sh, sw] = stride;
        let [ph, pw] = padding;

        if h + 2 * ph < kh || w + 2 * pw < kw {
            return Err(Error::msg("max_pool2d: kernel larger than padded input"));
        }

        let h_out = (h + 2 * ph - kh) / sh + 1;
        let w_out = (w + 2 * pw - kw) / sw + 1;

        let input_data = self.contiguous()?.to_f64_vec()?;
        let out_size = n * c * h_out * w_out;
        let mut output = vec![f64::NEG_INFINITY; out_size];
        let mut indices = vec![0usize; out_size];

        for ni in 0..n {
            for ci in 0..c {
                for oh in 0..h_out {
                    for ow in 0..w_out {
                        let out_idx = ((ni * c + ci) * h_out + oh) * w_out + ow;
                        let mut max_val = f64::NEG_INFINITY;
                        let mut max_idx = 0usize;
                        for ki in 0..kh {
                            for kj in 0..kw {
                                let ih = (oh * sh + ki) as isize - ph as isize;
                                let iw = (ow * sw + kj) as isize - pw as isize;
                                if ih >= 0 && ih < h as isize && iw >= 0 && iw < w as isize {
                                    let ih = ih as usize;
                                    let iw = iw as usize;
                                    let in_idx = ((ni * c + ci) * h + ih) * w + iw;
                                    if input_data[in_idx] > max_val {
                                        max_val = input_data[in_idx];
                                        max_idx = in_idx;
                                    }
                                }
                            }
                        }
                        output[out_idx] = max_val;
                        indices[out_idx] = max_idx;
                    }
                }
            }
        }

        let result_shape = Shape::new(vec![n, c, h_out, w_out]);
        let result_op = Op::MaxPool2d {
            input: self.clone(),
            kernel_size,
            stride,
            padding,
            indices: indices.clone(),
        };
        Self::from_f64_slice(&output, result_shape.clone(), self.dtype(), self.device()).map(|t| {
            Self::from_storage(
                {
                    let s = t.inner.storage.read().expect("storage lock poisoned");
                    s.clone()
                },
                Layout::contiguous(result_shape),
                self.inner.dtype,
                self.inner.device.clone(),
                result_op,
            )
        })
    }

    // 2D Average Pooling

    /// Apply 2D average pooling to a 4D tensor [N, C, H, W].
    pub fn avg_pool2d(
        &self,
        kernel_size: [usize; 2],
        stride: [usize; 2],
        padding: [usize; 2],
    ) -> Result<Self> {
        if self.rank() != 4 {
            return Err(Error::msg(format!(
                "avg_pool2d input must be 4D [N,C,H,W], got rank {}",
                self.rank()
            )));
        }

        let dims = self.dims();
        let (n, c, h, w) = (dims[0], dims[1], dims[2], dims[3]);
        let [kh, kw] = kernel_size;
        let [sh, sw] = stride;
        let [ph, pw] = padding;

        if h + 2 * ph < kh || w + 2 * pw < kw {
            return Err(Error::msg("avg_pool2d: kernel larger than padded input"));
        }

        let h_out = (h + 2 * ph - kh) / sh + 1;
        let w_out = (w + 2 * pw - kw) / sw + 1;

        let input_data = self.contiguous()?.to_f64_vec()?;
        let out_size = n * c * h_out * w_out;
        let mut output = vec![0.0f64; out_size];

        for ni in 0..n {
            for ci in 0..c {
                for oh in 0..h_out {
                    for ow in 0..w_out {
                        let out_idx = ((ni * c + ci) * h_out + oh) * w_out + ow;
                        let mut sum = 0.0f64;
                        let mut count = 0usize;
                        for ki in 0..kh {
                            for kj in 0..kw {
                                let ih = (oh * sh + ki) as isize - ph as isize;
                                let iw = (ow * sw + kj) as isize - pw as isize;
                                if ih >= 0 && ih < h as isize && iw >= 0 && iw < w as isize {
                                    let in_idx =
                                        ((ni * c + ci) * h + ih as usize) * w + iw as usize;
                                    sum += input_data[in_idx];
                                    count += 1;
                                }
                            }
                        }
                        output[out_idx] = if count > 0 { sum / count as f64 } else { 0.0 };
                    }
                }
            }
        }

        let result_shape = Shape::new(vec![n, c, h_out, w_out]);
        let result_op = Op::AvgPool2d {
            input: self.clone(),
            kernel_size,
            stride,
            padding,
        };
        Self::from_f64_slice(&output, result_shape.clone(), self.dtype(), self.device()).map(|t| {
            Self::from_storage(
                {
                    let s = t.inner.storage.read().expect("storage lock poisoned");
                    s.clone()
                },
                Layout::contiguous(result_shape),
                self.inner.dtype,
                self.inner.device.clone(),
                result_op,
            )
        })
    }

    // 1D Convolution

    /// Apply 1D convolution to a 3D tensor [N, C_in, L].
    /// weight: [C_out, C_in, K]
    #[allow(clippy::needless_range_loop)]
    pub fn conv1d(
        &self,
        weight: &Self,
        bias: Option<&Self>,
        stride: usize,
        padding: usize,
    ) -> Result<Self> {
        if self.rank() != 3 {
            return Err(Error::msg(format!(
                "conv1d input must be 3D [N,C_in,L], got rank {}",
                self.rank()
            )));
        }
        if weight.rank() != 3 {
            return Err(Error::msg(format!(
                "conv1d weight must be 3D [C_out,C_in,K], got rank {}",
                weight.rank()
            )));
        }

        let in_dims = self.dims();
        let w_dims = weight.dims();
        let (n, c_in, l) = (in_dims[0], in_dims[1], in_dims[2]);
        let (c_out, wc_in, k) = (w_dims[0], w_dims[1], w_dims[2]);

        if c_in != wc_in {
            return Err(Error::msg(format!(
                "conv1d: input channels {} != weight channels {}",
                c_in, wc_in
            )));
        }
        if let Some(b) = bias {
            if b.elem_count() != c_out {
                return Err(Error::msg(format!(
                    "conv1d: bias size {} != output channels {}",
                    b.elem_count(),
                    c_out
                )));
            }
        }

        if l + 2 * padding < k {
            return Err(Error::msg("conv1d: kernel larger than padded input"));
        }

        let l_out = (l + 2 * padding - k) / stride + 1;

        let input_data = self.contiguous()?.to_f64_vec()?;
        let weight_data = weight.contiguous()?.to_f64_vec()?;
        let bias_data: Option<Vec<f64>> = match bias {
            Some(b) => Some(b.to_f64_vec()?),
            None => None,
        };

        let out_size = n * c_out * l_out;
        let mut output = vec![0.0f64; out_size];

        // im2col + GEMM for conv1d (treat as 2D with h=1)
        let col_rows = c_in * k;
        let col_cols = l_out;
        let mut columns = vec![0.0f64; col_rows * col_cols];
        let sample_size = c_in * l;

        for ni in 0..n {
            // im2col for 1D: unroll patches
            let in_offset = ni * sample_size;
            im2col(
                &input_data[in_offset..in_offset + sample_size],
                c_in,
                1,
                l,
                1,
                k,
                1,
                stride,
                0,
                padding,
                1,
                l_out,
                &mut columns,
            );

            // GEMM: output[ni] = weight × columns
            let out_offset = ni * c_out * l_out;
            gemm(
                &weight_data,
                &columns,
                &mut output[out_offset..out_offset + c_out * col_cols],
                c_out,
                col_cols,
                col_rows,
            );

            // Add bias
            if let Some(ref bd) = bias_data {
                for co in 0..c_out {
                    let row_start = out_offset + co * col_cols;
                    for j in 0..col_cols {
                        output[row_start + j] += bd[co];
                    }
                }
            }
        }

        let result_shape = Shape::new(vec![n, c_out, l_out]);
        let result_op = Op::Conv1d {
            input: self.clone(),
            weight: weight.clone(),
            bias: bias.cloned(),
            stride,
            padding,
        };
        Self::from_f64_slice(&output, result_shape.clone(), self.dtype(), self.device()).map(|t| {
            Self::from_storage(
                {
                    let s = t.inner.storage.read().expect("storage lock poisoned");
                    s.clone()
                },
                Layout::contiguous(result_shape),
                self.inner.dtype,
                self.inner.device.clone(),
                result_op,
            )
        })
    }

    // Affine transform

    /// Affine transform: result[i] = self[i] * mul + add.
    /// Useful for normalization and scaling.
    pub fn affine(&self, mul: f64, add: f64) -> Result<Self> {
        let storage = self.read_storage()?;
        let result = B::affine(&storage, &self.inner.layout, mul, add)?;
        let result_layout = Layout::contiguous(self.shape().clone());
        let result_op = Op::Affine {
            input: self.clone(),
            mul,
            add,
        };
        Ok(Self::from_storage(
            result,
            result_layout,
            self.inner.dtype,
            self.inner.device.clone(),
            result_op,
        ))
    }

    // Data extraction (for testing and debugging)

    /// Extract all elements as a flat Vec<f64>.
    pub fn to_f64_vec(&self) -> Result<Vec<f64>> {
        let storage = self.read_storage()?;
        B::to_f64_vec(&storage, &self.inner.layout)
    }

    /// Extract a scalar value (tensor must have exactly 1 element).
    pub fn to_scalar_f64(&self) -> Result<f64> {
        if self.elem_count() != 1 {
            return Err(Error::NotAScalar {
                shape: self.shape().clone(),
            });
        }
        let vec = self.to_f64_vec()?;
        Ok(vec[0])
    }

    /// Convert this tensor to a different dtype.
    ///
    /// Returns a new tensor with the same shape but different element type.
    /// Uses the backend's on-device cast when available, avoiding host round-trips.
    /// Records Op::ToDtype so gradients flow back through dtype conversions.
    pub fn to_dtype(&self, dtype: DType) -> Result<Self> {
        if self.dtype() == dtype {
            return Ok(self.clone());
        }
        let src_dtype = self.dtype();
        let guard = self.inner.storage.read().unwrap();
        let storage = B::cast(&*guard, &self.inner.layout, dtype, self.device())?;
        let layout = Layout::contiguous(self.shape().clone());
        let op = if self.is_variable() {
            Op::ToDtype {
                input: self.clone(),
                src_dtype,
            }
        } else {
            Op::None
        };
        Ok(Self::from_storage(
            storage,
            layout,
            dtype,
            self.device().clone(),
            op,
        ))
    }

    /// Display the tensor contents in a human-readable format.
    pub fn to_string_with_data(&self) -> Result<String> {
        let data = self.to_f64_vec()?;
        Ok(format!(
            "Tensor(shape={}, dtype={}, data={:?})",
            self.shape(),
            self.dtype(),
            data
        ))
    }

    // Autograd

    /// Compute gradients via reverse-mode automatic differentiation.
    ///
    /// This tensor must be a scalar (single element). Returns a GradStore
    /// containing gradients for all tensors in the computation graph.
    ///
    /// # Example
    /// ```ignore
    /// let a = Tensor::from_f64_slice(&[2.0], 1, DType::F32, &dev)?.set_variable();
    /// let b = Tensor::from_f64_slice(&[3.0], 1, DType::F32, &dev)?.set_variable();
    /// let c = a.mul(&b)?;
    /// let grads = c.backward()?;
    /// // grad_a = b = 3.0, grad_b = a = 2.0
    /// ```
    pub fn backward(&self) -> Result<crate::backprop::GradStore<B>> {
        crate::backprop::backward(self)
    }

    /// Create a detached copy: same data but no gradient tracking.
    /// The new tensor has Op::None and a fresh TensorId.
    pub fn detach(&self) -> Self {
        self.view_with_layout(self.layout().clone(), Op::None)
    }

    /// Freeze this tensor: same data and id, but `is_variable = false`.
    ///
    /// Frozen tensors do NOT accumulate gradients during backward().
    /// This is the functional equivalent of PyTorch's `param.requires_grad_(False)`.
    pub fn freeze(&self) -> Self {
        Tensor {
            inner: Arc::new(TensorInner {
                id: self.inner.id,
                storage: Arc::clone(&self.inner.storage),
                layout: self.inner.layout.clone(),
                dtype: self.inner.dtype,
                device: self.inner.device.clone(),
                op: self.inner.op.clone(),
                is_variable: false,
            }),
        }
    }

    /// Unfreeze this tensor: same data and id, but `is_variable = true`.
    ///
    /// This is the opposite of `freeze()`.
    pub fn unfreeze(&self) -> Self {
        self.set_variable_ref()
    }

    // Additional composite operations

    /// Select entries along `dim` using the given 1-D index tensor.
    ///
    /// The output has the same rank, with `dim` resized to `indices.len()`.
    /// Wraps the `Backend::index_select` kernel.
    pub fn index_select(&self, dim: usize, indices: &Self) -> Result<Self> {
        if dim >= self.rank() {
            return Err(Error::DimOutOfRange {
                dim,
                rank: self.rank(),
            });
        }
        let guard = self.inner.storage.read().unwrap();
        let idx_guard = indices.inner.storage.read().unwrap();
        let storage = B::index_select(
            &*guard,
            &self.inner.layout,
            &*idx_guard,
            &indices.inner.layout,
            dim,
        )?;
        let mut out_dims = self.dims().to_vec();
        out_dims[dim] = indices.elem_count();
        let layout = Layout::contiguous(Shape::new(out_dims));
        // Record op for autograd — gradient flows back to input via scatter-add
        let op = Op::IndexSelect {
            input: self.clone(),
            indices: indices.clone(),
            dim,
        };
        Ok(Self::from_storage(
            storage,
            layout,
            self.dtype(),
            self.device().clone(),
            op,
        ))
    }

    /// Split a tensor into chunks of `split_size` along `dim`.
    ///
    /// The last chunk may be smaller if the dimension is not evenly divisible.
    pub fn split(&self, split_size: usize, dim: usize) -> Result<Vec<Self>> {
        if dim >= self.rank() {
            return Err(Error::DimOutOfRange {
                dim,
                rank: self.rank(),
            });
        }
        if split_size == 0 {
            return Err(Error::msg("split: split_size must be > 0"));
        }
        let dim_size = self.dims()[dim];
        let mut parts = Vec::new();
        let mut start = 0;
        while start < dim_size {
            let len = split_size.min(dim_size - start);
            parts.push(self.narrow(dim, start, len)?);
            start += len;
        }
        Ok(parts)
    }

    /// Flatten dimensions `start_dim..=end_dim` into a single dimension.
    ///
    /// Negative-style indexing is **not** supported; both bounds are inclusive
    /// and zero-based.
    pub fn flatten(&self, start_dim: usize, end_dim: usize) -> Result<Self> {
        let rank = self.rank();
        if start_dim >= rank || end_dim >= rank || start_dim > end_dim {
            return Err(Error::msg(format!(
                "flatten: invalid range [{}, {}] for rank {}",
                start_dim, end_dim, rank
            )));
        }
        let dims = self.dims();
        let mut new_dims: Vec<usize> = Vec::new();
        new_dims.extend_from_slice(&dims[..start_dim]);
        let flat: usize = dims[start_dim..=end_dim].iter().product();
        new_dims.push(flat);
        if end_dim + 1 < rank {
            new_dims.extend_from_slice(&dims[end_dim + 1..]);
        }
        self.reshape(new_dims)
    }

    /// Standard deviation along a dimension.
    ///
    /// Computed as `sqrt(var(x, dim))`.
    pub fn std(&self, dim: usize, keep_dim: bool) -> Result<Self> {
        self.var(dim, keep_dim)?.sqrt()
    }

    /// Element-wise reciprocal: `1 / x`.
    pub fn reciprocal(&self) -> Result<Self> {
        let one = Self::ones(self.dims(), self.dtype(), self.device())?;
        one.div(self)
    }

    /// Element-wise reciprocal square-root: `1 / sqrt(x)`.
    pub fn rsqrt(&self) -> Result<Self> {
        self.sqrt()?.reciprocal()
    }

    /// Element-wise sign: returns -1, 0, or +1.
    ///
    /// Implemented via `x / (|x| + eps)` clamped to [-1, 1], with exact 0 for
    /// inputs that are exactly zero.
    pub fn sign(&self) -> Result<Self> {
        let eps = 1e-12;
        let abs_x = self.abs()?;
        let denom = abs_x.affine(1.0, eps)?;
        let raw = self.div(&denom)?;
        raw.clamp(-1.0, 1.0)
    }

    /// Log-sum-exp along a dimension (numerically stable).
    ///
    /// `logsumexp(x, d) = max(x,d) + log(sum(exp(x - max(x,d)), d))`
    pub fn logsumexp(&self, dim: usize, keep_dim: bool) -> Result<Self> {
        let m = self.max(dim, true)?.detach();
        let shifted = self.sub(&m)?;
        let sum_exp = shifted.exp()?.sum(dim, true)?.log()?;
        let result = m.add(&sum_exp)?;
        if keep_dim {
            Ok(result)
        } else {
            result.squeeze(dim)
        }
    }

    /// Product of elements along a dimension.
    ///
    /// Computed as `exp(sum(log(|x|)))` with sign correction.
    /// **Warning**: undefined for inputs containing zero.
    pub fn prod(&self, dim: usize, keep_dim: bool) -> Result<Self> {
        let log_abs = self.abs()?.log()?;
        let sum_log = log_abs.sum(dim, keep_dim)?;
        let magnitude = sum_log.exp()?;
        // Sign: count negatives via (sign < 0) then parity
        // For simplicity, assume positive inputs (like PyTorch's default usage).
        // A full sign-tracking impl would need additional ops.
        Ok(magnitude)
    }

    /// Like `set_variable(self)` but takes `&self` instead of `self`.
    fn set_variable_ref(&self) -> Self {
        Tensor {
            inner: Arc::new(TensorInner {
                id: self.inner.id,
                storage: Arc::clone(&self.inner.storage),
                layout: self.inner.layout.clone(),
                dtype: self.inner.dtype,
                device: self.inner.device.clone(),
                op: self.inner.op.clone(),
                is_variable: true,
            }),
        }
    }
}

// im2col / col2im — Efficient convolution via matrix multiplication
//
// im2col extracts all sliding-window patches from the input and arranges them
// as columns of a matrix. This converts convolution into a single large GEMM:
//
//   columns = im2col(input)          shape: [C_in * kH * kW,  H_out * W_out]
//   output  = weight × columns       shape: [C_out, H_out * W_out]
//
// col2im is the reverse: it scatters columns back into an image-shaped buffer,
// accumulating overlapping contributions. Used in the backward pass.

/// im2col: Extract sliding-window patches from a single sample.
///
/// Input: `[C_in, H, W]` (one sample, no batch dim)
/// Output: columns `[C_in * kH * kW, H_out * W_out]`
#[inline]
#[allow(clippy::too_many_arguments)]
pub(crate) fn im2col(
    input: &[f64],
    c_in: usize,
    h: usize,
    w: usize,
    kh: usize,
    kw: usize,
    sh: usize,
    sw: usize,
    ph: usize,
    pw: usize,
    h_out: usize,
    w_out: usize,
    columns: &mut [f64],
) {
    let col_cols = h_out * w_out;
    // Each row of `columns` corresponds to one element of the kernel
    // across all spatial output positions
    for ci in 0..c_in {
        for ki in 0..kh {
            for kj in 0..kw {
                let row = (ci * kh + ki) * kw + kj;
                let row_offset = row * col_cols;
                for oh in 0..h_out {
                    for ow in 0..w_out {
                        let ih = (oh * sh + ki) as isize - ph as isize;
                        let iw = (ow * sw + kj) as isize - pw as isize;
                        let val = if ih >= 0 && ih < h as isize && iw >= 0 && iw < w as isize {
                            input[(ci * h + ih as usize) * w + iw as usize]
                        } else {
                            0.0
                        };
                        columns[row_offset + oh * w_out + ow] = val;
                    }
                }
            }
        }
    }
}

/// col2im: Scatter columns back into an image buffer (for backward).
///
/// Accumulates into `output` (which should be zeroed before calling).
/// columns: `[C_in * kH * kW, H_out * W_out]`
/// output: `[C_in, H, W]`
#[inline]
#[allow(clippy::too_many_arguments)]
pub(crate) fn col2im(
    columns: &[f64],
    c_in: usize,
    h: usize,
    w: usize,
    kh: usize,
    kw: usize,
    sh: usize,
    sw: usize,
    ph: usize,
    pw: usize,
    h_out: usize,
    w_out: usize,
    output: &mut [f64],
) {
    let col_cols = h_out * w_out;
    for ci in 0..c_in {
        for ki in 0..kh {
            for kj in 0..kw {
                let row = (ci * kh + ki) * kw + kj;
                let row_offset = row * col_cols;
                for oh in 0..h_out {
                    for ow in 0..w_out {
                        let ih = (oh * sh + ki) as isize - ph as isize;
                        let iw = (ow * sw + kj) as isize - pw as isize;
                        if ih >= 0 && ih < h as isize && iw >= 0 && iw < w as isize {
                            output[(ci * h + ih as usize) * w + iw as usize] +=
                                columns[row_offset + oh * w_out + ow];
                        }
                    }
                }
            }
        }
    }
}

/// Simple GEMM: C = A × B
///
/// A: [m, k], B: [k, n], C: [m, n]
/// All row-major.
#[inline]
pub(crate) fn gemm(a: &[f64], b: &[f64], c: &mut [f64], m: usize, n: usize, k: usize) {
    for i in 0..m {
        let a_row = i * k;
        let c_row = i * n;
        for p in 0..k {
            let a_val = a[a_row + p];
            let b_row = p * n;
            for j in 0..n {
                c[c_row + j] += a_val * b[b_row + j];
            }
        }
    }
}

/// GEMM: C = A^T × B
///
/// A: [k, m] (transposed to [m, k]), B: [k, n], C: [m, n]
#[inline]
pub(crate) fn gemm_at_b(a: &[f64], b: &[f64], c: &mut [f64], m: usize, n: usize, k: usize) {
    for i in 0..m {
        let c_row = i * n;
        for p in 0..k {
            let a_val = a[p * m + i]; // A^T[i,p] = A[p,i]
            let b_row = p * n;
            for j in 0..n {
                c[c_row + j] += a_val * b[b_row + j];
            }
        }
    }
}

/// GEMM: C = A × B^T
///
/// A: [m, k], B: [n, k] (transposed to [k, n]), C: [m, n]
#[inline]
pub(crate) fn gemm_a_bt(a: &[f64], b: &[f64], c: &mut [f64], m: usize, n: usize, k: usize) {
    for i in 0..m {
        let a_row = i * k;
        let c_row = i * n;
        for j in 0..n {
            let b_row = j * k;
            let mut val = 0.0f64;
            for p in 0..k {
                val += a[a_row + p] * b[b_row + p];
            }
            c[c_row + j] += val;
        }
    }
}