Skip to content

[tune] Fix HyperBand ending brackets short of max_t - #66710

Open
vineethsaivs wants to merge 1 commit into
ray-project:masterfrom
vineethsaivs:fix/hyperband-last-rung-max-t
Open

vineethsaivs wants to merge 1 commit into
ray-project:masterfrom
vineethsaivs:fix/hyperband-last-rung-max-t

Conversation

@vineethsaivs

@vineethsaivs vineethsaivs commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Description

HyperBand stops a bracket's best trials before max_t unless max_t is a multiple of eta**s. In hyperband_example.py (max_t=200), the 98-trial bracket's rungs are 2, 6, 18, 54, 162.

Cause: successive_halving multiplies the floored r0 by eta, so the last rung is max_t rounded down to a multiple of eta**s. #39157 fixed this only when eta**s divides max_t.

Fix: rung i is int(max_t / eta**(s - i)), per the HyperBand paper's Alg. 1. r0 uses the same division, so 729 * 3**-6 == 0.9999999999999999 no longer drops a bracket. The default max_t=81 is unchanged.

Why this is not a duplicate

Searched PRs and issues (open and closed) for hyperband, HyperBandScheduler and successive_halving; no other PR changes HyperBand's rung computation. #67137 (NaN ranking) edits successive_halving nearby. #11695 reported this symptom under the older formula.

Tests run and their results

Master's python/ray/tune on the ray 2.56.1 wheel:

pytest python/ray/tune/tests/test_trial_scheduler.py -k testLastRungIsMaxT
  master:      1 failed (the max_t=50 brackets end at 48, 45, 27)
  this branch: 1 passed
pytest python/ray/tune/tests/test_trial_scheduler.py
  master:      82 passed, 2 skipped
  this branch: 83 passed, 2 skipped

A real Tuner run with hyperband_example.py's settings: the 98-trial bracket's best trials stop at 162 on master, 200 on this branch.

@vineethsaivs
vineethsaivs requested a review from a team as a code owner October 5, 2026 02:07

@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 updates the HyperBand scheduler to ensure that the last rung of every bracket correctly ends at max_t even when max_t is not a power of eta. It replaces the multiplication and negative exponent calculations with division to avoid floating-point rounding issues (e.g., preventing a bracket from being dropped due to rounding). A new unit test testLastRungIsMaxT has been added to verify these changes. There are no review comments to address, and I have no additional feedback to provide.

@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 the stale The issue is stale. It will be closed within 7 days unless there are further conversation label Oct 19, 2026
@vineethsaivs

Copy link
Copy Markdown
Contributor Author

Still relevant. It merges cleanly with master and is ready for review.

@github-actions github-actions Bot added 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 20, 2026
dstrodtman added a commit that referenced this pull request Oct 30, 2026
…rs (#67608)

## Summary

Adds the missing docstrings, `Args:` entries, and `Returns:` sections on
the public `ray.tune.schedulers` API: `FIFOScheduler`,
`AsyncHyperBandScheduler`, `HyperBandScheduler`, `HyperBandForBOHB`,
`MedianStoppingRule`, `PopulationBasedTraining`,
`PopulationBasedTrainingReplay`, and `ResourceChangingScheduler`. Each
describes existing behavior, written from the code. A follow-up PR
covers the rest of the public Tune API.

## Before

On these schedulers, 5 public methods had no docstring, 85 parameters
had no `Args:` entry, and 23 methods that annotate a return type had no
`Returns:` section.

## After

All of those are documented. Ray's `api_param_coverage.py` reports no
undocumented parameters in `python/ray/tune/schedulers/`. Most of the
scheduler callbacks, such as `on_trial_add`, `on_trial_result`, and
`choose_trial_to_run`, override `TrialScheduler` without a docstring of
their own. Sphinx renders the base docstring for them, and that
docstring had no `Args:` or `Returns:`. So this PR documents the
`TrialScheduler` methods once instead of repeating the same entries on
each override. Overrides that already had their own docstring, mostly in
`HyperBandScheduler` and `HyperBandForBOHB`, get their entries in place.

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

## For reviewers

These are the docstrings worth a close read:

- `TrialScheduler.on_trial_result` now documents `TrialScheduler.NOOP`
as a return value, for a scheduler that has already changed the trial's
state itself. `PopulationBasedTraining` and
`PopulationBasedTrainingReplay` return it, and `TuneController` treats
it as no action. The existing prose above it still lists only
`CONTINUE`, `PAUSE`, and `STOP`.
- `HyperBandForBOHB.choose_trial_to_run` documents `allow_recurse`, and
describes how the method processes brackets that have a paused trial
when no trial is running.
- `PopulationBasedTraining.reset_stats` and `last_scores` carry a "Unit
test only" comment but are public methods, so they now have docstrings.

Two existing docstrings say something the code doesn't do. This PR
leaves them as they are, since changing what a docstring asserts belongs
in a separate correctness PR:

- `HyperBandForBOHB.choose_trial_to_run` repeats the "Fair scheduling
within iteration by completion percentage" summary from
`HyperBandScheduler`, but it iterates brackets in creation order without
sorting by completion.
- `MedianStoppingRule.on_trial_result` says it compares against "all
completed trials". The code compares against every trial that has
reported a result at or beyond the current step, whether or not it has
completed.

## Duplicate check

No open PR documents these symbols. Open PRs #66710 and #67137 change
code in `hyperband.py`, and #58165, #59383, and #60646 change code in
`pbt.py`. None of them edit the docstrings changed here, and #66710,
#67137, and #58165 apply cleanly on top of this branch.

## Testing

- `python ci/ray_ci/doc/check_docstring_only.py --base "$(git merge-base
upstream/master HEAD)"`: `docstring-only guard: 6 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/schedulers/` with a class index of `python/ray/`: 85
undocumented parameters on `master`, 0 on this branch.
- 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]>
Rungs were built as r0 * eta**i with r0 = int(max_t * eta**-s), so a
bracket's last rung is max_t rounded down to a multiple of eta**s, and
its best trials stop there (162 of 200 in hyperband_example.py). Compute
rung i as int(max_t / eta**(s - i)) so every bracket ends at max_t. The
division also stops 729 * 3**-6 from rounding r0 down to 0.

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

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