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

[Tune] Fix docstring failures #32484

Merged
merged 3 commits into from
Feb 14, 2023
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
1 change: 1 addition & 0 deletions python/ray/air/checkpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ def from_checkpoint(cls, other: "Checkpoint") -> "Checkpoint":
generic :py:class:`Checkpoint` object.

Examples:

>>> result = TorchTrainer.fit(...) # doctest: +SKIP
>>> checkpoint = TorchCheckpoint.from_checkpoint(result.checkpoint) # doctest: +SKIP # noqa: E501
>>> model = checkpoint.get_model() # doctest: +SKIP
Expand Down
58 changes: 39 additions & 19 deletions python/ray/tune/stopper/stopper.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import abc
from typing import Any, Dict

from ray.util.annotations import PublicAPI

Expand All @@ -15,34 +16,42 @@ class Stopper(abc.ABC):

>>> import time
>>> from ray import air, tune
>>> from ray.air import session
>>> from ray.tune import Stopper
>>>
>>> class TimeStopper(Stopper):
... def __init__(self):
... self._start = time.time()
... self._deadline = 5
... self._deadline = 5 # Stop all trials after 5 seconds
...
... def __call__(self, trial_id, result):
... return False
...
... def stop_all(self):
... return time.time() - self._start > self._deadline
>>>
...
>>> def train_fn(config):
... for i in range(100):
... time.sleep(1)
... session.report({"iter": i})
...
>>> tuner = tune.Tuner(
... tune.Trainable,
... tune_config=tune.TuneConfig(num_samples=200),
... run_config=air.RunConfig(stop=TimeStopper())
... train_fn,
... tune_config=tune.TuneConfig(num_samples=2),
... run_config=air.RunConfig(stop=TimeStopper()),
... )
>>> tuner.fit()
== Status ==...
>>> print("[ignore]"); result_grid = tuner.fit() # doctest: +ELLIPSIS
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any other way of doing this? This was the only workaround I could find :(

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess lambda config: {"metric": 1} or something would work, but also happy with this. LMK what you think

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, for the training function, I wanted it to actually show the stopper being used -- maybe I'll actually reduce this to 1 second - 5 is a bit excessive.

The print("[ignore]") workaround to catch the Tune output is what I'm wondering about.

[ignore]...
>>> all(result.metrics["time_total_s"] < 6 for result in result_grid)
True

"""

def __call__(self, trial_id, result):
def __call__(self, trial_id: str, result: Dict[str, Any]) -> bool:
"""Returns true if the trial should be terminated given the result."""
raise NotImplementedError

def stop_all(self):
def stop_all(self) -> bool:
"""Returns true if the experiment should be terminated."""
raise NotImplementedError

Expand All @@ -56,28 +65,39 @@ class CombinedStopper(Stopper):

Examples:

>>> from ray.tune.stopper import (CombinedStopper,
... MaximumIterationStopper, TrialPlateauStopper)
>>> import numpy as np
>>> from ray import air, tune
>>> from ray.air import session
>>> from ray.tune.stopper import (
... CombinedStopper,
... MaximumIterationStopper,
... TrialPlateauStopper,
... )
>>>
>>> stopper = CombinedStopper(
... MaximumIterationStopper(max_iter=20),
... TrialPlateauStopper(metric="my_metric")
... TrialPlateauStopper(metric="my_metric"),
... )
>>>
>>> def train_fn(config):
... for i in range(25):
... session.report({"my_metric": np.random.normal(0, 1 - i / 25)})
...
>>> tuner = tune.Tuner(
... tune.Trainable,
... run_config=air.RunConfig(stop=stopper)
... train_fn,
... run_config=air.RunConfig(stop=stopper),
... )
>>> tuner.fit()
== Status ==...
>>> print("[ignore]"); result_grid = tuner.fit() # doctest: +ELLIPSIS
[ignore]...
>>> all(result.metrics["training_iteration"] <= 20 for result in result_grid)
True

"""

def __init__(self, *stoppers: Stopper):
self._stoppers = stoppers

def __call__(self, trial_id, result):
def __call__(self, trial_id: str, result: Dict[str, Any]) -> bool:
return any(s(trial_id, result) for s in self._stoppers)

def stop_all(self):
def stop_all(self) -> bool:
return any(s.stop_all() for s in self._stoppers)