numr 0.9.0

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
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
//! Cumulative operation kernels (cumsum, cumprod, logsumexp)

use super::wide_acc::WideAcc;
use crate::dtype::Element;

/// Cumulative sum along a contiguous dimension
///
/// # Arguments
/// * `a` - Input pointer (scan_size * outer_size elements, contiguous)
/// * `out` - Output pointer (scan_size * outer_size elements)
/// * `scan_size` - Number of elements to scan over per segment
/// * `outer_size` - Number of independent scans
///
/// # Safety
/// - `a` must point to `scan_size * outer_size` elements
/// - `out` must point to `scan_size * outer_size` elements
#[inline]
pub unsafe fn cumsum_kernel<T: Element>(
    a: *const T,
    out: *mut T,
    scan_size: usize,
    outer_size: usize,
) {
    // The running total outgrows the element type long before a scan of any
    // length ends, for every float narrower than F32 and for every integer.
    // Those accumulate wide and narrow once per element written; F32, F64, and
    // the complex types already accumulate in a type as wide as their output
    // and keep the direct path.
    if T::DTYPE.is_narrow_float() {
        cumsum_kernel_acc::<T, f32>(a, out, scan_size, outer_size);
        return;
    }
    if T::DTYPE.is_int() {
        cumsum_kernel_acc::<T, i128>(a, out, scan_size, outer_size);
        return;
    }

    for o in 0..outer_size {
        let base = o * scan_size;
        let mut acc = T::zero();
        for i in 0..scan_size {
            acc = acc + *a.add(base + i);
            *out.add(base + i) = acc;
        }
    }
}

/// Cumulative sum over a contiguous dimension with a wide accumulator.
///
/// # Safety
/// Same as [`cumsum_kernel`].
#[inline]
unsafe fn cumsum_kernel_acc<T: Element, A: WideAcc>(
    a: *const T,
    out: *mut T,
    scan_size: usize,
    outer_size: usize,
) {
    for o in 0..outer_size {
        let base = o * scan_size;
        let mut acc = A::ZERO;
        for i in 0..scan_size {
            acc = acc.wide_add(A::from_elem(*a.add(base + i)));
            *out.add(base + i) = acc.to_elem::<T>();
        }
    }
}

/// Cumulative sum along a strided dimension
///
/// # Arguments
/// * `a` - Input pointer
/// * `out` - Output pointer
/// * `scan_size` - Number of elements to scan over per segment
/// * `outer_size` - Number of independent scans
/// * `inner_size` - Stride between consecutive elements in scan dimension
///
/// # Safety
/// - Pointers must be valid for the given strides and sizes
#[inline]
pub unsafe fn cumsum_strided_kernel<T: Element>(
    a: *const T,
    out: *mut T,
    scan_size: usize,
    outer_size: usize,
    inner_size: usize,
) {
    // Use SIMD for f32/f64 on x86_64 and aarch64
    #[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
    {
        use super::simd::cumulative;
        use crate::dtype::DType;

        match T::DTYPE {
            DType::F32 => {
                cumulative::cumsum_strided_f32(
                    a as *const f32,
                    out as *mut f32,
                    scan_size,
                    outer_size,
                    inner_size,
                );
                return;
            }
            DType::F64 => {
                cumulative::cumsum_strided_f64(
                    a as *const f64,
                    out as *mut f64,
                    scan_size,
                    outer_size,
                    inner_size,
                );
                return;
            }
            #[cfg(feature = "f16")]
            DType::F16 => {
                cumulative::cumsum_strided_f16(
                    a as *const half::f16,
                    out as *mut half::f16,
                    scan_size,
                    outer_size,
                    inner_size,
                );
                return;
            }
            #[cfg(feature = "f16")]
            DType::BF16 => {
                cumulative::cumsum_strided_bf16(
                    a as *const half::bf16,
                    out as *mut half::bf16,
                    scan_size,
                    outer_size,
                    inner_size,
                );
                return;
            }
            _ => {} // Fall through to scalar
        }
    }

    // Scalar fallback. FP8 never reaches the SIMD block above, and neither do
    // the integer dtypes, so both need the wide accumulator here for the same
    // reason `cumsum_kernel` does.
    if T::DTYPE.is_narrow_float() {
        cumsum_strided_kernel_acc::<T, f32>(a, out, scan_size, outer_size, inner_size);
        return;
    }
    if T::DTYPE.is_int() {
        cumsum_strided_kernel_acc::<T, i128>(a, out, scan_size, outer_size, inner_size);
        return;
    }

    // For strided access: element [o, s, i] is at offset o * scan_size * inner_size + s * inner_size + i
    for o in 0..outer_size {
        for i in 0..inner_size {
            let mut acc = T::zero();
            for s in 0..scan_size {
                let idx = o * scan_size * inner_size + s * inner_size + i;
                acc = acc + *a.add(idx);
                *out.add(idx) = acc;
            }
        }
    }
}

