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 missing call to untoggle_optimizer when accumulating gradients #8284

Merged
merged 6 commits into from
Jul 5, 2021

Conversation

awaelchli
Copy link
Contributor

@awaelchli awaelchli commented Jul 5, 2021

What does this PR do?

Fixes #8281

The training loop toggles the optimizers in case there are mutliple, and untoggles after the optimizer step is completed.
However, the untoggle is missing during the accumulation phase, and it is certainly necessary.

Every toggle_optimizer() call needs a matching untoggle_optimizer() call.

Test fails on master

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 🙃

@awaelchli awaelchli added the bug Something isn't working label Jul 5, 2021
@awaelchli awaelchli added this to the v1.3.x milestone Jul 5, 2021
@codecov
Copy link

codecov bot commented Jul 5, 2021

Codecov Report

Merging #8284 (43c7fb0) into master (ea5cfd2) will decrease coverage by 5%.
The diff coverage is 100%.

@@           Coverage Diff           @@
##           master   #8284    +/-   ##
=======================================
- Coverage      93%     88%    -5%     
=======================================
  Files         212     212            
  Lines       13716   13729    +13     
=======================================
- Hits        12747   12075   -672     
- Misses        969    1654   +685     

@awaelchli awaelchli marked this pull request as ready for review July 5, 2021 10:39
Copy link
Contributor

@carmocca carmocca left a comment

Choose a reason for hiding this comment

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

Is the milestone correct? Is this also an issue in the bug-fix branch or just after the loop refactor?

edit: nvm saw the original reported issue is for 1.3.7

@ethanwharris ethanwharris enabled auto-merge (squash) July 5, 2021 11:47
@ethanwharris ethanwharris merged commit ced2c94 into master Jul 5, 2021
@ethanwharris ethanwharris deleted the bugfix/untoggle branch July 5, 2021 11:59
@bmahlbrand
Copy link
Contributor

bmahlbrand commented Jul 10, 2021

I know this is closed, but I just pulled master, and tried testing w/3 optimizers instead of 2 (this problem is fixed when I only have 2) and the issue persists.

Check this out: #8365

@@ -185,20 +185,17 @@ def _run_optimization(
else:
if self.trainer.lightning_module.automatic_optimization:
self._optimizer_step(optimizer, opt_idx, batch_idx, closure)
if len(self.trainer.optimizers) > 1:
Copy link
Contributor

Choose a reason for hiding this comment

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

This assumes len() == 2, consider 3 optimizers - it should be a cyclic toggle instead of boolean

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn
5 participants