Skip to content

Copy non-contiguous inputs before launching restraint Triton kernels - #114

Merged
HatPdotS merged 1 commit into
mainfrom
fix/113-noncontiguous-triton-inputs
Nov 16, 2026
Merged

HatPdotS merged 1 commit into
mainfrom
fix/113-noncontiguous-triton-inputs

Conversation

@HatPdotS

@HatPdotS HatPdotS commented Nov 16, 2026 •

Copy link
Copy Markdown
Owner

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 reports bond_math, but the same pattern is in every restraint Triton wrapper, and it also affects index, reference and sigma tensors.

Fix

  • New helper torchref.base.targets._dispatch.dense(*tensors). It returns contiguous tensors unchanged and copies the rest.
  • Every restraint autograd.Function.forward calls it on the tensors its kernel indexes, before the launch and before ctx.save_for_backward, so the backward pass reads the same packed copies.
  • Covered: bond, angle, chiral, torsion (omega and unimodal), planarity, Ramachandran, non-bonded, SIMU and riding-hydrogen placement. xray_ls, xray_nll and direct summation already called .contiguous().
  • The fix is in the Triton wrappers rather than the *_math dispatchers because TorsionTarget (refinement/targets/geometry/torsions.py) and place_riding_hydrogens (topology/riding.py) call the Triton functions directly.
  • Removed the comment in triton/bond.py claiming the stride problem was handled in MixedTensor.__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

  • Contiguous inputs (normal refinement path): no copy. On 1DAW, 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.
  • Strided inputs: one copy of N_atoms × 3 floats per target, roughly the cost of one small kernel launch. That is far below the cost of a refinement step.

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:

  • Runs the eight geometry targets on 1DAW with its own restraints, fed packed coordinates and then a strided view of the same values. Loss and coordinate gradient must match, and the unused half of the strided base tensor must get no gradient.
  • Checks bond_math with strided index, reference and sigma tensors.
  • Checks SIMU with a strided B-factor tensor.
  • Runs on CPU and on CUDA. The CUDA cases carry @pytest.mark.cuda, so --run-cuda turns a missing GPU into a failure instead of a skip.

Results:

  • CUDA (A100, torch 2.13.0+cu126, triton 3.7.1, at 74cd191): the new test passes, 20 of 20 (10 CPU, 10 CUDA). pytest tests/unit -m "gpu or cuda" --run-cuda gives 269 passed and 34 skipped; none of the skips is a missing device. Details in this comment.
  • CI: all 13 checks pass, including MPS.
  • Triton interpreter: the bond, SIMU and planarity kernels ran under TRITON_INTERPRET=1 on 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

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
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-16T13:30:21.791565Z 74cd191 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@HatPdotS

Copy link
Copy Markdown
Owner Author

GPU tests pass on an A100 (gmerlin7, gpu106, driver 610.57.04) at 74cd191.

  • pytest tests/unit/refinement/test_target_input_layout.py --run-cuda -v: 20 passed (10 CPU, 10 CUDA). All eight geometry targets plus the strided bond_math and SIMU cases ran on CUDA.
  • pytest tests/unit -m "gpu or cuda" --run-cuda: 269 passed, 34 skipped. None of the skips is a missing device: 1 is OpenMM not installed, and 33 are tests that skip by design ("portable is the reference", double-backward variants, one empty-data case).

Environment: Python 3.11, torch 2.13.0+cu126, triton 3.7.1. torchref-kernels 0.2.1 was built from this branch's kernels/, because PyPI has only 0.2.0.

@HatPdotS
HatPdotS merged commit 8fdbf64 into main Nov 16, 2026
13 checks passed
@HatPdotS HatPdotS mentioned this pull request Nov 16, 2026
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CUDA bond_math returns wrong values for non-contiguous xyz

2 participants