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 grad getting enabled for parameters on which explicitly disabled in case of multiple optimizers #5356

Closed
wants to merge 3 commits into from

Conversation

jain-anshul
Copy link

What does this PR do?

In case of multiple optimizers, the bug causes grad to be enabled for all the parameters associated to an optimizer. This is erroneous in case there are any parameters which are explicitly kept non-trainable.

Fixed it by storing the original grad setting for all the parameters, and then setting it accordingly when that optimizer is called.

Co-authored by : @nisheethlahoti

Fixes #5292 <- this links related issue to this PR

Before submitting

  • Was this discussed/approved via a Github issue? (no need for typos and docs improvements)
  • 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 needed]?
  • Did you write any new necessary tests [no need for typos, docs]?
  • Did you verify new and existing tests pass locally with your changes?
  • If you made a notable change (that affects users), did you update the CHANGELOG?

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 are aligned!

Did you have fun?

Make sure you had fun coding 🙃

@codecov
Copy link

codecov bot commented Jan 5, 2021

Codecov Report

Merging #5356 (b046f18) into master (389186c) will decrease coverage by 0%.
The diff coverage is 100%.

@@          Coverage Diff           @@
##           master   #5356   +/-   ##
======================================
- Coverage      93%     93%   -0%     
======================================
  Files         135     134    -1     
  Lines       10005    9979   -26     
======================================
- Hits         9339    9297   -42     
- Misses        666     682   +16     

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.

Would you mind adding a test ?

@@ -112,6 +112,8 @@ def __init__(self, *args, **kwargs):
self._current_hook_fx_name = None
self._current_dataloader_idx = None

self.param_grad_dict = {}
Copy link
Contributor

Choose a reason for hiding this comment

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

Should be private.

@tchaton
Copy link
Contributor

tchaton commented Jan 19, 2021

Dear @jain-anshul @nisheethlahoti,

Thanks a lot for your work ! I gave me the base to resolve the bug.

I solved in this PR: #5574

Closing this one for now.

Best,
T.C

@tchaton tchaton closed this Jan 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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.
2 participants