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
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
//! Memory operation kernels (fill, copy, cast, random)

use super::rng;
use crate::dtype::Element;

/// Fill buffer with a constant value
///
/// # Safety
/// - `out` must be a valid pointer to `len` elements
#[inline]
pub unsafe fn fill_kernel<T: Element>(out: *mut T, value: T, len: usize) {
    let out_slice = std::slice::from_raw_parts_mut(out, len);
    out_slice.fill(value);
}

/// Copy elements from src to dst
///
/// # Safety
/// - `src` and `dst` must be valid pointers to `len` elements
/// - `dst` must not overlap with `src`
#[inline]
pub unsafe fn copy_kernel<T: Element>(src: *const T, dst: *mut T, len: usize) {
    std::ptr::copy_nonoverlapping(src, dst, len);
}

/// Cast tensor data from one dtype to another.
///
/// Converts elements by going through f64 as an intermediate representation,
/// which works for all numeric types via the Element trait.
///
/// # Safety
/// - `src` must be valid pointer to `len` elements of `src_dtype`
/// - `dst` must be valid pointer to `len` elements of `dst_dtype`
/// - `src` and `dst` must not overlap
#[inline]
pub unsafe fn cast_kernel(
    src: *const u8,
    dst: *mut u8,
    len: usize,
    src_dtype: crate::dtype::DType,
    dst_dtype: crate::dtype::DType,
) -> crate::error::Result<()> {
    use crate::dtype::DType;
    use crate::error::Error;

    // Helper macro to cast from a known source type to any destination type
    macro_rules! cast_from {
        ($src_ty:ty, $src_ptr:expr, $dst_ptr:expr, $len:expr, $dst_dtype:expr) => {{
            let src_slice = std::slice::from_raw_parts($src_ptr as *const $src_ty, $len);
            match $dst_dtype {
                DType::F64 => {
                    let dst_slice = std::slice::from_raw_parts_mut($dst_ptr as *mut f64, $len);
                    for i in 0..$len {
                        dst_slice[i] = src_slice[i].to_f64();
                    }
                }
                DType::F32 => {
                    let dst_slice = std::slice::from_raw_parts_mut($dst_ptr as *mut f32, $len);
                    for i in 0..$len {
                        dst_slice[i] = src_slice[i].to_f64() as f32;
                    }
                }
                DType::F16 => {
                    #[cfg(feature = "f16")]
                    {
                        let dst_slice =
                            std::slice::from_raw_parts_mut($dst_ptr as *mut half::f16, $len);
                        for i in 0..$len {
                            dst_slice[i] = half::f16::from_f64(src_slice[i].to_f64());
                        }
                    }
                    #[cfg(not(feature = "f16"))]
                    {
                        return Err(Error::UnsupportedDType {
                            dtype: DType::F16,
                            op: "cast",
                        });
                    }
                }
                DType::BF16 => {
                    #[cfg(feature = "f16")]
                    {
                        let dst_slice =
                            std::slice::from_raw_parts_mut($dst_ptr as *mut half::bf16, $len);
                        for i in 0..$len {
                            dst_slice[i] = half::bf16::from_f64(src_slice[i].to_f64());
                        }
                    }
                    #[cfg(not(feature = "f16"))]
                    {
                        return Err(Error::UnsupportedDType {
                            dtype: DType::BF16,
                            op: "cast",
                        });
                    }
                }
                DType::FP8E4M3 => {
                    let dst_slice = std::slice::from_raw_parts_mut(
                        $dst_ptr as *mut crate::dtype::FP8E4M3,
                        $len,
                    );
                    for i in 0..$len {
                        dst_slice[i] =
                            crate::dtype::FP8E4M3::from_f32(src_slice[i].to_f64() as f32);
                    }
                }
                DType::FP8E5M2 => {
                    let dst_slice = std::slice::from_raw_parts_mut(
                        $dst_ptr as *mut crate::dtype::FP8E5M2,
                        $len,
                    );
                    for i in 0..$len {
                        dst_slice[i] =
                            crate::dtype::FP8E5M2::from_f32(src_slice[i].to_f64() as f32);
                    }
                }
                DType::I64 => {
                    let dst_slice = std::slice::from_raw_parts_mut($dst_ptr as *mut i64, $len);
                    for i in 0..$len {
                        dst_slice[i] = src_slice[i].to_f64() as i64;
                    }
                }
                DType::I32 => {
                    let dst_slice = std::slice::from_raw_parts_mut($dst_ptr as *mut i32, $len);
                    for i in 0..$len {
                        dst_slice[i] = src_slice[i].to_f64() as i32;
                    }
                }
                DType::I16 => {
                    let dst_slice = std::slice::from_raw_parts_mut($dst_ptr as *mut i16, $len);
                    for i in 0..$len {
                        dst_slice[i] = src_slice[i].to_f64() as i16;
                    }
                }
                DType::I8 => {
                    let dst_slice = std::slice::from_raw_parts_mut($dst_ptr as *mut i8, $len);
                    for i in 0..$len {
                        dst_slice[i] = src_slice[i].to_f64() as i8;
                    }
                }
                DType::U64 => {
                    let dst_slice = std::slice::from_raw_parts_mut($dst_ptr as *mut u64, $len);
                    for i in 0..$len {
                        dst_slice[i] = src_slice[i].to_f64() as u64;
                    }
                }
                DType::U32 => {
                    let dst_slice = std::slice::from_raw_parts_mut($dst_ptr as *mut u32, $len);
                    for i in 0..$len {
                        dst_slice[i] = src_slice[i].to_f64() as u32;
                    }
                }
                DType::U16 => {
                    let dst_slice = std::slice::from_raw_parts_mut($dst_ptr as *mut u16, $len);
                    for i in 0..$len {
                        dst_slice[i] = src_slice[i].to_f64() as u16;
                    }
                }
                DType::U8 => {
                    let dst_slice = std::slice::from_raw_parts_mut($dst_ptr as *mut u8, $len);
                    for i in 0..$len {
                        dst_slice[i] = src_slice[i].to_f64() as u8;
                    }
                }
                DType::Bool => {
                    let dst_slice = std::slice::from_raw_parts_mut($dst_ptr as *mut u8, $len);
                    for i in 0..$len {
                        dst_slice[i] = if src_slice[i].to_f64() != 0.0 { 1 } else { 0 };
                    }
                }
                DType::Complex64 => {
                    let dst_slice = std::slice::from_raw_parts_mut(
                        $dst_ptr as *mut crate::dtype::Complex64,
                        $len,
                    );
                    for i in 0..$len {
                        dst_slice[i] =
                            crate::dtype::Complex64::new(src_slice[i].to_f64() as f32, 0.0);
                    }
                }
                DType::Complex128 => {
                    let dst_slice = std::slice::from_raw_parts_mut(
                        $dst_ptr as *mut crate::dtype::Complex128,
                        $len,
                    );
                    for i in 0..$len {
                        dst_slice[i] = crate::dtype::Complex128::new(src_slice[i].to_f64(), 0.0);
                    }
                }
            }
        }};
    }

    // Dispatch based on source dtype
    match src_dtype {
        DType::F64 => cast_from!(f64, src, dst, len, dst_dtype),
        DType::F32 => cast_from!(f32, src, dst, len, dst_dtype),
        DType::F16 => {
            #[cfg(feature = "f16")]
            {
                cast_from!(half::f16, src, dst, len, dst_dtype)
            }
            #[cfg(not(feature = "f16"))]
            {
                return Err(Error::UnsupportedDType {
                    dtype: DType::F16,
                    op: "cast",
                });
            }
        }
        DType::BF16 => {
            #[cfg(feature = "f16")]
            {
                cast_from!(half::bf16, src, dst, len, dst_dtype)
            }
            #[cfg(not(feature = "f16"))]
            {
                return Err(Error::UnsupportedDType {
                    dtype: DType::BF16,
                    op: "cast",
                });
            }
        }
        DType::FP8E4M3 => {
            cast_from!(crate::dtype::FP8E4M3, src, dst, len, dst_dtype)
        }
        DType::FP8E5M2 => {
            cast_from!(crate::dtype::FP8E5M2, src, dst, len, dst_dtype)
        }
        DType::I64 => cast_from!(i64, src, dst, len, dst_dtype),
        DType::I32 => cast_from!(i32, src, dst, len, dst_dtype),
        DType::I16 => cast_from!(i16, src, dst, len, dst_dtype),
        DType::I8 => cast_from!(i8, src, dst, len, dst_dtype),
        DType::U64 => cast_from!(u64, src, dst, len, dst_dtype),
        DType::U32 => cast_from!(u32, src, dst, len, dst_dtype),
        DType::U16 => cast_from!(u16, src, dst, len, dst_dtype),
        DType::U8 => cast_from!(u8, src, dst, len, dst_dtype),
        DType::Bool => {
            // Bool is stored as u8 (0 or 1)
            cast_from!(u8, src, dst, len, dst_dtype)
        }
        DType::Complex64 => {
            // Complex64 source: extract real part for non-complex destinations
            let src_slice = std::slice::from_raw_parts(src as *const crate::dtype::Complex64, len);
            match dst_dtype {
                DType::Complex64 => {
                    let dst_slice =
                        std::slice::from_raw_parts_mut(dst as *mut crate::dtype::Complex64, len);
                    dst_slice.copy_from_slice(src_slice);
                }
                DType::Complex128 => {
                    let dst_slice =
                        std::slice::from_raw_parts_mut(dst as *mut crate::dtype::Complex128, len);
                    for i in 0..len {
                        dst_slice[i] = crate::dtype::Complex128::new(
                            src_slice[i].re as f64,
                            src_slice[i].im as f64,
                        );
                    }
                }
                _ => {
                    // For non-complex destinations, take the real part
                    let dst_slice = std::slice::from_raw_parts_mut(dst as *mut f32, len);
                    for i in 0..len {
                        dst_slice[i] = src_slice[i].re;
                    }
                    // Re-interpret as the target type via Element trait
                    return Err(Error::UnsupportedDType {
                        dtype: dst_dtype,
                        op: "cast from Complex64",
                    });
                }
            }
        }
        DType::Complex128 => {
            // Complex128 source: extract real part for non-complex destinations
            let src_slice = std::slice::from_raw_parts(src as *const crate::dtype::Complex128, len);
            match dst_dtype {
                DType::Complex128 => {
                    let dst_slice =
                        std::slice::from_raw_parts_mut(dst as *mut crate::dtype::Complex128, len);
                    dst_slice.copy_from_slice(src_slice);
                }
                DType::Complex64 => {
                    let dst_slice =
                        std::slice::from_raw_parts_mut(dst as *mut crate::dtype::Complex64, len);
                    for i in 0..len {
                        dst_slice[i] = crate::dtype::Complex64::new(
                            src_slice[i].re as f32,
                            src_slice[i].im as f32,
                        );
                    }
                }
                _ => {
                    return Err(Error::UnsupportedDType {
                        dtype: dst_dtype,
                        op: "cast from Complex128",
                    });
                }
            }
        }
    }

    Ok(())
}

/// Fill output with uniform random values in [0, 1)
///
/// # Safety
/// - `out` must be a valid pointer to `len` elements
#[inline]
pub unsafe fn rand_uniform_kernel<T: Element>(out: *mut T, len: usize) {
    let mut prng = rng::thread_rng();
    let out_slice = std::slice::from_raw_parts_mut(out, len);

    // Check once if this type can round values near 1.0 up to 1.0
    let needs_clamp = T::from_f64(0.9999).to_f64() >= 1.0;

    for elem in out_slice.iter_mut() {
        let val = rng::sample_uniform(&mut prng);
        *elem = T::from_f64(val);
        // For reduced-precision types (BF16, FP8), rounding can push values
        // near 1.0 up to exactly 1.0. Clamp to the largest representable
        // value below 1.0 in this type.
        if needs_clamp && elem.to_f64() >= 1.0 {
            *elem = T::from_f64(0.0);
        }
    }
}

/// Fill output with standard normal random values (mean=0, std=1)
///
/// Uses the Box-Muller transform for generating normally distributed values.
///
/// # Safety
/// - `out` must be a valid pointer to `len` elements
#[inline]
pub unsafe fn rand_normal_kernel<T: Element>(out: *mut T, len: usize) {
    let mut prng = rng::thread_rng();
    let out_slice = std::slice::from_raw_parts_mut(out, len);

    for elem in out_slice.iter_mut() {
        let val = rng::sample_normal(&mut prng);
        *elem = T::from_f64(val);
    }
}

