Skip to content
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

[air] Tuner should use run_config from Trainer per default #24079

Merged
merged 2 commits into from
Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion python/ray/tune/impl/tuner_internal.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ class TunerInternal:
tune_config: Tuning algorithm specific configs.
Refer to ray.tune.tune_config.TuneConfig for more info.
run_config: Runtime configuration that is specific to individual trials.
Refer to ray.ml.config.RunConfig for more info.
If passed, this will overwrite the run config passed to the Trainer,
if applicable. Refer to ray.ml.config.RunConfig for more info.
"""

def __init__(
Expand Down Expand Up @@ -82,6 +83,11 @@ def __init__(
if not trainable:
raise TuneError("You need to provide a trainable to tune.")

# If no run config was passed to Tuner directly, use the one from the Trainer,
# if available
if not run_config and isinstance(trainable, Trainer):
run_config = trainable.run_config

self._is_restored = False
self._trainable = trainable
self._tune_config = tune_config or TuneConfig()
Expand Down
33 changes: 20 additions & 13 deletions python/ray/tune/tests/test_tuner.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@
from ray.tune.tuner import Tuner


class DummyTrainer(Trainer):
_scaling_config_allowed_keys = [
"num_workers",
"num_cpus_per_worker",
"num_gpus_per_worker",
"additional_resources_per_worker",
"use_gpu",
"trainer_resources",
]

def training_loop(self) -> None:
raise RuntimeError("There is an error in trainer!")


class TestDatasource(Datasource):
def __init__(self, do_shuffle: bool):
self._shuffle = do_shuffle
Expand Down Expand Up @@ -174,19 +188,6 @@ def on_step_end(self, iteration, trials, **kwargs):
assert len(results) == 2

def test_tuner_trainer_fail(self):
class DummyTrainer(Trainer):
_scaling_config_allowed_keys = [
"num_workers",
"num_cpus_per_worker",
"num_gpus_per_worker",
"additional_resources_per_worker",
"use_gpu",
"trainer_resources",
]

def training_loop(self) -> None:
raise RuntimeError("There is an error in trainer!")

trainer = DummyTrainer()
param_space = {
"scaling_config": {
Expand Down Expand Up @@ -245,6 +246,12 @@ def test_tuner_with_torch_trainer(self):
results = tuner.fit()
assert len(results) == 8

def test_tuner_run_config_override(self):
trainer = DummyTrainer(run_config=RunConfig(stop={"metric": 4}))
tuner = Tuner(trainer)

assert tuner._local_tuner._run_config.stop == {"metric": 4}


if __name__ == "__main__":
import pytest
Expand Down
3 changes: 2 additions & 1 deletion python/ray/tune/tuner.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ class Tuner:
tune_config: Tuning algorithm specific configs.
Refer to ray.tune.tune_config.TuneConfig for more info.
run_config: Runtime configuration that is specific to individual trials.
Refer to ray.ml.config.RunConfig for more info.
If passed, this will overwrite the run config passed to the Trainer,
if applicable. Refer to ray.ml.config.RunConfig for more info.

Usage pattern:

Expand Down