numr 0.15.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
//! Real FFT helpers (rfft, irfft)

use super::super::parallel_gate::fft_work;
use super::super::{CpuClient, CpuRuntime, kernels};
use crate::algorithm::fft::{
    FftDirection, FftNormalization, complex_dtype_for_real, real_dtype_for_complex,
    validate_fft_complex_dtype, validate_rfft_real_dtype,
};
use crate::dtype::{Complex64, Complex128, DType};
use crate::error::{Error, Result};
use crate::tensor::Tensor;
#[cfg(feature = "rayon")]
use rayon::prelude::*;

pub(super) fn rfft_impl(
    client: &CpuClient,
    input: &Tensor<CpuRuntime>,
    norm: FftNormalization,
) -> Result<Tensor<CpuRuntime>> {
    let dtype = input.dtype();
    validate_rfft_real_dtype(dtype, "rfft")?;

    let ndim = input.ndim();
    if ndim == 0 {
        return Err(Error::InvalidArgument {
            arg: "input",
            reason: "rfft requires at least 1D input".to_string(),
        });
    }

    let n = input.shape()[ndim - 1];
    // The CPU backend handles any size: power-of-two via Stockham, everything
    // else via Bluestein. Only an empty transform axis is invalid.
    if n == 0 {
        return Err(Error::InvalidArgument {
            arg: "n",
            reason: "rfft requires size >= 1 along the last dim, got 0".to_string(),
        });
    }

    let input_contig = if input.is_contiguous() {
        input.clone()
    } else {
        input.contiguous()?
    };

    let output_dtype = complex_dtype_for_real(dtype)?;
    let normalize_factor = norm.factor(FftDirection::Forward, n);

    let mut out_shape = input_contig.shape().to_vec();
    out_shape[ndim - 1] = n / 2 + 1;

    let output = Tensor::<CpuRuntime>::empty(&out_shape, output_dtype, &client.device)?;

    // Unclamped: rank-1 already products to 1. Clamping a zero batch dim to 1 would
    // build a `from_raw_parts` slice longer than the zero-element allocation.
    let batch_size: usize = input_contig.shape()[..ndim - 1].iter().product();
    #[cfg(feature = "rayon")]
    let min_len = client.rayon_min_len();
    // Rows are independent, so a small batch runs on this thread with the same
    // per-row arithmetic and no fork-join.
    let parallel = batch_size > 1 && client.parallel_worthwhile(fft_work(batch_size, n));

    let input_ptr = input_contig.ptr();
    let output_ptr = output.ptr();

    match dtype {
        DType::F32 => {
            let input_slice: &[f32] =
                unsafe { std::slice::from_raw_parts(input_ptr as *const f32, batch_size * n) };
            let output_slice: &mut [Complex64] = unsafe {
                std::slice::from_raw_parts_mut(
                    output_ptr as *mut Complex64,
                    batch_size * (n / 2 + 1),
                )
            };
            let norm_f32 = normalize_factor as f32;
            let plan = kernels::RfftPlanC64::new(n);

            client.install_parallelism_if(parallel, || {
                #[cfg(feature = "rayon")]
                if parallel {
                    output_slice
                        .par_chunks_mut(n / 2 + 1)
                        .enumerate()
                        .with_min_len(min_len)
                        .for_each(|(batch_idx, out_chunk)| {
                            let in_start = batch_idx * n;
                            unsafe {
                                plan.execute(
                                    &input_slice[in_start..in_start + n],
                                    out_chunk,
                                    norm_f32,
                                );
                            }
                        });
                    return;
                }

                for batch_idx in 0..batch_size {
                    let in_start = batch_idx * n;
                    let out_start = batch_idx * (n / 2 + 1);
                    unsafe {
                        plan.execute(
                            &input_slice[in_start..in_start + n],
                            &mut output_slice[out_start..out_start + n / 2 + 1],
                            norm_f32,
                        );
                    }
                }
            });
        }
        DType::F64 => {
            let input_slice: &[f64] =
                unsafe { std::slice::from_raw_parts(input_ptr as *const f64, batch_size * n) };
            let output_slice: &mut [Complex128] = unsafe {
                std::slice::from_raw_parts_mut(
                    output_ptr as *mut Complex128,
                    batch_size * (n / 2 + 1),
                )
            };
            let plan = kernels::RfftPlanC128::new(n);

            client.install_parallelism_if(parallel, || {
                #[cfg(feature = "rayon")]
                if parallel {
                    output_slice
                        .par_chunks_mut(n / 2 + 1)
                        .enumerate()
                        .with_min_len(min_len)
                        .for_each(|(batch_idx, out_chunk)| {
                            let in_start = batch_idx * n;
                            unsafe {
                                plan.execute(
                                    &input_slice[in_start..in_start + n],
                                    out_chunk,
                                    normalize_factor,
                                );
                            }
                        });
                    return;
                }

                for batch_idx in 0..batch_size {
                    let in_start = batch_idx * n;
                    let out_start = batch_idx * (n / 2 + 1);
                    unsafe {
                        plan.execute(
                            &input_slice[in_start..in_start + n],
                            &mut output_slice[out_start..out_start + n / 2 + 1],
                            normalize_factor,
                        );
                    }
                }
            });
        }
        _ => unreachable!(),
    }

    Ok(output)
}

