Skip to content

Avoid duplications in _log_gauss_mass evaluations#6202

Merged
nabenabe0928 merged 10 commits intooptuna:masterfrom
nabenabe0928:enhance/avoid-duplicated-eval-of-log-gauss-mass
Jul 25, 2025
Merged

Avoid duplications in _log_gauss_mass evaluations#6202
nabenabe0928 merged 10 commits intooptuna:masterfrom
nabenabe0928:enhance/avoid-duplicated-eval-of-log-gauss-mass

Conversation

@nabenabe0928
Copy link
Copy Markdown
Contributor

Motivation

This PR speeds up _log_gauss_mass evaluations for TPESampler.

Description of the changes

  • Apply the np.unique_inverse equivalent operation to avoid duplications
  • Use the application-specific implementation of np.unique_inverse for speedup
  • Replace _truncnorm._log_gauss_mass with the new implementation

@nabenabe0928
Copy link
Copy Markdown
Contributor Author

@not522 @kAIto47802
Could you review this PR?

@nabenabe0928 nabenabe0928 added the enhancement Change that does not break compatibility and not affect public interfaces, but improves performance. label Jul 14, 2025
@nabenabe0928
Copy link
Copy Markdown
Contributor Author

This PR Master
15 seconds 29 seconds

Important

49% Speedup 🚀

Benchmarking Code
import optuna


def objective(trial: optuna.Trial) -> float:
    f1 = sum(trial.suggest_float(f"x{i}", -5, 5)**2 for i in range(5))
    f2 = sum(trial.suggest_float(f"x{i+5}", -5, 5, step=0.5)**2 for i in range(5))
    return f1 + f2


sampler = optuna.samplers.TPESampler(seed=0, multivariate=True)
study = optuna.create_study(sampler=sampler)
study.optimize(objective, n_trials=1000)
print((study.trials[-1].datetime_complete - study.trials[0].datetime_start).total_seconds())

Copy link
Copy Markdown
Member

@not522 not522 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@not522 not522 removed their assignment Jul 15, 2025
Copy link
Copy Markdown
Collaborator

@kAIto47802 kAIto47802 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!
I confirmed that the benchmark code you provided produces exactly the same output on the branch in this PR (8ff8e8f510725eb7f7789eefe01c729f91944fac) and the current master branch (49b26b384774e96243c6115423a9e16ac9802183) using the following code:

#!/bin/bash

git switch master
master=$(git rev-parse HEAD)
python benckmark.py > ${master}.txt

git switch nabenabe0928/enhance/avoid-duplicated-eval-of-log-gauss-mass
refactor=$(git rev-parse HEAD)
python benckmark.py > ${refactor}.txt

if cmp -s ${master}.txt ${refactor}.txt; then
    echo "Both branches produce the same output."
    exit 0
else
    echo "The outputs differ between the branches."
    exit 1
fi

@nabenabe0928 nabenabe0928 merged commit 1ee1c4f into optuna:master Jul 25, 2025
15 of 18 checks passed
@kAIto47802 kAIto47802 removed their assignment Jul 25, 2025
@nabenabe0928 nabenabe0928 added this to the v4.5.0 milestone Jul 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Change that does not break compatibility and not affect public interfaces, but improves performance.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants