Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use shallow copy in _get_latest_trial #4774

Merged
merged 1 commit into from
Jul 12, 2023

Conversation

not522
Copy link
Member

@not522 not522 commented Jun 28, 2023

Motivation

The deep copy in _get_latest_trial takes a long time when the parameters are many. This PR changes it to the shallow copy.

The returned FrozenTrial is passed to samplers and pruners, but they must not modify the trial object. This behavior is described in the base classes.

trial:
Target trial object.
Take a copy before modifying this object.

benchmark

import optuna

def objective(trial):
    return sum([trial.suggest_float(f"x_{i}", -100, 100) ** 2 for i in range(1000)])

study = optuna.create_study(sampler=optuna.samplers.RandomSampler(seed=42))
study.optimize(objective, n_trials=10)
  • master
real	1m16.989s
user	1m15.750s
sys	0m0.710s
  • PR
real	0m2.370s
user	0m2.277s
sys	0m0.307s

Description of the changes

  • Use shallow copy in _get_latest_trial

@not522 not522 added the enhancement Change that does not break compatibility and not affect public interfaces, but improves performance. label Jun 28, 2023
@github-actions github-actions bot added the optuna.trial Related to the `optuna.trial` submodule. This is automatically labeled by github-actions. label Jun 28, 2023
@codecov-commenter
Copy link

Codecov Report

Merging #4774 (84d71ba) into master (b00caef) will not change coverage.
The diff coverage is 100.00%.

❗ Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the Github App Integration for your organization. Read more.

@@           Coverage Diff           @@
##           master    #4774   +/-   ##
=======================================
  Coverage   90.81%   90.81%           
=======================================
  Files         189      189           
  Lines       14416    14416           
=======================================
  Hits        13092    13092           
  Misses       1324     1324           
Impacted Files Coverage Δ
optuna/trial/_trial.py 95.67% <100.00%> (ø)

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@not522
Copy link
Member Author

not522 commented Jun 29, 2023

@toshihikoyanase @HideakiImamura Could you review this PR?

Copy link
Member

@HideakiImamura HideakiImamura left a comment

Choose a reason for hiding this comment

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

LGTM.

@HideakiImamura HideakiImamura removed their assignment Jul 6, 2023
@toshihikoyanase
Copy link
Member

I'm sorry for the delayed response.

The returned FrozenTrial is passed to samplers and pruners, but they must not modify the trial object. This behavior is described in the base classes.

Let me check the implementation of the built-in samplers/pruners.

Copy link
Member

@toshihikoyanase toshihikoyanase left a comment

Choose a reason for hiding this comment

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

LGTM.

The trial objects returned by _get_latest_trial are used in the following methods:

  • BaseSampler.sample_independent
  • BasePruner._filter_study
sampler check comment
BoTorchSampler OK pass to independent sampler
PyCmaSampler OK pass to independent sampler
SkoptSampler OK pass to independent sampler
BruteForceSampler OK read trial.number
CmaEsSampler OK pass to independent sampler
GridSampler OK read trial.system_attrs
NSGAIIISampler OK pass to independent sampler
PartialFixedSampler OK pass to independent sampler
QMCSampler OK pass to independent sampler
RandomSampler OK not used
TPESampler OK pass to independent sampler. not used in _sample ?
NSGAIISampler OK pass to random sampler
DeterministicSampler OK not used
FirstTrialOnlyRandomSampler OK not used
(tests) FixedSampler OK not used
(tests) SamplerStubForTestRelativeParameters OK not used
(tutorial) SimulatedAnnealingSampler OK pass to independent sampler
pruner check comment
HyperbandPruner OK read trial.number in _get_bracket_id

@toshihikoyanase toshihikoyanase added this to the v3.3.0 milestone Jul 12, 2023
@toshihikoyanase toshihikoyanase merged commit 8409371 into optuna:master Jul 12, 2023
34 checks passed
@not522 not522 deleted the shallow-copy-trial branch July 12, 2023 05:22
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. optuna.trial Related to the `optuna.trial` submodule. This is automatically labeled by github-actions.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants