Update _compute_pairwise_periodic to allow double backward of the energies#151
Merged
Update _compute_pairwise_periodic to allow double backward of the energies#151
Conversation
lilyminium
approved these changes
Mar 9, 2026
Contributor
lilyminium
left a comment
There was a problem hiding this comment.
(blocking) Thanks @JMorado! The changes to the code look good to me by eye. Could you please add a test specifically that resolves the issue in #149? e.g.
def test_compute_pairwise_periodic_double_backward():
conformer = torch.tensor(..., requires_grad=True)
box_vectors = torch.tensor(...)
cutoff = torch.tensor(...)
pairwise = _compute_pairwise_periodic(conformer, box_vectors, cutoff)
# first backward (forces)
grad = torch.autograd.grad(pairwise.distances.sum(), conformer, create_graph=True)[0]
# second backward
grad.sum().backward()I've approved so after you've added the test please feel free to merge -- thanks!
Collaborator
Author
|
Thanks @lilyminium! The tricky bit is that this issue only occurs on CUDA. Would it still be worth adding it even though, as far as I can tell, the CI won’t test it? |
Contributor
|
Ah... yes, that's a good point. I think it would be still worth adding even though the issue can't be reproduced without CUDA, because with this update the test will still run through the code path. |
Collaborator
Author
|
I've added a test. All tests are passing -- I'll merge this PR. Thanks! |
This file contains hidden or 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
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.
Description
This PR is related to #149. It recomputes the distances in a way that preserves gradients, thus allowing the computation of derivatives of the energy with respect to the FF parameters.
P.S.: @lilyminium, I now understand what you meant by having to retain the
distancesto determineare_interacting. I was indeed missing that. Thanks!Status