Skip to content

Vectorize log PDF calculation - #195

Open
not522 wants to merge 1 commit into
optuna:mainfrom
not522:vectorize-log-pdf
Open

Vectorize log PDF calculation#195
not522 wants to merge 1 commit into
optuna:mainfrom
not522:vectorize-log-pdf

Conversation

@not522

@not522 not522 commented Aug 12, 2026

Copy link
Copy Markdown
Member

Calculating the log PDF is a computationally intensive task within TPE, and improving its performance is crucial. This PR addresses the issue by preventing branching operations from occurring during these heavy computations, thereby achieving speed improvements.

Benchmark

  • master
n_params  n_trials    total [ms]  per trial [us]
      40       500         410.3         820.5
      40      1000        1457.6        1457.6
      40      2000        5691.5        2845.7
  • PR
n_params  n_trials    total [ms]  per trial [us]
      40       500         260.1         520.1
      40      1000         867.1         867.1
      40      2000        3334.2        1667.1
Details
use std::hint::black_box;
use std::time::{Duration, Instant};

use rustuna_core::storage::InMemoryStorage;
use rustuna_core::study::{create_study, Direction};
use rustuna_core::Result;
use rustuna_sampler::tpe::TpeSampler;

const N_TRIALS: [usize; 3] = [500, 1000, 2000];
const N_PARAMS: [usize; 1] = [40];

fn run_study(n_trials: usize, n_params: usize) -> Result<Duration> {
    let storage = InMemoryStorage::new();
    let study = create_study(
        "tpe-benchmark",
        storage,
        TpeSampler::seed_from_u64(0),
        vec![Direction::Minimize],
    )?;

    let start = Instant::now();
    study.optimize(
        |mut trial| {
            let mut value = 0.0;
            for i in 0..n_params {
                let x = trial.suggest_float(&format!("x{i}"), -10.0, 10.0)?;
                value += x * x;
            }
            Ok(vec![black_box(value)])
        },
        n_trials,
    )?;
    Ok(start.elapsed())
}

fn main() -> Result<()> {
    println!(
        "{:>8}  {:>8}  {:>12}  {:>12}",
        "n_params", "n_trials", "total [ms]", "per trial [us]"
    );
    for n_params in N_PARAMS {
        for n_trials in N_TRIALS {
            let duration = run_study(n_trials, n_params)?;
            println!(
                "{:>8}  {:>8}  {:>12.1}  {:>12.1}",
                n_params,
                n_trials,
                duration.as_secs_f64() * 1e3,
                duration.as_secs_f64() * 1e6 / n_trials as f64,
            );
        }
    }
    Ok(())
}

@c-bata

c-bata commented Aug 12, 2026

Copy link
Copy Markdown
Member

@y0z Could you review this PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants