Skip to content

Commit

Permalink
fix compatibility and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhylkaaa committed Dec 8, 2022
1 parent 587c509 commit f59ec7b
Show file tree
Hide file tree
Showing 17 changed files with 357 additions and 33 deletions.
41 changes: 38 additions & 3 deletions hydra/_internal/core_plugins/basic_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def setup(
self.task_function = task_function

def launch(
self, job_overrides: Union[Sequence[Sequence[str]], ExperimentSequence], initial_job_idx: int
self, job_overrides: Sequence[Sequence[str]], initial_job_idx: int
) -> Sequence[JobReturn]:
setup_globals()
assert self.hydra_context is not None
Expand All @@ -66,7 +66,42 @@ def launch(
idx = initial_job_idx + idx
lst = " ".join(filter_overrides(overrides))
log.info(f"\t#{idx} : {lst}")
print(overrides)

sweep_config = self.hydra_context.config_loader.load_sweep_config(
self.config, list(overrides)
)
with open_dict(sweep_config):
sweep_config.hydra.job.id = idx
sweep_config.hydra.job.num = idx
ret = run_job(
hydra_context=self.hydra_context,
task_function=self.task_function,
config=sweep_config,
job_dir_key="hydra.sweep.dir",
job_subdir_key="hydra.sweep.subdir",
)
runs.append(ret)
configure_log(self.config.hydra.hydra_logging, self.config.hydra.verbose)
return runs

def launch_experiment_sequence(
self, job_overrides: ExperimentSequence, initial_job_idx: int
) -> Sequence[JobReturn]:
setup_globals()
assert self.hydra_context is not None
assert self.config is not None
assert self.task_function is not None

configure_log(self.config.hydra.hydra_logging, self.config.hydra.verbose)
sweep_dir = self.config.hydra.sweep.dir
Path(str(sweep_dir)).mkdir(parents=True, exist_ok=True)
log.info(f"Launching {len(job_overrides)} jobs locally")
runs: List[JobReturn] = []
for idx, overrides in enumerate(job_overrides):
idx = initial_job_idx + idx
lst = " ".join(filter_overrides(overrides))
log.info(f"\t#{idx} : {lst}")

sweep_config = self.hydra_context.config_loader.load_sweep_config(
self.config, list(overrides)
)
Expand All @@ -84,4 +119,4 @@ def launch(
if isinstance(job_overrides, ExperimentSequence):
job_overrides.update_sequence((overrides, ret))
configure_log(self.config.hydra.hydra_logging, self.config.hydra.verbose)
return runs
return runs
15 changes: 13 additions & 2 deletions hydra/plugins/launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
Launcher plugin interface
"""
from abc import abstractmethod
from typing import Sequence, Union
from typing import Sequence

from omegaconf import DictConfig

Expand All @@ -44,10 +44,21 @@ def setup(

@abstractmethod
def launch(
self, job_overrides: Union[Sequence[Sequence[str]], ExperimentSequence], initial_job_idx: int
self, job_overrides: Sequence[Sequence[str]], initial_job_idx: int
) -> Sequence[JobReturn]:
"""
:param job_overrides: a batch of job arguments
:param initial_job_idx: Initial job idx. used by sweepers that executes several batches
"""
raise NotImplementedError()

def launch_experiment_sequence(
self, job_overrides: ExperimentSequence, initial_job_idx: int
) -> Sequence[JobReturn]:
"""
:param job_overrides: a batch of job arguments
:param initial_job_idx: Initial job idx. used by sweepers that executes several batches
"""
raise NotImplementedError(
"This launcher doesn't support launching experiment sequence."
)
2 changes: 1 addition & 1 deletion hydra/plugins/sweeper.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
Sweeper plugin interface
"""
from abc import abstractmethod
from typing import Any, List, Sequence, Optional, Dict, Tuple
from typing import Any, List, Sequence, Optional

from hydra.types import TaskFunction
from omegaconf import DictConfig
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
import logging
from typing import Any, Optional, Sequence, Union
from typing import Any, Optional, Sequence

from hydra.core.utils import JobReturn
from hydra.plugins.launcher import Launcher
Expand Down Expand Up @@ -39,7 +39,16 @@ def setup(
self.hydra_context = hydra_context

def launch(
self, job_overrides: Union[Sequence[Sequence[str]], ExperimentSequence], initial_job_idx: int
self, job_overrides: Sequence[Sequence[str]], initial_job_idx: int
) -> Sequence[JobReturn]:
from . import _core

return _core.launch(
launcher=self, job_overrides=job_overrides, initial_job_idx=initial_job_idx
)

def launch_experiment_sequence(
self, job_overrides: ExperimentSequence, initial_job_idx: int
) -> Sequence[JobReturn]:
from . import _core

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,16 @@ def setup(
self.hydra_context = hydra_context

def launch(
self, job_overrides: Union[Sequence[Sequence[str]], ExperimentSequence], initial_job_idx: int
self, job_overrides: Sequence[Sequence[str]], initial_job_idx: int
) -> Sequence[JobReturn]:
from . import _core

return _core.launch(
launcher=self, job_overrides=job_overrides, initial_job_idx=initial_job_idx
)

def launch_experiment_sequence(
self, job_overrides: ExperimentSequence, initial_job_idx: int
) -> Sequence[JobReturn]:
from . import _core

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,16 @@ def setup(
self.executor = NestablePool(**self.mp_config)

def launch(
self, job_overrides: Union[Sequence[Sequence[str]], ExperimentSequence], initial_job_idx: int
self, job_overrides: Sequence[Sequence[str]], initial_job_idx: int
) -> Sequence[JobReturn]:
from . import _core

return _core.launch(
launcher=self, job_overrides=job_overrides, initial_job_idx=initial_job_idx
)

def launch_experiment_sequence(
self, job_overrides: ExperimentSequence, initial_job_idx: int
) -> Sequence[JobReturn]:
from . import _core

Expand Down
2 changes: 1 addition & 1 deletion plugins/hydra_optuna_sweeper/example/conf/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ hydra:
study_name: sphere
storage: null
n_trials: 20
experiment_sequence: hydra_plugins.hydra_optuna_sweeper._impl.OptunaExperimentSequence
n_jobs: 1
max_failure_rate: 0.0
params:
x: range(-5.5, 5.5, step=0.5)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ hydra:
study_name: custom-search-space
storage: null
n_trials: 20
n_jobs: 1

params:
x: range(-5.5, 5.5, 0.5)
y: choice(-5, 0, 5)
# `custom_search_space` should be a dotpath pointing to a
# callable that provides search-space configuration logic:
custom_search_space: custom-search-space-objective.configure
experiment_sequence: hydra_plugins.hydra_optuna_sweeper._impl.OptunaExperimentSequence

x: 1
y: 1
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
defaults:
- override hydra/sweeper: optuna_v2
- override hydra/sweeper/sampler: tpe

hydra:
sweeper:
sampler:
seed: 123
direction: minimize
study_name: sphere
storage: null
n_trials: 20
max_failure_rate: 0.0
params:
x: range(-5.5, 5.5, step=0.5)
y: choice(-5 ,0 ,5)

x: 1
y: 1
z: 1

# if true, simulate a failure by raising an exception
error: false
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ hydra:
study_name: multi-objective
storage: null
n_trials: 20
experiment_sequence: hydra_plugins.hydra_optuna_sweeper._impl.OptunaExperimentSequence
n_jobs: 1
params:
x: range(0, 5, step=0.5)
y: range(0, 3, step=0.5)
Expand Down
18 changes: 18 additions & 0 deletions plugins/hydra_optuna_sweeper/example/sphere_sequence.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
import hydra
from omegaconf import DictConfig


@hydra.main(version_base=None, config_path="experiment-sequence-conf", config_name="config")
def sphere(cfg: DictConfig) -> float:
x: float = cfg.x
y: float = cfg.y

if cfg.get("error", False):
raise RuntimeError("cfg.error is True")

return x**2 + y**2


if __name__ == "__main__":
sphere()
Loading

0 comments on commit f59ec7b

Please sign in to comment.