-
Notifications
You must be signed in to change notification settings - Fork 6.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[tune] Limit maximum number of pending trials. Add convergence test. #14835
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0604026
Limit maximum number of pending trials. Add convergence test. Minor s…
00f389b
Pre-review
0e1c618
Fix environment variables in tests
c03bdf9
Don't stop too early
84393db
Set default env value to "auto"
549e07c
Have trials ready when placement gorup is available for trial
9b814f6
Move to init
8327788
Auto-generate unique tune placement group prefixes
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
import math | ||
import numpy as np | ||
|
||
import ray | ||
from ray import tune | ||
from ray.tune.stopper import ExperimentPlateauStopper | ||
from ray.tune.suggest import ConcurrencyLimiter | ||
import unittest | ||
|
||
|
||
def loss(config, reporter): | ||
x = config.get("x") | ||
reporter(loss=x**2) # A simple function to optimize | ||
|
||
|
||
class ConvergenceTest(unittest.TestCase): | ||
"""Test convergence in gaussian process.""" | ||
|
||
@classmethod | ||
def setUpClass(cls) -> None: | ||
ray.init(local_mode=False, num_cpus=1, num_gpus=0) | ||
|
||
@classmethod | ||
def tearDownClass(cls) -> None: | ||
ray.shutdown() | ||
|
||
def _testConvergence(self, searcher, top=3, patience=20): | ||
# This is the space of parameters to explore | ||
space = {"x": tune.uniform(0, 20)} | ||
|
||
resources_per_trial = {"cpu": 1, "gpu": 0} | ||
|
||
analysis = tune.run( | ||
loss, | ||
metric="loss", | ||
mode="min", | ||
stop=ExperimentPlateauStopper( | ||
metric="loss", top=top, patience=patience), | ||
search_alg=searcher, | ||
config=space, | ||
num_samples=100, # Number of iterations | ||
resources_per_trial=resources_per_trial, | ||
raise_on_failed_trial=False, | ||
fail_fast=True, | ||
reuse_actors=True, | ||
verbose=1) | ||
print(f"Num trials: {len(analysis.trials)}. " | ||
f"Best result: {analysis.best_config['x']}") | ||
|
||
return analysis | ||
|
||
def testConvergenceAx(self): | ||
from ray.tune.suggest.ax import AxSearch | ||
|
||
np.random.seed(0) | ||
|
||
searcher = AxSearch() | ||
analysis = self._testConvergence(searcher, patience=10) | ||
|
||
assert math.isclose(analysis.best_config["x"], 0, abs_tol=1e-5) | ||
|
||
def testConvergenceBayesOpt(self): | ||
from ray.tune.suggest.bayesopt import BayesOptSearch | ||
|
||
np.random.seed(0) | ||
|
||
# Following bayesian optimization | ||
searcher = BayesOptSearch(random_search_steps=10) | ||
searcher.repeat_float_precision = 5 | ||
searcher = ConcurrencyLimiter(searcher, 1) | ||
|
||
analysis = self._testConvergence(searcher) | ||
|
||
assert len(analysis.trials) < 50 | ||
assert math.isclose(analysis.best_config["x"], 0, abs_tol=1e-5) | ||
|
||
def testConvergenceDragonfly(self): | ||
from ray.tune.suggest.dragonfly import DragonflySearch | ||
|
||
np.random.seed(0) | ||
searcher = DragonflySearch(domain="euclidean", optimizer="bandit") | ||
analysis = self._testConvergence(searcher) | ||
|
||
assert len(analysis.trials) < 100 | ||
assert math.isclose(analysis.best_config["x"], 0, abs_tol=1e-5) | ||
|
||
def testConvergenceHEBO(self): | ||
from ray.tune.suggest.hebo import HEBOSearch | ||
|
||
np.random.seed(0) | ||
searcher = HEBOSearch() | ||
analysis = self._testConvergence(searcher) | ||
|
||
assert len(analysis.trials) < 100 | ||
assert math.isclose(analysis.best_config["x"], 0, abs_tol=1e-2) | ||
|
||
def testConvergenceHyperopt(self): | ||
from ray.tune.suggest.hyperopt import HyperOptSearch | ||
|
||
np.random.seed(0) | ||
searcher = HyperOptSearch(random_state_seed=1234) | ||
analysis = self._testConvergence(searcher, patience=50, top=5) | ||
|
||
assert math.isclose(analysis.best_config["x"], 0, abs_tol=1e-2) | ||
|
||
def testConvergenceNevergrad(self): | ||
from ray.tune.suggest.nevergrad import NevergradSearch | ||
import nevergrad as ng | ||
|
||
np.random.seed(0) | ||
searcher = NevergradSearch(optimizer=ng.optimizers.PSO) | ||
analysis = self._testConvergence(searcher, patience=50, top=5) | ||
|
||
assert math.isclose(analysis.best_config["x"], 0, abs_tol=1e-3) | ||
|
||
def testConvergenceOptuna(self): | ||
from ray.tune.suggest.optuna import OptunaSearch | ||
|
||
np.random.seed(0) | ||
searcher = OptunaSearch() | ||
analysis = self._testConvergence( | ||
searcher, | ||
top=3, | ||
) | ||
|
||
# This assertion is much weaker than in the BO case, but TPE | ||
# don't converge too close. It is still unlikely to get to this | ||
# tolerance with random search (~0.01% chance) | ||
assert len(analysis.trials) < 100 | ||
assert math.isclose(analysis.best_config["x"], 0, abs_tol=1e-2) | ||
|
||
def testConvergenceSkOpt(self): | ||
from ray.tune.suggest.skopt import SkOptSearch | ||
|
||
np.random.seed(0) | ||
searcher = SkOptSearch() | ||
analysis = self._testConvergence(searcher) | ||
|
||
assert len(analysis.trials) < 100 | ||
assert math.isclose(analysis.best_config["x"], 0, abs_tol=1e-3) | ||
|
||
def testConvergenceZoopt(self): | ||
from ray.tune.suggest.zoopt import ZOOptSearch | ||
|
||
np.random.seed(0) | ||
searcher = ZOOptSearch(budget=100) | ||
analysis = self._testConvergence(searcher) | ||
|
||
assert len(analysis.trials) < 100 | ||
assert math.isclose(analysis.best_config["x"], 0, abs_tol=1e-3) | ||
|
||
|
||
if __name__ == "__main__": | ||
import pytest | ||
import sys | ||
sys.exit(pytest.main(["-v", __file__])) |
56 changes: 0 additions & 56 deletions
56
python/ray/tune/tests/test_convergence_gaussian_process.py
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we add a hex to differentiate among different Tune runs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just the prefix here - and it's used to identify leftover placement groups to remove before starting a new Tune run. It thus has to be constant across runs, otherwise removal wouldn't make sense. The trial PGs are actually using unique hex identifiers.
I guess one possibility would be to create a hex, store it in a global variable, and re-use this for sequential runs. Effectively this would mean the auto-removal process will only be triggered for sequential runs (such as in our tests). Parallel trials in different remote functions would work out of the box. Parallel trials using shared global state (threads?) would still interfere, but they do this right now, too.
Hm, this might be a good idea, I'll think about it a bit more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I ended up implementing this - I'll see if the tests pass, but the examples in the issues run for me without problems (other than setting a separate logdir)