Skip to content

[tune] Give each grid variant its own resolved_vars - #65991

Merged
matthewdeng merged 3 commits into
ray-project:masterfrom
vineethsaivs:fix/constant-grid-search-variant-vars
Sep 22, 2026
Merged

matthewdeng merged 3 commits into
ray-project:masterfrom
vineethsaivs:fix/constant-grid-search-variant-vars

Conversation

@vineethsaivs

Copy link
Copy Markdown
Contributor

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 #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:

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.

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]>
@vineethsaivs
vineethsaivs requested a review from a team as a code owner September 8, 2026 16:30

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

@ray-gardener ray-gardener Bot added tune Tune-related issues community-contribution Contributed by the community labels Sep 8, 2026
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 pseudo-rnd-thoughts left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

reduce to "Hold only what the first pass can not reach"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Reduced to that line in 3e460d5.

@pseudo-rnd-thoughts pseudo-rnd-thoughts added the go add ONLY when ready to merge, run all tests label Sep 21, 2026

@pseudo-rnd-thoughts pseudo-rnd-thoughts left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for updating

@matthewdeng
matthewdeng merged commit 9e63bd9 into ray-project:master Sep 22, 2026
6 checks passed
owenowenisme pushed a commit to owenowenisme/ray that referenced this pull request Sep 23, 2026
## 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]>
nadongjun pushed a commit to nadongjun/ray that referenced this pull request Oct 6, 2026
## 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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution Contributed by the community go add ONLY when ready to merge, run all tests tune Tune-related issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants