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 a warning when nn.Module instance is saved with save_hyperparameters() #12068

Merged
merged 9 commits into from
Mar 23, 2022

Conversation

rohitgr7
Copy link
Contributor

@rohitgr7 rohitgr7 commented Feb 23, 2022

What does this PR do?

Addresses: #11834 (comment)

nn.Modules are not hparams and when someone uses LM as systems, they might forget to ignore the arguments and they will be saved inside hparams thus will increase the size of the checkpoint.

Does your PR introduce any breaking changes? If yes, please list them.

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 list all the breaking changes introduced by this pull request?
  • Did you update the CHANGELOG? (not for typos, docs, test updates, or internal minor changes/refactorings)

PR review

Anyone in the community is welcome to review the PR.
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?

Make sure you had fun coding 🙃

cc @Borda

@rohitgr7 rohitgr7 added feature Is an improvement or enhancement argparse (removed) Related to argument parsing (argparse, Hydra, ...) labels Feb 23, 2022
@rohitgr7 rohitgr7 added this to the 1.6 milestone Feb 23, 2022
@rohitgr7 rohitgr7 changed the title Disable saving nn.Module with save_hyperparameters() Disable saving nn.Module within save_hyperparameters() Feb 23, 2022
@rohitgr7 rohitgr7 changed the title Disable saving nn.Module within save_hyperparameters() Disable saving nn.Module with save_hyperparameters() Feb 23, 2022
@rohitgr7 rohitgr7 changed the title Disable saving nn.Module with save_hyperparameters() Disable saving nn.Module with save_hyperparameters() Feb 23, 2022
pytorch_lightning/utilities/parsing.py Outdated Show resolved Hide resolved
tests/models/test_hparams.py Outdated Show resolved Hide resolved
@awaelchli
Copy link
Contributor

awaelchli commented Feb 23, 2022

It's probably ok to do this, but we should stop here. There are many other parameter type that may or may not be desired to serialize, however we can't do the guess work for everything.

Note: If we go with this PR, there is no way for the user to force what is being ignored here. I hesitate to accept here, because normally we shouldn't make such assumptions for the user (the framework should not be limiting them).

@rohitgr7
Copy link
Contributor Author

It's probably ok to do this, but we should stop here. There are many other parameter type that may or may not be desired to serialize, however we can't do the guess work for everything.

Note: If we go with this PR, there is no way for the user to force what is being ignored here. I hesitate to accept here, because normally we shouldn't make such assumptions for the user (the framework should not be limiting them).

I agree, my idea here was to avoid only nn.Modules because that's already been captured when we save state_dict inside the checkpoint. Other different types of arguments will not be captured automatically.

I was running t5-small, and it got stuck due to this, with no errors at all, so it was hard to figure out what went wrong here until I did some debugging with @krshrimali .

We can mention this inside docstrings if required.

@carmocca
Copy link
Contributor

I'm also hesitant about this.

I was running t5-small, and it got stuck due to this, with no errors at all, so it was hard to figure out what went wrong here until I did some debugging with @krshrimali .

What if we focus instead on notifying the user about the associated problems? We can suggest excluding the keys with save_hyperaprameters

@rohitgr7
Copy link
Contributor Author

I'm also hesitant about this.

I was running t5-small, and it got stuck due to this, with no errors at all, so it was hard to figure out what went wrong here until I did some debugging with @krshrimali .

What if we focus instead on notifying the user about the associated problems? We can suggest excluding the keys with save_hyperaprameters

well, that's a good suggestion. works for me since it will happen during init. If everyone agrees on this, I'll update the PR :)

@rohitgr7 rohitgr7 changed the title Disable saving nn.Module with save_hyperparameters() Raise a warning when nn.Module instance is saved with save_hyperparameters() Feb 28, 2022
@rohitgr7 rohitgr7 requested a review from krshrimali February 28, 2022 17:08
pytorch_lightning/utilities/parsing.py Outdated Show resolved Hide resolved
pytorch_lightning/utilities/parsing.py Show resolved Hide resolved
Copy link
Contributor

@krshrimali krshrimali left a comment

Choose a reason for hiding this comment

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

LGTM, thanks Rohit!

pytorch_lightning/utilities/parsing.py Show resolved Hide resolved
@mergify mergify bot added ready PRs ready to be merged and removed has conflicts labels Mar 7, 2022
@rohitgr7 rohitgr7 enabled auto-merge (squash) March 7, 2022 08:28
@mergify mergify bot removed the has conflicts label Mar 21, 2022
@rohitgr7 rohitgr7 merged commit 312c5a5 into master Mar 23, 2022
@rohitgr7 rohitgr7 deleted the ref/hparam_modules branch March 23, 2022 12:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
argparse (removed) Related to argument parsing (argparse, Hydra, ...) feature Is an improvement or enhancement ready PRs ready to be merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants