rten-tensor 0.24.0

Tensor library for the RTen machine learning runtime
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
//! Iterators over tensor elements and sub-views.

use std::iter::FusedIterator;
use std::mem::transmute;
use std::ops::Range;

use rten_base::iter::SplitIterator;

use super::{AsView, DynLayout, NdTensorView, NdTensorViewMut, TensorBase, TensorViewMut};
use crate::layout::{Layout, MutLayout, NdLayout, OverlapPolicy, RemoveDim, merge_axes};
use crate::storage::{StorageMut, ViewData, ViewMutData};

mod parallel;

/// Tracks the iteration position within a single dimension.
#[derive(Copy, Clone, Debug, Default)]
struct IterPos {
    /// Remaining steps along this dimension before it needs to be reset.
    remaining: usize,

    /// Current index in this dimension pre-multiplied by stride.
    offset: usize,

    /// Update to `offset` for each step.
    stride: usize,

    /// Maximum value of `self.remaining`. Used when resetting position.
    max_remaining: usize,
}

impl IterPos {
    fn from_size_stride(size: usize, stride: usize) -> Self {
        let remaining = size.saturating_sub(1);
        IterPos {
            remaining,
            offset: 0,
            stride,
            max_remaining: remaining,
        }
    }

    #[inline(always)]
    fn step(&mut self) -> bool {
        if self.remaining != 0 {
            self.remaining -= 1;
            self.offset += self.stride;
            true
        } else {
            self.remaining = self.max_remaining;
            self.offset = 0;
            false
        }
    }

    /// Return the size of this dimension.
    fn size(&self) -> usize {
        // nb. The size is always > 0 since if any dim has zero size, the
        // iterator will have a length of zero.
        self.max_remaining + 1
    }

    /// Return the current index along this dimension.
    fn index(&self) -> usize {
        self.max_remaining - self.remaining
    }

    /// Set the current index along this dimension.
    fn set_index(&mut self, index: usize) {
        self.remaining = self.max_remaining - index;
        self.offset = index * self.stride;
    }
}

const INNER_NDIM: usize = 2;

/// Iterator over offsets of a tensor's elements.
#[derive(Clone, Debug)]
struct OffsetsBase {
    /// Remaining number of elements this iterator will yield.
    ///
    /// The offsets and positions in other fields are only valid if this is
    /// non-zero.
    len: usize,

    /// Component of next element offset from innermost (fastest-changing) dims.
    inner_offset: usize,

    /// Current position in innermost dims.
    inner_pos: [IterPos; INNER_NDIM],

    /// Component of next element offset from outermost (slowest-changing) dims.
    outer_offset: usize,

    /// Current position in outermost dims.
    ///
    /// Optimization note: The number of outermost dims will usually be small,
    /// so you might be tempted to use `SmallVec`. However this resulted in
    /// worse performance for `IndexingIterBase::step`, as the compiler was
    /// less likely/able to unroll iteration loops.
    outer_pos: Vec<IterPos>,
}

impl OffsetsBase {
    /// Create an iterator over element offsets in `tensor`.
    fn new<L: Layout>(layout: &L) -> OffsetsBase {
        // Merge axes to maximize the number of iterations that use the fast
        // path for stepping over the inner dimensions.
        let merged = merge_axes(layout.shape().as_ref(), layout.strides().as_ref());

        let inner_pos_pad = INNER_NDIM.saturating_sub(merged.len());
        let n_outer = merged.len().saturating_sub(INNER_NDIM);

        let inner_pos = std::array::from_fn(|dim| {
            let (size, stride) = if dim < inner_pos_pad {
                (1, 0)
            } else {
                merged[n_outer + dim - inner_pos_pad]
            };
            IterPos::from_size_stride(size, stride)
        });

        let outer_pos = (0..n_outer)
            .map(|i| {
                let (size, stride) = merged[i];
                IterPos::from_size_stride(size, stride)
            })
            .collect();

        OffsetsBase {
            len: merged.iter().map(|dim| dim.0).product(),
            inner_pos,
            inner_offset: 0,
            outer_pos,
            outer_offset: 0,
        }
    }

    /// Step in the outer dimensions.
    ///
    /// Returns `true` if the position was advanced or `false` if the end was
    /// reached.
    fn step_outer_pos(&mut self) -> bool {
        let mut done = self.outer_pos.is_empty();
        for (i, dim) in self.outer_pos.iter_mut().enumerate().rev() {
            if dim.step() {
                break;
            } else if i == 0 {
                done = true;
            }
        }
        self.outer_offset = self.outer_pos.iter().map(|p| p.offset).sum();
        !done
    }

    fn pos(&self, dim: usize) -> IterPos {
        let outer_ndim = self.outer_pos.len();
        if dim >= outer_ndim {
            self.inner_pos[dim - outer_ndim]
        } else {
            self.outer_pos[dim]
        }
    }

    fn pos_mut(&mut self, dim: usize) -> &mut IterPos {
        let outer_ndim = self.outer_pos.len();
        if dim >= outer_ndim {
            &mut self.inner_pos[dim - outer_ndim]
        } else {
            &mut self.outer_pos[dim]
        }
    }

    /// Advance iterator by up to `n` indices.
    fn step_by(&mut self, n: usize) {
        let mut remaining = n.min(self.len);
        self.len -= remaining;

        for dim in (0..self.ndim()).rev() {
            if remaining == 0 {
                break;
            }

            let pos = self.pos_mut(dim);
            let size = pos.size();
            let new_index = pos.index() + remaining;
            pos.set_index(new_index % size);
            remaining = new_index / size;
        }

        // Update offset of next element.
        self.inner_offset = self.inner_pos.iter().map(|p| p.offset).sum();
        self.outer_offset = self.outer_pos.iter().map(|p| p.offset).sum();
    }

    fn ndim(&self) -> usize {
        self.outer_pos.len() + self.inner_pos.len()
    }

    /// Compute the storage offset of an element given a linear index into a
    /// tensor's element sequence.
    fn offset_from_linear_index(&self, index: usize) -> usize {
        let mut offset = 0;
        let mut shape_product = 1;
        for dim in (0..self.ndim()).rev() {
            let pos = self.pos(dim);
            let dim_index = (index / shape_product) % pos.size();
            shape_product *= pos.size();
            offset += dim_index * pos.stride;
        }
        offset
    }

    /// Truncate this iterator so that it yields at most `len` elements.
    fn truncate(&mut self, len: usize) {
        // We adjust `self.len` here but not any of the iteration positions.
        // This means that methods like `next` and `fold` must always check
        // `self.len` before each step.
        self.len = self.len.min(len);
    }
}

impl Iterator for OffsetsBase {
    type Item = usize;

    #[inline(always)]
    fn next(&mut self) -> Option<usize> {
        if self.len == 0 {
            return None;
        }
        let offset = self.outer_offset + self.inner_offset;

        self.len -= 1;

        // Optimistically update offset, assuming we haven't reached the
        // end of the last dimension.
        self.inner_offset += self.inner_pos[1].stride;

        // Use a fast path to step inner dimensions and fall back to the slower
        // path to step the outer dimensions only when we reach the end.
        if !self.inner_pos[1].step() {
            if !self.inner_pos[0].step() {
                self.step_outer_pos();
            }

            // `inner_offset` is the sum of `inner_pos[i].offset`. It only
            // contains two entries, and we know `inner_pos[1].offset` is zero
            // since `inner_pos[1].step()` returned false. Hence we can use
            // an assignment.
            self.inner_offset = self.inner_pos[0].offset;
        }

        Some(offset)
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        (self.len, Some(self.len))
    }

    fn fold<B, F>(mut self, init: B, mut f: F) -> B
    where
        Self: Sized,
        F: FnMut(B, usize) -> B,
    {
        // Iter positions are only valid if `self.len > 0`.
        if self.len == 0 {
            return init;
        }

        let mut accum = init;
        'outer: loop {
            for i0 in self.inner_pos[0].index()..self.inner_pos[0].size() {
                for i1 in self.inner_pos[1].index()..self.inner_pos[1].size() {
                    let inner_offset =
                        i0 * self.inner_pos[0].stride + i1 * self.inner_pos[1].stride;
                    accum = f(accum, self.outer_offset + inner_offset);

                    self.len -= 1;
                    if self.len == 0 {
                        break 'outer;
                    }
                }
                self.inner_pos[1].set_index(0);
            }
            self.inner_pos[0].set_index(0);

            if !self.step_outer_pos() {
                break;
            }
        }

        accum
    }
}

impl ExactSizeIterator for OffsetsBase {}

impl DoubleEndedIterator for OffsetsBase {
    fn next_back(&mut self) -> Option<usize> {
        if self.len == 0 {
            return None;
        }

        // This is inefficient compared to forward iteration, but that's OK
        // because reverse iteration is not performance critical.
        let index = self.len - 1;
        let offset = self.offset_from_linear_index(index);
        self.len -= 1;

        Some(offset)
    }
}

impl SplitIterator for OffsetsBase {
    /// Split this iterator into two. The left result visits indices before
    /// `index`, the right result visits indices from `index` onwards.
    fn split_at(mut self, index: usize) -> (Self, Self) {
        assert!(self.len >= index);

        let mut right = self.clone();
        OffsetsBase::step_by(&mut right, index);

        self.truncate(index);

        (self, right)
    }
}

/// Iterator over elements of a tensor, in their logical order.
pub struct Iter<'a, T> {
    offsets: Offsets,
    data: ViewData<'a, T>,
}

impl<'a, T> Iter<'a, T> {
    pub(super) fn new<L: Layout + Clone>(view: TensorBase<ViewData<'a, T>, L>) -> Iter<'a, T> {
        Iter {
            offsets: Offsets::new(view.layout()),
            data: view.storage(),
        }
    }
}

impl<T> Clone for Iter<'_, T> {
    fn clone(&self) -> Self {
        Iter {
            offsets: self.offsets.clone(),
            data: self.data,
        }
    }
}

impl<'a, T> Iterator for Iter<'a, T> {
    type Item = &'a T;

    #[inline(always)]
    fn next(&mut self) -> Option<Self::Item> {
        let offset = self.offsets.next()?;

        // Safety: Offset is valid for data length.
        Some(unsafe { self.data.get_unchecked(offset) })
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.offsets.size_hint()
    }

    fn nth(&mut self, n: usize) -> Option<Self::Item> {
        let offset = self.offsets.nth(n)?;

        // Safety: Offset is valid for data length.
        Some(unsafe { self.data.get_unchecked(offset) })
    }

    fn fold<B, F>(self, init: B, mut f: F) -> B
    where
        Self: Sized,
        F: FnMut(B, Self::Item) -> B,
    {
        self.offsets.fold(init, |acc, offset| {
            // Safety: Offset is valid for data length.
            let item = unsafe { self.data.get_unchecked(offset) };
            f(acc, item)
        })
    }
}

impl<'a, T> DoubleEndedIterator for Iter<'a, T> {
    fn next_back(&mut self) -> Option<Self::Item> {
        let offset = self.offsets.next_back()?;

        // Safety: Offset is valid for data length.
        Some(unsafe { self.data.get_unchecked(offset) })
    }
}

impl<T> ExactSizeIterator for Iter<'_, T> {}

impl<T> FusedIterator for Iter<'_, T> {}

/// Wrapper around [`transmute`] which allows transmuting only the lifetime,
/// not the type, of a reference.
unsafe fn transmute_lifetime_mut<'a, 'b, T>(x: &'a mut T) -> &'b mut T {
    unsafe { transmute::<&'a mut T, &'b mut T>(x) }
}

/// Mutable iterator over elements of a tensor.
pub struct IterMut<'a, T> {
    offsets: Offsets,
    data: ViewMutData<'a, T>,
}

impl<'a, T> IterMut<'a, T> {
    pub(super) fn new<L: Layout + Clone>(
        view: TensorBase<ViewMutData<'a, T>, L>,
    ) -> IterMut<'a, T> {
        IterMut {
            offsets: Offsets::new(view.layout()),
            data: view.into_storage(),
        }
    }
}

impl<'a, T> Iterator for IterMut<'a, T> {
    type Item = &'a mut T;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        let offset = self.offsets.next()?;

        // Safety: Offset is valid for data length, `offsets.next` yields each
        // offset only once.
        Some(unsafe { transmute_lifetime_mut(self.data.get_unchecked_mut(offset)) })
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        self.offsets.size_hint()
    }

    fn nth(&mut self, n: usize) -> Option<Self::Item> {
        let offset = self.offsets.nth(n)?;

        // Safety: Offset is valid for data length, `offsets.next` yields each
        // offset only once.
        Some(unsafe { transmute_lifetime_mut(self.data.get_unchecked_mut(offset)) })
    }

    fn fold<B, F>(mut self, init: B, mut f: F) -> B
    where
        Self: Sized,
        F: FnMut(B, Self::Item) -> B,
    {
        self.offsets.fold(init, |acc, offset| {
            // Safety: Offset is valid for data length, `offsets.fold` yields
            // each offset only once.
            let item = unsafe { transmute_lifetime_mut(self.data.get_unchecked_mut(offset)) };
            f(acc, item)
        })
    }
}

impl<T> DoubleEndedIterator for IterMut<'_, T> {
    fn next_back(&mut self) -> Option<Self::Item> {
        let offset = self.offsets.next_back()?;

        // Safety: Offset is valid for data length, `offsets.next` yields each
        // offset only once.
        Some(unsafe { transmute_lifetime_mut(self.data.get_unchecked_mut(offset)) })
    }
}

impl<T> ExactSizeIterator for IterMut<'_, T> {}

impl<T> FusedIterator for IterMut<'_, T> {}

#[derive(Clone)]
enum OffsetsKind {
    Range(Range<usize>),
    Indexing(OffsetsBase),
}

/// Iterator over element offsets of a tensor.
///
/// `Offsets` does not hold a reference to the tensor, allowing the tensor to
/// be modified during iteration. It is the caller's responsibilty not to modify
/// the tensor in ways that invalidate the offset sequence returned by this
/// iterator.
#[derive(Clone)]
struct Offsets {
    base: OffsetsKind,
}

impl Offsets {
    fn new<L: Layout>(layout: &L) -> Offsets {
        Offsets {
            base: if layout.is_contiguous() {
                OffsetsKind::Range(0..layout.min_data_len())
            } else {
                OffsetsKind::Indexing(OffsetsBase::new(layout))
            },
        }
    }
}

impl Iterator for Offsets {
    type Item = usize;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        match &mut self.base {
            OffsetsKind::Range(r) => r.next(),
            OffsetsKind::Indexing(base) => base.next(),
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        match &self.base {
            OffsetsKind::Range(r) => r.size_hint(),
            OffsetsKind::Indexing(base) => (base.len, Some(base.len)),
        }
    }

    fn nth(&mut self, n: usize) -> Option<Self::Item> {
        match &mut self.base {
            OffsetsKind::Range(r) => r.nth(n),
            OffsetsKind::Indexing(base) => {
                base.step_by(n);
                self.next()
            }
        }
    }

    fn fold<B, F>(self, init: B, f: F) -> B
    where
        Self: Sized,
        F: FnMut(B, Self::Item) -> B,
    {
        match self.base {
            OffsetsKind::Range(r) => r.fold(init, f),
            OffsetsKind::Indexing(base) => base.fold(init, f),
        }
    }
}

impl DoubleEndedIterator for Offsets {
    fn next_back(&mut self) -> Option<Self::Item> {
        match &mut self.base {
            OffsetsKind::Range(r) => r.next_back(),
            OffsetsKind::Indexing(base) => base.next_back(),
        }
    }
}

impl ExactSizeIterator for Offsets {}

impl FusedIterator for Offsets {}

/// Iterator over the ranges of a tensor's data that correspond to 1D lanes
/// along a particular dimension.
struct LaneRanges {
    /// Start offsets of each lane.
    offsets: Offsets,

    // Number of elements in each lane and gap between them.
    dim_size: usize,
    dim_stride: usize,
}

impl LaneRanges {
    fn new<L: Layout + RemoveDim>(layout: &L, dim: usize) -> LaneRanges {
        // If the layout is empty (has any zero-sized dims), we need to make
        // sure that `offsets` is as well.
        let offsets = if layout.is_empty() {
            Offsets::new(layout)
        } else {
            let other_dims = layout.remove_dim(dim);
            Offsets::new(&other_dims)
        };

        LaneRanges {
            offsets,
            dim_size: layout.size(dim),
            dim_stride: layout.stride(dim),
        }
    }

    /// Return the range of storage offsets for a 1D lane where the first
    /// element is at `start_offset`.
    fn lane_offset_range(&self, start_offset: usize) -> Range<usize> {
        lane_offsets(start_offset, self.dim_size, self.dim_stride)
    }
}

fn lane_offsets(start_offset: usize, size: usize, stride: usize) -> Range<usize> {
    start_offset..start_offset + (size - 1) * stride + 1
}

impl Iterator for LaneRanges {
    type Item = Range<usize>;

    #[inline]
    fn next(&mut self) -> Option<Range<usize>> {
        self.offsets
            .next()
            .map(|offset| self.lane_offset_range(offset))
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.offsets.size_hint()
    }

    fn fold<B, F>(self, init: B, mut f: F) -> B
    where
        Self: Sized,
        F: FnMut(B, Self::Item) -> B,
    {
        let Self {
            offsets,
            dim_size,
            dim_stride,
        } = self;

        offsets.fold(init, |acc, offset| {
            f(acc, lane_offsets(offset, dim_size, dim_stride))
        })
    }
}

impl DoubleEndedIterator for LaneRanges {
    fn next_back(&mut self) -> Option<Range<usize>> {
        self.offsets
            .next_back()
            .map(|offset| self.lane_offset_range(offset))
    }
}

impl ExactSizeIterator for LaneRanges {}

impl FusedIterator for LaneRanges {}

/// Iterator over 1D slices of a tensor along a target dimension of size N.
///
/// Conceptually this iterator steps through every distinct slice of a tensor
/// where a target dim is varied from 0..N and other indices are held fixed.
pub struct Lanes<'a, T> {
    data: ViewData<'a, T>,
    ranges: LaneRanges,
    lane_layout: NdLayout<1>,
}

/// Iterator over items in a 1D slice of a tensor.
#[derive(Clone, Debug)]
pub struct Lane<'a, T> {
    view: NdTensorView<'a, T, 1>,
    index: usize,
}

impl<'a, T> Lane<'a, T> {
    /// Return the remaining part of the lane as a slice, if it is contiguous.
    pub fn as_slice(&self) -> Option<&'a [T]> {
        self.view.data().map(|data| &data[self.index..])
    }

    /// Return the item at a given index in this lane.
    pub fn get(&self, idx: usize) -> Option<&'a T> {
        self.view.get([idx])
    }

    /// Return the entire lane as a 1D tensor view.
    pub fn as_view(&self) -> NdTensorView<'a, T, 1> {
        self.view
    }
}

impl<'a, T> From<NdTensorView<'a, T, 1>> for Lane<'a, T> {
    fn from(val: NdTensorView<'a, T, 1>) -> Self {
        Lane {
            view: val,
            index: 0,
        }
    }
}

impl<'a, T> Iterator for Lane<'a, T> {
    type Item = &'a T;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        if self.index < self.view.len() {
            let index = self.index;
            self.index += 1;

            // Safety: Index is in bounds for axis 0.
            Some(unsafe { self.view.get_unchecked([index]) })
        } else {
            None
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let size = self.view.size(0);
        (size, Some(size))
    }
}

impl<T> ExactSizeIterator for Lane<'_, T> {}

impl<T> FusedIterator for Lane<'_, T> {}

impl<T: PartialEq> PartialEq<Lane<'_, T>> for Lane<'_, T> {
    fn eq(&self, other: &Lane<'_, T>) -> bool {
        self.view.slice(self.index..) == other.view.slice(other.index..)
    }
}

impl<T: PartialEq> PartialEq<Lane<'_, T>> for LaneMut<'_, T> {
    fn eq(&self, other: &Lane<'_, T>) -> bool {
        self.view.slice(self.index..) == other.view.slice(other.index..)
    }
}

impl<'a, T> Lanes<'a, T> {
    /// Create an iterator which yields all possible slices over the `dim`
    /// dimension of `tensor`.
    pub(crate) fn new<L: Layout + RemoveDim + Clone>(
        view: TensorBase<ViewData<'a, T>, L>,
        dim: usize,
    ) -> Lanes<'a, T> {
        let size = view.size(dim);
        let stride = view.stride(dim);
        let lane_layout =
            NdLayout::from_shape_and_strides([size], [stride], OverlapPolicy::AllowOverlap)
                .unwrap();
        Lanes {
            data: view.storage(),
            ranges: LaneRanges::new(view.layout(), dim),
            lane_layout,
        }
    }
}

fn lane_for_offset_range<T>(
    data: ViewData<T>,
    layout: NdLayout<1>,
    offsets: Range<usize>,
) -> Lane<T> {
    let view = NdTensorView::from_storage_and_layout(data.slice(offsets), layout);
    Lane { view, index: 0 }
}

impl<'a, T> Iterator for Lanes<'a, T> {
    type Item = Lane<'a, T>;

    /// Yield the next slice over the target dimension.
    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        self.ranges
            .next()
            .map(|range| lane_for_offset_range(self.data, self.lane_layout, range))
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.ranges.size_hint()
    }

    fn fold<B, F>(self, init: B, mut f: F) -> B
    where
        Self: Sized,
        F: FnMut(B, Self::Item) -> B,
    {
        self.ranges.fold(init, |acc, offsets| {
            let lane = lane_for_offset_range(self.data, self.lane_layout, offsets);
            f(acc, lane)
        })
    }
}

impl<T> DoubleEndedIterator for Lanes<'_, T> {
    fn next_back(&mut self) -> Option<Self::Item> {
        self.ranges
            .next_back()
            .map(|range| lane_for_offset_range(self.data, self.lane_layout, range))
    }
}

impl<T> ExactSizeIterator for Lanes<'_, T> {}

impl<T> FusedIterator for Lanes<'_, T> {}

/// Mutable version of [`Lanes`].
///
/// Unlike [`Lanes`], this does not implement [`Iterator`] due to complications
/// in implementing this for an iterator that returns mutable references, but
/// it has a similar interface.
pub struct LanesMut<'a, T> {
    data: ViewMutData<'a, T>,
    ranges: LaneRanges,
    lane_layout: NdLayout<1>,
}

impl<'a, T> LanesMut<'a, T> {
    /// Create an iterator which yields all possible slices over the `dim`
    /// dimension of `view`.
    pub(crate) fn new<L: Layout + RemoveDim + Clone>(
        view: TensorBase<ViewMutData<'a, T>, L>,
        dim: usize,
    ) -> LanesMut<'a, T> {
        // See notes in `Layout` about internal overlap.
        assert!(
            !view.is_broadcast(),
            "Cannot mutably iterate over broadcasting view"
        );

        let size = view.size(dim);
        let stride = view.stride(dim);

        // We allow overlap here to handle the case where the stride is zero,
        // but the tensor is empty. If the tensor was not empty, the assert above
        // would have caught this.
        let lane_layout =
            NdLayout::from_shape_and_strides([size], [stride], OverlapPolicy::AllowOverlap)
                .unwrap();

        LanesMut {
            ranges: LaneRanges::new(view.layout(), dim),
            data: view.into_storage(),
            lane_layout,
        }
    }
}

impl<'a, T> Iterator for LanesMut<'a, T> {
    type Item = LaneMut<'a, T>;

    #[inline]
    fn next(&mut self) -> Option<LaneMut<'a, T>> {
        self.ranges.next().map(|offsets| {
            // Safety: Offsets range length is sufficient for layout, elements
            // in each lane do not overlap.
            unsafe {
                LaneMut::from_storage_layout(self.data.to_view_slice_mut(offsets), self.lane_layout)
            }
        })
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.ranges.size_hint()
    }

    fn fold<B, F>(mut self, init: B, mut f: F) -> B
    where
        Self: Sized,
        F: FnMut(B, Self::Item) -> B,
    {
        self.ranges.fold(init, |acc, offsets| {
            // Safety: Offsets range length is sufficient for layout, elements
            // in each lane do not overlap.
            let lane = unsafe {
                LaneMut::from_storage_layout(self.data.to_view_slice_mut(offsets), self.lane_layout)
            };
            f(acc, lane)
        })
    }
}

impl<'a, T> ExactSizeIterator for LanesMut<'a, T> {}

impl<'a, T> DoubleEndedIterator for LanesMut<'a, T> {
    fn next_back(&mut self) -> Option<LaneMut<'a, T>> {
        self.ranges.next_back().map(|offsets| {
            // Safety: Offsets range length is sufficient for layout, elements
            // in each lane do not overlap.
            unsafe {
                LaneMut::from_storage_layout(self.data.to_view_slice_mut(offsets), self.lane_layout)
            }
        })
    }
}

/// Iterator over items in a 1D slice of a tensor.
#[derive(Debug)]
pub struct LaneMut<'a, T> {
    view: NdTensorViewMut<'a, T, 1>,
    index: usize,
}

impl<'a, T> LaneMut<'a, T> {
    /// Create a new lane given the storage and layout.
    ///
    /// # Safety
    ///
    /// - Caller must ensure that no two lanes are created which overlap.
    /// - Storage length must exceed `layout.min_data_len()`.
    unsafe fn from_storage_layout(data: ViewMutData<'a, T>, layout: NdLayout<1>) -> Self {
        let view = unsafe {
            // Safety: Caller promises that each call uses the offset ranges for
            // a different lane and that the range length is sufficient for the
            // lane's size and stride.
            NdTensorViewMut::from_storage_and_layout_unchecked(data, layout)
        };
        LaneMut { view, index: 0 }
    }

    /// Return the remaining part of the lane as a slice, if it is contiguous.
    pub fn as_slice_mut(&mut self) -> Option<&mut [T]> {
        self.view.data_mut().map(|data| &mut data[self.index..])
    }

    /// Return the entire lane as a mutable 1D tensor view.
    pub fn into_view(self) -> NdTensorViewMut<'a, T, 1> {
        self.view
    }
}

impl<'a, T> Iterator for LaneMut<'a, T> {
    type Item = &'a mut T;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        if self.index < self.view.size(0) {
            let index = self.index;
            self.index += 1;
            let item = unsafe { self.view.get_unchecked_mut([index]) };

            // Transmute to preserve lifetime of data. This is safe as we
            // yield each element only once.
            Some(unsafe { transmute::<&mut T, Self::Item>(item) })
        } else {
            None
        }
    }

    #[inline]
    fn nth(&mut self, nth: usize) -> Option<Self::Item> {
        self.index = (self.index + nth).min(self.view.size(0));
        self.next()
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let size = self.view.size(0);
        (size, Some(size))
    }
}

impl<T> ExactSizeIterator for LaneMut<'_, T> {}

impl<T: PartialEq> PartialEq<LaneMut<'_, T>> for LaneMut<'_, T> {
    fn eq(&self, other: &LaneMut<'_, T>) -> bool {
        self.view.slice(self.index..) == other.view.slice(other.index..)
    }
}

/// Base for iterators over views of the inner dimensions of a tensor, where
/// the inner dimensions have layout `L`.
struct InnerIterBase<L: Layout> {
    // Iterator over storage start offsets for each inner view. The storage
    // range for each view is `offset..offset + inner_data_len`.
    outer_offsets: Offsets,
    inner_layout: L,
    inner_data_len: usize,
}

impl<L: Layout + Clone> InnerIterBase<L> {
    fn new_impl<PL: Layout, F: Fn(&[usize], &[usize]) -> L>(
        parent_layout: &PL,
        inner_dims: usize,
        make_inner_layout: F,
    ) -> InnerIterBase<L> {
        assert!(parent_layout.ndim() >= inner_dims);
        let outer_dims = parent_layout.ndim() - inner_dims;
        let parent_shape = parent_layout.shape();
        let parent_strides = parent_layout.strides();
        let (outer_shape, inner_shape) = parent_shape.as_ref().split_at(outer_dims);
        let (outer_strides, inner_strides) = parent_strides.as_ref().split_at(outer_dims);

        let outer_layout = DynLayout::from_shape_and_strides(
            outer_shape,
            outer_strides,
            OverlapPolicy::AllowOverlap,
        )
        .unwrap();

        let inner_layout = make_inner_layout(inner_shape, inner_strides);

        InnerIterBase {
            outer_offsets: Offsets::new(&outer_layout),
            inner_data_len: inner_layout.min_data_len(),
            inner_layout,
        }
    }
}

impl<const N: usize> InnerIterBase<NdLayout<N>> {
    pub(crate) fn new<L: Layout>(parent_layout: &L) -> Self {
        Self::new_impl(parent_layout, N, |inner_shape, inner_strides| {
            let inner_shape: [usize; N] = inner_shape.try_into().unwrap();
            let inner_strides: [usize; N] = inner_strides.try_into().unwrap();
            NdLayout::from_shape_and_strides(
                inner_shape,
                inner_strides,
                // We allow overlap here, but the view that owns `parent_layout`
                // will enforce there is no overlap if it is a mutable view.
                OverlapPolicy::AllowOverlap,
            )
            .expect("failed to create layout")
        })
    }
}

impl InnerIterBase<DynLayout> {
    pub(crate) fn new_dyn<L: Layout>(parent_layout: &L, inner_dims: usize) -> Self {
        Self::new_impl(parent_layout, inner_dims, |inner_shape, inner_strides| {
            DynLayout::from_shape_and_strides(
                inner_shape,
                inner_strides,
                // We allow overlap here, but the view that owns `parent_layout`
                // will enforce there is no overlap if it is a mutable view.
                OverlapPolicy::AllowOverlap,
            )
            .expect("failed to create layout")
        })
    }
}

impl<L: Layout> Iterator for InnerIterBase<L> {
    /// Storage offset range for next view
    type Item = Range<usize>;

    fn next(&mut self) -> Option<Range<usize>> {
        self.outer_offsets
            .next()
            .map(|offset| offset..offset + self.inner_data_len)
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.outer_offsets.size_hint()
    }

    fn fold<B, F>(self, init: B, mut f: F) -> B
    where
        Self: Sized,
        F: FnMut(B, Self::Item) -> B,
    {
        self.outer_offsets.fold(init, |acc, offset| {
            f(acc, offset..offset + self.inner_data_len)
        })
    }
}

impl<L: Layout> ExactSizeIterator for InnerIterBase<L> {}

impl<L: Layout> DoubleEndedIterator for InnerIterBase<L> {
    fn next_back(&mut self) -> Option<Self::Item> {
        self.outer_offsets
            .next_back()
            .map(|offset| offset..offset + self.inner_data_len)
    }
}

/// Iterator over views of the innermost dimensions of a tensor, where the
/// tensor has element type T and the inner dimensions have layout L.
pub struct InnerIter<'a, T, L: Layout> {
    base: InnerIterBase<L>,
    data: ViewData<'a, T>,
}

impl<'a, T, const N: usize> InnerIter<'a, T, NdLayout<N>> {
    pub(crate) fn new<L: Layout + Clone>(view: TensorBase<ViewData<'a, T>, L>) -> Self {
        let base = InnerIterBase::new(&view);
        InnerIter {
            base,
            data: view.storage(),
        }
    }
}

impl<'a, T> InnerIter<'a, T, DynLayout> {
    pub(crate) fn new_dyn<L: Layout + Clone>(
        view: TensorBase<ViewData<'a, T>, L>,
        inner_dims: usize,
    ) -> Self {
        let base = InnerIterBase::new_dyn(&view, inner_dims);
        InnerIter {
            base,
            data: view.storage(),
        }
    }
}

impl<'a, T, L: Layout + Clone> Iterator for InnerIter<'a, T, L> {
    type Item = TensorBase<ViewData<'a, T>, L>;

    fn next(&mut self) -> Option<Self::Item> {
        self.base.next().map(|offset_range| {
            TensorBase::from_storage_and_layout(
                self.data.slice(offset_range),
                self.base.inner_layout.clone(),
            )
        })
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.base.size_hint()
    }

    fn fold<B, F>(self, init: B, mut f: F) -> B
    where
        Self: Sized,
        F: FnMut(B, Self::Item) -> B,
    {
        let inner_layout = self.base.inner_layout.clone();
        self.base.fold(init, |acc, offset_range| {
            let item = TensorBase::from_storage_and_layout(
                self.data.slice(offset_range),
                inner_layout.clone(),
            );
            f(acc, item)
        })
    }
}

impl<T, L: Layout + Clone> ExactSizeIterator for InnerIter<'_, T, L> {}

impl<T, L: Layout + Clone> DoubleEndedIterator for InnerIter<'_, T, L> {
    fn next_back(&mut self) -> Option<Self::Item> {
        self.base.next_back().map(|offset_range| {
            TensorBase::from_storage_and_layout(
                self.data.slice(offset_range),
                self.base.inner_layout.clone(),
            )
        })
    }
}

/// Iterator over mutable views of the innermost dimensions of a tensor, where
/// the tensor has element type T and the inner dimensions have layout L.
pub struct InnerIterMut<'a, T, L: Layout> {
    base: InnerIterBase<L>,
    data: ViewMutData<'a, T>,
}

impl<'a, T, const N: usize> InnerIterMut<'a, T, NdLayout<N>> {
    pub(crate) fn new<L: Layout>(view: TensorBase<ViewMutData<'a, T>, L>) -> Self {
        let base = InnerIterBase::new(&view);
        InnerIterMut {
            base,
            data: view.into_storage(),
        }
    }
}

impl<'a, T> InnerIterMut<'a, T, DynLayout> {
    pub(crate) fn new_dyn<L: Layout>(
        view: TensorBase<ViewMutData<'a, T>, L>,
        inner_dims: usize,
    ) -> Self {
        let base = InnerIterBase::new_dyn(&view, inner_dims);
        InnerIterMut {
            base,
            data: view.into_storage(),
        }
    }
}

impl<'a, T, L: Layout + Clone> Iterator for InnerIterMut<'a, T, L> {
    type Item = TensorBase<ViewMutData<'a, T>, L>;

    fn next(&mut self) -> Option<Self::Item> {
        self.base.next().map(|offset_range| {
            let storage = self.data.slice_mut(offset_range);
            let storage = unsafe {
                // Safety: The iterator was constructed from a tensor with a
                // non-overlapping layout, and no two views yielded by this
                // iterator overlap. Hence we can transmute the lifetime without
                // creating multiple mutable references to the same elements.
                std::mem::transmute::<ViewMutData<'_, T>, ViewMutData<'a, T>>(storage)
            };
            TensorBase::from_storage_and_layout(storage, self.base.inner_layout.clone())
        })
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.base.size_hint()
    }

    fn fold<B, F>(mut self, init: B, mut f: F) -> B
    where
        Self: Sized,
        F: FnMut(B, Self::Item) -> B,
    {
        let inner_layout = self.base.inner_layout.clone();
        self.base.fold(init, |acc, offset_range| {
            let storage = self.data.slice_mut(offset_range);
            let storage = unsafe {
                // Safety: The iterator was constructed from a tensor with a
                // non-overlapping layout, and no two views yielded by this
                // iterator overlap. Hence we can transmute the lifetime without
                // creating multiple mutable references to the same elements.
                std::mem::transmute::<ViewMutData<'_, T>, ViewMutData<'a, T>>(storage)
            };
            let item = TensorBase::from_storage_and_layout(storage, inner_layout.clone());
            f(acc, item)
        })
    }
}

impl<T, L: Layout + Clone> ExactSizeIterator for InnerIterMut<'_, T, L> {}

impl<'a, T, L: Layout + Clone> DoubleEndedIterator for InnerIterMut<'a, T, L> {
    fn next_back(&mut self) -> Option<Self::Item> {
        self.base.next_back().map(|offset_range| {
            let storage = self.data.slice_mut(offset_range);
            let storage = unsafe {
                // Safety: Outer view is non-broadcasting, and we increment the
                // outer index each time, so returned views will not overlap.
                std::mem::transmute::<ViewMutData<'_, T>, ViewMutData<'a, T>>(storage)
            };
            TensorBase::from_storage_and_layout(storage, self.base.inner_layout.clone())
        })
    }
}

/// Iterator over slices of a tensor along an axis. See
/// [`TensorView::axis_iter`](crate::TensorView::axis_iter).
pub struct AxisIter<'a, T, L: Layout + RemoveDim> {
    view: TensorBase<ViewData<'a, T>, L>,
    axis: usize,
    index: usize,
    end: usize,
}

impl<'a, T, L: MutLayout + RemoveDim> AxisIter<'a, T, L> {
    pub(crate) fn new(view: &TensorBase<ViewData<'a, T>, L>, axis: usize) -> AxisIter<'a, T, L> {
        assert!(axis < view.ndim());
        AxisIter {
            view: view.clone(),
            axis,
            index: 0,
            end: view.size(axis),
        }
    }
}

impl<'a, T, L: MutLayout + RemoveDim> Iterator for AxisIter<'a, T, L> {
    type Item = TensorBase<ViewData<'a, T>, <L as RemoveDim>::Output>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.index >= self.end {
            None
        } else {
            let slice = self.view.index_axis(self.axis, self.index);
            self.index += 1;
            Some(slice)
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let len = self.end - self.index;
        (len, Some(len))
    }
}

impl<'a, T, L: MutLayout + RemoveDim> ExactSizeIterator for AxisIter<'a, T, L> {}