/// Cumulative sum over a strided dimension with a wide accumulator.
///
/// # Safety
/// Same as [`cumsum_strided_kernel`].
#[inline]
unsafe fn cumsum_strided_kernel_acc<T: Element, A: WideAcc>(
    a: *const T,
    out: *mut T,
    scan_size: usize,
    outer_size: usize,
    inner_size: usize,
) {
    for o in 0..outer_size {
        for i in 0..inner_size {
            let mut acc = A::ZERO;
            for s in 0..scan_size {
                let idx = o * scan_size * inner_size + s * inner_size + i;
                acc = acc.wide_add(A::from_elem(*a.add(idx)));
                *out.add(idx) = acc.to_elem::<T>();
            }
        }
    }
}

/// Cumulative product along a contiguous dimension
///
/// # Arguments
/// * `a` - Input pointer (scan_size * outer_size elements, contiguous)
/// * `out` - Output pointer (scan_size * outer_size elements)
/// * `scan_size` - Number of elements to scan over per segment
/// * `outer_size` - Number of independent scans
///
/// # Safety
/// - `a` must point to `scan_size * outer_size` elements
/// - `out` must point to `scan_size * outer_size` elements
#[inline]
pub unsafe fn cumprod_kernel<T: Element>(
    a: *const T,
    out: *mut T,
    scan_size: usize,
    outer_size: usize,
) {
    // A running product in a float narrower than F32 underflows to zero or
    // overflows to infinity far sooner than the true value warrants, the same
    // failure `cumsum_kernel` fixes for sums. It widens for the same reason.
    if T::DTYPE.is_narrow_float() {
        cumprod_kernel_acc::<T, f32>(a, out, scan_size, outer_size);
        return;
    }
    // An integer running product leaves the element type's range far faster
    // than a running sum does: it wraps in release and panics in debug. It
    // accumulates in i128 and clamps once per element written, the same
    // convention `cumsum_kernel` follows.
    if T::DTYPE.is_int() {
        cumprod_kernel_acc::<T, i128>(a, out, scan_size, outer_size);
        return;
    }

    for o in 0..outer_size {
        let base = o * scan_size;
        let mut acc = T::one();
        for i in 0..scan_size {
            acc = acc * *a.add(base + i);
            *out.add(base + i) = acc;
        }
    }
}

/// Cumulative product over a contiguous dimension with a wide accumulator.
///
/// For `A = i128` the accumulator saturates, so the output is the true product
/// clamped to the element type's range. i128 is exact for any product that
/// stays inside it, and once it saturates a later factor still moves the sign
/// correctly: `i128::MAX * -1` saturates to `i128::MIN`, which narrows to
/// `T::MIN`. For `A = f32` the accumulator holds every product a narrow float
/// can represent, so no clamping happens before the final narrow.
///
/// # Safety
/// Same as [`cumprod_kernel`].
#[inline]
unsafe fn cumprod_kernel_acc<T: Element, A: WideAcc>(
    a: *const T,
    out: *mut T,
    scan_size: usize,
    outer_size: usize,
) {
    for o in 0..outer_size {
        let base = o * scan_size;
        let mut acc = A::ONE;
        for i in 0..scan_size {
            acc = acc.wide_mul(A::from_elem(*a.add(base + i)));
            *out.add(base + i) = acc.to_elem::<T>();
        }
    }
}

