-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Introduce CheckpointIO Plugin #8743
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
Merged
Merged
Changes from 29 commits
Commits
Show all changes
40 commits
Select commit
Hold shift + click to select a range
a93e452
poc API
72f4dfd
Merge branch 'master' into feat/ckpt_plugin
e7d2b66
Fix up the API, unsure on connection
b41e794
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 9161980
Example API
7aa4e8c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] dffe088
Update all constructors
cacf0e5
Move towards having the checkpoint plugin not require the plugin, and…
028ac38
Remove import
99c7a46
Fix tests
3adc486
Change name
b7d5b55
Cleanups
0a0a068
Fixes/Cleanups
97fb2a2
Use property
402156e
Fixes to signature
5310a7f
Merge branch 'master' into feat/ckpt_plugin
d7f567a
Add warning for TPU plugins that they do not support custom checkpoin…
fcc24b4
Cleanup API, introduce storage options
4421276
Update signature to be more general
d84cce1
Address feedback, add test for support check
38c22a2
Merge branch 'master' into feat/ckpt_plugin
b7f37ee
Add CHANGELOG.md
49086cc
fix tests
936f65a
change name
049a676
Fix mypy
1ff0912
Reviews
1841d3b
Add ability to pass checkpoint plugin through the trainer
b909dfe
Add constraints
50b11b5
Match signature to see if mypy works
9e16e34
Address review points
642e6fa
Revert changes to typing
5c9e973
Add docs/doc strings and API
6361c87
Address feedback
c921dbb
Update pytorch_lightning/plugins/training_type/training_type_plugin.py
fd82276
Address reviews
2fc3558
Update typing
21783f6
Refactor name
3b8c3f5
Clear up signature of function; checkpoint_plugin -> checkpoint_io
9cfe98f
Slightly cleaner
8f234e0
Address reviews
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 hidden or 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 hidden or 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 hidden or 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,2 @@ | ||
from pytorch_lightning.plugins.checkpoint.checkpoint import CheckpointIOPlugin # noqa: F401 | ||
from pytorch_lightning.plugins.checkpoint.torch import TorchCheckpointIOPlugin # noqa: F401 |
This file contains hidden or 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,33 @@ | ||
from abc import ABC, abstractmethod | ||
from pathlib import Path | ||
from typing import Any, Callable, Dict, Mapping, Optional, TypeVar, Union | ||
|
||
TSaveStorageOptions = TypeVar("TSaveStorageOptions", Mapping, Callable) | ||
TLoadStorageOptions = TypeVar("TLoadStorageOptions", Mapping, Callable) | ||
SeanNaren marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
class CheckpointIOPlugin(ABC): | ||
SeanNaren marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@abstractmethod | ||
def save_checkpoint( | ||
self, checkpoint: Dict[str, Any], path: Union[str, Path], storage_options: Optional[TSaveStorageOptions] = None | ||
) -> None: | ||
"""Save model/training states as a checkpoint file through state-dump and file-write. | ||
|
||
Args: | ||
checkpoint: dict containing model and trainer state | ||
path: write-target path | ||
storage_options: Optional parameters when saving the model/training states. | ||
""" | ||
|
||
@abstractmethod | ||
def load_checkpoint( | ||
self, path: Union[str, Path], storage_options: Optional[TLoadStorageOptions] = None | ||
) -> Dict[str, Any]: | ||
""" | ||
Load checkpoint from a path when resuming or loading ckpt for test/validate/predict stages. | ||
Args: | ||
path: Path to checkpoint | ||
storage_options: Optional parameters when loading the model/training states. | ||
|
||
Returns: The loaded checkpoint. | ||
""" |
This file contains hidden or 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,27 @@ | ||
from pathlib import Path | ||
from typing import Any, Callable, Dict, Optional, Union | ||
|
||
import pytorch_lightning as pl | ||
from pytorch_lightning.plugins.checkpoint.checkpoint import CheckpointIOPlugin | ||
from pytorch_lightning.utilities import rank_zero_warn | ||
from pytorch_lightning.utilities.cloud_io import atomic_save | ||
from pytorch_lightning.utilities.cloud_io import load as pl_load | ||
|
||
|
||
class TorchCheckpointIOPlugin(CheckpointIOPlugin): | ||
def save_checkpoint( | ||
self, checkpoint: Dict[str, Any], path: Union[str, Path], storage_options: Optional[Any] = None | ||
SeanNaren marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) -> None: | ||
try: | ||
# write the checkpoint dictionary on the file | ||
atomic_save(checkpoint, path) | ||
except AttributeError as err: | ||
key = pl.LightningModule.CHECKPOINT_HYPER_PARAMS_KEY | ||
SeanNaren marked this conversation as resolved.
Show resolved
Hide resolved
|
||
checkpoint.pop(key, None) | ||
rank_zero_warn(f"Warning, `{key}` dropped from checkpoint. An attribute is not picklable: {err}") | ||
atomic_save(checkpoint, path) | ||
|
||
def load_checkpoint( | ||
self, path: Union[str, Path], storage_options: Optional[Callable] = lambda storage, loc: storage | ||
) -> Dict[str, Any]: | ||
return pl_load(path, map_location=storage_options) |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.