pub(super) fn irfft_impl(
    client: &CpuClient,
    input: &Tensor<CpuRuntime>,
    n: Option<usize>,
    norm: FftNormalization,
) -> Result<Tensor<CpuRuntime>> {
    let dtype = input.dtype();
    validate_fft_complex_dtype(dtype, "irfft")?;

    let ndim = input.ndim();
    if ndim == 0 {
        return Err(Error::InvalidArgument {
            arg: "input",
            reason: "irfft requires at least 1D input".to_string(),
        });
    }

    let input_n = input.shape()[ndim - 1];
    if input_n == 0 {
        return Err(Error::InvalidArgument {
            arg: "input",
            reason: "irfft requires size >= 1 along the last dim, got 0".to_string(),
        });
    }
    let output_n = n.unwrap_or(2 * (input_n - 1));

    if output_n / 2 + 1 != input_n {
        return Err(Error::InvalidArgument {
            arg: "n",
            reason: format!(
                "For irfft with n={}, input must have size {}, got {}",
                output_n,
                output_n / 2 + 1,
                input_n
            ),
        });
    }

    // The CPU backend handles any size: power-of-two via Stockham, everything
    // else via Bluestein. Only an empty transform axis is invalid.
    if output_n == 0 {
        return Err(Error::InvalidArgument {
            arg: "n",
            reason: "irfft requires output size >= 1, got 0".to_string(),
        });
    }

    let input_contig = if input.is_contiguous() {
        input.clone()
    } else {
        input.contiguous()?
    };

    let output_dtype = real_dtype_for_complex(dtype)?;
    let normalize_factor = norm.factor(FftDirection::Inverse, output_n);

    let mut out_shape = input_contig.shape().to_vec();
    out_shape[ndim - 1] = output_n;

    let output = Tensor::<CpuRuntime>::empty(&out_shape, output_dtype, &client.device)?;

    // Unclamped: rank-1 already products to 1. Clamping a zero batch dim to 1 would
    // build a `from_raw_parts` slice longer than the zero-element allocation.
    let batch_size: usize = input_contig.shape()[..ndim - 1].iter().product();
    #[cfg(feature = "rayon")]
    let min_len = client.rayon_min_len();
    // Rows are independent, so a small batch runs on this thread with the same
    // per-row arithmetic and no fork-join.
    let parallel = batch_size > 1 && client.parallel_worthwhile(fft_work(batch_size, output_n));

    let input_ptr = input_contig.ptr();
    let output_ptr = output.ptr();

    match dtype {
        DType::Complex64 => {
            let input_slice: &[Complex64] = unsafe {
                std::slice::from_raw_parts(input_ptr as *const Complex64, batch_size * input_n)
            };
            let output_slice: &mut [f32] = unsafe {
                std::slice::from_raw_parts_mut(output_ptr as *mut f32, batch_size * output_n)
            };
            let norm_f32 = normalize_factor as f32;
            let plan = kernels::IrfftPlanC64::new(output_n);

            client.install_parallelism_if(parallel, || {
                #[cfg(feature = "rayon")]
                if parallel {
                    output_slice
                        .par_chunks_mut(output_n)
                        .enumerate()
                        .with_min_len(min_len)
                        .for_each(|(batch_idx, out_chunk)| {
                            let in_start = batch_idx * input_n;
                            unsafe {
                                plan.execute(
                                    &input_slice[in_start..in_start + input_n],
                                    out_chunk,
                                    norm_f32,
                                );
                            }
                        });
                    return;
                }

                for batch_idx in 0..batch_size {
                    let in_start = batch_idx * input_n;
                    let out_start = batch_idx * output_n;
                    unsafe {
                        plan.execute(
                            &input_slice[in_start..in_start + input_n],
                            &mut output_slice[out_start..out_start + output_n],
                            norm_f32,
                        );
                    }
                }
            });
        }
        DType::Complex128 => {
            let input_slice: &[Complex128] = unsafe {
                std::slice::from_raw_parts(input_ptr as *const Complex128, batch_size * input_n)
            };
            let output_slice: &mut [f64] = unsafe {
                std::slice::from_raw_parts_mut(output_ptr as *mut f64, batch_size * output_n)
            };
            let plan = kernels::IrfftPlanC128::new(output_n);

            client.install_parallelism_if(parallel, || {
                #[cfg(feature = "rayon")]
                if parallel {
                    output_slice
                        .par_chunks_mut(output_n)
                        .enumerate()
                        .with_min_len(min_len)
                        .for_each(|(batch_idx, out_chunk)| {
                            let in_start = batch_idx * input_n;
                            unsafe {
                                plan.execute(
                                    &input_slice[in_start..in_start + input_n],
                                    out_chunk,
                                    normalize_factor,
                                );
                            }
                        });
                    return;
                }

                for batch_idx in 0..batch_size {
                    let in_start = batch_idx * input_n;
                    let out_start = batch_idx * output_n;
                    unsafe {
                        plan.execute(
                            &input_slice[in_start..in_start + input_n],
                            &mut output_slice[out_start..out_start + output_n],
                            normalize_factor,
                        );
                    }
                }
            });
        }
        _ => unreachable!(),
    }

    Ok(output)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::algorithm::fft::FftAlgorithms;
    use crate::runtime::cpu::CpuDevice;

    /// Whisper's mel front end needs exactly `n_fft = 400`, which is not a
    /// power of two. The kernel-level tests cover Bluestein directly; this one
    /// pins the PUBLIC `rfft`/`irfft` path that consumers actually call, which
    /// used to reject the size outright.
    const N: usize = 400;

    fn signal() -> Vec<f64> {
        (0..N)
            .map(|i| {
                let t = i as f64 / N as f64;
                (2.0 * std::f64::consts::PI * 3.0 * t).sin()
                    + 0.5 * (2.0 * std::f64::consts::PI * 47.0 * t).cos()
            })
            .collect()
    }

    /// Naive O(n^2) DFT, first `N/2 + 1` bins.
    fn naive_rdft(x: &[f64]) -> Vec<Complex128> {
        let n = x.len();
        (0..n / 2 + 1)
            .map(|k| {
                let mut acc = Complex128::new(0.0, 0.0);
                for (j, v) in x.iter().enumerate() {
                    let theta = -2.0 * std::f64::consts::PI * ((j * k) % n) as f64 / n as f64;
                    acc += Complex128::new(v * theta.cos(), v * theta.sin());
                }
                acc
            })
            .collect()
    }

    #[test]
    fn rfft_at_400_matches_naive_dft() {
        let device = CpuDevice::new();
        let client = CpuClient::new(device.clone());
        let x = signal();
        let input = Tensor::<CpuRuntime>::from_slice(&x, &[N], &device).expect("input");

        let spectrum = client
            .rfft(&input, FftNormalization::None)
            .expect("rfft must accept a non-power-of-two size");
        assert_eq!(spectrum.shape(), &[N / 2 + 1]);

        let got: Vec<Complex128> = spectrum.contiguous().expect("contiguous").to_vec();
        let expected = naive_rdft(&x);
        for (k, (a, b)) in got.iter().zip(expected.iter()).enumerate() {
            assert!(
                (a.re - b.re).abs() < 1e-9 && (a.im - b.im).abs() < 1e-9,
                "bin {k}: got ({}, {}), want ({}, {})",
                a.re,
                a.im,
                b.re,
                b.im
            );
        }
    }

    #[test]
    fn rfft_irfft_at_400_round_trips() {
        let device = CpuDevice::new();
        let client = CpuClient::new(device.clone());
        let x = signal();
        let input = Tensor::<CpuRuntime>::from_slice(&x, &[N], &device).expect("input");

        // `Backward` is the numpy-matching default: the inverse divides by N,
        // so the round trip returns the original samples. `None` would return
        // `N * x` by design.
        let spectrum = client
            .rfft(&input, FftNormalization::Backward)
            .expect("rfft");
        let restored = client
            .irfft(&spectrum, Some(N), FftNormalization::Backward)
            .expect("irfft must accept a non-power-of-two size");
        assert_eq!(restored.shape(), &[N]);

        let got: Vec<f64> = restored.contiguous().expect("contiguous").to_vec();
        for (i, (a, b)) in got.iter().zip(x.iter()).enumerate() {
            assert!((a - b).abs() < 1e-9, "sample {i}: got {a}, want {b}");
        }
    }

    /// A batched call must build one plan per batch and still produce the same
    /// spectrum each row would get on its own.
    #[test]
    fn batched_rfft_at_400_matches_per_row() {
        let device = CpuDevice::new();
        let client = CpuClient::new(device.clone());
        let x = signal();
        let mut batched = Vec::with_capacity(3 * N);
        for row in 0..3 {
            batched.extend(x.iter().map(|v| v * (row as f64 + 1.0)));
        }
        let input = Tensor::<CpuRuntime>::from_slice(&batched, &[3, N], &device).expect("input");
        let spectrum = client.rfft(&input, FftNormalization::None).expect("rfft");
        assert_eq!(spectrum.shape(), &[3, N / 2 + 1]);

        let got: Vec<Complex128> = spectrum.contiguous().expect("contiguous").to_vec();
        let base = naive_rdft(&x);
        let bins = N / 2 + 1;
        for row in 0..3 {
            let scale = row as f64 + 1.0;
            for k in 0..bins {
                let a = got[row * bins + k];
                let want = base[k];
                assert!(
                    (a.re - want.re * scale).abs() < 1e-9 && (a.im - want.im * scale).abs() < 1e-9,
                    "row {row} bin {k}: got ({}, {}), want ({}, {})",
                    a.re,
                    a.im,
                    want.re * scale,
                    want.im * scale
                );
            }
        }
    }
}