/// Cumulative product along a strided dimension
///
/// # Arguments
/// * `a` - Input pointer
/// * `out` - Output pointer
/// * `scan_size` - Number of elements to scan over per segment
/// * `outer_size` - Number of independent scans
/// * `inner_size` - Stride between consecutive elements in scan dimension
///
/// # Safety
/// - Pointers must be valid for the given strides and sizes
#[inline]
pub unsafe fn cumprod_strided_kernel<T: Element>(
    a: *const T,
    out: *mut T,
    scan_size: usize,
    outer_size: usize,
    inner_size: usize,
) {
    // Use SIMD for f32/f64 on x86_64 and aarch64
    #[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
    {
        use super::simd::cumulative;
        use crate::dtype::DType;

        match T::DTYPE {
            DType::F32 => {
                cumulative::cumprod_strided_f32(
                    a as *const f32,
                    out as *mut f32,
                    scan_size,
                    outer_size,
                    inner_size,
                );
                return;
            }
            DType::F64 => {
                cumulative::cumprod_strided_f64(
                    a as *const f64,
                    out as *mut f64,
                    scan_size,
                    outer_size,
                    inner_size,
                );
                return;
            }
            #[cfg(feature = "f16")]
            DType::F16 => {
                cumulative::cumprod_strided_f16(
                    a as *const half::f16,
                    out as *mut half::f16,
                    scan_size,
                    outer_size,
                    inner_size,
                );
                return;
            }
            #[cfg(feature = "f16")]
            DType::BF16 => {
                cumulative::cumprod_strided_bf16(
                    a as *const half::bf16,
                    out as *mut half::bf16,
                    scan_size,
                    outer_size,
                    inner_size,
                );
                return;
            }
            _ => {} // Fall through to scalar
        }
    }

    // Scalar fallback. FP8 never reaches the SIMD block above, and neither do
    // the integer dtypes, so both need the wide accumulator here for the same
    // reason `cumprod_kernel` does.
    if T::DTYPE.is_narrow_float() {
        cumprod_strided_kernel_acc::<T, f32>(a, out, scan_size, outer_size, inner_size);
        return;
    }
    if T::DTYPE.is_int() {
        cumprod_strided_kernel_acc::<T, i128>(a, out, scan_size, outer_size, inner_size);
        return;
    }

    for o in 0..outer_size {
        for i in 0..inner_size {
            let mut acc = T::one();
            for s in 0..scan_size {
                let idx = o * scan_size * inner_size + s * inner_size + i;
                acc = acc * *a.add(idx);
                *out.add(idx) = acc;
            }
        }
    }
}

/// Cumulative product over a strided dimension with a wide accumulator.
///
/// # Safety
/// Same as [`cumprod_strided_kernel`].
#[inline]
unsafe fn cumprod_strided_kernel_acc<T: Element, A: WideAcc>(
    a: *const T,
    out: *mut T,
    scan_size: usize,
    outer_size: usize,
    inner_size: usize,
) {
    for o in 0..outer_size {
        for i in 0..inner_size {
            let mut acc = A::ONE;
            for s in 0..scan_size {
                let idx = o * scan_size * inner_size + s * inner_size + i;
                acc = acc.wide_mul(A::from_elem(*a.add(idx)));
                *out.add(idx) = acc.to_elem::<T>();
            }
        }
    }
}

