-
Notifications
You must be signed in to change notification settings - Fork 3.6k
[bug-fix] Trainer.test points to latest best_model_path #5161
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
33 commits
Select commit
Hold shift + click to select a range
02f5f85
resolve bug
tchaton 069dcd1
Merge branch 'master' into bugfix/5091_resume_from_checkpoint_test
tchaton b60a0ad
update code
tchaton c42149e
add set -e
tchaton 96e167e
Merge branch 'master' into bugfix/5091_resume_from_checkpoint_test
tchaton 9e433aa
Update pytorch_lightning/callbacks/model_checkpoint.py
tchaton 725d38d
Merge branch 'master' into bugfix/5091_resume_from_checkpoint_test
tchaton 17cb6a1
update test
tchaton 4106da2
Merge branch 'master' into bugfix/5091_resume_from_checkpoint_test
tchaton 014f79c
Update tests/checkpointing/test_trainer_checkpoint.py
tchaton f266c6d
Merge branch 'master' into bugfix/5091_resume_from_checkpoint_test
tchaton f09af65
Update tests/checkpointing/test_trainer_checkpoint.py
tchaton a2a9fa0
update on comments
tchaton 01aa10c
Merge branch 'master' into bugfix/5091_resume_from_checkpoint_test
tchaton 4914735
Merge branch 'master' into bugfix/5091_resume_from_checkpoint_test
tchaton bd3d2da
resolve test
tchaton 64e3b5b
convert to set
tchaton ed84588
update
tchaton 41355d5
add error triggering
tchaton 53455af
update
fa8d952
update on comments
tchaton 0cf5e5c
Merge branch 'bugfix/5091_resume_from_checkpoint_test' of https://git…
tchaton ef75de5
update
tchaton d85662d
resolve import
tchaton a09ec3d
Merge branch 'master' into bugfix/5091_resume_from_checkpoint_test
tchaton 6c4948c
update
tchaton a893c7f
Merge branch 'bugfix/5091_resume_from_checkpoint_test' of https://git…
tchaton 3197762
update
tchaton e44a328
Merge branch 'master' into bugfix/5091_resume_from_checkpoint_test
tchaton b8f64bf
Update pytorch_lightning/plugins/rpc_plugin.py
tchaton 34986bb
update
tchaton 9252a06
add _module_available
tchaton 16ccc66
Merge branch 'master' into bugfix/5091_resume_from_checkpoint_test
tchaton 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,6 +30,7 @@ steps: | |
| MKL_THREADING_LAYER: GNU | ||
|
|
||
| commands: | ||
| - set -e | ||
| - python --version | ||
| - pip --version | ||
| - nvidia-smi | ||
|
|
||
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,87 @@ | ||
| # 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 copy import deepcopy | ||
| import os | ||
|
|
||
| import torch | ||
|
|
||
| import pytorch_lightning as pl | ||
| from pytorch_lightning import seed_everything, Trainer | ||
| from pytorch_lightning.callbacks import ModelCheckpoint | ||
| from pytorch_lightning.utilities.cloud_io import load as pl_load | ||
| from tests.base import BoringModel | ||
|
|
||
|
|
||
| def test_finetuning_with_resume_from_checkpoint(tmpdir): | ||
| """ | ||
| This test validates that generated ModelCheckpoint is pointing to the right best_model_path during test | ||
| """ | ||
|
|
||
| seed_everything(3) | ||
|
|
||
| checkpoint_callback = ModelCheckpoint(monitor='val_loss', dirpath=tmpdir, filename="{epoch:02d}", save_top_k=-1) | ||
|
|
||
| class ExtendedBoringModel(BoringModel): | ||
|
|
||
| def configure_optimizers(self): | ||
| optimizer = torch.optim.SGD(self.layer.parameters(), lr=0.001) | ||
| lr_scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=1) | ||
| return [optimizer], [lr_scheduler] | ||
|
|
||
| def validation_step(self, batch, batch_idx): | ||
| output = self.layer(batch) | ||
| loss = self.loss(batch, output) | ||
| self.log("val_loss", loss, on_epoch=True, prog_bar=True) | ||
|
|
||
| model = ExtendedBoringModel() | ||
| model.validation_epoch_end = None | ||
| trainer = Trainer( | ||
| default_root_dir=tmpdir, | ||
| max_epochs=1, | ||
| limit_train_batches=12, | ||
| limit_val_batches=6, | ||
| limit_test_batches=12, | ||
| callbacks=[checkpoint_callback], | ||
| logger=False, | ||
| ) | ||
| trainer.fit(model) | ||
| assert os.listdir(tmpdir) == ['epoch=00.ckpt'] | ||
|
|
||
| best_model_paths = [checkpoint_callback.best_model_path] | ||
| results = [] | ||
|
|
||
| for idx in range(3, 6): | ||
| # load from checkpoint | ||
| trainer = pl.Trainer( | ||
| default_root_dir=tmpdir, | ||
| max_epochs=idx, | ||
| limit_train_batches=12, | ||
| limit_val_batches=12, | ||
| limit_test_batches=12, | ||
| resume_from_checkpoint=best_model_paths[-1], | ||
| progress_bar_refresh_rate=0, | ||
| ) | ||
| trainer.fit(model) | ||
| trainer.test() | ||
| results.append(deepcopy(trainer.callback_metrics)) | ||
| best_model_paths.append(trainer.checkpoint_callback.best_model_path) | ||
|
|
||
| for idx in range(len(results) - 1): | ||
| assert results[idx]["val_loss"] > results[idx + 1]["val_loss"] | ||
|
|
||
| for idx, best_model_path in enumerate(best_model_paths): | ||
| if idx == 0: | ||
| assert best_model_path.endswith(f"epoch=0{idx}.ckpt") | ||
| else: | ||
| assert f"epoch={idx + 1}" in best_model_path | ||
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 |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ | |
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # Running special tests | ||
| set -e | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what does this do?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I noticed special_tests.sh doesn't return an error when a test fails. I found set -e might. |
||
| export PL_RUNNING_SPECIAL_TESTS=1 | ||
| DEFAULTS="-m coverage run --source pytorch_lightning -a -m pytest --verbose --capture=no" | ||
| python ${DEFAULTS} tests/trainer/optimization/test_manual_optimization.py::test_step_with_optimizer_closure_with_different_frequencies_ddp | ||
|
|
||
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.