-
Notifications
You must be signed in to change notification settings - Fork 129
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
Correctly handle memory taken by objects in loops #1250
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Currently, object adjoints copy the structure of the original variables (e.g. ``std::vector<...> _d_x(x);``). However, if this happens in a loop, the structure of the adjoint ``_d_x`` might change every iteration. In the reverse pass, we will be left with whatever adjoint was left after the last iteration. This could lead to errors if, for example, the corresponding array had more elements on some previous iteration.
… in the reverse pass. When calling ``_reverse_forward`` functions, we store the result as a reference. The problem is that the result is stored as a local variable in the forward pass and cannot be used in the reverse mode. This was left unnoticed because the memory location of the local reference was not overwritten. However, when dealing with a local variable inside a loop, it's guaranteed to be overwritten on next iterations. The solution is to store the references on a tape.
clang-tidy review says "All clean, LGTM! 👍" |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #1250 +/- ##
=======================================
Coverage 94.66% 94.67%
=======================================
Files 51 51
Lines 8910 8919 +9
=======================================
+ Hits 8435 8444 +9
Misses 475 475
|
… being used in a non-linear way.
e893479
to
a3294e3
Compare
clang-tidy review says "All clean, LGTM! 👍" |
vgvassilev
approved these changes
Feb 26, 2025
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.
LGTM!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR addresses 2 issues:
std::vector<...> _d_x(x);
). However, if this happens in a loop, the structure of the adjoint_d_x
might change every iteration. In the reverse pass, we will be left with whatever adjoint was left after the last iteration. This could lead to errors if, for example, the corresponding array had more elements on some previous iteration._reverse_forward
functions, we store the result as a reference. The problem is that the result is stored as a local variable in the forward pass and cannot be used in the reverse mode. This was left unnoticed because the memory location of the local reference was not overwritten. However, when dealing with a local variable inside a loop, it's guaranteed to be overwritten on next iterations. The solution is to store the references on a tape.This PR also introduces a test for both issues. In theory, these issues could be considered and tested separately but each one breaks other tests when being introduced on its own. This happens because those tests are currently bugged but work under specific conditions.