/// Log-sum-exp along a contiguous dimension (numerically stable)
///
/// Computes log(sum(exp(x))) = max(x) + log(sum(exp(x - max(x))))
///
/// On x86-64, dispatches to optimized SIMD implementations for f32/f64:
/// - AVX-512: 16 f32s or 8 f64s per iteration with vectorized exp
/// - AVX2: 8 f32s or 4 f64s per iteration with vectorized exp
/// - Scalar fallback for other types or non-x86 platforms
///
/// # Arguments
/// * `a` - Input pointer (reduce_size * outer_size elements, contiguous)
/// * `out` - Output pointer (outer_size elements)
/// * `reduce_size` - Number of elements to reduce per segment
/// * `outer_size` - Number of independent reductions
///
/// # Safety
/// - `a` must point to `reduce_size * outer_size` elements
/// - `out` must point to `outer_size` elements
#[inline]
pub unsafe fn logsumexp_kernel<T: Element>(
    a: *const T,
    out: *mut T,
    reduce_size: usize,
    outer_size: usize,
) {
    // A zero-length reduce dimension makes every output element a fold over no
    // input: `log(sum of nothing)` is `log(0)`, so the answer is -inf. Both the
    // SIMD paths below and the scalar fallback seed their max from `a[base]`,
    // which on an empty allocation reads past the end (`CpuRuntime::allocate`
    // hands back a dangling, non-null address for zero bytes), so this must
    // answer before any of them run.
    if reduce_size == 0 {
        let identity = T::from_f64(f64::NEG_INFINITY);
        for o in 0..outer_size {
            *out.add(o) = identity;
        }
        return;
    }

    // Dispatch to SIMD for f32/f64 on x86-64 and aarch64
    #[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
    {
        use super::simd::logsumexp;
        use crate::dtype::DType;

        match T::DTYPE {
            DType::F32 => {
                logsumexp::logsumexp_f32(a as *const f32, out as *mut f32, reduce_size, outer_size);
                return;
            }
            DType::F64 => {
                logsumexp::logsumexp_f64(a as *const f64, out as *mut f64, reduce_size, outer_size);
                return;
            }
            #[cfg(feature = "f16")]
            DType::F16 => {
                logsumexp::logsumexp_f16(
                    a as *const half::f16,
                    out as *mut half::f16,
                    reduce_size,
                    outer_size,
                );
                return;
            }
            #[cfg(feature = "f16")]
            DType::BF16 => {
                logsumexp::logsumexp_bf16(
                    a as *const half::bf16,
                    out as *mut half::bf16,
                    reduce_size,
                    outer_size,
                );
                return;
            }
            _ => {} // Fall through to scalar
        }
    }

    // Scalar fallback
    logsumexp_kernel_scalar(a, out, reduce_size, outer_size);
}

/// Scalar logsumexp for all Element types
#[inline]
unsafe fn logsumexp_kernel_scalar<T: Element>(
    a: *const T,
    out: *mut T,
    reduce_size: usize,
    outer_size: usize,
) {
    for o in 0..outer_size {
        let base = o * reduce_size;

        // Step 1: Find max
        let mut max_val = *a.add(base);
        for i in 1..reduce_size {
            let val = *a.add(base + i);
            if val > max_val {
                max_val = val;
            }
        }

        // Step 2: Compute sum(exp(x - max))
        //
        // A float narrower than F32 must not hold this accumulator. Every term
        // is at most 1.0, so the running sum saturates quickly and stalls, and
        // `log(sum)` then comes back short — which makes every log_softmax
        // built on it wrong. Widen those to f64 (what `logsumexp_strided_kernel`
        // already does) and narrow only the final result. Every other dtype
        // keeps its own accumulator and its existing result.
        if T::DTYPE.is_narrow_float() {
            let mut sum = 0.0f64;
            for i in 0..reduce_size {
                let val = (*a.add(base + i)).to_f64();
                sum += (val - max_val.to_f64()).exp();
            }
            *out.add(o) = T::from_f64(max_val.to_f64() + sum.ln());
            continue;
        }

        let mut sum = T::zero();
        for i in 0..reduce_size {
            let val = *a.add(base + i);
            // Compute exp(val - max_val) using f64 for precision
            let exp_val = T::from_f64((val.to_f64() - max_val.to_f64()).exp());
            sum = sum + exp_val;
        }

        // Step 3: Result = max + log(sum)
        *out.add(o) = T::from_f64(max_val.to_f64() + sum.to_f64().ln());
    }
}

/// Log-sum-exp along a strided dimension (numerically stable)
///
/// # Safety
/// - Pointers must be valid for the given strides and sizes
#[inline]
pub unsafe fn logsumexp_strided_kernel<T: Element>(
    a: *const T,
    out: *mut T,
    reduce_size: usize,
    outer_size: usize,
    inner_size: usize,
    _in_stride: usize, // stride along the reduce dimension in input (unused, kept for API parity)
    out_stride: usize, // stride in output
) {
    // Same identity as `logsumexp_kernel`: a fold over no input is `log(0)`,
    // and the seed read below would otherwise walk off an empty allocation.
    if reduce_size == 0 {
        let identity = T::from_f64(f64::NEG_INFINITY);
        for o in 0..outer_size {
            for i in 0..inner_size {
                *out.add(o * out_stride + i) = identity;
            }
        }
        return;
    }

    for o in 0..outer_size {
        for i in 0..inner_size {
            let out_idx = o * out_stride + i;

            // Step 1: Find max along reduce dimension
            let first_idx = o * reduce_size * inner_size + i;
            let mut max_val = *a.add(first_idx);
            for r in 1..reduce_size {
                let idx = o * reduce_size * inner_size + r * inner_size + i;
                let val = *a.add(idx);
                if val > max_val {
                    max_val = val;
                }
            }

            // Step 2: Compute sum(exp(x - max))
            let mut sum = 0.0f64;
            for r in 0..reduce_size {
                let idx = o * reduce_size * inner_size + r * inner_size + i;
                let val = (*a.add(idx)).to_f64();
                sum += (val - max_val.to_f64()).exp();
            }

            // Step 3: Result = max + log(sum)
            *out.add(out_idx) = T::from_f64(max_val.to_f64() + sum.ln());
        }
    }
}

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

    #[test]
    fn test_cumsum_basic() {
        let a = [1.0f32, 2.0, 3.0, 4.0];
        let mut out = [0.0f32; 4];

        unsafe {
            cumsum_kernel(a.as_ptr(), out.as_mut_ptr(), 4, 1);
        }

        assert_eq!(out, [1.0, 3.0, 6.0, 10.0]);
    }

    #[test]
    fn test_cumsum_multiple_segments() {
        // Two segments of 3 elements each
        let a = [1.0f32, 2.0, 3.0, 10.0, 20.0, 30.0];
        let mut out = [0.0f32; 6];

        unsafe {
            cumsum_kernel(a.as_ptr(), out.as_mut_ptr(), 3, 2);
        }

        assert_eq!(out, [1.0, 3.0, 6.0, 10.0, 30.0, 60.0]);
    }

    #[test]
    fn test_cumprod_basic() {
        let a = [1.0f32, 2.0, 3.0, 4.0];
        let mut out = [0.0f32; 4];

        unsafe {
            cumprod_kernel(a.as_ptr(), out.as_mut_ptr(), 4, 1);
        }

        assert_eq!(out, [1.0, 2.0, 6.0, 24.0]);
    }

    #[test]
    fn test_cumprod_multiple_segments() {
        let a = [1.0f32, 2.0, 3.0, 2.0, 3.0, 4.0];
        let mut out = [0.0f32; 6];

        unsafe {
            cumprod_kernel(a.as_ptr(), out.as_mut_ptr(), 3, 2);
        }

        assert_eq!(out, [1.0, 2.0, 6.0, 2.0, 6.0, 24.0]);
    }

    #[test]
    fn test_logsumexp_basic() {
        let a = [1.0f32, 2.0, 3.0];
        let mut out = [0.0f32; 1];

        unsafe {
            logsumexp_kernel(a.as_ptr(), out.as_mut_ptr(), 3, 1);
        }

        // log(exp(1) + exp(2) + exp(3)) ≈ 3.4076
        let expected = (1.0f64.exp() + 2.0f64.exp() + 3.0f64.exp()).ln();
        assert!((out[0] as f64 - expected).abs() < 1e-5);
    }

    #[test]
    fn test_logsumexp_multiple_segments() {
        let a = [1.0f32, 2.0, 3.0, 10.0, 20.0, 30.0];
        let mut out = [0.0f32; 2];

        unsafe {
            logsumexp_kernel(a.as_ptr(), out.as_mut_ptr(), 3, 2);
        }

        let expected0 = (1.0f64.exp() + 2.0f64.exp() + 3.0f64.exp()).ln();
        let expected1 = (10.0f64.exp() + 20.0f64.exp() + 30.0f64.exp()).ln();
        assert!((out[0] as f64 - expected0).abs() < 1e-5);
        assert!((out[1] as f64 - expected1).abs() < 1e-5);
    }

    #[test]
    fn test_logsumexp_numerical_stability() {
        // Test with large values that would overflow naive exp
        let a = [1000.0f32, 1000.0, 1000.0];
        let mut out = [0.0f32; 1];

        unsafe {
            logsumexp_kernel(a.as_ptr(), out.as_mut_ptr(), 3, 1);
        }

        // Should be log(3) + 1000 ≈ 1001.0986
        let expected = 1000.0 + (3.0f64).ln();
        assert!((out[0] as f64 - expected).abs() < 1e-3);
    }

    /// Catches a `cumsum` accumulator held in the element type for FP8.
    ///
    /// FP8E4M3 has three mantissa bits, so above 16 its spacing is 2 and
    /// `16 + 1` rounds back to 16. An FP8 accumulator therefore stalls at 16
    /// and every later output reads 16 instead of the true partial sum.
    #[test]
    fn test_cumsum_fp8_accumulates_wider_than_the_element_type() {
        use crate::dtype::FP8E4M3;

        let a = [FP8E4M3::from_f32(1.0); 32];
        let mut out = [FP8E4M3::from_f32(0.0); 32];

        unsafe {
            cumsum_kernel(a.as_ptr(), out.as_mut_ptr(), 32, 1);
        }

        // 24 and 32 are both exactly representable in FP8E4M3.
        assert_eq!(out[23].to_f32(), 24.0);
        assert_eq!(out[31].to_f32(), 32.0);
    }

    /// Catches a `cumsum` accumulator held in the element type for F16.
    ///
    /// F16 has ten mantissa bits, so above 2048 its spacing is 2 and
    /// `2048 + 1` rounds back to 2048. An F16 accumulator stalls there.
    #[cfg(feature = "f16")]
    #[test]
    fn test_cumsum_f16_accumulates_wider_than_the_element_type() {
        let a = vec![half::f16::from_f32(1.0); 3000];
        let mut out = vec![half::f16::from_f32(0.0); 3000];

        unsafe {
            cumsum_kernel(a.as_ptr(), out.as_mut_ptr(), 3000, 1);
        }

        // 2500 and 3000 are even, so both are exact in F16.
        assert_eq!(out[2499].to_f32(), 2500.0);
        assert_eq!(out[2999].to_f32(), 3000.0);
    }

    /// Catches an i32 `cumsum` accumulator.
    ///
    /// The running total leaves i32's range at element 1 and comes back at
    /// element 2. An i32 accumulator panics on that overflow in a debug build,
    /// and in a release build stores the wrapped -294_967_296 where the
    /// documented answer is the saturated `i32::MAX`.
    #[test]
    fn test_cumsum_i32_accumulates_in_a_wider_integer() {
        let a = [2_000_000_000i32, 2_000_000_000, -2_000_000_000];
        let mut out = [0i32; 3];

        unsafe {
            cumsum_kernel(a.as_ptr(), out.as_mut_ptr(), 3, 1);
        }

        assert_eq!(out, [2_000_000_000, i32::MAX, 2_000_000_000]);
    }

    /// Same accumulator defect on the strided path, which the SIMD block above
    /// never covers for integers.
    ///
    /// Layout is `[scan][inner]` with `inner_size = 2`: column 0 overflows i32,
    /// column 1 stays small and pins that the fix did not disturb it.
    #[test]
    fn test_cumsum_strided_i32_accumulates_in_a_wider_integer() {
        let a = [2_000_000_000i32, 1, 2_000_000_000, 2, -2_000_000_000, 3];
        let mut out = [0i32; 6];

        unsafe {
            cumsum_strided_kernel(a.as_ptr(), out.as_mut_ptr(), 3, 1, 2);
        }

        assert_eq!(out, [2_000_000_000, 1, i32::MAX, 3, 2_000_000_000, 6]);
    }

    /// Catches an FP8 accumulator on the strided path, which has no SIMD
    /// dispatch for FP8 on any architecture.
    #[test]
    fn test_cumsum_strided_fp8_accumulates_wider_than_the_element_type() {
        use crate::dtype::FP8E4M3;

        // 32 scan steps over 2 interleaved columns of 1.0.
        let a = [FP8E4M3::from_f32(1.0); 64];
        let mut out = [FP8E4M3::from_f32(0.0); 64];

        unsafe {
            cumsum_strided_kernel(a.as_ptr(), out.as_mut_ptr(), 32, 1, 2);
        }

        // Last scan step of each column is 32; an FP8 accumulator reads 16.
        assert_eq!(out[62].to_f32(), 32.0);
        assert_eq!(out[63].to_f32(), 32.0);
    }

    /// The reference case a per-step saturating multiply gets wrong.
    ///
    /// True products are 100_000, 10^10, -10^10, so the clamped answers are
    /// `i32::MAX` then `i32::MIN`. A saturating multiply in i32 clamps to
    /// `i32::MAX` first and then reports `-i32::MAX`, one off and for the wrong
    /// reason.
    #[test]
    fn test_cumprod_i32_saturates_to_the_true_product_sign() {
        let a = [100_000i32, 100_000, -1];
        let mut out = [0i32; 3];

        unsafe {
            cumprod_kernel(a.as_ptr(), out.as_mut_ptr(), 3, 1);
        }

        assert_eq!(out, [100_000, i32::MAX, i32::MIN]);
    }

    /// A zero factor after saturation still gives 0, because the true product
    /// is 0 from that element on.
    #[test]
    fn test_cumprod_i32_zero_after_saturation_is_zero() {
        let a = [100_000i32, 100_000, 0, 7];
        let mut out = [0i32; 4];

        unsafe {
            cumprod_kernel(a.as_ptr(), out.as_mut_ptr(), 4, 1);
        }

        assert_eq!(out, [100_000, i32::MAX, 0, 0]);
    }

    /// Each further negative factor flips the sign of a saturated product.
    #[test]
    fn test_cumprod_i32_sign_flips_across_a_saturated_run() {
        let a = [-100_000i32, 100_000, -1, -1];
        let mut out = [0i32; 4];

        unsafe {
            cumprod_kernel(a.as_ptr(), out.as_mut_ptr(), 4, 1);
        }

        assert_eq!(out, [-100_000, i32::MIN, i32::MAX, i32::MIN]);
    }

    /// U32 has no sign to track, so overflow pins at `u32::MAX` and stays.
    #[test]
    fn test_cumprod_u32_saturates_to_max() {
        let a = [100_000u32, 100_000, 2];
        let mut out = [0u32; 3];

        unsafe {
            cumprod_kernel(a.as_ptr(), out.as_mut_ptr(), 3, 1);
        }

        assert_eq!(out, [100_000, u32::MAX, u32::MAX]);
    }

    /// A product that never leaves i32 is untouched by the wide accumulator.
    #[test]
    fn test_cumprod_i32_without_overflow_is_exact() {
        let a = [2i32, 3, -4, 5];
        let mut out = [0i32; 4];

        unsafe {
            cumprod_kernel(a.as_ptr(), out.as_mut_ptr(), 4, 1);
        }

        assert_eq!(out, [2, 6, -24, -120]);
    }

    /// I64 saturates on the same rule; i128 holds every product of two i64s.
    #[test]
    fn test_cumprod_i64_saturates_to_the_true_product_sign() {
        let a = [4_000_000_000i64, 4_000_000_000, -1];
        let mut out = [0i64; 3];

        unsafe {
            cumprod_kernel(a.as_ptr(), out.as_mut_ptr(), 3, 1);
        }

        assert_eq!(out, [4_000_000_000, i64::MAX, i64::MIN]);
    }

    /// The strided path has no SIMD dispatch for integers, so it needs the same
    /// accumulator.
    ///
    /// Layout is `[scan][inner]` with `inner_size = 2`: column 0 overflows i32
    /// and then flips sign, column 1 stays small.
    #[test]
    fn test_cumprod_strided_i32_saturates_to_the_true_product_sign() {
        let a = [100_000i32, 2, 100_000, 3, -1, 4];
        let mut out = [0i32; 6];

        unsafe {
            cumprod_strided_kernel(a.as_ptr(), out.as_mut_ptr(), 3, 1, 2);
        }

        assert_eq!(out, [100_000, 2, i32::MAX, 6, i32::MIN, 24]);
    }

    /// Strided U32 overflow, which shares no code with the contiguous path.
    #[test]
    fn test_cumprod_strided_u32_saturates_to_max() {
        let a = [100_000u32, 2, 100_000, 3, 2, 4];
        let mut out = [0u32; 6];

        unsafe {
            cumprod_strided_kernel(a.as_ptr(), out.as_mut_ptr(), 3, 1, 2);
        }

        assert_eq!(out, [100_000, 2, u32::MAX, 6, u32::MAX, 24]);
    }

    /// Catches a `cumprod` accumulator held in the element type for BF16.
    ///
    /// All four factors are exact powers of two, so only exponent range can
    /// diverge the two paths. The true product is `2^0 = 1.0`, but the
    /// partial product after two steps is `2^-140`, below BF16's smallest
    /// subnormal (`2^-133`). An accumulator held in BF16 flushes that partial
    /// product to exactly zero, and every later factor keeps it zero.
    #[cfg(feature = "f16")]
    #[test]
    fn test_cumprod_bf16_accumulates_wider_than_the_element_type() {
        use half::bf16;

        let a = [
            bf16::from_f32(2f32.powi(-70)),
            bf16::from_f32(2f32.powi(-70)),
            bf16::from_f32(2f32.powi(70)),
            bf16::from_f32(2f32.powi(70)),
        ];
        let mut out = [bf16::from_f32(0.0); 4];

        unsafe {
            cumprod_kernel(a.as_ptr(), out.as_mut_ptr(), 4, 1);
        }

        // An F32 accumulator keeps 2^-140 as a subnormal instead of flushing
        // it to zero, so the running product recovers to the true 1.0.
        assert_eq!(out[3].to_f32(), 1.0);
    }

    /// Catches a `cumprod` accumulator held in the element type for F16.
    ///
    /// Both factors are exact powers of two. The true product is
    /// `2^-10 = 0.0009765625`, but the partial product after two steps is
    /// `2^20`, above F16's finite range. An accumulator held in F16 rounds
    /// that overflow to infinity, and infinity times any later finite factor
    /// stays infinity.
    #[cfg(feature = "f16")]
    #[test]
    fn test_cumprod_f16_accumulates_wider_than_the_element_type() {
        use half::f16;

        let a = [
            f16::from_f32(2f32.powi(10)),
            f16::from_f32(2f32.powi(10)),
            f16::from_f32(2f32.powi(-15)),
            f16::from_f32(2f32.powi(-15)),
        ];
        let mut out = [f16::from_f32(0.0); 4];

        unsafe {
            cumprod_kernel(a.as_ptr(), out.as_mut_ptr(), 4, 1);
        }

        // An F32 accumulator keeps 2^20 as a plain finite value instead of
        // rounding it to infinity, so the running product recovers to the
        // true 2^-10.
        assert_eq!(out[3].to_f32(), 2f32.powi(-10));
    }

    /// Catches a `cumprod` accumulator held in the element type for FP8E4M3,
    /// on the strided path.
    ///
    /// Column 0 is the same power-of-two overflow-then-recover shape as the
    /// F16 case above, but FP8E4M3 saturates to its finite `MAX` (448)
    /// instead of rounding to infinity. An accumulator held in FP8E4M3 loses
    /// the true magnitude at that saturating step, so later factors recover
    /// to the wrong value instead of the true `2^0 = 1.0`. Column 1 is a
    /// steady product of ones, pinning that the fix leaves the ordinary case
    /// untouched.
    #[test]
    fn test_cumprod_strided_fp8_accumulates_wider_than_the_element_type() {
        use crate::dtype::FP8E4M3;

        let a = [
            FP8E4M3::from_f32(2f32.powi(8)),
            FP8E4M3::from_f32(1.0),
            FP8E4M3::from_f32(2f32.powi(8)),
            FP8E4M3::from_f32(1.0),
            FP8E4M3::from_f32(2f32.powi(-8)),
            FP8E4M3::from_f32(1.0),
            FP8E4M3::from_f32(2f32.powi(-8)),
            FP8E4M3::from_f32(1.0),
        ];
        let mut out = [FP8E4M3::from_f32(0.0); 8];

        unsafe {
            cumprod_strided_kernel(a.as_ptr(), out.as_mut_ptr(), 4, 1, 2);
        }

        // An F32 accumulator keeps 2^16 exactly instead of saturating it to
        // FP8E4M3's MAX, so column 0 recovers to the true 1.0.
        assert_eq!(out[6].to_f32(), 1.0);
        // Column 1 never leaves 1.0, in either implementation.
        assert_eq!(out[7].to_f32(), 1.0);
    }
}