/// Fill output with random integers in [low, high)
///
/// Generates uniformly distributed random integers.
///
/// # Safety
/// - `out` must be a valid pointer to `len` elements
/// - `low < high` must be satisfied
#[inline]
pub unsafe fn randint_kernel<T: Element>(out: *mut T, low: i64, high: i64, len: usize) {
    let mut prng = rng::thread_rng();
    let out_slice = std::slice::from_raw_parts_mut(out, len);

    for elem in out_slice.iter_mut() {
        let val = rng::sample_uniform_int(&mut prng, low, high);
        *elem = T::from_f64(val as f64);
    }
}

/// Fill output with evenly spaced values in [start, stop)
///
/// Generates values: start + step * i for i in 0..len
///
/// # Safety
/// - `out` must be a valid pointer to `len` elements
#[inline]
pub unsafe fn arange_kernel<T: Element>(out: *mut T, start: f64, step: f64, len: usize) {
    let out_slice = std::slice::from_raw_parts_mut(out, len);

    for (i, elem) in out_slice.iter_mut().enumerate() {
        let val = start + step * (i as f64);
        *elem = T::from_f64(val);
    }
}

/// Fill output with evenly spaced values from start to stop (inclusive)
///
/// Generates values: start + (stop - start) * i / (steps - 1) for i in 0..steps
///
/// # Safety
/// - `out` must be a valid pointer to `steps` elements
/// - `steps` must be >= 2
#[inline]
pub unsafe fn linspace_kernel<T: Element>(out: *mut T, start: f64, stop: f64, steps: usize) {
    let out_slice = std::slice::from_raw_parts_mut(out, steps);

    if steps == 1 {
        out_slice[0] = T::from_f64(start);
        return;
    }

    let divisor = (steps - 1) as f64;
    let delta = stop - start;

    for (i, elem) in out_slice.iter_mut().enumerate() {
        let val = start + delta * (i as f64) / divisor;
        *elem = T::from_f64(val);
    }
}

