Avoid deepcopy in _tell_with_warning#6079
Conversation
Performance ImprovementThe test was conducted by alternating between the
The code used for testing is below. |
|
There is a significant speed difference with
import optuna
def objective(trial: optuna.Trial) -> float:
x = trial.suggest_float("x", -1, 1)
y = trial.suggest_int("y", -1, 1)
return x**2 + y
study = optuna.create_study(sampler=optuna.samplers.RandomSampler())
study.optimize(objective, n_trials=10000) |
optuna/study/_tell.py
Outdated
| frozen_trial = study._storage.get_trial(frozen_trial._trial_id) | ||
|
|
||
| if warning_message is not None: | ||
| frozen_trial._system_attrs[STUDY_TELL_WARNING_KEY] = warning_message |
There was a problem hiding this comment.
frozen_trial is modified in this line. So, could you refactor the warning message implementation first?
|
Let me reopen this PR since #6082 is merged into master |
|
Could you also add |
optuna/study/_optimize.py
Outdated
| frozen_trial = copy.deepcopy(frozen_trial) | ||
| callback(study, frozen_trial) |
There was a problem hiding this comment.
In the current implementation, if multiple callbacks are passed, modifying frozen_trial within any callback will affect the frozen_trial passed to subsequent callbacks.
| frozen_trial = copy.deepcopy(frozen_trial) | |
| callback(study, frozen_trial) | |
| callback(study, copy.deepcopy(frozen_trial)) |
|
Thank you for your feedback. I've incorporated the suggested changes. PTAL |
Motivation
Improve the performance of
_tell_with_warningby reducing unnecessary deep copies.Description of the Changes
deepcopywith a shallow copy in_tell_with_warningto reduce overhead when returning aFrozenTrial.deepcopyis performed only in contexts where theFrozenTrialmight be modified, such as when it is passed into callbacks or used instudy.tell().