numr 0.5.2

High-performance numerical computing with multi-backend GPU acceleration (CPU/CUDA/WebGPU)
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
//! SIMD-optimized cumulative operations dispatch
//!
//! Provides AVX2/AVX-512 accelerated cumsum and cumprod strided kernels.
//! The strided kernels vectorize over the inner_size dimension, where each
//! SIMD lane maintains its own independent accumulator.

#[cfg(target_arch = "x86_64")]
mod avx2;
#[cfg(target_arch = "x86_64")]
mod avx512;

#[cfg(target_arch = "aarch64")]
mod aarch64;

use super::{SimdLevel, detect_simd};

/// Minimum inner_size to justify SIMD overhead for strided operations
const SIMD_THRESHOLD: usize = 16;

// ============================================================================
// Cumsum Strided - SIMD Dispatch
// ============================================================================

/// SIMD-optimized strided cumsum for f32.
///
/// Vectorizes over the inner_size dimension - each SIMD lane maintains
/// its own running sum independently.
///
/// # Safety
/// - All pointers must be valid for the specified sizes and strides
pub unsafe fn cumsum_strided_f32(
    a: *const f32,
    out: *mut f32,
    scan_size: usize,
    outer_size: usize,
    inner_size: usize,
) {
    let level = detect_simd();

    if inner_size < SIMD_THRESHOLD || level == SimdLevel::Scalar {
        cumsum_strided_scalar_f32(a, out, scan_size, outer_size, inner_size);
        return;
    }

    #[cfg(target_arch = "x86_64")]
    match level {
        SimdLevel::Avx512 => avx512::cumsum_strided_f32(a, out, scan_size, outer_size, inner_size),
        SimdLevel::Avx2Fma => avx2::cumsum_strided_f32(a, out, scan_size, outer_size, inner_size),
        _ => cumsum_strided_scalar_f32(a, out, scan_size, outer_size, inner_size),
    }

    #[cfg(target_arch = "aarch64")]
    match level {
        SimdLevel::Neon | SimdLevel::NeonFp16 => {
            aarch64::neon::cumsum_strided_f32(a, out, scan_size, outer_size, inner_size)
        }
        _ => cumsum_strided_scalar_f32(a, out, scan_size, outer_size, inner_size),
    }

    #[cfg(not(any(target_arch = "x86_64", target_arch = "aarch64")))]
    cumsum_strided_scalar_f32(a, out, scan_size, outer_size, inner_size);
}

/// SIMD-optimized strided cumsum for f64.
pub unsafe fn cumsum_strided_f64(
    a: *const f64,
    out: *mut f64,
    scan_size: usize,
    outer_size: usize,
    inner_size: usize,
) {
    let level = detect_simd();

    if inner_size < SIMD_THRESHOLD || level == SimdLevel::Scalar {
        cumsum_strided_scalar_f64(a, out, scan_size, outer_size, inner_size);
        return;
    }

    #[cfg(target_arch = "x86_64")]
    match level {
        SimdLevel::Avx512 => avx512::cumsum_strided_f64(a, out, scan_size, outer_size, inner_size),
        SimdLevel::Avx2Fma => avx2::cumsum_strided_f64(a, out, scan_size, outer_size, inner_size),
        _ => cumsum_strided_scalar_f64(a, out, scan_size, outer_size, inner_size),
    }

    #[cfg(target_arch = "aarch64")]
    match level {
        SimdLevel::Neon | SimdLevel::NeonFp16 => {
            aarch64::neon::cumsum_strided_f64(a, out, scan_size, outer_size, inner_size)
        }
        _ => cumsum_strided_scalar_f64(a, out, scan_size, outer_size, inner_size),
    }

    #[cfg(not(any(target_arch = "x86_64", target_arch = "aarch64")))]
    cumsum_strided_scalar_f64(a, out, scan_size, outer_size, inner_size);
}

// ============================================================================
// Cumprod Strided - SIMD Dispatch
// ============================================================================

/// SIMD-optimized strided cumprod for f32.
pub unsafe fn cumprod_strided_f32(
    a: *const f32,
    out: *mut f32,
    scan_size: usize,
    outer_size: usize,
    inner_size: usize,
) {
    let level = detect_simd();

    if inner_size < SIMD_THRESHOLD || level == SimdLevel::Scalar {
        cumprod_strided_scalar_f32(a, out, scan_size, outer_size, inner_size);
        return;
    }

    #[cfg(target_arch = "x86_64")]
    match level {
        SimdLevel::Avx512 => avx512::cumprod_strided_f32(a, out, scan_size, outer_size, inner_size),
        SimdLevel::Avx2Fma => avx2::cumprod_strided_f32(a, out, scan_size, outer_size, inner_size),
        _ => cumprod_strided_scalar_f32(a, out, scan_size, outer_size, inner_size),
    }

    #[cfg(target_arch = "aarch64")]
    match level {
        SimdLevel::Neon | SimdLevel::NeonFp16 => {
            aarch64::neon::cumprod_strided_f32(a, out, scan_size, outer_size, inner_size)
        }
        _ => cumprod_strided_scalar_f32(a, out, scan_size, outer_size, inner_size),
    }

    #[cfg(not(any(target_arch = "x86_64", target_arch = "aarch64")))]
    cumprod_strided_scalar_f32(a, out, scan_size, outer_size, inner_size);
}