/// Fill output with identity matrix (1s on diagonal, 0s elsewhere)
///
/// # Safety
/// - `out` must be a valid pointer to `n * m` elements
#[inline]
pub unsafe fn eye_kernel<T: Element>(out: *mut T, n: usize, m: usize) {
    let out_slice = std::slice::from_raw_parts_mut(out, n * m);

    // Fill with zeros first
    out_slice.fill(T::from_f64(0.0));

    // Set diagonal to 1
    let diag_len = n.min(m);
    for i in 0..diag_len {
        out_slice[i * m + i] = T::from_f64(1.0);
    }
}

/// Sample from a multinomial distribution with replacement
///
/// Uses inverse transform sampling (CDF method):
/// 1. Compute cumulative sum of normalized probabilities
/// 2. For each sample, draw uniform random u ∈ [0, 1)
/// 3. Binary search to find smallest index i where CDF[i] ≥ u
///
/// # Safety
/// - `probs` must be valid pointer to `num_distributions * num_categories` floats
/// - `out` must be valid pointer to `num_distributions * num_samples` i64 values
/// - All probabilities must be non-negative
/// - Each distribution must have at least one non-zero probability
#[inline]
pub unsafe fn multinomial_kernel_with_replacement<T: Element>(
    probs: *const T,
    out: *mut i64,
    num_distributions: usize,
    num_categories: usize,
    num_samples: usize,
) {
    let mut prng = rng::thread_rng();

    for dist in 0..num_distributions {
        let prob_row = std::slice::from_raw_parts(probs.add(dist * num_categories), num_categories);

        // Compute sum for normalization
        let mut sum = 0.0f64;
        for &p in prob_row {
            sum += p.to_f64();
        }

        // Compute CDF (normalized cumulative sum)
        let mut cdf = Vec::with_capacity(num_categories);
        let mut cumsum = 0.0f64;
        for &p in prob_row {
            cumsum += p.to_f64() / sum;
            cdf.push(cumsum);
        }
        // Ensure last element is exactly 1.0 to avoid floating point issues
        if !cdf.is_empty() {
            *cdf.last_mut().unwrap() = 1.0;
        }

        // Sample using binary search
        let out_row = std::slice::from_raw_parts_mut(out.add(dist * num_samples), num_samples);

        for sample in out_row {
            let u = rng::sample_uniform(&mut prng);
            // Binary search: find first index where cdf[i] >= u
            let idx = cdf.partition_point(|&c| c < u);
            *sample = idx.min(num_categories - 1) as i64;
        }
    }
}