impl<'a, T, L: MutLayout + RemoveDim> DoubleEndedIterator for AxisIter<'a, T, L> {
    fn next_back(&mut self) -> Option<Self::Item> {
        if self.index >= self.end {
            None
        } else {
            let slice = self.view.index_axis(self.axis, self.end - 1);
            self.end -= 1;
            Some(slice)
        }
    }
}

/// Iterator over mutable slices of a tensor along an axis. See [`TensorViewMut::axis_iter_mut`].
pub struct AxisIterMut<'a, T, L: Layout + RemoveDim> {
    view: TensorBase<ViewMutData<'a, T>, L>,
    axis: usize,
    index: usize,
    end: usize,
}

impl<'a, T, L: Layout + RemoveDim + Clone> AxisIterMut<'a, T, L> {
    pub(crate) fn new(
        view: TensorBase<ViewMutData<'a, T>, L>,
        axis: usize,
    ) -> AxisIterMut<'a, T, L> {
        // See notes in `Layout` about internal overlap.
        assert!(
            !view.layout().is_broadcast(),
            "Cannot mutably iterate over broadcasting view"
        );
        assert!(axis < view.ndim());
        AxisIterMut {
            axis,
            index: 0,
            end: view.size(axis),
            view,
        }
    }
}

/// Mutable tensor view with one less dimension than `L`.
type SmallerMutView<'b, T, L> = TensorBase<ViewMutData<'b, T>, <L as RemoveDim>::Output>;

impl<'a, T, L: MutLayout + RemoveDim> Iterator for AxisIterMut<'a, T, L> {
    type Item = TensorBase<ViewMutData<'a, T>, <L as RemoveDim>::Output>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.index >= self.end {
            None
        } else {
            let index = self.index;
            self.index += 1;

            let slice = self.view.index_axis_mut(self.axis, index);

            // Promote lifetime from self -> 'a.
            //
            // Safety: This is non-broadcasting view, and we increment the index
            // each time, so returned views will not overlap.
            let view = unsafe { transmute::<SmallerMutView<'_, T, L>, Self::Item>(slice) };

            Some(view)
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let len = self.end - self.index;
        (len, Some(len))
    }
}

impl<'a, T, L: MutLayout + RemoveDim> ExactSizeIterator for AxisIterMut<'a, T, L> {}

impl<'a, T, L: MutLayout + RemoveDim> DoubleEndedIterator for AxisIterMut<'a, T, L> {
    fn next_back(&mut self) -> Option<Self::Item> {
        if self.index >= self.end {
            None
        } else {
            let index = self.end - 1;
            self.end -= 1;

            let slice = self.view.index_axis_mut(self.axis, index);

            // Promote lifetime from self -> 'a.
            //
            // Safety: This is non-broadcasting view, and we increment the index
            // each time, so returned views will not overlap.
            let view = unsafe { transmute::<SmallerMutView<'_, T, L>, Self::Item>(slice) };

            Some(view)
        }
    }
}

/// Iterator over slices of a tensor along an axis. See
/// [`TensorView::axis_chunks`](crate::TensorView::axis_chunks).
pub struct AxisChunks<'a, T, L: MutLayout> {
    remainder: Option<TensorBase<ViewData<'a, T>, L>>,
    axis: usize,
    chunk_size: usize,
}

impl<'a, T, L: MutLayout> AxisChunks<'a, T, L> {
    pub(crate) fn new(
        view: &TensorBase<ViewData<'a, T>, L>,
        axis: usize,
        chunk_size: usize,
    ) -> AxisChunks<'a, T, L> {
        assert!(chunk_size > 0, "chunk size must be > 0");
        AxisChunks {
            remainder: if view.size(axis) > 0 {
                Some(view.view())
            } else {
                None
            },
            axis,
            chunk_size,
        }
    }
}

impl<'a, T, L: MutLayout> Iterator for AxisChunks<'a, T, L> {
    type Item = TensorBase<ViewData<'a, T>, L>;

    fn next(&mut self) -> Option<Self::Item> {
        let remainder = self.remainder.take()?;
        let chunk_len = self.chunk_size.min(remainder.size(self.axis));
        let (current, next_remainder) = remainder.split_at(self.axis, chunk_len);
        self.remainder = if next_remainder.size(self.axis) > 0 {
            Some(next_remainder)
        } else {
            None
        };
        Some(current)
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let len = self
            .remainder
            .as_ref()
            .map(|r| r.size(self.axis))
            .unwrap_or(0)
            .div_ceil(self.chunk_size);
        (len, Some(len))
    }
}

impl<'a, T, L: MutLayout> ExactSizeIterator for AxisChunks<'a, T, L> {}

impl<'a, T, L: MutLayout> DoubleEndedIterator for AxisChunks<'a, T, L> {
    fn next_back(&mut self) -> Option<Self::Item> {
        let remainder = self.remainder.take()?;
        let chunk_len = self.chunk_size.min(remainder.size(self.axis));
        let (prev_remainder, current) =
            remainder.split_at(self.axis, remainder.size(self.axis) - chunk_len);
        self.remainder = if prev_remainder.size(self.axis) > 0 {
            Some(prev_remainder)
        } else {
            None
        };
        Some(current)
    }
}

/// Iterator over mutable slices of a tensor along an axis. See [`TensorViewMut::axis_chunks_mut`].
pub struct AxisChunksMut<'a, T, L: MutLayout> {
    remainder: Option<TensorBase<ViewMutData<'a, T>, L>>,
    axis: usize,
    chunk_size: usize,
}

impl<'a, T, L: MutLayout> AxisChunksMut<'a, T, L> {
    pub(crate) fn new(
        view: TensorBase<ViewMutData<'a, T>, L>,
        axis: usize,
        chunk_size: usize,
    ) -> AxisChunksMut<'a, T, L> {
        // See notes in `Layout` about internal overlap.
        assert!(
            !view.layout().is_broadcast(),
            "Cannot mutably iterate over broadcasting view"
        );
        assert!(chunk_size > 0, "chunk size must be > 0");
        AxisChunksMut {
            remainder: if view.size(axis) > 0 {
                Some(view)
            } else {
                None
            },
            axis,
            chunk_size,
        }
    }
}

impl<'a, T, L: MutLayout> Iterator for AxisChunksMut<'a, T, L> {
    type Item = TensorBase<ViewMutData<'a, T>, L>;

    fn next(&mut self) -> Option<Self::Item> {
        let remainder = self.remainder.take()?;
        let chunk_len = self.chunk_size.min(remainder.size(self.axis));
        let (current, next_remainder) = remainder.split_at_mut(self.axis, chunk_len);
        self.remainder = if next_remainder.size(self.axis) > 0 {
            Some(next_remainder)
        } else {
            None
        };
        Some(current)
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let len = self
            .remainder
            .as_ref()
            .map(|r| r.size(self.axis))
            .unwrap_or(0)
            .div_ceil(self.chunk_size);
        (len, Some(len))
    }
}

impl<'a, T, L: MutLayout> ExactSizeIterator for AxisChunksMut<'a, T, L> {}

impl<'a, T, L: MutLayout> DoubleEndedIterator for AxisChunksMut<'a, T, L> {
    fn next_back(&mut self) -> Option<Self::Item> {
        let remainder = self.remainder.take()?;
        let remainder_size = remainder.size(self.axis);
        let chunk_len = self.chunk_size.min(remainder_size);
        let (prev_remainder, current) =
            remainder.split_at_mut(self.axis, remainder_size - chunk_len);
        self.remainder = if prev_remainder.size(self.axis) > 0 {
            Some(prev_remainder)
        } else {
            None
        };
        Some(current)
    }
}

