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
use std::iter::FusedIterator;
use std::ops::Range;

use smallvec::{smallvec, SmallVec};

pub trait IndexArray: AsMut<[usize]> + AsRef<[usize]> + Clone {}
impl<const N: usize> IndexArray for SmallVec<[usize; N]> {}
impl<const N: usize> IndexArray for [usize; N] {}

/// The index type used for dynamic-rank tensors.
pub type DynIndex = SmallVec<[usize; 5]>;

/// Iterator over a range of N-dimensional indices, where N may be known at
/// compile time (see [NdIndices]) or only at runtime ([DynIndices]).
///
/// The number of dimensions may be zero, in which case the iterator will yield
/// a single empty index. This is consistent with eg. `ndindex` in NumPy.
pub struct Indices<Index: IndexArray>
where
    Index: IndexArray,
{
    /// Start index along each dimension.
    start: Index,

    /// End index (exclusive) along each dimension.
    end: Index,

    next: Option<Index>,

    /// Remaining iteration steps.
    steps: usize,
}

/// Return the number of steps for an index iterator over the range of indices
/// from `from` to `to`.
///
/// If any index in `from` is greater than the corresponding index in `to`,
/// this returns zero.
fn steps(from: &[usize], to: &[usize]) -> usize {
    assert!(from.len() == to.len());
    let mut product = 1;
    for (&from, &to) in from.iter().zip(to.iter()).rev() {
        let size = to.saturating_sub(from);
        product *= size;
    }
    product
}

impl<Index: IndexArray> Indices<Index> {
    fn from_start_and_end(start: Index, end: Index) -> Indices<Index> {
        let steps = steps(start.as_ref(), end.as_ref());
        Indices {
            // Note that if the index is empty, `start == end` but the iterator
            // should yield a single empty element in that case.
            next: if steps > 0 || start.as_ref().is_empty() {
                Some(start.clone())
            } else {
                None
            },
            start,
            end,
            steps,
        }
    }
}

impl<const N: usize> Indices<SmallVec<[usize; N]>> {
    /// Return an iterator over all the indices where each dimension lies
    /// within the corresponding range in `ranges`.
    pub fn from_ranges(ranges: &[Range<usize>]) -> Indices<SmallVec<[usize; N]>> {
        let start: SmallVec<[usize; N]> = ranges.iter().map(|r| r.start).collect();
        let end = ranges.iter().map(|r| r.end).collect();
        Self::from_start_and_end(start, end)
    }

    /// Return an iterator over all the indices where each dimension is between
    /// `0` and `shape[dim]`.
    pub fn from_shape(shape: &[usize]) -> Indices<SmallVec<[usize; N]>> {
        let start = smallvec![0; shape.len()];
        let end = shape.iter().copied().collect();
        Self::from_start_and_end(start, end)
    }
}

impl<const N: usize> Indices<[usize; N]> {
    /// Return an iterator over all the indices where each dimension lies
    /// within the corresponding range in `ranges`.
    pub fn from_ranges(ranges: [Range<usize>; N]) -> Indices<[usize; N]> {
        let start = ranges.clone().map(|r| r.start);
        let end = ranges.map(|r| r.end);
        Self::from_start_and_end(start, end)
    }

    /// Return an iterator over all the indices where each dimension is between
    /// `0` and `shape[dim]`.
    pub fn from_shape(shape: [usize; N]) -> Indices<[usize; N]> {
        Self::from_ranges(shape.map(|size| 0..size))
    }
}

impl<Index: IndexArray> Iterator for Indices<Index> {
    type Item = Index;

