From ec8f6b2fa91dc7fcde9bd83626bfce4415a93208 Mon Sep 17 00:00:00 2001 From: mtantaoui Date: Fri, 20 Nov 2026 15:38:24 +0100 Subject: [PATCH] perf: vectorise the dense Gaussian kernel across points --- algorithms/linfa-kernel/Cargo.toml | 10 ++ algorithms/linfa-kernel/benches/gaussian.rs | 54 ++++++++++ algorithms/linfa-kernel/src/lib.rs | 104 ++++++++++++++++++-- 3 files changed, 159 insertions(+), 9 deletions(-) create mode 100644 algorithms/linfa-kernel/benches/gaussian.rs diff --git a/algorithms/linfa-kernel/Cargo.toml b/algorithms/linfa-kernel/Cargo.toml index 7d038b387..1c9bed983 100644 --- a/algorithms/linfa-kernel/Cargo.toml +++ b/algorithms/linfa-kernel/Cargo.toml @@ -30,3 +30,13 @@ sprs = { version = "=0.11.2", default-features = false } linfa = { version = "0.8.1", path = "../.." } linfa-nn = { version = "0.8.1", path = "../linfa-nn" } + +[dev-dependencies] +criterion = "0.5" +linfa = { version = "0.8.1", path = "../..", features = ["benchmarks"] } +rand_xoshiro = "0.6" +ndarray-rand = "0.15" + +[[bench]] +name = "gaussian" +harness = false diff --git a/algorithms/linfa-kernel/benches/gaussian.rs b/algorithms/linfa-kernel/benches/gaussian.rs new file mode 100644 index 000000000..838463871 --- /dev/null +++ b/algorithms/linfa-kernel/benches/gaussian.rs @@ -0,0 +1,54 @@ +//! Dense Gaussian kernel construction: +//! +//! cargo bench -p linfa-kernel --bench gaussian + +use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput}; +use linfa::benchmarks::config; +use linfa::traits::Transformer; +use linfa::Float; +use linfa_kernel::{Kernel, KernelMethod, KernelType}; +use ndarray::Array2; +use ndarray_rand::{rand::SeedableRng, rand_distr::Uniform, RandomExt}; +use rand_xoshiro::Xoshiro256Plus; + +fn bench_type(c: &mut Criterion, ty: &str) { + let mut rng = Xoshiro256Plus::seed_from_u64(42); + + for n in [500, 2_000] { + let mut group = c.benchmark_group(format!("Gaussian kernel {ty} n={n}")); + config::set_default_benchmark_configs(&mut group); + group.throughput(Throughput::Elements((n * n) as u64)); + + for d in [2, 16, 64, 120] { + let x: Array2 = + Array2::random_using((n, d), Uniform::new(0., 1.), &mut rng).mapv(F::cast); + // The mean squared distance between uniform points in [0, 1)^d is d/6: with + // eps = d/3 the exponents average -1/2, a typical bandwidth. + let params = Kernel::params() + .kind(KernelType::Dense) + .method(KernelMethod::Gaussian(F::cast(d as f64 / 3.0))); + + group.bench_with_input(BenchmarkId::from_parameter(d), &x, |b, x| { + b.iter(|| params.transform(x.view())) + }); + } + group.finish(); + } +} + +fn bench(c: &mut Criterion) { + bench_type::(c, "f32"); + bench_type::(c, "f64"); +} + +#[cfg(not(target_os = "windows"))] +criterion_group! { + name = benches; + config = config::get_default_profiling_configs(); + targets = bench +} + +#[cfg(target_os = "windows")] +criterion_group!(benches, bench); + +criterion_main!(benches); diff --git a/algorithms/linfa-kernel/src/lib.rs b/algorithms/linfa-kernel/src/lib.rs index a2bb37997..0f31f4e0c 100644 --- a/algorithms/linfa-kernel/src/lib.rs +++ b/algorithms/linfa-kernel/src/lib.rs @@ -272,15 +272,7 @@ pub enum KernelMethod { impl KernelMethod { pub fn distance(&self, a: ArrayView1, b: ArrayView1) -> F { match *self { - KernelMethod::Gaussian(eps) => { - let distance = a - .iter() - .zip(b.iter()) - .map(|(x, y)| (*x - *y) * (*x - *y)) - .sum::(); - - (-distance / eps).exp() - } + KernelMethod::Gaussian(eps) => gaussian_exponent(a, b, eps).exp(), KernelMethod::Linear => a.mul(&b).sum(), KernelMethod::Polynomial(c, d) => (a.mul(&b).sum() + c).powf(d), } @@ -477,10 +469,36 @@ impl< } } +/// The exponent of the Gaussian kernel, `-‖a - b‖² / eps`. +#[inline] +fn gaussian_exponent(a: ArrayView1, b: ArrayView1, eps: F) -> F { + let distance = match (a.as_slice(), b.as_slice()) { + // Contiguous rows, the usual case: a plain slice loop compiles to a tight body whatever + // the surrounding code, where ndarray's strided iterators do not reliably. Same order + // of summation, so the same result. + (Some(a), Some(b)) => a + .iter() + .zip(b) + .map(|(x, y)| (*x - *y) * (*x - *y)) + .sum::(), + _ => a + .iter() + .zip(b.iter()) + .map(|(x, y)| (*x - *y) * (*x - *y)) + .sum::(), + }; + + -distance / eps +} + fn dense_from_fn>( dataset: &ArrayBase, method: &KernelMethod, ) -> Array2 { + if let KernelMethod::Gaussian(eps) = *method { + return dense_gaussian(dataset, eps); + } + let n_observations = dataset.len_of(Axis(0)); let mut similarity = Array2::eye(n_observations); @@ -496,6 +514,38 @@ fn dense_from_fn>( similarity } +/// Dense Gaussian kernel matrix, built row by row with the distances vectorised across points. +/// +/// Bit-identical to calling [`KernelMethod::distance`] for every pair. +fn dense_gaussian>(dataset: &ArrayBase, eps: F) -> Array2 { + let n_observations = dataset.len_of(Axis(0)); + // The dataset feature by feature: row `k` holds feature `k` of every point, contiguously. + let features = dataset.t().as_standard_layout().into_owned(); + let mut similarity = Array2::::zeros((n_observations, n_observations)); + + for (i, mut row) in similarity.rows_mut().into_iter().enumerate() { + // The rows of a new standard-layout matrix are contiguous. + let row = row.as_slice_mut().unwrap(); + + // Row `i` of the squared distances, one feature at a time across all points, which + // vectorises across points. Each distance is still summed feature by feature in order, + // as `gaussian_exponent` does, so the result is the same. + for (feature, &x_i) in features.rows().into_iter().zip(dataset.row(i)) { + let feature = feature.as_slice().unwrap(); + for (sum, &x_j) in row.iter_mut().zip(feature) { + let diff = x_j - x_i; + *sum += diff * diff; + } + } + + for v in row.iter_mut() { + *v = (-*v / eps).exp(); + } + } + + similarity +} + fn sparse_from_fn, N: NearestNeighbour>( dataset: &ArrayBase, k: usize, @@ -614,6 +664,42 @@ mod tests { } } + #[test] + fn dense_gaussian_matches_distance() { + fn check() { + // Deterministic points in [0, 1)^d. + let mut s = 0x9E37_79B9_7F4A_7C15u64; + for &d in &[1, 2, 7, 33] { + let x = Array2::from_shape_simple_fn((61, d), || { + s ^= s << 13; + s ^= s >> 7; + s ^= s << 17; + F::cast((s >> 11) as f64 / (1u64 << 53) as f64) + }); + let method = KernelMethod::Gaussian(F::cast(d as f64 / 3.0)); + let k = dense_from_fn(&x, &method); + for i in 0..x.nrows() { + for j in 0..x.nrows() { + let expected = method.distance(x.row(i), x.row(j)); + // Same exponent, same `exp`: exactly the same value. + assert!( + k[(i, j)] == expected, + "({}, {}), d = {}: {} != {}", + i, + j, + d, + k[(i, j)], + expected + ); + } + } + } + } + + check::(); + check::(); + } + #[test] fn gaussian_test() { let gauss_1 = KernelMethod::Gaussian(1.);