-
Notifications
You must be signed in to change notification settings - Fork 5.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
[Tune] [Doc] Tune checkpointing and Tuner restore docfix #29411
Merged
richardliaw
merged 15 commits into
ray-project:master
from
justinvyu:tuner_restore_docfix
Oct 27, 2022
Merged
Changes from 6 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
bdc8ea7
Clarify Tuner() vs. Tuner.restore behavior and provide more API ref l…
justinvyu 6cca1c8
Merge branch 'master' of https://github.com/ray-project/ray into tune…
justinvyu fc4dab7
Fix checkpoint config arg names
justinvyu 9ea4298
Fix Trainable class docstrings
justinvyu 7157941
Add checkpointing examples in Tune checkpointing guide
justinvyu 2056287
Move code to a runnable file in doc_code, address comments
justinvyu 3e56923
Exclude doc code from linting to allow imports in each code block
justinvyu c046a12
Merge branch 'master' of https://github.com/ray-project/ray into tune…
justinvyu 26ac285
Remove trailing space added by autoformatting
justinvyu 6927410
Reference Trainable checkpointing examples instead of duplicating
justinvyu 7f96c0d
Merge branch 'master' of https://github.com/ray-project/ray into tune…
justinvyu 444b8e6
Add back final metric return docs for function api
justinvyu e41f2bf
Add table comparison of Function/Class APIs
justinvyu d0def39
Merge branch 'master' of https://github.com/ray-project/ray into tune…
justinvyu 7f6876f
Disable formatting for single function to avoid extra lines
justinvyu 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
9 changes: 9 additions & 0 deletions
9
doc/source/tune/api_docs/checkpointing/class-checkpointing.rst
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,9 @@ | ||
Class API Checkpointing | ||
~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
You can also implement checkpoint/restore using the Trainable Class API: | ||
|
||
.. literalinclude:: /tune/doc_code/trainable.py | ||
:language: python | ||
:start-after: __class_api_checkpointing_start__ | ||
:end-before: __class_api_checkpointing_end__ |
17 changes: 17 additions & 0 deletions
17
doc/source/tune/api_docs/checkpointing/function-checkpointing.rst
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,17 @@ | ||
Function API Checkpointing | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
Many Tune features rely on checkpointing, including the usage of certain Trial Schedulers and fault tolerance. | ||
You can save and load checkpoints in Ray Tune in the following manner: | ||
|
||
.. literalinclude:: /tune/doc_code/trainable.py | ||
:language: python | ||
:start-after: __function_api_checkpointing_start__ | ||
:end-before: __function_api_checkpointing_end__ | ||
|
||
.. note:: ``checkpoint_frequency`` and ``checkpoint_at_end`` will not work with Function API checkpointing. | ||
|
||
In this example, checkpoints will be saved by training iteration to ``<local_dir>/<exp_name>/trial_name/checkpoint_<step>``. | ||
|
||
Tune also may copy or move checkpoints during the course of tuning. For this purpose, | ||
it is important not to depend on absolute paths in the implementation of ``save``. |
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,138 @@ | ||
# __class_api_checkpointing_start__ | ||
import os | ||
import torch | ||
from torch import nn | ||
|
||
from ray import air, tune | ||
|
||
|
||
class MyTrainableClass(tune.Trainable): | ||
def setup(self, config): | ||
self.model = nn.Sequential( | ||
nn.Linear(config.get("input_size", 32), 32), nn.ReLU(), nn.Linear(32, 10) | ||
) | ||
|
||
def step(self): | ||
return {} | ||
|
||
def save_checkpoint(self, tmp_checkpoint_dir): | ||
checkpoint_path = os.path.join(tmp_checkpoint_dir, "model.pth") | ||
torch.save(self.model.state_dict(), checkpoint_path) | ||
return tmp_checkpoint_dir | ||
|
||
def load_checkpoint(self, tmp_checkpoint_dir): | ||
checkpoint_path = os.path.join(tmp_checkpoint_dir, "model.pth") | ||
self.model.load_state_dict(torch.load(checkpoint_path)) | ||
|
||
|
||
tuner = tune.Tuner( | ||
MyTrainableClass, | ||
param_space={"input_size": 64}, | ||
run_config=air.RunConfig( | ||
stop={"training_iteration": 2}, | ||
checkpoint_config=air.CheckpointConfig(checkpoint_frequency=2), | ||
), | ||
) | ||
tuner.fit() | ||
# __class_api_checkpointing_end__ | ||
|
||
# __function_api_checkpointing_start__ | ||
from ray import tune | ||
from ray.air import session | ||
from ray.air.checkpoint import Checkpoint | ||
|
||
|
||
def train_func(config): | ||
epochs = config.get("epochs", 2) | ||
start = 0 | ||
loaded_checkpoint = session.get_checkpoint() | ||
if loaded_checkpoint: | ||
last_step = loaded_checkpoint.to_dict()["step"] | ||
start = last_step + 1 | ||
|
||
for step in range(start, epochs): | ||
# Model training here | ||
# ... | ||
|
||
# Report metrics and save a checkpoint | ||
metrics = {"metric": "my_metric"} | ||
checkpoint = Checkpoint.from_dict({"step": step}) | ||
session.report(metrics, checkpoint=checkpoint) | ||
|
||
|
||
tuner = tune.Tuner(train_func) | ||
results = tuner.fit() | ||
# __function_api_checkpointing_end__ | ||
|
||
|
||
# __example_objective_start__ | ||
def objective(x, a, b): | ||
return a * (x ** 0.5) + b | ||
|
||
|
||
# __example_objective_end__ | ||
|
||
# __function_api_report_intermediate_metrics_start__ | ||
from ray import tune | ||
from ray.air import session | ||
|
||
|
||
def trainable(config: dict): | ||
intermediate_score = 0 | ||
for x in range(20): | ||
intermediate_score = objective(x, config["a"], config["b"]) | ||
session.report({"score": intermediate_score}) # This sends the score to Tune. | ||
|
||
|
||
tuner = tune.Tuner(trainable, param_space={"a": 2, "b": 4}) | ||
results = tuner.fit() | ||
# __function_api_report_intermediate_metrics_end__ | ||
|
||
# __function_api_report_final_metrics_start__ | ||
from ray import tune | ||
from ray.air import session | ||
|
||
|
||
def trainable(config: dict): | ||
final_score = 0 | ||
for x in range(20): | ||
final_score = objective(x, config["a"], config["b"]) | ||
|
||
session.report({"score": final_score}) # This sends the score to Tune. | ||
|
||
|
||
tuner = tune.Tuner(trainable, param_space={"a": 2, "b": 4}) | ||
results = tuner.fit() | ||
# __function_api_report_final_metrics_end__ | ||
|
||
# __class_api_example_start__ | ||
from ray import air, tune | ||
|
||
|
||
class Trainable(tune.Trainable): | ||
def setup(self, config: dict): | ||
# config (dict): A dict of hyperparameters | ||
self.x = 0 | ||
self.a = config["a"] | ||
self.b = config["b"] | ||
|
||
def step(self): # This is called iteratively. | ||
score = objective(self.x, self.a, self.b) | ||
self.x += 1 | ||
return {"score": score} | ||
|
||
|
||
tuner = tune.Tuner( | ||
Trainable, | ||
run_config=air.RunConfig( | ||
# Train for 20 steps | ||
stop={"training_iteration": 20}, | ||
checkpoint_config=air.CheckpointConfig( | ||
# We haven't implemented checkpointing yet. See below! | ||
checkpoint_at_end=False | ||
), | ||
), | ||
param_space={"a": 2, "b": 4}, | ||
) | ||
results = tuner.fit() | ||
# __class_api_example_end__ |
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.
Why break this out into a separate file?
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 reuse this section in the Tune "working with checkpoints" user guide, since that's where I would intuitively look for an example of how to actually checkpoint. I've commented where that is below.
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 you instead link it, instead of re-rendering it in two places?
Otherwise for example, the search results are going to get cluttered.
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.
So concretely, don't break it out into a separate file, put it in the "working with checkpoints" part, and use a relative reference here linking to that "working with checkpoints" section