/// Call `f` on each element of `view`.
pub(crate) fn for_each_mut<T, F: Fn(&mut T)>(mut view: TensorViewMut<T>, f: F) {
    while view.ndim() < 4 {
        view.insert_axis(0);
    }

    // This could be improved by sorting dimensions of `view` in order of
    // decreasing stride. If the resulting view is contiguous, `f` can be
    // applied to the underlying data directly. Even if it isn't, this will
    // still make memory access as contiguous as possible.

    view.inner_iter_mut::<4>().for_each(|mut src| {
        for i0 in 0..src.size(0) {
            for i1 in 0..src.size(1) {
                for i2 in 0..src.size(2) {
                    for i3 in 0..src.size(3) {
                        // Safety: i0..i3 are in `[0, src.size(i))`.
                        let x = unsafe { src.get_unchecked_mut([i0, i1, i2, i3]) };
                        f(x);
                    }
                }
            }
        }
    });
}

// Tests for iterator internals. Most tests of iterators are currently done via
// tests on tensor methods.
#[cfg(test)]
mod tests {
    use super::{AxisChunks, AxisChunksMut, Lanes, LanesMut};
    use crate::{AsView, Layout, NdLayout, NdTensor, Tensor};

    fn compare_reversed<T: PartialEq + std::fmt::Debug>(fwd: &[T], rev: &[T]) {
        assert_eq!(fwd.len(), rev.len());
        for (x, y) in fwd.iter().zip(rev.iter().rev()) {
            assert_eq!(x, y);
        }
    }

    /// Apply a standard set of tests to an iterator.
    fn test_iterator<I: Iterator + ExactSizeIterator + DoubleEndedIterator>(
        create_iter: impl Fn() -> I,
        expected: &[I::Item],
    ) where
        I::Item: PartialEq + std::fmt::Debug,
    {
        let iter = create_iter();

        let (min_len, max_len) = iter.size_hint();
        let items: Vec<_> = iter.collect();

        assert_eq!(&items, expected);

        // Test ExactSizeIterator via `size_hint`.
        assert_eq!(min_len, items.len(), "incorrect size lower bound");
        assert_eq!(max_len, Some(items.len()), "incorrect size upper bound");

        // Test DoubleEndedIterator via `rev`.
        let rev_items: Vec<_> = create_iter().rev().collect();
        compare_reversed(&items, &rev_items);

        // Test FusedIterator.
        let mut iter = create_iter();
        for _x in &mut iter { /* noop */ }
        assert_eq!(iter.next(), None);

        // Test fold.
        let mut fold_items = Vec::new();
        let mut idx = 0;
        create_iter().fold(0, |acc, item| {
            assert_eq!(acc, idx);
            fold_items.push(item);
            idx += 1;
            idx
        });
        assert_eq!(items, fold_items);
    }

    /// A collection that can be mutably iterated over multiple times.
    ///
    /// We use a different pattern for testing mutable iterators to avoid
    /// restrictions on values returned from `FnMut` closures.
    trait MutIterable {
        type Iter<'a>: Iterator + ExactSizeIterator + DoubleEndedIterator
        where
            Self: 'a;

        fn iter_mut(&mut self) -> Self::Iter<'_>;
    }

    /// Apply a standard set of tests to a mutable iterator.
    fn test_mut_iterator<M, T>(mut iterable: M, expected: &[T])
    where
        M: MutIterable,
        T: std::fmt::Debug,
        for<'a> <M::Iter<'a> as Iterator>::Item: std::fmt::Debug + PartialEq + PartialEq<T>,
    {
        // Test Iterator and ExactSizeIterator.
        {
            let iter = iterable.iter_mut();
            let (min_len, max_len) = iter.size_hint();
            let items: Vec<_> = iter.collect();

            // Test `next`
            assert_eq!(items, expected);

            // Test `size_hint`
            assert_eq!(min_len, items.len(), "incorrect size lower bound");
            assert_eq!(max_len, Some(items.len()), "incorrect size upper bound");
        }

        // Test FusedIterator.
        {
            let mut iter = iterable.iter_mut();
            for _x in &mut iter { /* noop */ }
            assert!(iter.next().is_none());
        }

        // Test DoubleEndedIterator via `rev`.
        //
        // We use `format!` here to convert mutable references into comparable
        // items that have no connection to the mutable references yielded by
        // the iterator. Ideally this should be replaced by a clone or something.
        {
            let items: Vec<_> = iterable.iter_mut().map(|x| format!("{:?}", x)).collect();
            let rev_items: Vec<_> = iterable
                .iter_mut()
                .rev()
                .map(|x| format!("{:?}", x))
                .collect();
            compare_reversed(&items, &rev_items);
        }

        // Test fold.
        {
            let items: Vec<_> = iterable.iter_mut().map(|x| format!("{:?}", x)).collect();
            let mut fold_items = Vec::new();
            let mut idx = 0;
            iterable.iter_mut().fold(0, |acc, item| {
                assert_eq!(acc, idx);
                fold_items.push(format!("{:?}", item));
                idx += 1;
                idx
            });
            assert_eq!(items, fold_items);
        }
    }

    #[test]
    fn test_axis_chunks() {
        let tensor = NdTensor::from([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]);
        test_iterator(
            || tensor.axis_chunks(0, 1),
            &[tensor.slice(0..1), tensor.slice(1..2)],
        );
    }

    #[test]
    fn test_axis_chunks_empty() {
        let x = Tensor::<i32>::zeros(&[5, 0]);
        assert!(AxisChunks::new(&x.view(), 1, 1).next().is_none());
    }

    #[test]
    #[should_panic(expected = "chunk size must be > 0")]
    fn test_axis_chunks_zero_size() {
        let x = Tensor::<i32>::zeros(&[5, 0]);
        assert!(AxisChunks::new(&x.view(), 1, 0).next().is_none());
    }

    #[test]
    fn test_axis_chunks_mut_empty() {
        let mut x = Tensor::<i32>::zeros(&[5, 0]);
        assert!(AxisChunksMut::new(x.view_mut(), 1, 1).next().is_none());
    }

    #[test]
    fn test_axis_chunks_mut_rev() {
        let mut tensor = NdTensor::from([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]);
        let fwd: Vec<_> = tensor
            .axis_chunks_mut(0, 1)
            .map(|view| view.to_vec())
            .collect();
        let mut tensor = NdTensor::from([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]);
        let rev: Vec<_> = tensor
            .axis_chunks_mut(0, 1)
            .rev()
            .map(|view| view.to_vec())
            .collect();
        compare_reversed(&fwd, &rev);
    }

    #[test]
    #[should_panic(expected = "chunk size must be > 0")]
    fn test_axis_chunks_mut_zero_size() {
        let mut x = Tensor::<i32>::zeros(&[5, 0]);
        assert!(AxisChunksMut::new(x.view_mut(), 1, 0).next().is_none());
    }

    #[test]
    fn test_axis_iter() {
        let tensor = NdTensor::from([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]);
        test_iterator(|| tensor.axis_iter(0), &[tensor.slice(0), tensor.slice(1)]);
    }

    #[test]
    fn test_axis_iter_mut_rev() {
        let mut tensor = NdTensor::from([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]);
        let fwd: Vec<_> = tensor.axis_iter_mut(0).map(|view| view.to_vec()).collect();
        let mut tensor = NdTensor::from([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]);
        let rev: Vec<_> = tensor
            .axis_iter_mut(0)
            .rev()
            .map(|view| view.to_vec())
            .collect();
        compare_reversed(&fwd, &rev);
    }

    #[test]
    fn test_inner_iter() {
        let tensor = NdTensor::from([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]);
        test_iterator(
            || tensor.inner_iter::<2>(),
            &[tensor.slice(0), tensor.slice(1)],
        );
    }

