Repository navigation
[tune] Give each grid variant its own resolved_vars - #65991
matthewdeng merged 3 commits into
Conversation
With constant_grid_search=True, resolved_vars is built once before the grid loop, then mutated and yielded again for every variant, so all variants share one dict holding the last grid value. _VariantIterator materializes the generator with list(), so every trial is created from that same dict: the experiment tag and evaluated_params report the last grid value for every trial, while each trial runs its own correct config. Copy resolved_vars per variant. The constant_grid_search=False path already gets a fresh dict from _resolve_domain_vars each iteration. Signed-off-by: Vineeth Sai <[email protected]>
There was a problem hiding this comment.
Code Review
This pull request fixes an issue in Ray Tune's variant generator where resolved_vars was mutated and yielded across variants under constant_grid_search, causing all variants to share the last grid value. This is resolved by copying resolved_vars into variant_vars for each variant. Corresponding unit tests have been added to verify this behavior. The reviewer identified a bug in the dependency resolution logic when constant_grid_search=True and some domain variables depend on grid search variables, which leads to first-pass resolved variables being discarded. A fix was suggested to preserve and merge first-pass resolved variables.
When constant_grid_search=True and a domain depends on a grid value, the first pass cannot resolve that domain, so a second pass runs once per grid variant. That second pass only covers what is left in to_resolve, and its result replaced resolved_vars outright, so the variables the first pass had already fixed were dropped from the yielded vars. Those are exactly the variables constant_grid_search exists to hold constant, so they went missing from evaluated_params and the experiment tag while still being set correctly in the trial config. Keep the first pass in constant_vars and merge the per-variant pass on top. constant_vars is empty unless constant_grid_search filled it, so the constant_grid_search=False path is unchanged. Reported by gemini-code-assist on the PR. Signed-off-by: Vineeth Sai <[email protected]>
pseudo-rnd-thoughts
left a comment
There was a problem hiding this comment.
Thanks for the PR, overall looks good however could you reduce the length of this comment. Then good to merge
| _, pass_vars = _resolve_domain_vars( | ||
| resolved_spec, to_resolve, random_state=random_state | ||
| ) | ||
| # `to_resolve` holds only what the first pass could not reach, so the first |
There was a problem hiding this comment.
reduce to "Hold only what the first pass can not reach"
pseudo-rnd-thoughts
left a comment
There was a problem hiding this comment.
Thanks for updating
## Why this is not a duplicate ``` gh pr list --repo ray-project/ray --state open --search "constant_grid_search" -> none gh pr list --repo ray-project/ray --state open --search "variant_generator resolved_vars" -> none gh search prs --repo ray-project/ray "constant_grid_search" --state closed -> none ``` No open or closed PR touches `_generate_variants_internal`. The last change to `variant_generator.py` on master is ray-project#63500 (docstrings, 2026-05-22). ## What is wrong With `constant_grid_search=True`, every trial reports the **last** grid value as its hyperparameter, whatever it actually ran. ``` constant_grid_search=False actual lr each trial runs : [0.1, 0.2, 0.3] experiment_tag : ['0_lr=0.1000,seed=200', '1_lr=0.2000,seed=391', '2_lr=0.3000,seed=767'] evaluated_params lr : [0.1, 0.2, 0.3] constant_grid_search=True actual lr each trial runs : [0.1, 0.2, 0.3] experiment_tag : ['0_lr=0.3000,seed=135', '1_lr=0.3000,seed=135', '2_lr=0.3000,seed=135'] evaluated_params lr : [0.3, 0.3, 0.3] ``` Each trial runs the right config. What is wrong is the record of it: all three experiment tags are identical, and `evaluated_params` is what search algorithms and `ExperimentAnalysis` read to know what was tried. **Cause:** on the `constant_grid_search` path `resolved_vars` is built once, before the grid loop, by the pre-sampling pass. The loop then does `resolved_vars[path] = _get_value(spec, path)` and `yield resolved_vars, spec`, mutating and re-yielding that one dict for every variant. `_VariantIterator` materializes the generator with `list(iterable)`, so by the time `create_trial` runs over the list, every entry is the same dict holding the last grid value. The `constant_grid_search=False` path is unaffected only because `_resolve_domain_vars` reassigns `resolved_vars` on each iteration, handing out a fresh dict by accident. ## What changed Copy `resolved_vars` per variant before filling in that variant's grid values: ```python variant_vars = resolved_vars.copy() for path, value in grid_vars: variant_vars[path] = _get_value(spec, path) ... yield variant_vars, spec ``` The pre-sampled constants still stay constant across the grid, which is the point of the flag. Nothing changes for `constant_grid_search=False`. ## Tests run and their results ``` pytest python/ray/tune/tests/test_sample.py -k ConstantGridSearch master: 2 failed, 1 passed (assert [3, 3, 3] == [1, 2, 3]) this branch: 3 passed pytest python/ray/tune/tests/test_sample.py master: 31 failed, 20 passed, 2 skipped this branch: 29 failed, 20 passed, 2 skipped ``` The two failures that disappear are exactly the new tests, and the failure sets are otherwise identical (`comm` on the sorted FAILED lists reports no other difference). The remaining 29 are pre-existing on master here and need the optuna, hyperopt and bohb backends, which are not installed on this machine. Two new tests in `SearchSpaceTest`: `testConstantGridSearchResolvedVarsPerVariant` checks `resolved_vars` against the spec for both flag values, and `testConstantGridSearchTrialsReportTheirOwnGridValue` goes through `BasicVariantGenerator` and asserts the tags and `evaluated_params` match what each trial runs. Ray cannot be built on this machine (`ray._raylet` is unavailable), so the module under test was the installed `ray==2.58.0` copy after confirming it is byte identical to master and to the stock PyPI wheel, patched in place and restored afterwards. `ruff check` reports the identical 68 findings before and after, and `ruff format --check` is clean. ## AI assistance AI assistance was used for this change. --------- Signed-off-by: Vineeth Sai <[email protected]>
## Why this is not a duplicate ``` gh pr list --repo ray-project/ray --state open --search "constant_grid_search" -> none gh pr list --repo ray-project/ray --state open --search "variant_generator resolved_vars" -> none gh search prs --repo ray-project/ray "constant_grid_search" --state closed -> none ``` No open or closed PR touches `_generate_variants_internal`. The last change to `variant_generator.py` on master is ray-project#63500 (docstrings, 2026-05-22). ## What is wrong With `constant_grid_search=True`, every trial reports the **last** grid value as its hyperparameter, whatever it actually ran. ``` constant_grid_search=False actual lr each trial runs : [0.1, 0.2, 0.3] experiment_tag : ['0_lr=0.1000,seed=200', '1_lr=0.2000,seed=391', '2_lr=0.3000,seed=767'] evaluated_params lr : [0.1, 0.2, 0.3] constant_grid_search=True actual lr each trial runs : [0.1, 0.2, 0.3] experiment_tag : ['0_lr=0.3000,seed=135', '1_lr=0.3000,seed=135', '2_lr=0.3000,seed=135'] evaluated_params lr : [0.3, 0.3, 0.3] ``` Each trial runs the right config. What is wrong is the record of it: all three experiment tags are identical, and `evaluated_params` is what search algorithms and `ExperimentAnalysis` read to know what was tried. **Cause:** on the `constant_grid_search` path `resolved_vars` is built once, before the grid loop, by the pre-sampling pass. The loop then does `resolved_vars[path] = _get_value(spec, path)` and `yield resolved_vars, spec`, mutating and re-yielding that one dict for every variant. `_VariantIterator` materializes the generator with `list(iterable)`, so by the time `create_trial` runs over the list, every entry is the same dict holding the last grid value. The `constant_grid_search=False` path is unaffected only because `_resolve_domain_vars` reassigns `resolved_vars` on each iteration, handing out a fresh dict by accident. ## What changed Copy `resolved_vars` per variant before filling in that variant's grid values: ```python variant_vars = resolved_vars.copy() for path, value in grid_vars: variant_vars[path] = _get_value(spec, path) ... yield variant_vars, spec ``` The pre-sampled constants still stay constant across the grid, which is the point of the flag. Nothing changes for `constant_grid_search=False`. ## Tests run and their results ``` pytest python/ray/tune/tests/test_sample.py -k ConstantGridSearch master: 2 failed, 1 passed (assert [3, 3, 3] == [1, 2, 3]) this branch: 3 passed pytest python/ray/tune/tests/test_sample.py master: 31 failed, 20 passed, 2 skipped this branch: 29 failed, 20 passed, 2 skipped ``` The two failures that disappear are exactly the new tests, and the failure sets are otherwise identical (`comm` on the sorted FAILED lists reports no other difference). The remaining 29 are pre-existing on master here and need the optuna, hyperopt and bohb backends, which are not installed on this machine. Two new tests in `SearchSpaceTest`: `testConstantGridSearchResolvedVarsPerVariant` checks `resolved_vars` against the spec for both flag values, and `testConstantGridSearchTrialsReportTheirOwnGridValue` goes through `BasicVariantGenerator` and asserts the tags and `evaluated_params` match what each trial runs. Ray cannot be built on this machine (`ray._raylet` is unavailable), so the module under test was the installed `ray==2.58.0` copy after confirming it is byte identical to master and to the stock PyPI wheel, patched in place and restored afterwards. `ruff check` reports the identical 68 findings before and after, and `ruff format --check` is clean. ## AI assistance AI assistance was used for this change. --------- Signed-off-by: Vineeth Sai <[email protected]>
Why this is not a duplicate
No open or closed PR touches
_generate_variants_internal. The last change tovariant_generator.pyon master is #63500 (docstrings, 2026-05-22).What is wrong
With
constant_grid_search=True, every trial reports the last grid value as its hyperparameter, whatever it actually ran.Each trial runs the right config. What is wrong is the record of it: all three experiment tags are identical, and
evaluated_paramsis what search algorithms andExperimentAnalysisread to know what was tried.Cause: on the
constant_grid_searchpathresolved_varsis built once, before the grid loop, by the pre-sampling pass. The loop then doesresolved_vars[path] = _get_value(spec, path)andyield resolved_vars, spec, mutating and re-yielding that one dict for every variant._VariantIteratormaterializes the generator withlist(iterable), so by the timecreate_trialruns over the list, every entry is the same dict holding the last grid value.The
constant_grid_search=Falsepath is unaffected only because_resolve_domain_varsreassignsresolved_varson each iteration, handing out a fresh dict by accident.What changed
Copy
resolved_varsper variant before filling in that variant's grid values:The pre-sampled constants still stay constant across the grid, which is the point of the flag. Nothing changes for
constant_grid_search=False.Tests run and their results
The two failures that disappear are exactly the new tests, and the failure sets are otherwise identical (
common the sorted FAILED lists reports no other difference). The remaining 29 are pre-existing on master here and need the optuna, hyperopt and bohb backends, which are not installed on this machine.Two new tests in
SearchSpaceTest:testConstantGridSearchResolvedVarsPerVariantchecksresolved_varsagainst the spec for both flag values, andtestConstantGridSearchTrialsReportTheirOwnGridValuegoes throughBasicVariantGeneratorand asserts the tags andevaluated_paramsmatch what each trial runs.Ray cannot be built on this machine (
ray._rayletis unavailable), so the module under test was the installedray==2.58.0copy after confirming it is byte identical to master and to the stock PyPI wheel, patched in place and restored afterwards.ruff checkreports the identical 68 findings before and after, andruff format --checkis clean.AI assistance
AI assistance was used for this change.