fix(optuna-sweeper): pass values=None when marking trial as FAIL, add NaN check - #3333
Conversation
… NaN check When study.tell(state=COMPLETE, values=[nan]) raises a ValueError (optuna rejects NaN objective values), the exception handler sets state=FAIL but then calls study.tell(state=FAIL, values=[nan]) again — which raises a second ValueError because optuna does not allow values to be specified for FAIL/PRUNED trials. Two-part fix: 1. Before calling study.tell, explicitly check whether any value in 'values' is NaN and raise a descriptive ValueError so the failure is captured by the existing except block with a helpful message. 2. In the except block, pass values=None to study.tell so the FAIL state is recorded without crashing with a secondary 'Values cannot be specified when state is PRUNED or FAIL' error. Fixes hydra-ecosystem#2237
|
Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
|
Thanks.
|
omry
left a comment
There was a problem hiding this comment.
Thanks. The CLA and plugin news fragment are now in place, and changing the failed-trial retry to values=None matches the agreed fix in #2237 (comment).
Before this can be merged, please:
- Rebase the branch onto the latest
main. - Add a regression test reproducing the reported NaN objective case.
- Verify that the trial is recorded as
FAIL, the sweep does not crash, and subsequent trials can continue. - Run the Optuna sweeper test suite after rebasing.
The proactive math.isnan() check duplicates validation already performed by Optuna, but it also provides a clearer Hydra-controlled error message, so it can remain if it is covered by the regression test.
Problem
Closes #2237
When a Hydra run returns NaN objective values (e.g. a diverged model), the Optuna sweeper crashes with a double exception:
study.tell(state=COMPLETE, values=[nan])raisesValueError: Trial N failed, because the objective function returned nan.except Exceptionhandler setsstate = TrialState.FAILbut still passes the originalvaluesto a secondstudy.tellcall, which raises anotherValueError: Values cannot be specified when state is TrialState.PRUNED or TrialState.FAIL.This second exception is unhandled and crashes the entire sweep.
Fix
Two-part change in
plugins/hydra_optuna_sweeper/hydra_plugins/hydra_optuna_sweeper/_impl.py:1. Proactive NaN check — after computing
values, raise a descriptiveValueErrorif any value is NaN, so the failure is captured cleanly by the existingexceptblock with a readable warning message.2. Fix the except block — change
study.tell(trial=trial, state=state, values=values)tostudy.tell(trial=trial, state=state, values=None). Optuna's API requiresvalues=Nonewhen state isFAILorPRUNED; passing the NaN values caused the secondary crash.Behaviour after fix
A trial that returns NaN objective values is recorded as
FAILin the Optuna study (consistent with a diverged run), a warning is logged, and the sweep continues to the next trial instead of crashing.