/// SIMD-optimized strided cumprod for f64.
pub unsafe fn cumprod_strided_f64(
    a: *const f64,
    out: *mut f64,
    scan_size: usize,
    outer_size: usize,
    inner_size: usize,
) {
    let level = detect_simd();

    if inner_size < SIMD_THRESHOLD || level == SimdLevel::Scalar {
        cumprod_strided_scalar_f64(a, out, scan_size, outer_size, inner_size);
        return;
    }

    #[cfg(target_arch = "x86_64")]
    match level {
        SimdLevel::Avx512 => avx512::cumprod_strided_f64(a, out, scan_size, outer_size, inner_size),
        SimdLevel::Avx2Fma => avx2::cumprod_strided_f64(a, out, scan_size, outer_size, inner_size),
        _ => cumprod_strided_scalar_f64(a, out, scan_size, outer_size, inner_size),
    }

    #[cfg(target_arch = "aarch64")]
    match level {
        SimdLevel::Neon | SimdLevel::NeonFp16 => {
            aarch64::neon::cumprod_strided_f64(a, out, scan_size, outer_size, inner_size)
        }
        _ => cumprod_strided_scalar_f64(a, out, scan_size, outer_size, inner_size),
    }

    #[cfg(not(any(target_arch = "x86_64", target_arch = "aarch64")))]
    cumprod_strided_scalar_f64(a, out, scan_size, outer_size, inner_size);
}

// ============================================================================
// Scalar Fallbacks
// ============================================================================

#[inline]
unsafe fn cumsum_strided_scalar_f32(
    a: *const f32,
    out: *mut f32,
    scan_size: usize,
    outer_size: usize,
    inner_size: usize,
) {
    for o in 0..outer_size {
        for i in 0..inner_size {
            let mut acc = 0.0f32;
            for s in 0..scan_size {
                let idx = o * scan_size * inner_size + s * inner_size + i;
                acc += *a.add(idx);
                *out.add(idx) = acc;
            }
        }
    }
}

#[inline]
unsafe fn cumsum_strided_scalar_f64(
    a: *const f64,
    out: *mut f64,
    scan_size: usize,
    outer_size: usize,
    inner_size: usize,
) {
    for o in 0..outer_size {
        for i in 0..inner_size {
            let mut acc = 0.0f64;
            for s in 0..scan_size {
                let idx = o * scan_size * inner_size + s * inner_size + i;
                acc += *a.add(idx);
                *out.add(idx) = acc;
            }
        }
    }
}

#[inline]
unsafe fn cumprod_strided_scalar_f32(
    a: *const f32,
    out: *mut f32,
    scan_size: usize,
    outer_size: usize,
    inner_size: usize,
) {
    for o in 0..outer_size {
        for i in 0..inner_size {
            let mut acc = 1.0f32;
            for s in 0..scan_size {
                let idx = o * scan_size * inner_size + s * inner_size + i;
                acc *= *a.add(idx);
                *out.add(idx) = acc;
            }
        }
    }
}

#[inline]
unsafe fn cumprod_strided_scalar_f64(
    a: *const f64,
    out: *mut f64,
    scan_size: usize,
    outer_size: usize,
    inner_size: usize,
) {
    for o in 0..outer_size {
        for i in 0..inner_size {
            let mut acc = 1.0f64;
            for s in 0..scan_size {
                let idx = o * scan_size * inner_size + s * inner_size + i;
                acc *= *a.add(idx);
                *out.add(idx) = acc;
            }
        }
    }
}

// ============================================================================
// f16 / bf16 wrappers
// ============================================================================

#[cfg(feature = "f16")]
/// f16 wrapper for cumsum_strided: converts input to f32, runs f32 cumsum, converts output back.
///
/// # Safety
/// - All pointers must be valid for the specified sizes
pub unsafe fn cumsum_strided_f16(
    a: *const half::f16,
    out: *mut half::f16,
    scan_size: usize,
    outer_size: usize,
    inner_size: usize,
) {
    use super::half_convert_utils::*;
    let total = outer_size * scan_size * inner_size;
    let mut a_f32 = vec![0.0f32; total];
    let mut out_f32 = vec![0.0f32; total];
    convert_f16_to_f32(a as *const u16, a_f32.as_mut_ptr(), total);
    cumsum_strided_f32(
        a_f32.as_ptr(),
        out_f32.as_mut_ptr(),
        scan_size,
        outer_size,
        inner_size,
    );
    convert_f32_to_f16(out_f32.as_ptr(), out as *mut u16, total);
}