/// Sample from a multinomial distribution without replacement
///
/// Uses the reservoir sampling approach:
/// For each sample, draw from remaining categories weighted by their probabilities.
///
/// # Safety
/// - `probs` must be valid pointer to `num_distributions * num_categories` floats
/// - `out` must be valid pointer to `num_distributions * num_samples` i64 values
/// - `num_samples <= num_categories`
/// - All probabilities must be non-negative
/// - Each distribution must have at least `num_samples` non-zero probabilities
#[inline]
pub unsafe fn multinomial_kernel_without_replacement<T: Element>(
    probs: *const T,
    out: *mut i64,
    num_distributions: usize,
    num_categories: usize,
    num_samples: usize,
) {
    let mut prng = rng::thread_rng();

    for dist in 0..num_distributions {
        let prob_row = std::slice::from_raw_parts(probs.add(dist * num_categories), num_categories);

        // Copy probabilities so we can modify them
        let mut remaining_probs: Vec<f64> = prob_row.iter().map(|p| p.to_f64()).collect();
        let out_row = std::slice::from_raw_parts_mut(out.add(dist * num_samples), num_samples);

        for sample in out_row {
            // Compute sum of remaining probabilities
            let sum: f64 = remaining_probs.iter().sum();

            // Compute CDF for remaining probabilities
            let mut cdf = Vec::with_capacity(num_categories);
            let mut cumsum = 0.0f64;
            for &p in &remaining_probs {
                cumsum += p / sum;
                cdf.push(cumsum);
            }
            if !cdf.is_empty() {
                *cdf.last_mut().unwrap() = 1.0;
            }

            // Sample
            let u = rng::sample_uniform(&mut prng);
            let idx = cdf.partition_point(|&c| c < u).min(num_categories - 1);
            *sample = idx as i64;

            // Zero out the selected category so it can't be selected again
            remaining_probs[idx] = 0.0;
        }
    }
}

/// Fisher-Yates shuffle to generate random permutation of [0, n)
///
/// # Safety
/// - `out` must be a valid pointer to `n` elements of i64
pub unsafe fn randperm_kernel(out: *mut i64, n: usize) {
    let mut prng = rng::thread_rng();
    let out_slice = std::slice::from_raw_parts_mut(out, n);

    // Initialize with [0, 1, 2, ..., n-1]
    for i in 0..n {
        out_slice[i] = i as i64;
    }

    // Fisher-Yates shuffle
    for i in (1..n).rev() {
        let j = (prng.next() % (i as u64 + 1)) as usize;
        out_slice.swap(i, j);
    }
}

/// One-hot encode integer indices
///
/// # Safety
/// - `indices` must be a valid pointer to `numel` elements of type T (integer)
/// - `out` must be a valid pointer to `numel * num_classes` f32 elements, zero-initialized
pub unsafe fn one_hot_kernel<T: Element>(
    indices: *const T,
    out: *mut f32,
    numel: usize,
    num_classes: usize,
) {
    let indices_slice = std::slice::from_raw_parts(indices, numel);
    let out_slice = std::slice::from_raw_parts_mut(out, numel * num_classes);

    for i in 0..numel {
        let idx = indices_slice[i].to_f64() as usize;
        if idx < num_classes {
            out_slice[i * num_classes + idx] = 1.0;
        }
    }
}