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

[bugfix] Resolve bug with multiple optimizers and toggle. #5574

Merged
merged 22 commits into from
Jan 25, 2021
Merged

Conversation

tchaton
Copy link
Contributor

@tchaton tchaton commented Jan 19, 2021

What does this PR do?

In case of multiple optimizers, toggle optimizer was called.
It would lose user requieres_grad state by setting everything to True or False.

This is erroneous in case there are any parameters which are explicitly kept non-trainable.

Fixes #5292 #5642

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 🙃

@pep8speaks
Copy link

pep8speaks commented Jan 19, 2021

Hello @tchaton! Thanks for updating this PR.

Line 183:121: E501 line too long (150 > 120 characters)

Comment last updated at 2021-01-25 20:48:57 UTC

@tchaton tchaton self-assigned this Jan 19, 2021
@tchaton tchaton added this to the 1.1.x milestone Jan 19, 2021
@tchaton tchaton added bug Something isn't working priority: 1 Medium priority task labels Jan 19, 2021
pytorch_lightning/core/lightning.py Outdated Show resolved Hide resolved
@@ -1166,16 +1166,44 @@ def toggle_optimizer(self, optimizer: Optimizer, optimizer_idx: int):

Copy link
Contributor

Choose a reason for hiding this comment

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

maybe just use the opt_idx to get the optimizer down there... just to unify the arguments with untoggle_optimizer. Either way is fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds great ! I will do so.

Copy link
Contributor

Choose a reason for hiding this comment

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

Do you plan to do it in this PR?

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

@codecov
Copy link

codecov bot commented Jan 19, 2021

Codecov Report

Merging #5574 (9f11c76) into master (e87424a) will decrease coverage by 0%.
The diff coverage is 100%.

@@          Coverage Diff           @@
##           master   #5574   +/-   ##
======================================
- Coverage      93%     93%   -0%     
======================================
  Files         135     135           
  Lines       10034   10051   +17     
======================================
+ Hits         9369    9383   +14     
- Misses        665     668    +3     

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
pytorch_lightning/core/lightning.py Show resolved Hide resolved
tchaton and others added 2 commits January 19, 2021 19:32
Co-authored-by: Rohit Gupta <rohitgr1998@gmail.com>
@tchaton tchaton enabled auto-merge (squash) January 21, 2021 08:13
CHANGELOG.md Outdated Show resolved Hide resolved
@tchaton tchaton added the ready PRs ready to be merged label Jan 25, 2021
@mergify mergify bot removed the has conflicts label Jan 25, 2021
@tchaton tchaton merged commit c76cc23 into master Jan 25, 2021
@tchaton tchaton deleted the bugfix/toggle branch January 25, 2021 23:45
Borda pushed a commit that referenced this pull request Feb 4, 2021
* fix toggle_optimizer

* update doc

* resolve bug

* update

* Update pytorch_lightning/core/lightning.py

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

* update on comments

* update on comments

* update

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

(cherry picked from commit c76cc23)
Borda pushed a commit that referenced this pull request Feb 4, 2021
* fix toggle_optimizer

* update doc

* resolve bug

* update

* Update pytorch_lightning/core/lightning.py

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

* update on comments

* update on comments

* update

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

(cherry picked from commit c76cc23)
Borda pushed a commit that referenced this pull request Feb 4, 2021
* fix toggle_optimizer

* update doc

* resolve bug

* update

* Update pytorch_lightning/core/lightning.py

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

* update on comments

* update on comments

* update

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

(cherry picked from commit c76cc23)
Borda pushed a commit that referenced this pull request Feb 4, 2021
* fix toggle_optimizer

* update doc

* resolve bug

* update

* Update pytorch_lightning/core/lightning.py

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

* update on comments

* update on comments

* update

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

(cherry picked from commit c76cc23)
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: 1 Medium priority task ready PRs ready to be merged
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.
10 participants