Merge commit 'd803bfe2b1fe7f5e219e50ac20d6801a0a58ac75' as 'vendor/ruvector'

This commit is contained in:
ruv
2026-02-28 14:39:40 -05:00
7854 changed files with 3522914 additions and 0 deletions

View File

@@ -0,0 +1,61 @@
//! Benchmarks for SIMD kernel performance
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion};
use ruvector_sparse_inference::backend::{cpu::CpuBackend, Backend};
use ruvector_sparse_inference::sparse::ActivationType;
fn bench_dot_product(c: &mut Criterion) {
let backend = CpuBackend;
let mut group = c.benchmark_group("dot_product");
for size in [128, 256, 512, 1024, 2048, 4096].iter() {
let a: Vec<f32> = (0..*size).map(|i| i as f32).collect();
let b: Vec<f32> = (0..*size).map(|i| (i * 2) as f32).collect();
group.bench_with_input(BenchmarkId::from_parameter(size), size, |bench, _| {
bench.iter(|| black_box(backend.dot_product(black_box(&a), black_box(&b))));
});
}
group.finish();
}
fn bench_relu(c: &mut Criterion) {
let backend = CpuBackend;
let mut group = c.benchmark_group("relu");
for size in [128, 256, 512, 1024, 2048, 4096].iter() {
let data: Vec<f32> = (0..*size).map(|i| i as f32 - (*size / 2) as f32).collect();
group.bench_with_input(BenchmarkId::from_parameter(size), size, |bench, _| {
bench.iter(|| {
let mut d = data.clone();
backend.activation(black_box(&mut d), ActivationType::Relu);
black_box(d);
});
});
}
group.finish();
}
fn bench_axpy(c: &mut Criterion) {
let backend = CpuBackend;
let mut group = c.benchmark_group("axpy");
for size in [128, 256, 512, 1024, 2048, 4096].iter() {
let a: Vec<f32> = (0..*size).map(|i| i as f32).collect();
let b: Vec<f32> = (0..*size).map(|i| (i * 2) as f32).collect();
let scalar = 2.5f32;
group.bench_with_input(BenchmarkId::from_parameter(size), size, |bench, _| {
bench.iter(|| {
let mut a_copy = a.clone();
backend.axpy(black_box(&mut a_copy), black_box(&b), black_box(scalar));
black_box(a_copy);
});
});
}
group.finish();
}
criterion_group!(benches, bench_dot_product, bench_relu, bench_axpy);
criterion_main!(benches);

View File

@@ -0,0 +1,134 @@
//! Benchmark tests for sparse inference
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion};
use rand::Rng;
use ruvector_sparse_inference::{
ActivationType, LowRankPredictor, Predictor, SparseFfn, SparseInferenceEngine, SparsityConfig,
};
// Test utilities
fn random_vector(dim: usize) -> Vec<f32> {
let mut rng = rand::thread_rng();
(0..dim).map(|_| rng.gen_range(-1.0..1.0)).collect()
}
fn benchmark_sparse_vs_dense(c: &mut Criterion) {
let dense_engine = SparseInferenceEngine::new_dense(512, 2048).unwrap();
let sparse_engine = SparseInferenceEngine::new_sparse(512, 2048, 0.3).unwrap();
let input = random_vector(512);
let mut group = c.benchmark_group("inference");
group.bench_function("dense", |b| {
b.iter(|| black_box(dense_engine.infer(&input).unwrap()))
});
group.bench_function("sparse_70pct", |b| {
b.iter(|| black_box(sparse_engine.infer(&input).unwrap()))
});
group.finish();
}
fn benchmark_predictor(c: &mut Criterion) {
let config = SparsityConfig::with_top_k(500);
let predictor = LowRankPredictor::new(512, 4096, 128, config).unwrap();
let input = random_vector(512);
c.bench_function("predictor_predict", |b| {
b.iter(|| black_box(predictor.predict(&input).unwrap()))
});
}
fn benchmark_predictor_top_k(c: &mut Criterion) {
let mut group = c.benchmark_group("predictor_top_k");
let input = random_vector(512);
for k in [100, 500, 1000, 2000] {
let config = SparsityConfig::with_top_k(k);
let predictor = LowRankPredictor::new(512, 4096, 128, config).unwrap();
group.bench_with_input(BenchmarkId::from_parameter(k), &input, |b, input| {
b.iter(|| black_box(predictor.predict(input).unwrap()))
});
}
group.finish();
}
fn benchmark_sparse_ffn(c: &mut Criterion) {
let ffn = SparseFfn::new(512, 2048, 512, ActivationType::Silu).unwrap();
let input = random_vector(512);
let mut group = c.benchmark_group("sparse_ffn");
group.bench_function("dense_forward", |b| {
b.iter(|| black_box(ffn.forward_dense(&input).unwrap()))
});
let active_10pct: Vec<usize> = (0..204).collect();
group.bench_function("sparse_10pct", |b| {
b.iter(|| black_box(ffn.forward_sparse(&input, &active_10pct).unwrap()))
});
let active_50pct: Vec<usize> = (0..1024).collect();
group.bench_function("sparse_50pct", |b| {
b.iter(|| black_box(ffn.forward_sparse(&input, &active_50pct).unwrap()))
});
group.finish();
}
fn benchmark_activation_functions(c: &mut Criterion) {
let input = random_vector(512);
let active: Vec<usize> = (0..500).collect();
let mut group = c.benchmark_group("activation_functions");
for activation in [
ActivationType::Relu,
ActivationType::Gelu,
ActivationType::Silu,
] {
let ffn = SparseFfn::new(512, 2048, 512, activation).unwrap();
let name = format!("{:?}", activation);
group.bench_with_input(BenchmarkId::from_parameter(&name), &input, |b, input| {
b.iter(|| black_box(ffn.forward_sparse(input, &active).unwrap()))
});
}
group.finish();
}
fn benchmark_sparsity_levels(c: &mut Criterion) {
let input = random_vector(512);
let mut group = c.benchmark_group("sparsity_levels");
for active_pct in [10, 30, 50, 70] {
let num_active = (2048 * active_pct) / 100;
let active: Vec<usize> = (0..num_active).collect();
let ffn = SparseFfn::new(512, 2048, 512, ActivationType::Silu).unwrap();
group.bench_with_input(
BenchmarkId::from_parameter(format!("{}%_active", active_pct)),
&(&input, &active),
|b, (input, active)| b.iter(|| black_box(ffn.forward_sparse(input, active).unwrap())),
);
}
group.finish();
}
criterion_group!(
benches,
benchmark_sparse_vs_dense,
benchmark_predictor,
benchmark_predictor_top_k,
benchmark_sparse_ffn,
benchmark_activation_functions,
benchmark_sparsity_levels,
);
criterion_main!(benches);