    #[test]
    fn test_inner_iter_mut() {
        struct InnerIterMutTest(NdTensor<i32, 3>);

        impl MutIterable for InnerIterMutTest {
            type Iter<'a> = super::InnerIterMut<'a, i32, NdLayout<2>>;

            fn iter_mut(&mut self) -> Self::Iter<'_> {
                self.0.inner_iter_mut::<2>()
            }
        }

        let tensor = NdTensor::from([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]);
        test_mut_iterator(
            InnerIterMutTest(tensor.clone()),
            &[tensor.slice(0), tensor.slice(1)],
        );
    }

    #[test]
    fn test_lanes() {
        let x = NdTensor::from([[1, 2], [3, 4]]);
        test_iterator(
            || x.lanes(0),
            &[x.slice((.., 0)).into(), x.slice((.., 1)).into()],
        );
        test_iterator(|| x.lanes(1), &[x.slice(0).into(), x.slice(1).into()]);
    }

    #[test]
    fn test_lanes_empty() {
        let x = Tensor::<i32>::zeros(&[5, 0]);
        assert!(Lanes::new(x.view().view_ref(), 0).next().is_none());
        assert!(Lanes::new(x.view().view_ref(), 1).next().is_none());
    }

    #[test]
    fn test_lanes_mut() {
        use super::Lane;

        struct LanesMutTest(NdTensor<i32, 2>);

        impl MutIterable for LanesMutTest {
            type Iter<'a> = super::LanesMut<'a, i32>;

            fn iter_mut(&mut self) -> Self::Iter<'_> {
                self.0.lanes_mut(0)
            }
        }

        let tensor = NdTensor::from([[1, 2], [3, 4]]);
        test_mut_iterator::<_, Lane<i32>>(
            LanesMutTest(tensor.clone()),
            &[
                Lane::from(tensor.slice((.., 0))),
                Lane::from(tensor.slice((.., 1))),
            ],
        );
    }

    #[test]
    fn test_lane_as_slice() {
        // Contiguous lane
        let x = NdTensor::from([0, 1, 2]);
        let mut lane = x.lanes(0).next().unwrap();
        assert_eq!(lane.as_slice(), Some([0, 1, 2].as_slice()));
        lane.next();
        assert_eq!(lane.as_slice(), Some([1, 2].as_slice()));
        lane.next();
        lane.next();
        assert_eq!(lane.as_slice(), Some([0i32; 0].as_slice()));
        lane.next();
        assert_eq!(lane.as_slice(), Some([0i32; 0].as_slice()));

        // Non-contiguous lane
        let x = NdTensor::from([[1i32, 2], [3, 4]]);
        let lane = x.lanes(0).next().unwrap();
        assert_eq!(lane.as_slice(), None);
    }

    #[test]
    fn test_lanes_mut_empty() {
        let mut x = Tensor::<i32>::zeros(&[5, 0]);
        assert!(LanesMut::new(x.mut_view_ref(), 0).next().is_none());
        assert!(LanesMut::new(x.mut_view_ref(), 1).next().is_none());
    }

    #[test]
    fn test_iter_step_by() {
        let tensor = Tensor::<f32>::full(&[1, 3, 16, 8], 1.);

        // Take a non-contiguous slice so we don't use the fast path for
        // contiguous tensors.
        let tensor = tensor.slice((.., .., 1.., ..));

        let sum = tensor.iter().sum::<f32>();
        for n_skip in 0..tensor.len() {
            let sum_skip = tensor.iter().skip(n_skip).sum::<f32>();
            assert_eq!(
                sum_skip,
                sum - n_skip as f32,
                "wrong sum for n_skip={}",
                n_skip
            );
        }
    }

    #[test]
    fn test_iter_broadcast() {
        let tensor = Tensor::<f32>::full(&[1], 1.);
        let broadcast = tensor.broadcast([1, 3, 16, 8]);
        assert_eq!(broadcast.iter().len(), broadcast.len());
        let count = broadcast.iter().count();
        assert_eq!(count, broadcast.len());
        let sum = broadcast.iter().sum::<f32>();
        assert_eq!(sum, broadcast.len() as f32);
    }

    #[test]
    fn test_iter() {
        let tensor = NdTensor::from([[[1, 2], [3, 4]]]);

        // Test iterator over contiguous tensor.
        test_iterator(|| tensor.iter().copied(), &[1, 2, 3, 4]);

        // Test iterator over non-contiguous tensor.
        test_iterator(|| tensor.transposed().iter().copied(), &[1, 3, 2, 4]);
    }

    #[test]
    fn test_iter_mut() {
        struct IterTest(NdTensor<i32, 3>);

        impl MutIterable for IterTest {
            type Iter<'a> = super::IterMut<'a, i32>;

            fn iter_mut(&mut self) -> Self::Iter<'_> {
                self.0.iter_mut()
            }
        }

        let tensor = NdTensor::from([[[1, 2], [3, 4]]]);
        test_mut_iterator(IterTest(tensor), &[&1, &2, &3, &4]);
    }

    #[test]
    #[ignore]
    fn bench_iter() {
        use crate::Layout;
        use rten_bench::run_bench;

        type Elem = i32;

        let tensor = std::hint::black_box(Tensor::<Elem>::full(&[1, 6, 768, 64], 1));
        let n_trials = 1000;
        let mut result = Elem::default();

        fn reduce<'a>(iter: impl Iterator<Item = &'a Elem>) -> Elem {
            iter.fold(Elem::default(), |acc, x| acc.wrapping_add(*x))
        }

        // Iterate directly over data slice.
        run_bench(n_trials, Some("slice iter"), || {
            result = reduce(tensor.data().unwrap().iter());
        });
        println!("sum {}", result);

        // Use tensor iterator with contiguous tensor. This will use the fast
        // path which wraps a slice iterator.
        run_bench(n_trials, Some("contiguous iter"), || {
            result = reduce(tensor.iter());
        });
        println!("sum {}", result);

        run_bench(n_trials, Some("contiguous reverse iter"), || {
            result = reduce(tensor.iter().rev());
        });
        println!("sum {}", result);

        // Use tensor iterator with non-contiguous slice. This will fall back
        // to indexed iteration.
        let slice = tensor.slice((.., .., 1.., ..));
        assert!(!slice.is_contiguous());
        let n_trials = 1000;
        run_bench(n_trials, Some("non-contiguous iter"), || {
            result = reduce(slice.iter());
        });
        println!("sum {}", result);

        // Reverse iteration with non-contiguous slice. This is much slower
        // because it translates linear indexes into offsets using division.
        let n_trials = 100;
        run_bench(n_trials, Some("non-contiguous reverse iter"), || {
            result = reduce(slice.iter().rev());
        });
        println!("sum {}", result);
    }

    #[test]
    #[ignore]
    fn bench_inner_iter() {
        use crate::rng::XorShiftRng;
        use rten_bench::run_bench;

        let n_trials = 100;
        let mut rng = XorShiftRng::new(1234);

        // Tensor with many steps along the outer two dimensions relative to the
        // steps along the inner two dimensions. This emphasizes the overhead of
        // stepping `inner_iter`.
        let tensor = Tensor::<f32>::rand(&[512, 512, 12, 1], &mut rng);

        let mut sum = 0.;
        run_bench(n_trials, Some("inner iter"), || {
            for inner in tensor.inner_iter::<2>() {
                for i0 in 0..inner.size(0) {
                    for i1 in 0..inner.size(1) {
                        sum += inner[[i0, i1]];
                    }
                }
            }
        });
        println!("sum {}", sum);
    }
}