-
Notifications
You must be signed in to change notification settings - Fork 19.7k
Fix: torch layer losses keyword arguments in rematscope #21865
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
base: master
Are you sure you want to change the base?
Fix: torch layer losses keyword arguments in rematscope #21865
Conversation
Summary of ChangesHello @Abhinavexists, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request resolves an issue within the Keras Torch backend where the Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request correctly fixes an issue in the Torch backend's remat implementation where keyword arguments were being ignored. The fix introduces a wrapper function to properly handle keyword arguments with torch.utils.checkpoint.checkpoint, which is the correct approach. The accompanying new test case is comprehensive and validates the fix across different scenarios. I have one minor suggestion to improve code readability.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #21865 +/- ##
=======================================
Coverage 82.57% 82.57%
=======================================
Files 577 577
Lines 59568 59573 +5
Branches 9345 9346 +1
=======================================
+ Hits 49187 49194 +7
+ Misses 7975 7974 -1
+ Partials 2406 2405 -1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
| return torch.utils.checkpoint.checkpoint(f, *args, use_reentrant=False) | ||
| if not kwargs: | ||
| return checkpoint(f, *args, use_reentrant=False) | ||
|
|
||
| def positional_wrapper(*pos_args): | ||
| return f(*pos_args, **kwargs) | ||
|
|
||
| return checkpoint(positional_wrapper, *args, use_reentrant=False) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at the documentation, it looks like you can just do:
return torch.utils.checkpoint.checkpoint(f, *args, use_reentrant=False, **kwargs)Is that not the case?
My concern with your approach is that I think it statically binds the kwargs so they cannot be tensors.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hertschuh
PyTorch's checkpoint() doesn't support passing kwargs to the checkpointed function as i tried to use return torch.utils.checkpoint.checkpoint(f, *args, use_reentrant=False, **kwargs) because it was my initial approach but it looses there kwargs and only accept positional args.
Regarding the static binding concern: you're right that this could be problematic. Let me add a test case with tensor kwargs to verify gradient tracking works correctly.
if that works here ?
Fix: #21861
wrapped the layer call in a small function that accepts only positional arguments. That wrapper remembers the original keyword arguments.
passed this wrapper to
torch.utils.checkpoint, so replay uses positional args but the wrapper restores the missing kwargs before calling the layer.