Skip to content
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

Raise better error when calling Trainer.save_checkpoint without a model attached #12772

Merged
merged 4 commits into from
Apr 26, 2022

Conversation

awaelchli
Copy link
Contributor

@awaelchli awaelchli commented Apr 15, 2022

What does this PR do?

A user on Slack had a question why calling trainer.save_checkpoint fails with this error:

Traceback (most recent call last):
  File "pretrain.py", line 85, in <module>
    trainer.save_checkpoint('plain.cpkt')
  File "/home/pete/.pyenv/versions/3.8.6/envs/python38/lib/python3.8/site-packages/pytorch_lightning/trainer/trainer.py", line 2450, in save_checkpoint
    self._checkpoint_connector.save_checkpoint(filepath, weights_only=weights_only, storage_options=storage_options)
  File "/home/pete/.pyenv/versions/3.8.6/envs/python38/lib/python3.8/site-packages/pytorch_lightning/trainer/connectors/checkpoint_connector.py", line 444, in save_checkpoint
    _checkpoint = self.dump_checkpoint(weights_only)
  File "/home/pete/.pyenv/versions/3.8.6/envs/python38/lib/python3.8/site-packages/pytorch_lightning/trainer/connectors/checkpoint_connector.py", line 369, in dump_checkpoint
    "state_dict": self._get_lightning_module_state_dict(),
  File "/home/pete/.pyenv/versions/3.8.6/envs/python38/lib/python3.8/site-packages/pytorch_lightning/trainer/connectors/checkpoint_connector.py", line 458, in _get_lightning_module_state_dict
    state_dict = self.trainer.strategy.lightning_module_state_dict()
  File "/home/pete/.pyenv/versions/3.8.6/envs/python38/lib/python3.8/site-packages/pytorch_lightning/strategies/strategy.py", line 405, in lightning_module_state_dict
    return model.state_dict()
AttributeError: 'NoneType' object has no attribute 'state_dict'

This PR improves makes the error message friendlier

Before submitting

  • Was this discussed/approved via a GitHub issue? (not for typos and docs)
  • Did you read the contributor guideline, Pull Request section?
  • Did you make sure your PR does only one thing, instead of bundling different changes together?
  • Did you make sure to update the documentation with your changes? (if necessary)
  • Did you write any new necessary tests? (not for typos and docs)
  • Did you verify new and existing tests pass locally with your changes?
  • Did you update the CHANGELOG? (not for typos, docs, test updates, or internal minor changes/refactorings)

PR review

Anyone in the community is free to review the PR once the tests have passed.
Before you start reviewing make sure you have read Review guidelines. In short, see the following bullet-list:

  • Is this pull request ready for review? (if not, please submit in draft mode)
  • Check that all items from Before submitting are resolved
  • Make sure the title is self-explanatory and the description concisely explains the PR
  • Add labels and milestones (and optionally projects) to the PR so it can be classified

Did you have fun?

I made sure I had fun coding 🙃

cc @Borda @justusschock @kaushikb11 @awaelchli @ananthsub @ninginthecloud @jjenniferdai @rohitgr7 @akihironitta

@awaelchli awaelchli added feature Is an improvement or enhancement trainer labels Apr 15, 2022
@awaelchli awaelchli added this to the 1.7 milestone Apr 15, 2022
@awaelchli awaelchli marked this pull request as ready for review April 15, 2022 12:59
@carmocca carmocca added code quality and removed feature Is an improvement or enhancement labels Apr 22, 2022
@mergify mergify bot removed the has conflicts label Apr 22, 2022
@mergify mergify bot added the ready PRs ready to be merged label Apr 25, 2022
@akihironitta akihironitta enabled auto-merge (squash) April 26, 2022 05:04
Copy link
Contributor

@tchaton tchaton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat !

@akihironitta akihironitta merged commit ab60cdb into master Apr 26, 2022
@akihironitta akihironitta deleted the feature/save-checkpoint-error-message branch April 26, 2022 10:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code quality ready PRs ready to be merged trainer
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants