-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Introduce checkpoint migration #15237
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 all commits
Commits
Show all changes
49 commits
Select commit
Hold shift + click to select a range
277b0b8
migration
awaelchli cc110a3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] e13fcc6
import
awaelchli 7cb1d24
Merge remote-tracking branch 'origin/feature/migration-utils' into fe…
awaelchli 9838f00
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] ed1ab3f
refactor
awaelchli 756b2e7
protected
awaelchli d64b5ed
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 954b4d3
typo
awaelchli 0febf26
Merge remote-tracking branch 'origin/feature/migration-utils' into fe…
awaelchli 5223dff
Merge branch 'master' into feature/migration-utils
awaelchli 0f988d1
tests
awaelchli 3a6aa87
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] ac0d2fc
prune
awaelchli aec0257
Merge remote-tracking branch 'origin/feature/migration-utils' into fe…
awaelchli f7f1250
reset
awaelchli 24a5d60
wip
awaelchli c349ec7
messaging
awaelchli 0e6f867
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] cc333c6
checkpint path
awaelchli c1afc87
Merge remote-tracking branch 'origin/feature/migration-utils' into fe…
awaelchli 3734981
type
awaelchli da53672
warn
awaelchli f745259
tests
awaelchli 372dfaf
tests
awaelchli ec9b0f8
changelog
awaelchli 978b0f0
add commit info
awaelchli 815f7f6
mark index as protected
awaelchli d890069
test fix
awaelchli 62ae611
extend test to check inplace modification
awaelchli 7124604
Merge branch 'master' into feature/migration-utils
awaelchli 9ff9350
set legacy version if upgrade happened
awaelchli 947bbbe
Merge branch 'master' into feature/migration-utils
awaelchli 3e6fd84
Merge branch 'master' into feature/migration-utils
awaelchli bc032c5
Merge branch 'master' into feature/migration-utils
awaelchli 7419eca
changelog
awaelchli ccc9ba3
Merge branch 'master' into feature/migration-utils
awaelchli 173c2da
fix typing
awaelchli 70e1110
Merge branch 'master' into feature/migration-utils
awaelchli e3debf9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 47d56ca
notebook
awaelchli 4d2767e
notebook
awaelchli 4967134
Merge branch 'master' into feature/migration-utils
awaelchli 15dbfdf
fix mypy error
awaelchli 59e05b0
Update src/pytorch_lightning/utilities/migration/utils.py
awaelchli 1743f64
rename
awaelchli 82bb977
Merge branch 'feature/migration-utils' of github.com:Lightning-AI/lig…
awaelchli 40353c3
add test for legacy migration twice
awaelchli a73e89b
clarify the instructions are for Lightning developers
awaelchli 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
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 was deleted.
Oops, something went wrong.
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,16 @@ | ||
# Copyright The PyTorch Lightning team. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from pytorch_lightning.utilities.migration.utils import migrate_checkpoint # noqa: F401 | ||
from pytorch_lightning.utilities.migration.utils import pl_legacy_patch # 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,69 @@ | ||
# Copyright The PyTorch Lightning team. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
"""Contains migration functions to upgrade legacy checkpoints to the format of the current Lightning version. | ||
|
||
When Lightning loads a checkpoint, these migrations will be applied on the loaded checkpoint dictionary sequentially, | ||
see :func:`~pytorch_lightning.utilities.migration.utils.migrate_checkpoint`. | ||
|
||
For the Lightning developer: How to add a new migration? | ||
|
||
1. Create a new function with a descriptive name and docstring that explains the details of this migration. Include | ||
version information as well as the specific commit or PR where the breaking change happened. | ||
2. Add the function to the `_migration_index()` below. The key in the index is the version of Lightning in which the | ||
change happened. Any checkpoint with a version greater or equal to that version will apply the given function. | ||
Multiple migrations per version get executed in the provided list order. | ||
3. You can test the migration on a checkpoint (backup your files first) by running: | ||
|
||
cp model.ckpt model.ckpt.backup | ||
python -m pytorch_lightning.utilities.upgrade_checkpoint --file model.ckpt | ||
""" | ||
|
||
from typing import Any, Callable, Dict, List | ||
|
||
from pytorch_lightning.callbacks.early_stopping import EarlyStopping | ||
from pytorch_lightning.callbacks.model_checkpoint import ModelCheckpoint | ||
|
||
_CHECKPOINT = Dict[str, Any] | ||
|
||
|
||
def _migration_index() -> Dict[str, List[Callable[[_CHECKPOINT], _CHECKPOINT]]]: | ||
"""Migration functions returned here will get executed in the order they are listed.""" | ||
return { | ||
"0.10.0": [_migrate_model_checkpoint_early_stopping], | ||
} | ||
|
||
|
||
def _migrate_model_checkpoint_early_stopping(checkpoint: _CHECKPOINT) -> _CHECKPOINT: | ||
"""The checkpoint and early stopping keys were renamed. | ||
|
||
Version: 0.10.0 | ||
Commit: a5d1176 | ||
""" | ||
keys_mapping = { | ||
"checkpoint_callback_best_model_score": (ModelCheckpoint, "best_model_score"), | ||
"checkpoint_callback_best_model_path": (ModelCheckpoint, "best_model_path"), | ||
"checkpoint_callback_best": (ModelCheckpoint, "best_model_score"), | ||
"early_stop_callback_wait": (EarlyStopping, "wait_count"), | ||
"early_stop_callback_patience": (EarlyStopping, "patience"), | ||
} | ||
checkpoint["callbacks"] = checkpoint.get("callbacks") or {} | ||
|
||
for key, new_path in keys_mapping.items(): | ||
if key in checkpoint: | ||
value = checkpoint[key] | ||
callback_type, callback_key = new_path | ||
checkpoint["callbacks"][callback_type] = checkpoint["callbacks"].get(callback_type) or {} | ||
checkpoint["callbacks"][callback_type][callback_key] = value | ||
del checkpoint[key] | ||
return checkpoint |
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.