    /// Return the next index in the sequence, or `None` after all indices
    /// have been returned.
    fn next(&mut self) -> Option<Self::Item> {
        let current = self.next.clone()?;

        let mut next = current.clone();
        let mut has_next = false;
        for ((&dim_end, &dim_start), index) in self
            .end
            .as_ref()
            .iter()
            .zip(self.start.as_ref())
            .zip(next.as_mut().iter_mut())
            .rev()
        {
            *index += 1;
            if *index == dim_end {
                *index = dim_start;
            } else {
                has_next = true;
                break;
            }
        }

        self.next = has_next.then_some(next);

        Some(current)
    }

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

impl<Index: IndexArray> ExactSizeIterator for Indices<Index> {}

impl<Index: IndexArray> FusedIterator for Indices<Index> {}

/// Iterator over a range of N-dimensional indices, where N is known at compile
/// time.
pub struct NdIndices<const N: usize> {
    inner: Indices<[usize; N]>,
}

impl<const N: usize> NdIndices<N> {
    pub fn from_ranges(ranges: [Range<usize>; N]) -> NdIndices<N> {
        NdIndices {
            inner: Indices::<[usize; N]>::from_ranges(ranges),
        }
    }

    pub fn from_shape(shape: [usize; N]) -> NdIndices<N> {
        NdIndices {
            inner: Indices::<[usize; N]>::from_shape(shape),
        }
    }
}

impl<const N: usize> Iterator for NdIndices<N> {
    type Item = [usize; N];

    fn next(&mut self) -> Option<Self::Item> {
        self.inner.next()
    }

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

impl<const N: usize> ExactSizeIterator for NdIndices<N> {}
impl<const N: usize> FusedIterator for NdIndices<N> {}

/// Max tensor rank supported by the variant of [DynIndices] that is optimized
/// for small-rank tensors.
const DYN_SMALL_LEN: usize = 4;

enum DynIndicesInner {
    Small {
        iter: NdIndices<DYN_SMALL_LEN>,
        pad: usize,
    },
    Large(Indices<DynIndex>),
}

/// Iterator over a range of N-dimensional indices, where N is not known at
/// compile time.
pub struct DynIndices {
    inner: DynIndicesInner,
}

/// Left-pad a shape with 1s to size N (eg. [32, 32] => [1, 1, 32, 32]).
fn left_pad_shape<const N: usize>(shape: &[usize]) -> (usize, [usize; N]) {
    assert!(shape.len() <= N);
    let mut padded_shape = [0; N];
    let pad = N - shape.len();
    for i in 0..pad {
        padded_shape[i] = 1;
    }
    for i in pad..N {
        padded_shape[i] = shape[i - pad];
    }
    (N - shape.len(), padded_shape)
}

/// Left-pad ranges with `[0..1]` to size N.
fn left_pad_ranges<const N: usize>(ranges: &[Range<usize>]) -> (usize, [Range<usize>; N]) {
    assert!(ranges.len() <= N);

    // We use a `SmallVec` here because sadly `[elem; N]` doesn't work with
    // Range, which is a non-Copy type :(
    let mut padded_ranges = SmallVec::<[Range<usize>; N]>::from_elem(0..1, N);
    let pad = N - ranges.len();
    for i in 0..pad {
        padded_ranges[i] = 0..1;
    }
    for i in pad..N {
        padded_ranges[i] = ranges[i - pad].clone();
    }
    (N - ranges.len(), padded_ranges.into_inner().unwrap())
}

impl DynIndices {
    pub fn from_shape(shape: &[usize]) -> DynIndices {
        let inner = if shape.len() <= DYN_SMALL_LEN {
            let (pad, padded) = left_pad_shape(shape);
            DynIndicesInner::Small {
                iter: NdIndices::from_shape(padded),
                pad,
            }
        } else {
            DynIndicesInner::Large(Indices::<DynIndex>::from_shape(shape))
        };
        DynIndices { inner }
    }

    pub fn from_ranges(ranges: &[Range<usize>]) -> DynIndices {
        let inner = if ranges.len() <= DYN_SMALL_LEN {
            let (pad, padded) = left_pad_ranges(ranges);
            DynIndicesInner::Small {
                iter: NdIndices::from_ranges(padded),
                pad,
            }
        } else {
            DynIndicesInner::Large(Indices::<DynIndex>::from_ranges(ranges))
        };
        DynIndices { inner }
    }
}

impl Iterator for DynIndices {
    type Item = DynIndex;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        match self.inner {
            DynIndicesInner::Small { ref mut iter, pad } => {
                iter.next().map(|idx| SmallVec::from_slice(&idx[pad..]))
            }
            DynIndicesInner::Large(ref mut inner) => inner.next(),
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        match self.inner {
            DynIndicesInner::Small { ref iter, .. } => iter.size_hint(),
            DynIndicesInner::Large(ref inner) => inner.size_hint(),
        }
    }
}

impl ExactSizeIterator for DynIndices {}
impl FusedIterator for DynIndices {}

#[cfg(test)]
mod tests {
    use super::{DynIndices, NdIndices};

