nuvec-cpu 0.2.0

Pure-Rust CPU backend for nuvec
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
//! Pure-Rust CPU kernels for nuvec.
//!
//! Kernels work on typed slices and know nothing about arrays or graphs; `nuvec-core`
//! dispatches to them. Inputs may have any layout (contiguous, transposed, sliced with
//! negative steps, broadcast with zero strides); outputs are always row-contiguous.
//!
//! **Status:** pre-alpha. Element-wise, reduction and matrix-multiplication kernels.

mod matmul;

pub use matmul::{Gemm, MatView, matmul};

/// The version of this crate.
pub const VERSION: &str = env!("CARGO_PKG_VERSION");

/// A strided view of elements: element `i` (a multi-index) lives at
/// `data[offset + Σ i_k · strides_k]`.
#[derive(Debug, Clone, Copy)]
pub struct View<'a, T> {
    /// The underlying storage.
    pub data: &'a [T],
    /// Position of the first element, in elements.
    pub offset: isize,
    /// Per-dimension steps, in elements. May be zero (broadcast) or negative.
    pub strides: &'a [isize],
}

/// Applies `f` element-wise: `out[i] = f(a[i])` in row-major order of `shape`.
///
/// # Panics
///
/// If `out.len()` is not the element count of `shape`, or a view indexes outside its data.
/// Both are caller bugs: `nuvec-core` validates shapes and layouts beforehand.
pub fn map1<A: Copy, U>(shape: &[usize], a: View<'_, A>, out: &mut [U], f: impl Fn(A) -> U) {
    for_rows(
        shape,
        [a.strides],
        [a.offset],
        out,
        |row, [ba], [sa]| match lane(a.data, ba, sa, row.len()) {
            Lane::Slice(x) => row.iter_mut().zip(x).for_each(|(o, &x)| *o = f(x)),
            la => row
                .iter_mut()
                .enumerate()
                .for_each(|(j, o)| *o = f(la.get(j))),
        },
    );
}

/// Applies `f` element-wise: `out[i] = f(a[i], b[i])` in row-major order of `shape`.
///
/// # Panics
///
/// As [`map1`].
pub fn map2<A: Copy, B: Copy, U>(
    shape: &[usize],
    a: View<'_, A>,
    b: View<'_, B>,
    out: &mut [U],
    f: impl Fn(A, B) -> U,
) {
    for_rows(
        shape,
        [a.strides, b.strides],
        [a.offset, b.offset],
        out,
        |row, [ba, bb], [sa, sb]| {
            let n = row.len();
            match (lane(a.data, ba, sa, n), lane(b.data, bb, sb, n)) {
                (Lane::Slice(x), Lane::Slice(y)) => {
                    row.iter_mut()
                        .zip(x)
                        .zip(y)
                        .for_each(|((o, &x), &y)| *o = f(x, y));
                }
                (Lane::Slice(x), Lane::Repeat(y)) => {
                    row.iter_mut().zip(x).for_each(|(o, &x)| *o = f(x, y))
                }
                (Lane::Repeat(x), Lane::Slice(y)) => {
                    row.iter_mut().zip(y).for_each(|(o, &y)| *o = f(x, y))
                }
                (la, lb) => row
                    .iter_mut()
                    .enumerate()
                    .for_each(|(j, o)| *o = f(la.get(j), lb.get(j))),
            }
        },
    );
}

/// Applies `f` element-wise: `out[i] = f(a[i], b[i], c[i])` in row-major order of `shape`.
///
/// # Panics
///
/// As [`map1`].
pub fn map3<A: Copy, B: Copy, C: Copy, U>(
    shape: &[usize],
    a: View<'_, A>,
    b: View<'_, B>,
    c: View<'_, C>,
    out: &mut [U],
    f: impl Fn(A, B, C) -> U,
) {
    let strides = [a.strides, b.strides, c.strides];
    for_rows(
        shape,
        strides,
        [a.offset, b.offset, c.offset],
        out,
        |row, [ba, bb, bc], [sa, sb, sc]| {
            let n = row.len();
            match (
                lane(a.data, ba, sa, n),
                lane(b.data, bb, sb, n),
                lane(c.data, bc, sc, n),
            ) {
                (Lane::Slice(x), Lane::Slice(y), Lane::Slice(z)) => {
                    row.iter_mut()
                        .zip(x)
                        .zip(y)
                        .zip(z)
                        .for_each(|(((o, &x), &y), &z)| *o = f(x, y, z));
                }
                (la, lb, lc) => row
                    .iter_mut()
                    .enumerate()
                    .for_each(|(j, o)| *o = f(la.get(j), lb.get(j), lc.get(j))),
            }
        },
    );
}

