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

Explicitly enable grad in closure #18268

Merged
merged 11 commits into from
Aug 10, 2023
Merged

Conversation

0x404
Copy link
Contributor

@0x404 0x404 commented Aug 10, 2023

What does this PR do?

When using optimizers from certain third-party libraries (such as Hugging Face's AdamW), their optimizer.step is decorated with torch.no_grad. However, Lightning encapsulates hooks like training_step and backward within a closure, which is executed during optimizer.step. This results in calculations within training_step that rely on gradients not having access to gradients.

To address this issue, the Closure.closure is decorated with torch.enable_grad. This ensures that gradients can be computed correctly within optimizer.step, while the remaining parts of the optimizer can still benefit from the no_grad context.

Fixes #18254

Before submitting
  • Was this discussed/agreed 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 minor internal changes/refactors)

PR review

Anyone in the community is welcome to review the PR.
Before you start reviewing, make sure you have read the review guidelines. In short, see the following bullet-list:

Reviewer checklist
  • 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

@github-actions github-actions bot added the pl Generic label for PyTorch Lightning package label Aug 10, 2023
@0x404 0x404 changed the title fix "no grad" error when using optimizer with @torch.no_grad step fix no grad error when using optimizer with @torch.no_grad step Aug 10, 2023
@awaelchli awaelchli changed the title fix no grad error when using optimizer with @torch.no_grad step Explicitly enable grad in closure Aug 10, 2023
@awaelchli awaelchli added optimization bug Something isn't working community This PR is from the community labels Aug 10, 2023
@awaelchli awaelchli added this to the 2.0.x milestone Aug 10, 2023
@Borda Borda requested a review from awaelchli August 10, 2023 08:25
@mergify mergify bot added the ready PRs ready to be merged label Aug 10, 2023
@awaelchli awaelchli merged commit b88b8b3 into Lightning-AI:master Aug 10, 2023
@awaelchli
Copy link
Contributor

Thanks @0x404 for jumping on this so quickly. Great improvement.

Borda pushed a commit that referenced this pull request Aug 14, 2023
Co-authored-by: awaelchli <[email protected]>
(cherry picked from commit b88b8b3)
lexierule pushed a commit that referenced this pull request Aug 14, 2023
Co-authored-by: awaelchli <[email protected]>
(cherry picked from commit b88b8b3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working community This PR is from the community optimization pl Generic label for PyTorch Lightning package ready PRs ready to be merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

"element 0 of tensors does not require grad and does not have a grad_fn" when using AdamW from Hugging Face
4 participants