    #[test]
    fn test_nd_indices() {
        // Empty iterator
        let mut iter = NdIndices::from_ranges([0..0]);
        assert_eq!(iter.next(), None);
        assert_eq!(iter.next(), None);

        // Scalar index iterator
        let mut iter = NdIndices::from_ranges([]);
        assert_eq!(iter.next(), Some([]));
        assert_eq!(iter.next(), None);

        // 1D index iterator
        let iter = NdIndices::from_ranges([0..5]);
        let visited: Vec<_> = iter.collect();
        assert_eq!(visited, &[[0], [1], [2], [3], [4]]);

        // 2D index iterator
        let iter = NdIndices::from_ranges([2..4, 2..4]);
        let visited: Vec<_> = iter.collect();
        assert_eq!(visited, &[[2, 2], [2, 3], [3, 2], [3, 3]]);
    }

    #[test]
    fn test_dyn_indices() {
        type Index = <DynIndices as Iterator>::Item;

        // Empty iterator
        let mut iter = DynIndices::from_ranges(&[0..0]);
        assert_eq!(iter.next(), None);
        assert_eq!(iter.next(), None);

        // Scalar index iterator
        let mut iter = DynIndices::from_ranges(&[]);
        assert_eq!(iter.next(), Some(Index::new()));
        assert_eq!(iter.next(), None);

        // 1D index iterator
        let iter = DynIndices::from_ranges(&[0..5]);
        let visited: Vec<Vec<usize>> = iter.map(|ix| ix.into_iter().collect()).collect();
        assert_eq!(visited, vec![vec![0], vec![1], vec![2], vec![3], vec![4]]);

        // 2D index iterator
        let iter = DynIndices::from_ranges(&[2..4, 2..4]);
        let visited: Vec<Vec<usize>> = iter.map(|ix| ix.into_iter().collect()).collect();
        assert_eq!(
            visited,
            vec![vec![2, 2], vec![2, 3], vec![3, 2], vec![3, 3],]
        );

        // 5D index iterator. This exercises the path for tensors with more
        // than 4 dims.
        let iter = DynIndices::from_shape(&[2, 1, 1, 2, 2]);
        let visited: Vec<Vec<usize>> = iter.map(|ix| ix.into_iter().collect()).collect();
        assert_eq!(
            visited,
            vec![
                vec![0, 0, 0, 0, 0],
                vec![0, 0, 0, 0, 1],
                vec![0, 0, 0, 1, 0],
                vec![0, 0, 0, 1, 1],
                //
                vec![1, 0, 0, 0, 0],
                vec![1, 0, 0, 0, 1],
                vec![1, 0, 0, 1, 0],
                vec![1, 0, 0, 1, 1],
            ]
        );
    }

    #[test]
    #[ignore]
    fn bench_indices() {
        use std::time::Instant;

        // Shape taken from GatherElements usage in
        // https://huggingface.co/microsoft/deberta-v3-large.
        //
        // `black_box` is not necessary for the current implementations, but in
        // an experiment with some less branch-y implementations of NdIndices,
        // Rust was able to precompute the iteration count (!).
        let shape = std::hint::black_box([16, 128, 128]);

        // Dynamic rank
        let start = Instant::now();
        let mut count = 0;
        for _ in 0..100 {
            let indices = DynIndices::from_shape(&shape);
            for _ in indices {
                count += 1;
            }
        }
        let elapsed = start.elapsed().as_millis();
        println!("DynIndices stepped {} times in {} ms", count, elapsed);

        // Same shape, static rank
        let start = Instant::now();
        let mut count = 0;
        for _ in 0..100 {
            let indices = NdIndices::from_shape(shape);
            for _ in indices {
                count += 1;
            }
        }
        let elapsed = start.elapsed().as_millis();
        println!("NdIndices stepped {} times in {} ms", count, elapsed);
    }
}