Skip to content

Use cached trials in TPESampler's sample_relative#6265

Merged
nabenabe0928 merged 3 commits intooptuna:masterfrom
not522:relative-use-cache
Sep 5, 2025
Merged

Use cached trials in TPESampler's sample_relative#6265
nabenabe0928 merged 3 commits intooptuna:masterfrom
not522:relative-use-cache

Conversation

@not522
Copy link
Copy Markdown
Member

@not522 not522 commented Sep 1, 2025

Motivation

Currently, whether TPESampler uses use_cache in study._get_trials depends solely on whether it uses constant liar. However, since sample_relative is called only once at the beginning of a Trial, it can utilize use_cache.

Description of the changes

Making TPESampler's sample_relative method always use cached trials.

Benchmark

import optuna

def objective(trial: optuna.Trial) -> float:
    x = trial.suggest_float("x", -100, 100)
    y = trial.suggest_int("y", -100, 100)
    return x**2 + y**2

sampler = optuna.samplers.TPESampler(seed=42, multivariate=True, constant_liar=True)
study = optuna.create_study(sampler=sampler, storage="sqlite:///tmp.db")
study.optimize(objective, n_trials=1000)
master PR
13.389s 12.247s

@not522 not522 added the enhancement Change that does not break compatibility and not affect public interfaces, but improves performance. label Sep 1, 2025
@codecov
Copy link
Copy Markdown

codecov bot commented Sep 1, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.16%. Comparing base (57fade1) to head (b9bb801).
⚠️ Report is 494 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #6265      +/-   ##
==========================================
- Coverage   89.24%   89.16%   -0.08%     
==========================================
  Files         208      208              
  Lines       13821    13833      +12     
==========================================
  Hits        12334    12334              
- Misses       1487     1499      +12     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@nabenabe0928
Copy link
Copy Markdown
Contributor

@sawa3030
Could you review this PR?

Copy link
Copy Markdown
Contributor

@nabenabe0928 nabenabe0928 left a comment

Choose a reason for hiding this comment

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

Thank you for the PR, mostly LGTM!
I left some minor comments:)

not522 and others added 2 commits September 3, 2025 11:09
Co-authored-by: Shuhei Watanabe <47781922+nabenabe0928@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

@nabenabe0928 nabenabe0928 left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Copy Markdown
Collaborator

@sawa3030 sawa3030 left a comment

Choose a reason for hiding this comment

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

LGTM

@nabenabe0928 nabenabe0928 merged commit 6027868 into optuna:master Sep 5, 2025
17 of 18 checks passed
@not522 not522 deleted the relative-use-cache branch September 5, 2025 12:30
@nabenabe0928 nabenabe0928 added this to the v4.6.0 milestone Sep 22, 2025
not522 added a commit to not522/optuna that referenced this pull request Mar 11, 2026
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