Skip to content

Commit 5bbc327

Browse files
authored
Improve Matern kernels runtime performance (#405)
* Update Python API infos * Log info level on mopta08 * Linting * Improve matern52 performances 57% performance improvement — benchmark went from 237 ms → 102 ms. Two optimizations applied to correlation_models.rs: rval_from_distances: Replaced two-pass computation (separate a and b arrays with inner mapv().product() allocations) with a single-pass scalar loop — no intermediate array allocations. _jac_helper → _jac_from_r: Replaced the O(n·d²·h²) nested "product-excluding-one-factor" loop + einsum with a closed-form O(n·d·h) formula. Since the Matern 5/2 polynomial is always positive, the excluded-product can be computed via division: total_product / single_factor. * Add bench matern32 * Improve matern32 performances (same as matern52) * Add bench for squred_exp and abs_exp * Minor improvments in squared_exp and abs_exp The changes for SE and AE show marginal improvement (criterion reports "change within noise threshold" and "no change in performance detected" for AE). This is expected — these kernels were already simpler than the Matern ones. The main optimization (powf(2.) → v * v and shared theta_w computation) eliminates unnecessary allocations and expensive transcendental calls, but since the exponential kernels lack the O(n·d²·h²) product-excluding-one-factor structure that made Matern so costly, the absolute gains are modest. Summary of optimizations applied: SquaredExponential: Replaced all powf(F::cast(2.)) with v * v (avoids log+exp internally); shared neg_theta_w_sq computation in jac and rval_with_jac instead of recomputing theta_w² + separate negation AbsoluteExponential: Shared neg_theta_w in rval_with_jac, computing r and jr from the same intermediate; avoided redundant rval_from_distances call in rval_with_jac * Format
1 parent 28b0e07 commit 5bbc327

File tree

3 files changed

+218
-207
lines changed

3 files changed

+218
-207
lines changed

crates/ego/benches/ego.rs

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ fn criterion_ego(c: &mut Criterion) {
1616
let xlimits = array![[-32.768, 32.768], [-32.768, 32.768], [-32.768, 32.768]];
1717
let mut group = c.benchmark_group("ego");
1818
group.sample_size(20);
19-
group.bench_function("ego ackley", |b| {
19+
group.bench_function("ego ackley matern52", |b| {
2020
b.iter(|| {
2121
std::hint::black_box(
2222
EgorBuilder::optimize(ackley)
@@ -27,7 +27,49 @@ fn criterion_ego(c: &mut Criterion) {
2727
.correlation_spec(CorrelationSpec::MATERN52)
2828
})
2929
.infill_strategy(InfillStrategy::WB2S)
30-
.max_iters(10)
30+
.max_iters(5)
31+
.seed(42)
32+
})
33+
.min_within(&xlimits)
34+
.expect("Egor configured")
35+
.run()
36+
.expect("Minimization"),
37+
)
38+
});
39+
});
40+
group.bench_function("ego ackley matern32", |b| {
41+
b.iter(|| {
42+
std::hint::black_box(
43+
EgorBuilder::optimize(ackley)
44+
.configure(|config| {
45+
config
46+
.configure_gp(|conf| {
47+
conf.regression_spec(RegressionSpec::CONSTANT)
48+
.correlation_spec(CorrelationSpec::MATERN32)
49+
})
50+
.infill_strategy(InfillStrategy::WB2S)
51+
.max_iters(5)
52+
.seed(42)
53+
})
54+
.min_within(&xlimits)
55+
.expect("Egor configured")
56+
.run()
57+
.expect("Minimization"),
58+
)
59+
});
60+
});
61+
group.bench_function("ego ackley square exp", |b| {
62+
b.iter(|| {
63+
std::hint::black_box(
64+
EgorBuilder::optimize(ackley)
65+
.configure(|config| {
66+
config
67+
.configure_gp(|conf| {
68+
conf.regression_spec(RegressionSpec::CONSTANT)
69+
.correlation_spec(CorrelationSpec::SQUAREDEXPONENTIAL)
70+
})
71+
.infill_strategy(InfillStrategy::WB2S)
72+
.max_iters(5)
3173
.target(5e-1)
3274
.seed(42)
3375
})
@@ -38,6 +80,27 @@ fn criterion_ego(c: &mut Criterion) {
3880
)
3981
});
4082
});
83+
group.bench_function("ego ackley abs exp", |b| {
84+
b.iter(|| {
85+
std::hint::black_box(
86+
EgorBuilder::optimize(ackley)
87+
.configure(|config| {
88+
config
89+
.configure_gp(|conf| {
90+
conf.regression_spec(RegressionSpec::CONSTANT)
91+
.correlation_spec(CorrelationSpec::ABSOLUTEEXPONENTIAL)
92+
})
93+
.infill_strategy(InfillStrategy::WB2S)
94+
.max_iters(5)
95+
.seed(42)
96+
})
97+
.min_within(&xlimits)
98+
.expect("Egor configured")
99+
.run()
100+
.expect("Minimization"),
101+
)
102+
});
103+
});
41104

42105
group.finish();
43106
}

0 commit comments

Comments
 (0)