/// One operand's elements along an output row.
enum Lane<'a, T> {
    /// Contiguous (stride 1).
    Slice(&'a [T]),
    /// Broadcast (stride 0).
    Repeat(T),
    /// Any other stride.
    Strided {
        data: &'a [T],
        start: isize,
        step: isize,
    },
}

impl<T: Copy> Lane<'_, T> {
    fn get(&self, j: usize) -> T {
        match self {
            Self::Slice(x) => x[j],
            Self::Repeat(x) => *x,
            Self::Strided { data, start, step } => data[index(start + j as isize * step)],
        }
    }
}

/// The lane of `n` elements starting at `base` with `step`. Bounds are checked once here
/// for contiguous lanes.
fn lane<T: Copy>(data: &[T], base: isize, step: isize, n: usize) -> Lane<'_, T> {
    match step {
        1 => Lane::Slice(&data[index(base)..index(base) + n]),
        0 => Lane::Repeat(data[index(base)]),
        _ => Lane::Strided {
            data,
            start: base,
            step,
        },
    }
}

/// Calls `row_fn` for each row of the (collapsed) output, in row-major order, with every
/// operand's offset at the row start and its stride along the row.
fn for_rows<const N: usize, U>(
    shape: &[usize],
    strides: [&[isize]; N],
    offsets: [isize; N],
    out: &mut [U],
    mut row_fn: impl FnMut(&mut [U], [isize; N], [isize; N]),
) {
    let size: usize = shape.iter().product();
    assert_eq!(out.len(), size, "output length does not match shape");
    if size == 0 {
        return;
    }
    let (shape, strides) = collapse_dims(shape, strides);
    let ndim = shape.len();
    if ndim == 0 {
        row_fn(out, offsets, [0; N]);
        return;
    }

    let inner = shape[ndim - 1];
    let inner_strides: [isize; N] = std::array::from_fn(|k| strides[k][ndim - 1]);
    let mut position = vec![0usize; ndim - 1];
    let mut base = offsets;
    for row in out.chunks_exact_mut(inner) {
        row_fn(row, base, inner_strides);
        // Advance the odometer over the outer dimensions.
        for d in (0..ndim - 1).rev() {
            position[d] += 1;
            for k in 0..N {
                base[k] += strides[k][d];
            }
            if position[d] < shape[d] {
                break;
            }
            for k in 0..N {
                base[k] -= strides[k][d] * shape[d] as isize;
            }
            position[d] = 0;
        }
    }
}

/// Folds blocks of elements: `out[i]` is the fold of the `reduced` block at output position
/// `i`, starting from `init`. `kept` and `reduced` are `(shape, strides)` pairs that split the
/// input's dimensions; the output is row-major over `kept`.
///
/// Contiguous blocks are passed whole to `block`; other blocks are folded element by element
/// with `f`, which also receives each element's row-major index within its block. Both must
/// compute the same fold.
///
/// # Panics
///
/// As [`map1`].
#[allow(clippy::too_many_arguments)]
pub fn reduce<T: Copy, A: Clone>(
    kept: (&[usize], &[isize]),
    reduced: (&[usize], &[isize]),
    data: &[T],
    offset: isize,
    out: &mut [A],
    init: A,
    f: impl Fn(A, T, usize) -> A,
    block: impl Fn(A, &[T]) -> A,
) {
    let (shape, [strides]) = collapse_dims(reduced.0, [reduced.1]);
    let n: usize = shape.iter().product();
    let contiguous = shape.len() == 1 && strides[0] == 1;
    let fold = |base: isize| -> A {
        let mut acc = init.clone();
        if n == 0 {
            return acc;
        }
        if contiguous || shape.is_empty() {
            return block(acc, &data[index(base)..index(base) + n]);
        }
        let mut position = vec![0usize; shape.len()];
        let mut cursor = base;
        for i in 0..n {
            acc = f(acc, data[index(cursor)], i);
            for d in (0..shape.len()).rev() {
                position[d] += 1;
                cursor += strides[d];
                if position[d] < shape[d] {
                    break;
                }
                cursor -= strides[d] * shape[d] as isize;
                position[d] = 0;
            }
        }
        acc
    };
    for_rows(kept.0, [kept.1], [offset], out, |row, [base], [step]| {
        for (j, o) in row.iter_mut().enumerate() {
            *o = fold(base + j as isize * step);
        }
    });
}

