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

Fix toggle optimizer #5775

Merged
merged 10 commits into from
Feb 4, 2021
Merged

Fix toggle optimizer #5775

merged 10 commits into from
Feb 4, 2021

Conversation

ananthsub
Copy link
Contributor

@ananthsub ananthsub commented Feb 4, 2021

What does this PR do?

Fixes #5292
This more closely follows the suggestions from the issue. Inside of toggle_optimizers, first we scan over all requires_grad values for all the parameters and store them on the lightning module., and set requires_grad=False. Then we iterate over the values of the current optimizer's parameters and set them according to the dictionary created from the initial scan.

The issue with the initial fix was multiple optimizers with shared parameters were handled incorrectly.

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
  • Check that target branch and milestone match!

Did you have fun?

Make sure you had fun coding 🙃

@codecov
Copy link

codecov bot commented Feb 4, 2021

Codecov Report

Merging #5775 (7e38e88) into master (e8c1755) will decrease coverage by 0%.
The diff coverage is 100%.

@@           Coverage Diff           @@
##           master   #5775    +/-   ##
=======================================
- Coverage      94%     93%    -0%     
=======================================
  Files         134     134            
  Lines       10053    9913   -140     
=======================================
- Hits         9409    9255   -154     
- Misses        644     658    +14     

@ananthsub ananthsub marked this pull request as ready for review February 4, 2021 03:29
@ananthsub ananthsub changed the title [draft] Fix toggle optimizer Fix toggle optimizer Feb 4, 2021
@Borda Borda added bug Something isn't working priority: 1 Medium priority task labels Feb 4, 2021
Comment on lines 1187 to 1188
if param in param_requires_grad_state:
param.requires_grad = param_requires_grad_state[param]
Copy link
Contributor

Choose a reason for hiding this comment

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

This forces all optimizers to have the same grad state for all params?

@Borda Borda added the priority: 0 High priority task label Feb 4, 2021
@Borda
Copy link
Member

Borda commented Feb 4, 2021

@ananthsub could you please add a test so we won't accidentally break it again...

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.

Hey @ananthsub,

I apologise this part wasn't better tested and I introduced a critical bug.

I think there were still a bug in the code with the missing continue.

I have added a test with 3 optimizers.

Would you mind to check if everything works fine for you ?

Best,
T.C

pytorch_lightning/core/lightning.py Outdated Show resolved Hide resolved
@pep8speaks
Copy link

pep8speaks commented Feb 4, 2021

Hello @ananthsub! Thanks for updating this PR.

There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻

Comment last updated at 2021-02-04 20:27:46 UTC

@tchaton tchaton removed the priority: 1 Medium priority task label Feb 4, 2021
@carmocca carmocca added this to the 1.2 milestone Feb 4, 2021
Copy link
Contributor

@rohitgr7 rohitgr7 left a comment

Choose a reason for hiding this comment

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

nice!

pytorch_lightning/core/lightning.py Outdated Show resolved Hide resolved
pytorch_lightning/core/lightning.py Outdated Show resolved Hide resolved
pytorch_lightning/core/lightning.py Show resolved Hide resolved
tests/core/test_lightning_module.py Outdated Show resolved Hide resolved
tests/core/test_lightning_module.py Outdated Show resolved Hide resolved
tests/core/test_lightning_module.py Outdated Show resolved Hide resolved
@rohitgr7 rohitgr7 modified the milestones: 1.2, 1.1.x Feb 4, 2021
Co-authored-by: Rohit Gupta <rohitgr1998@gmail.com>
@ananthsub
Copy link
Contributor Author

thanks @tchaton for the test cases - confirming that this patch works for me

@tchaton tchaton merged commit 0b7f5a8 into Lightning-AI:master Feb 4, 2021
tchaton added a commit that referenced this pull request Feb 5, 2021
* Update lightning.py

* update changelog

* add a 3 optimizer test

* resolve flake8

* remove extra code

* typo

* resolve typo

* Apply suggestions from code review

Co-authored-by: Rohit Gupta <rohitgr1998@gmail.com>

Co-authored-by: tchaton <thomas@grid.ai>
Co-authored-by: Rohit Gupta <rohitgr1998@gmail.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Borda pushed a commit that referenced this pull request Feb 5, 2021
* Update lightning.py

* update changelog

* add a 3 optimizer test

* resolve flake8

* remove extra code

* typo

* resolve typo

* Apply suggestions from code review

Co-authored-by: Rohit Gupta <rohitgr1998@gmail.com>

Co-authored-by: tchaton <thomas@grid.ai>
Co-authored-by: Rohit Gupta <rohitgr1998@gmail.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Borda pushed a commit that referenced this pull request Feb 5, 2021
* Update lightning.py

* update changelog

* add a 3 optimizer test

* resolve flake8

* remove extra code

* typo

* resolve typo

* Apply suggestions from code review

Co-authored-by: Rohit Gupta <rohitgr1998@gmail.com>

Co-authored-by: tchaton <thomas@grid.ai>
Co-authored-by: Rohit Gupta <rohitgr1998@gmail.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working priority: 0 High priority task
Projects
None yet
Development

Successfully merging this pull request may close these issues.

For multiple optimizers, model.toggle_optimizer() is setting the requires_grad=True for all the params.
8 participants