Repository navigation
Copy non-contiguous inputs before launching restraint Triton kernels - #114
Merged
Merged
Conversation
The CUDA restraint kernels address their inputs as packed row-major arrays and ignore strides, so a strided coordinate view (e.g. one member of a NumPy-backed ensemble) was read as the wrong atoms and scored without error. Each Triton entry point now passes the tensors it indexes through targets._dispatch.dense(), a no-op for contiguous inputs. The fix lives in the autograd Functions rather than the *_math dispatchers because the torsion target and riding-hydrogen placement call the Triton wrappers directly. Covers bond, angle, chiral, both torsion kernels, planarity, Ramachandran, non-bonded, SIMU and riding-hydrogen placement. Adds a target-level test on 1DAW comparing loss and gradient for packed vs strided inputs on CPU and, where present, CUDA. Fixes #113 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018KZnrXKG1CGtWxeMnQ9C4Z
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Owner
Author
|
GPU tests pass on an A100 (gmerlin7, gpu106, driver 610.57.04) at 74cd191.
Environment: Python 3.11, torch 2.13.0+cu126, triton 3.7.1. torchref-kernels 0.2.1 was built from this branch's |
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.
Fixes #113.
Problem
The CUDA (Triton) restraint kernels read their inputs at fixed offsets (
xyz_ptr + i * 3 + c,idx_ptr + offs * 2 + k) and ignore strides. A strided coordinate view, such as one member of a NumPy-backed(n_members, n_atoms, 3)ensemble, is read as the wrong atoms. The kernel raises no error and returns a wrong loss and gradient. The issue reportsbond_math, but the same pattern is in every restraint Triton wrapper, and it also affects index, reference and sigma tensors.Fix
torchref.base.targets._dispatch.dense(*tensors). It returns contiguous tensors unchanged and copies the rest.autograd.Function.forwardcalls it on the tensors its kernel indexes, before the launch and beforectx.save_for_backward, so the backward pass reads the same packed copies.xray_ls,xray_nlland direct summation already called.contiguous().*_mathdispatchers becauseTorsionTarget(refinement/targets/geometry/torsions.py) andplace_riding_hydrogens(topology/riding.py) call the Triton functions directly.triton/bond.pyclaiming the stride problem was handled inMixedTensor.__init__. Added the layout rule to the Triton package docstring and one line to the changelog under 0.8.1, "Fixes that change results".Cost
model.xyz(),model.adp()and the restraint tensors are all contiguous. Checking 4 tensors takes about 0.7 µs of Python, with no kernel launch and no GPU→CPU sync.Making the kernels read strides would avoid the copy, but it means changing about 20 forward and backward kernels for little gain.
Tests
tests/unit/refinement/test_target_input_layout.py:bond_mathwith strided index, reference and sigma tensors.@pytest.mark.cuda, so--run-cudaturns a missing GPU into a failure instead of a skip.Results:
pytest tests/unit -m "gpu or cuda" --run-cudagives 269 passed and 34 skipped; none of the skips is a missing device. Details in this comment.TRITON_INTERPRET=1on CPU. Without this change, a strided view gave the wrong value and gradient for all three; with it, they match the packed result.Generated by Claude Code