fn index(offset: isize) -> usize {
    usize::try_from(offset).expect("view offset is negative")
}

/// Merges adjacent dimensions that are contiguous for every operand and drops unit
/// dimensions, so that common layouts iterate as one flat loop.
///
/// Dimension `d` merges into `d + 1` when `strides[d] == strides[d + 1] * shape[d + 1]`
/// holds for every operand.
pub fn collapse_dims<const N: usize>(
    shape: &[usize],
    strides: [&[isize]; N],
) -> (Vec<usize>, [Vec<isize>; N]) {
    let mut out_shape: Vec<usize> = Vec::with_capacity(shape.len());
    let mut out_strides: [Vec<isize>; N] = std::array::from_fn(|_| Vec::with_capacity(shape.len()));
    for d in 0..shape.len() {
        if shape[d] == 1 {
            continue;
        }
        let mergeable = out_shape.last().is_some() && {
            let last = out_strides[0].len() - 1;
            (0..N).all(|k| out_strides[k][last] == strides[k][d] * shape[d] as isize)
        };
        if mergeable {
            let last = out_shape.len() - 1;
            out_shape[last] *= shape[d];
            for k in 0..N {
                out_strides[k][last] = strides[k][d];
            }
        } else {
            out_shape.push(shape[d]);
            for k in 0..N {
                out_strides[k].push(strides[k][d]);
            }
        }
    }
    (out_shape, out_strides)
}

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

    #[test]
    fn version_is_set() {
        assert!(!VERSION.is_empty());
    }

    #[test]
    fn contiguous_binary() {
        let mut out = [0i32; 6];
        let s = [3, 1];
        let a = View {
            data: &[1, 2, 3, 4, 5, 6],
            offset: 0,
            strides: &s,
        };
        let b = View {
            data: &[10, 20, 30, 40, 50, 60],
            offset: 0,
            strides: &s,
        };
        map2(&[2, 3], a, b, &mut out, |x, y| x + y);
        assert_eq!(out, [11, 22, 33, 44, 55, 66]);
    }

    #[test]
    fn transposed_and_broadcast() {
        // a = [[1,2,3],[4,5,6]] read transposed (3x2); b = [10, 20] broadcast over rows.
        let mut out = [0i32; 6];
        let a = View {
            data: &[1, 2, 3, 4, 5, 6],
            offset: 0,
            strides: &[1, 3],
        };
        let b = View {
            data: &[10, 20],
            offset: 0,
            strides: &[0, 1],
        };
        map2(&[3, 2], a, b, &mut out, |x, y| x + y);
        assert_eq!(out, [11, 24, 12, 25, 13, 26]);
    }

    #[test]
    fn negative_strides_and_offsets() {
        // Reverse of [0, 1, 2, 3, 4], every second element: [4, 2, 0].
        let mut out = [0u8; 3];
        let a = View {
            data: &[0, 1, 2, 3, 4],
            offset: 4,
            strides: &[-2],
        };
        map1(&[3], a, &mut out, |x| x);
        assert_eq!(out, [4, 2, 0]);
    }

    #[test]
    fn scalar_and_empty() {
        let mut out = [0f32; 1];
        map1(
            &[],
            View {
                data: &[2.5],
                offset: 0,
                strides: &[],
            },
            &mut out,
            |x| x * 2.0,
        );
        assert_eq!(out, [5.0]);
        let mut none: [f32; 0] = [];
        map1(
            &[3, 0],
            View {
                data: &[],
                offset: 0,
                strides: &[0, 1],
            },
            &mut none,
            |x: f32| x,
        );
    }

    #[test]
    fn ternary() {
        let mut out = [0i32; 3];
        let s = [1];
        map3(
            &[3],
            View {
                data: &[1u8, 0, 1],
                offset: 0,
                strides: &s,
            },
            View {
                data: &[1, 2, 3],
                offset: 0,
                strides: &s,
            },
            View {
                data: &[-1],
                offset: 0,
                strides: &[0],
            },
            &mut out,
            |c, x, y| if c != 0 { x } else { y },
        );
        assert_eq!(out, [1, -1, 3]);
    }

    #[test]
    fn reduce_over_middle_axis_of_transposed_layout() {
        // Logical a[i][j][k] = data[k*6 + j*2 + i] for shape [2, 3, 4] (fully transposed).
        let data: Vec<i32> = (0..24).collect();
        let (shape, strides) = ([2usize, 3, 4], [1isize, 2, 6]);
        let mut out = [0i32; 8];
        // Keep axes 0 and 2, reduce axis 1.
        reduce(
            (&[shape[0], shape[2]], &[strides[0], strides[2]]),
            (&[shape[1]], &[strides[1]]),
            &data,
            0,
            &mut out,
            0,
            |acc, x, _| acc + x,
            |acc, xs| xs.iter().fold(acc, |a, &x| a + x),
        );
        let expected: Vec<i32> = (0..2)
            .flat_map(|i| (0..4).map(move |k| (0..3).map(|j| k * 6 + j * 2 + i).sum()))
            .collect();
        assert_eq!(out.to_vec(), expected);
    }

    #[test]
    fn reduce_passes_block_index_and_handles_empty() {
        let pick = |acc: (i32, usize), x: i32, i: usize| if x > acc.0 { (x, i) } else { acc };
        let block = |acc, xs: &[i32]| xs.iter().enumerate().fold(acc, |a, (i, &x)| pick(a, x, i));
        // Contiguous block (passed whole) and a strided one (element by element).
        let mut out = [(0, 0usize); 2];
        reduce(
            (&[2], &[4]),
            (&[4], &[1]),
            &[5, 9, 1, 7, 3, 2, 8, 0],
            0,
            &mut out,
            (i32::MIN, 0),
            pick,
            block,
        );
        assert_eq!(out, [(9, 1), (8, 2)]);
        reduce(
            (&[2], &[1]),
            (&[4], &[2]),
            &[5, 9, 1, 7, 3, 2, 8, 0],
            0,
            &mut out,
            (i32::MIN, 0),
            pick,
            block,
        );
        assert_eq!(out, [(8, 3), (9, 0)]);
        let mut sums = [7i32; 3];
        let add = |a: i32, x: i32, _| a + x;
        reduce(
            (&[3], &[1]),
            (&[0], &[1]),
            &[0i32; 0],
            0,
            &mut sums,
            0,
            add,
            |a, xs| xs.iter().fold(a, |a, &x| a + x),
        );
        assert_eq!(sums, [0, 0, 0]);
    }

    #[test]
    fn collapse_merges_contiguous_dims() {
        let (shape, [s]) = collapse_dims(&[2, 3, 4], [&[12, 4, 1]]);
        assert_eq!((shape, s), (vec![24], vec![1]));
        let (shape, [s]) = collapse_dims(&[2, 1, 3], [&[3, 99, 1]]);
        assert_eq!((shape, s), (vec![6], vec![1]));
        let (shape, [a, b]) = collapse_dims(&[2, 3], [&[3, 1], &[0, 1]]);
        assert_eq!((shape, a, b), (vec![2, 3], vec![3, 1], vec![0, 1]));
    }
}