-
Notifications
You must be signed in to change notification settings - Fork 3.6k
deprecate hpc_load() and integrate it with restore() #7955
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
15 commits
Select commit
Hold shift + click to select a range
a2560f1
deprecate
awaelchli 88f2015
test
awaelchli b0c0b07
tests
awaelchli 0f17119
ypf
awaelchli f62cd51
test hook calls
awaelchli 09dd67d
space
awaelchli 1dea5be
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 1a2c6e4
unused import
awaelchli c4e6786
make windows test good
awaelchli 37766ca
Update pytorch_lightning/trainer/connectors/checkpoint_connector.py
awaelchli 631d0eb
join os path
awaelchli fe4632b
move
awaelchli 526d3a4
mock logger
awaelchli d64a9dc
shorter message
awaelchli 017e36a
Merge branch 'master' into feature/resume-8
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 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,155 @@ | ||
| # 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. | ||
| import os | ||
| from unittest.mock import Mock | ||
|
|
||
| import torch | ||
|
|
||
| from pytorch_lightning import Trainer | ||
| from tests.helpers import BoringModel | ||
|
|
||
|
|
||
| class HPCHookdedModel(BoringModel): | ||
|
|
||
| def __init__(self): | ||
| super().__init__() | ||
| self.hpc_save_called = 0 | ||
| self.hpc_load_called = 0 | ||
|
|
||
| def on_hpc_save(self, checkpoint): | ||
| assert "state_dict" in checkpoint | ||
| self.hpc_save_called += 1 | ||
|
|
||
| def on_hpc_load(self, checkpoint): | ||
| assert "state_dict" in checkpoint | ||
| self.hpc_load_called += 1 | ||
|
|
||
|
|
||
| def test_hpc_hook_calls(tmpdir): | ||
| model = HPCHookdedModel() | ||
| trainer = Trainer( | ||
| default_root_dir=tmpdir, | ||
| max_steps=1, | ||
| checkpoint_callback=False, | ||
| logger=False, | ||
| ) | ||
| trainer.fit(model) | ||
| connector = trainer.checkpoint_connector | ||
| connector.hpc_save(tmpdir, logger=Mock()) | ||
| assert model.hpc_save_called == 1 | ||
| assert model.hpc_load_called == 0 | ||
|
|
||
| # new training run, restore from hpc checkpoint file automatically | ||
| assert set(os.listdir(tmpdir)) == {"hpc_ckpt_1.ckpt"} | ||
| trainer = Trainer( | ||
| default_root_dir=tmpdir, | ||
| max_steps=1, | ||
| checkpoint_callback=False, | ||
| logger=False, | ||
| ) | ||
| trainer.fit(model) | ||
| assert model.hpc_save_called == 1 | ||
| assert model.hpc_load_called == 1 | ||
|
|
||
|
|
||
| def test_preloaded_checkpoint_lifecycle(tmpdir): | ||
| """ Tests that the preloaded checkpoint contents gets cleared from memory when it is not required anymore. """ | ||
| model = BoringModel() | ||
| trainer = Trainer( | ||
| default_root_dir=tmpdir, | ||
| max_steps=1, | ||
| ) | ||
| trainer.fit(model) | ||
|
|
||
| connector = trainer.checkpoint_connector | ||
|
|
||
| assert not trainer.resume_from_checkpoint | ||
| assert not connector.resume_checkpoint_path | ||
| assert not connector._loaded_checkpoint | ||
|
|
||
| connector.resume_start() | ||
| assert not connector.resume_checkpoint_path | ||
| assert not connector._loaded_checkpoint | ||
| connector.resume_end() | ||
| assert not connector.resume_checkpoint_path | ||
| assert not connector._loaded_checkpoint | ||
|
|
||
| ckpt_path = trainer.checkpoint_callback.best_model_path | ||
| trainer = Trainer(default_root_dir=tmpdir, max_steps=2, resume_from_checkpoint=ckpt_path) | ||
| connector = trainer.checkpoint_connector | ||
| connector.resume_start() | ||
| assert connector.resume_checkpoint_path == ckpt_path | ||
| assert connector._loaded_checkpoint | ||
| assert isinstance(connector._loaded_checkpoint, dict) | ||
| connector.resume_end() | ||
| assert not connector.resume_checkpoint_path | ||
| assert not connector._loaded_checkpoint | ||
|
|
||
|
|
||
| def test_hpc_restore_attempt(tmpdir): | ||
| """ Test that restore() attempts to restore the hpc_ckpt with highest priority. """ | ||
| model = BoringModel() | ||
| trainer = Trainer( | ||
| default_root_dir=tmpdir, | ||
| max_steps=1, | ||
| checkpoint_callback=False, | ||
| logger=False, | ||
| ) | ||
| trainer.fit(model) | ||
|
|
||
| hpc_ckpt_path = tmpdir / "hpc_ckpt_3.ckpt" | ||
| trainer.save_checkpoint(hpc_ckpt_path) | ||
| assert os.listdir(tmpdir) == ["hpc_ckpt_3.ckpt"] | ||
|
|
||
| # set weights to zero | ||
| for param in model.parameters(): | ||
| torch.nn.init.constant_(param, 0) | ||
|
|
||
| # case 1: restore hpc first, no explicit resume path provided | ||
| trainer = Trainer( | ||
| default_root_dir=tmpdir, | ||
| max_steps=2, | ||
| checkpoint_callback=False, | ||
| logger=False, | ||
| ) | ||
| trainer.fit(model) | ||
|
|
||
| for param in model.parameters(): | ||
| assert param.abs().sum() > 0 | ||
| torch.nn.init.constant_(param, 0) | ||
|
|
||
| # case 2: explicit resume path provided, restore hpc anyway | ||
| trainer = Trainer(default_root_dir=tmpdir, max_steps=3, resume_from_checkpoint="not existing") | ||
| trainer.fit(model) | ||
|
|
||
| for param in model.parameters(): | ||
| assert param.abs().sum() > 0 | ||
|
|
||
|
|
||
| def test_hpc_max_ckpt_version(tmpdir): | ||
| """ Test that the CheckpointConnector is able to find the hpc checkpoint file with the highest version. """ | ||
| model = BoringModel() | ||
| trainer = Trainer( | ||
| default_root_dir=tmpdir, | ||
| max_steps=1, | ||
| ) | ||
| trainer.fit(model) | ||
| trainer.save_checkpoint(tmpdir / "hpc_ckpt.ckpt") | ||
| trainer.save_checkpoint(tmpdir / "hpc_ckpt_0.ckpt") | ||
| trainer.save_checkpoint(tmpdir / "hpc_ckpt_3.ckpt") | ||
| trainer.save_checkpoint(tmpdir / "hpc_ckpt_33.ckpt") | ||
|
|
||
| assert trainer.checkpoint_connector.hpc_resume_path == str(tmpdir / "hpc_ckpt_33.ckpt") | ||
| assert trainer.checkpoint_connector.max_ckpt_version_in_folder(tmpdir) == 33 | ||
| assert trainer.checkpoint_connector.max_ckpt_version_in_folder(tmpdir / "not" / "existing") is None |
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.