-
Notifications
You must be signed in to change notification settings - Fork 27.8k
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
Refactor hyperparameter search backends #24384
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
280f81b
Refactor hyperparameter search backends
alexmojaki 89018bc
Simpler refactoring without abstract base class
alexmojaki 57a76b8
black
alexmojaki 010d333
review comments:
alexmojaki 758e94a
review comments: safer bool checking, log multiple available backends
alexmojaki 05fb506
test ALL_HYPERPARAMETER_SEARCH_BACKENDS vs HPSearchBackend in unit te…
alexmojaki 2aca068
copyright
alexmojaki 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
from .integrations import ( | ||
is_optuna_available, | ||
is_ray_available, | ||
is_sigopt_available, | ||
is_wandb_available, | ||
run_hp_search_optuna, | ||
run_hp_search_ray, | ||
run_hp_search_sigopt, | ||
run_hp_search_wandb, | ||
) | ||
from .trainer_utils import ( | ||
HPSearchBackend, | ||
default_hp_space_optuna, | ||
default_hp_space_ray, | ||
default_hp_space_sigopt, | ||
default_hp_space_wandb, | ||
) | ||
from .utils import logging | ||
|
||
|
||
logger = logging.get_logger(__name__) | ||
|
||
|
||
class HyperParamSearchBackendBase: | ||
name: str | ||
pip_package: str = None | ||
|
||
def is_available(self): | ||
raise NotImplementedError | ||
|
||
def run(self, trainer, n_trials: int, direction: str, **kwargs): | ||
raise NotImplementedError | ||
|
||
def default_hp_space(self, trial): | ||
raise NotImplementedError | ||
|
||
def ensure_available(self): | ||
if not self.is_available(): | ||
raise RuntimeError( | ||
f"You picked the {self.name} backend, but it is not installed. Run {self.pip_install()}." | ||
) | ||
|
||
@classmethod | ||
def pip_install(cls): | ||
return f"`pip install {cls.pip_package or cls.name}`" | ||
|
||
|
||
class OptunaBackend(HyperParamSearchBackendBase): | ||
name = "optuna" | ||
|
||
def is_available(self): | ||
return is_optuna_available() | ||
|
||
def run(self, trainer, n_trials: int, direction: str, **kwargs): | ||
return run_hp_search_optuna(trainer, n_trials, direction, **kwargs) | ||
|
||
def default_hp_space(self, trial): | ||
return default_hp_space_optuna(trial) | ||
|
||
|
||
class RayTuneBackend(HyperParamSearchBackendBase): | ||
name = "ray" | ||
pip_package = "'ray[tune]'" | ||
|
||
def is_available(self): | ||
return is_ray_available() | ||
|
||
def run(self, trainer, n_trials: int, direction: str, **kwargs): | ||
return run_hp_search_ray(trainer, n_trials, direction, **kwargs) | ||
|
||
def default_hp_space(self, trial): | ||
return default_hp_space_ray(trial) | ||
|
||
|
||
class SigOptBackend(HyperParamSearchBackendBase): | ||
name = "sigopt" | ||
|
||
def is_available(self): | ||
return is_sigopt_available() | ||
|
||
def run(self, trainer, n_trials: int, direction: str, **kwargs): | ||
return run_hp_search_sigopt(trainer, n_trials, direction, **kwargs) | ||
|
||
def default_hp_space(self, trial): | ||
return default_hp_space_sigopt(trial) | ||
|
||
|
||
class WandbBackend(HyperParamSearchBackendBase): | ||
name = "wandb" | ||
|
||
def is_available(self): | ||
return is_wandb_available() | ||
|
||
def run(self, trainer, n_trials: int, direction: str, **kwargs): | ||
return run_hp_search_wandb(trainer, n_trials, direction, **kwargs) | ||
|
||
def default_hp_space(self, trial): | ||
return default_hp_space_wandb(trial) | ||
|
||
|
||
ALL_HYPERPARAMETER_SEARCH_BACKENDS = { | ||
HPSearchBackend(backend.name): backend for backend in [OptunaBackend, RayTuneBackend, SigOptBackend, WandbBackend] | ||
} | ||
|
||
|
||
def default_hp_search_backend() -> str: | ||
available_backends = [backend for backend in ALL_HYPERPARAMETER_SEARCH_BACKENDS.values() if backend.is_available()] | ||
if len(available_backends) > 0: | ||
name = available_backends[0].name | ||
if len(available_backends) > 1: | ||
logger.info( | ||
f"{len(available_backends)} hyperparameter search backends available. Using {name} as the default." | ||
) | ||
return name | ||
raise RuntimeError( | ||
"No hyperparameter search backend available.\n" | ||
+ "\n".join( | ||
f" - To install {backend.name} run {backend.pip_install()}" | ||
for backend in ALL_HYPERPARAMETER_SEARCH_BACKENDS.values() | ||
) | ||
) |
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
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.
Can just add a copyright here similar to all other files in the lib (potentially switching the year to 2023 if it's not)?