tfg: avoid NaN gradients for unbounded distances - #339
Open
dajiaohuang wants to merge 1 commit into
Open
Conversation
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.
Summary
PairwiseDistancePotentialautograd pathRoot cause
Clash-only pairwise constraints use
upper = +inf. The inactive upper-bound branch previously computed(value - upper) ** 2before masking it withtorch.where. Although the forward energy remained finite, autograd propagated a NaN gradient from the infinite intermediate.Clamping the violation before squaring directly implements the helper's documented
max(0, violation)formula and avoids constructing that infinite inactive branch. This does not change public APIs or finite-bound behavior.Fixes #336.
Validation
python -m pytest tests/test_tfg_potentials.py -q(2 passed)main(1 failed, 1 passed; the real caller produced all-NaN autograd gradients)uvx pre-commit run --files protenix/tfg/potentials.py tests/test_tfg_potentials.py(all hooks passed)python -m compileall -q protenix/tfg/potentials.py tests/test_tfg_potentials.pygit diff --check upstream/main...HEADThe full test suite was not run locally because collection in the isolated Windows test environment stops on missing optional/runtime packages (
optree, Biopython, Biotite, andml_collections). The focused test imports the real RDKit-backed module and passed with RDKit 2025.09.3, PyTorch 2.12.0.dev20260408+cu128, Python 3.13.13, and CUDA available.