#[cfg(feature = "f16")]
/// bf16 wrapper for cumsum_strided: converts input to f32, runs f32 cumsum, converts output back.
///
/// # Safety
/// - All pointers must be valid for the specified sizes
pub unsafe fn cumsum_strided_bf16(
    a: *const half::bf16,
    out: *mut half::bf16,
    scan_size: usize,
    outer_size: usize,
    inner_size: usize,
) {
    use super::half_convert_utils::*;
    let total = outer_size * scan_size * inner_size;
    let mut a_f32 = vec![0.0f32; total];
    let mut out_f32 = vec![0.0f32; total];
    convert_bf16_to_f32(a as *const u16, a_f32.as_mut_ptr(), total);
    cumsum_strided_f32(
        a_f32.as_ptr(),
        out_f32.as_mut_ptr(),
        scan_size,
        outer_size,
        inner_size,
    );
    convert_f32_to_bf16(out_f32.as_ptr(), out as *mut u16, total);
}

#[cfg(feature = "f16")]
/// f16 wrapper for cumprod_strided: converts input to f32, runs f32 cumprod, converts output back.
///
/// # Safety
/// - All pointers must be valid for the specified sizes
pub unsafe fn cumprod_strided_f16(
    a: *const half::f16,
    out: *mut half::f16,
    scan_size: usize,
    outer_size: usize,
    inner_size: usize,
) {
    use super::half_convert_utils::*;
    let total = outer_size * scan_size * inner_size;
    let mut a_f32 = vec![0.0f32; total];
    let mut out_f32 = vec![0.0f32; total];
    convert_f16_to_f32(a as *const u16, a_f32.as_mut_ptr(), total);
    cumprod_strided_f32(
        a_f32.as_ptr(),
        out_f32.as_mut_ptr(),
        scan_size,
        outer_size,
        inner_size,
    );
    convert_f32_to_f16(out_f32.as_ptr(), out as *mut u16, total);
}

#[cfg(feature = "f16")]
/// bf16 wrapper for cumprod_strided: converts input to f32, runs f32 cumprod, converts output back.
///
/// # Safety
/// - All pointers must be valid for the specified sizes
pub unsafe fn cumprod_strided_bf16(
    a: *const half::bf16,
    out: *mut half::bf16,
    scan_size: usize,
    outer_size: usize,
    inner_size: usize,
) {
    use super::half_convert_utils::*;
    let total = outer_size * scan_size * inner_size;
    let mut a_f32 = vec![0.0f32; total];
    let mut out_f32 = vec![0.0f32; total];
    convert_bf16_to_f32(a as *const u16, a_f32.as_mut_ptr(), total);
    cumprod_strided_f32(
        a_f32.as_ptr(),
        out_f32.as_mut_ptr(),
        scan_size,
        outer_size,
        inner_size,
    );
    convert_f32_to_bf16(out_f32.as_ptr(), out as *mut u16, total);
}

// ============================================================================
// Tests
// ============================================================================

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

    #[test]
    fn test_cumsum_strided_f32() {
        // 2 outer segments, scan_size=3, inner_size=4
        // Layout: [o=0,s=0,i=0-3], [o=0,s=1,i=0-3], [o=0,s=2,i=0-3],
        //         [o=1,s=0,i=0-3], [o=1,s=1,i=0-3], [o=1,s=2,i=0-3]
        let input: Vec<f32> = (0..24).map(|x| x as f32).collect();
        let mut output = vec![0.0f32; 24];

        unsafe {
            cumsum_strided_f32(input.as_ptr(), output.as_mut_ptr(), 3, 2, 4);
        }

        // Check first outer segment (o=0), i=0
        // s=0: 0, s=1: 0+4=4, s=2: 0+4+8=12
        assert_eq!(output[0], 0.0);
        assert_eq!(output[4], 4.0);
        assert_eq!(output[8], 12.0);

        // Check first outer segment (o=0), i=1
        // s=0: 1, s=1: 1+5=6, s=2: 1+5+9=15
        assert_eq!(output[1], 1.0);
        assert_eq!(output[5], 6.0);
        assert_eq!(output[9], 15.0);
    }

    #[test]
    fn test_cumprod_strided_f32() {
        // 1 outer segment, scan_size=4, inner_size=2
        let input = vec![1.0f32, 2.0, 2.0, 3.0, 3.0, 4.0, 4.0, 5.0];
        let mut output = vec![0.0f32; 8];

        unsafe {
            cumprod_strided_f32(input.as_ptr(), output.as_mut_ptr(), 4, 1, 2);
        }

        // i=0: 1, 1*2=2, 2*3=6, 6*4=24
        assert_eq!(output[0], 1.0);
        assert_eq!(output[2], 2.0);
        assert_eq!(output[4], 6.0);
        assert_eq!(output[6], 24.0);

        // i=1: 2, 2*3=6, 6*4=24, 24*5=120
        assert_eq!(output[1], 2.0);
        assert_eq!(output[3], 6.0);
        assert_eq!(output[5], 24.0);
        assert_eq!(output[7], 120.0);
    }
}