Skip to content

[tune] Pass every result to every stopper in CombinedStopper - #66923

Open
vineethsaivs wants to merge 2 commits into
ray-project:masterfrom
vineethsaivs:fix/combined-stopper-call-every-stopper
Open

vineethsaivs wants to merge 2 commits into
ray-project:masterfrom
vineethsaivs:fix/combined-stopper-call-every-stopper

Conversation

@vineethsaivs

@vineethsaivs vineethsaivs commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Description

CombinedStopper, which RunConfig(stop=[...]) builds from a list, calls its stoppers with any() over a generator. That stops at the first stopper returning True, so later stoppers never see that result. Stateful stoppers miss it: with stop=[MaximumIterationStopper(2), ExperimentPlateauStopper("loss", top=3)], the plateau stopper never sees any trial's final result. If three trials report loss 1.0 and then 0.3, 0.2 and 0.1, it declares the experiment plateaued on the three identical 1.0 losses.

Fix: call every stopper, then combine the results. stop_all is unchanged, since a True there ends the experiment anyway.

Why this is not a duplicate

I searched open and closed PRs and issues for CombinedStopper and stopper changes and found none on this.

Tests run and their results

New test_combined_stopper_passes_every_result_to_every_stopper in test_stopper.py. Master's stopper.py (identical to the ray 2.56.1 wheel's) and this branch:

pytest python/ray/tune/tests/test_stopper.py
  master:      1 failed (stopper("t2", {"loss": 1.0}) returned True), 3 passed
  this branch: 4 passed

black is clean; ruff reports only the same typing-style findings as master.

CombinedStopper.__call__ used any() over a generator, which stops at
the first stopper that returns True, so later stoppers never see that
result. Stateful stoppers miss it: with stop=[MaximumIterationStopper,
ExperimentPlateauStopper], the plateau stopper never sees any trial's
final result and can call the experiment plateaued from earlier ones.
Call every stopper, then combine.

Signed-off-by: Vineeth Sai <[email protected]>
@vineethsaivs
vineethsaivs requested a review from a team as a code owner October 11, 2026 17:47

@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 modifies the CombinedStopper to evaluate all registered stoppers instead of short-circuiting on the first True value, ensuring stateful stoppers process every result. A corresponding unit test has been added to verify this behavior. The review feedback points out that using a list comprehension directly inside any() is susceptible to being automatically optimized back to a short-circuiting generator expression by linters (like Ruff), and suggests assigning the list comprehension to a local variable first to prevent this.

Comment thread python/ray/tune/stopper/stopper.py Outdated
return any(s(trial_id, result) for s in self._stoppers)
# Call every stopper, not just up to the first that returns True, so
# stateful stoppers (e.g. ExperimentPlateauStopper) see every result.
return any([s(trial_id, result) for s in self._stoppers])

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.

high

Using a list comprehension directly inside any() (i.e., any([...])) is highly susceptible to being flagged and automatically "optimized" by linters (such as Ruff's C419 or PIE802 rules) to a generator expression. If an automated tool or developer runs such a linter with auto-fix enabled, it will silently re-introduce the short-circuiting behavior and break this fix.\n\nTo make this robust against linter auto-fixes, assign the list comprehension to a local variable first.

        results = [s(trial_id, result) for s in self._stoppers]\n        return any(results)

@ray-gardener ray-gardener Bot added tune Tune-related issues community-contribution Contributed by the community labels Oct 11, 2026
@github-actions

Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has not had
any activity for 14 days. It will be closed in another 14 days if no further activity occurs.
Thank you for your contributions.

You can always ask for help on our discussion forum or Ray's public slack channel.

If you'd like to keep this open, just leave any comment, and the stale label will be removed.

@github-actions github-actions Bot added stale The issue is stale. It will be closed within 7 days unless there are further conversation unstale A PR that has been marked unstale. It will not get marked stale again if this label is on it. and removed stale The issue is stale. It will be closed within 7 days unless there are further conversation labels Oct 26, 2026
dstrodtman added a commit that referenced this pull request Oct 30, 2026
…67610)

## Summary

Adds the missing docstrings, `Args:` entries, and `Returns:` sections on
public Ray Tune symbols in `search`, `stopper`, `trainable`, `context`,
`utils`, `execution`, and `logger`. Each describes existing behavior,
written from the code. It follows #67608, which covers
`ray.tune.schedulers`.

Stacked on #67608: this PR targets that branch so the correctness PR
above it can build on both, and retargets to `master` once #67608
merges. The two PRs share no files.

## Before

Across these subpackages, 23 public symbols had no docstring, 43
parameters had no `Args:` entry, and 14 callables that annotate a return
type had no `Returns:` section. The sampling functions such as
`tune.uniform`, `tune.quniform`, and `tune.randint` described their
bounds in prose but had no `Args:` block.

## After

19 of the 23 docstrings, 35 of the 43 parameters, and 10 of the 14
`Returns:` sections are documented. Where a base class lacked the
docstring its overrides inherit, this PR documents the base instead of
each override: `Searcher.get_state`, `Searcher.set_state`,
`Stopper.__call__`, and `Stopper.stop_all`. That covers both
`ConcurrencyLimiter` classes and `Repeater`, and `CombinedStopper`.

Behavior is unchanged. The diff touches only docstrings, which the guard
below confirms.

## Not in this PR

- **Four methods with untyped signatures:**
`BasicVariantGenerator.set_state` and `save_to_dir`,
`FunctionStopper.is_valid_function`, and `Trainable.reset`. pydoclint,
run with `--arg-type-hints-in-signature`, rejects an `Args:` section on
a signature without type hints, so documenting these 6 parameters needs
a signature change. That signature change is in #67612, since this PR is
docstring-only. `BasicVariantGenerator.set_state` and
`FunctionStopper.is_valid_function` get a docstring here, without
sections.
- **`TuneContext.get_trial_name`, `get_trial_id`, `get_trial_resources`,
and `get_trial_dir`:** these take their docstrings at runtime from
`ray.train._internal.session` through `@_copy_doc`, so they render
documented already. A docstring written on the method would be
overwritten. Their `Returns:` sections belong on the `session`
functions, which Ray Train owns.
- **`Repeater.on_trial_complete`:** #67013 changes this method and edits
the same docstring, so its `Args:` entries are left to land after that
PR.

## For reviewers

These docstrings are worth a close read:

- `Verbosity` documents the four levels as the `verbose` argument of
`tune.run` describes the legacy output, and notes that the default
output treats levels 2 and 3 the same (`get_air_verbosity` in
`tune/experimental/output.py`).
- `PlacementGroupFactory.__call__` is deprecated. Its new docstring says
so and points to `to_placement_group()`.
- `BasicVariantGenerator.get_state` documents that it returns `False`
instead of a state when any variants are generated lazily.

## Duplicate check

No open PR documents these symbols. Open PRs #66923 (`stopper.py`),
#66699 (`logger/json.py`), and #58359 (`utils/util.py`) change code in
files this PR touches, and all three apply cleanly on top of this
branch. #67013 (`repeater.py`) also applies cleanly, because this PR
leaves the method it changes alone.

## Testing

- `python ci/ray_ci/doc/check_docstring_only.py --base "$(git merge-base
upstream/master HEAD)"`: `docstring-only guard: 15 file(s), docstrings
only.`
- The full pre-commit suite on the changed files, including ruff, black,
pydoclint, and the docstyle hook: passed.
- `ci/ray_ci/doc/api_param_coverage.py`, run over `python/ray/tune/`
outside `schedulers/`: 43 undocumented parameters on `master`, 8 on this
branch: 6 on the four untyped signatures and 2 on
`Repeater.on_trial_complete`, all listed above.
- The commit carries an `@rtd-build` line, so the Read the Docs preview
renders the changed docstrings.

AI assistance (Claude Code) was used to prepare this PR.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Signed-off-by: Douglas Strodtman <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
pseudo-rnd-thoughts pushed a commit that referenced this pull request Oct 30, 2026
## Summary

Corrects eight Ray Tune docstrings that describe behavior the code
doesn't have. This is a correctness change: it changes what the
docstrings assert, so each item needs a technical read rather than a
skim. The items surfaced while backfilling docstrings in #67608 and
#67610.

Stacked on #67610, which is stacked on #67608. Several corrected lines
sit right next to `Args:` blocks those PRs add, so a branch off `master`
would conflict with both. This PR retargets to `master` once they merge.

## Before and after

**`HyperBandForBOHB.choose_trial_to_run`** (`schedulers/hb_bohb.py`)
opened with "Fair scheduling within iteration by completion percentage,"
copied from `HyperBandScheduler`. `HyperBandScheduler` sorts brackets by
`completion_percentage()`, but the BOHB override iterates them in
creation order and returns the first ready trial. The next sentence said
the trial list from `tune_controller` isn't used, but the override reads
`tune_controller.get_trials()` to check whether any trial is running.
Both sentences now describe the override.

**`MedianStoppingRule.on_trial_result`**
(`schedulers/median_stopping_rule.py`) said it compares a trial against
"the running averages of all completed trials' objectives reported up to
step `t`." The code compares against every other trial whose last result
is at step `t` or later (`_trials_beyond_time`), whether or not it has
completed, and each running average covers results from `grace_period`
to `t` (`_running_mean`).

**`tune.quniform`** (`search/sample.py`) gave `tune.uniform(1, 10)` as
its example. It now uses `tune.quniform(1, 10, 0.5)` and says the value
is rounded to a multiple of `0.5`, which is what `Quantized.sample`
does.

**`tune.randint`** (`search/sample.py`) gave `tune.randint(10)` as its
example, but `upper` has no default, so that call raises a `TypeError`.
It now uses `tune.randint(0, 10)`.

**`Stopper`** (`stopper/stopper.py`) said "By default, this class does
not stop any trials." The base `__call__` and `stop_all` raise
`NotImplementedError`. The docstring now says so.

**`ExperimentPlateauStopper`** (`stopper/experiment_plateau.py`) listed
a `ValueError` when `patience` isn't "a strictly positive integer." The
check is `patience < 0`, and `patience` defaults to `0`. The `Raises:`
entry now says "a non-negative integer." The runtime error message says
"strictly positive" too, but it's a code string, so it changes in
#67612.

**`BasicVariantGenerator.restore_from_dir`** (`search/basic_variant.py`)
said it "Restores self + searcher + search wrappers," copied from
`SearchGenerator`. `BasicVariantGenerator` has no searcher or wrappers.
It loads the newest checkpoint and calls `set_state`, and the summary
now says that.

**`tune.run` and `RunConfig`, `verbose`** (`tune/tune.py` and
`air/config.py`) said that setting `RAY_AIR_NEW_OUTPUT=1` switches to
the old verbosity levels. It's the reverse. `get_air_verbosity` in
`tune/experimental/output.py` falls back to the old output only when the
variable is `"0"`, and it defaults to `"1"`. Both docstrings now say
`RAY_AIR_NEW_OUTPUT=0`.

Behavior is unchanged. The diff touches only docstrings, which the guard
below confirms.

## For reviewers

If `HyperBandForBOHB` is meant to schedule by completion percentage like
its parent, the bug is in the code rather than the docstring. Say so and
I'll drop that item in favor of a code fix. The override's comments mark
it as BOHB-specific ("MAIN CHANGE HERE"), but none of them explain the
ordering.

`air/config.py` is owned by `@ray-project/ray-train`. It carries the
same `RAY_AIR_NEW_OUTPUT` sentence as `tune.py`, so both get the same
fix here.

## Duplicate check

No open PR edits these docstrings. Open PRs #66710 and #67137 change
code in `hyperband.py`, and #66923 changes `CombinedStopper` code in
`stopper.py`. None of them touch the lines changed here.

## Testing

- `python ci/ray_ci/doc/check_docstring_only.py --base
doc-1922-tune-api-docstrings`: `docstring-only guard: 8 file(s),
docstrings only.`
- The full pre-commit suite on the changed files, including ruff, black,
pydoclint, and the docstyle hook: passed.
- The commit carries an `@rtd-build` line, so the Read the Docs preview
renders the changed docstrings.

AI assistance (Claude Code) was used to prepare this PR.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Signed-off-by: Douglas Strodtman <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
any() over a generator stops at the first True, so a linter autofix of
any([...]) would bring back the bug this PR fixes. Build the results
list in a named variable so the intent is explicit.

Signed-off-by: Vineeth Sai <[email protected]>
@vineethsaivs

Copy link
Copy Markdown
Contributor Author

Pushed d346010: results are collected in a named list first, so a linter autofix can't turn it into a generator. Would appreciate a review whenever someone has a moment, thanks!

This branch has not been deployed

No deployments
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 tune Tune-related issues unstale A PR that has been marked unstale. It will not get marked stale again if this label is on it.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant