Skip to content

perf(md): remove host-device syncs from the MD integrators - #650

Merged
CompRhys merged 3 commits into
TorchSim:mainfrom
steffen-wedig:host_sync_removal
Nov 18, 2026
Merged

CompRhys merged 3 commits into
TorchSim:mainfrom
steffen-wedig:host_sync_removal

Conversation

@steffen-wedig

@steffen-wedig steffen-wedig commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This PR removes host-device synchronization from the MD steps following #609 and #621. I'm interested in using compiled MD integrators, and removing syncs is one prerequisite for torch.compile. Removing host syncs also speeds up eager mode.

Most of the syncs come from bincount and int(system_idx.max()), used to count systems or atoms per system. Since #621, state.n_systems comes from the cell shape, so I replaced those with buffers of size state.n_systems, the same way #609 did it for FIRE.

Changes:

  • SimState.n_atoms_per_system uses index_add into n_systems entries instead of bincount.
  • get_number_of_degrees_of_freedom only checks for non-positive DOF when there are constraints. Without constraints every system has at least one atom, so at least 3 DOF.
  • calc_kT and calc_kinetic_energy get an optional n_systems argument (old behaviour if it is not given). MDState.calc_kT and the Nose-Hoover NVT/NPT integrators pass it.
  • The NPT integrators use state.n_atoms_per_system instead of their own bincounts.
  • The MTK cell force looped over systems in Python with a mask.any() per system. It is now a single calc_kinetic_energy call.

Syncs per step outside the force call, counted with torch.cuda.set_sync_debug_mode("warn") (4 systems, default arguments):

integrator main this PR
NVE, NVT Langevin 0 0
NVT Nose-Hoover 1 0
NVT CSVR 8 0
NPT Langevin (iso/aniso) 6 0
NPT Nose-Hoover isotropic (MTK) 6 + 6 per system 0
NPT C-rescale isotropic 16 0
NPT C-rescale triclinic 20 4

(The 4 left in triclinic C-rescale come from torch.matrix_exp, which syncs inside PyTorch. With constraints, the DOF check still syncs.)

Timings on an RTX 5080, LJ argon with 256 atoms per system in float64. I used a fixed neighbor list so the force call itself doesn't sync. ms per step, main vs this PR:

integrator 8 systems 32 systems
NVT Nose-Hoover 5.08 vs 4.69 7.13 vs 4.67
MTK 8.26 vs 6.74 13.78 vs 6.75

NVE and Langevin don't change. On CPU the trajectories are bitwise the same as main over 200 steps, except MTK which differs by up to 5e-15 because the kinetic energy is summed in a different order.

Tests: I extended two existing tests in tests/test_quantities.py with hand-computed values. test_calc_kinetic_energy_batched_system passes n_systems=3 for atoms in 2 systems and expects [3, 24, 0]. test_calc_kt_batched_system does the same for calc_kT with dof_per_system=[4, 12, 1] and expects [1.5, 4, 0], and also checks that a scalar dof_per_system still broadcasts. I didn't add a test for the syncs themselves since CI runs on CPU (#371). I checked them on a GPU with set_sync_debug_mode.

The diff was reviewed by a separate agent against AGENTS.md.

Checklist

Before a pull request can be merged, the following items must be checked:

  • Doc strings have been added in the Google docstring format.
  • Run ruff on your code.
  • Tests have been added or updated for any new functionality or bug fixes.
  • If you used a coding agent, either a separate agent reviewed the diff or the summary notes that the review was skipped.

View guided diff

Size per-system reductions from state.n_systems (the cell's leading
dimension, TorchSim#621) instead of bincount or system_idx.max(), so the MD
steps of states without constraints no longer read values back to the
host.

- SimState.n_atoms_per_system: index_add into n_systems counts.
- get_number_of_degrees_of_freedom: check for non-positive DOF only when
  constraints are present; without them every system has >= 3 DOF.
- calc_kT and calc_kinetic_energy: optional n_systems argument; MDState
  and the Nose-Hoover NVT and NPT integrators pass it.
- NPT integrators: state.n_atoms_per_system instead of inline bincounts.
- MTK cell force: one per-system calc_kinetic_energy call instead of a
  Python loop with mask.any() per system.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 6 files

Shadow auto-approve: would not auto-approve because issues were found.
Reply to a comment to ask cubic a question or push back. It learns from your replies.
Click Fix with cubic on a comment, or reply @cubic-dev-ai fix this, and cubic writes the fix.

View guided diff | Re-trigger cubic

Comment thread torch_sim/state.py Resolved
Comment thread torch_sim/quantities.py Resolved
Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>
Signed-off-by: Rhys Goodall <rhys.goodall@outlook.com>

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 2 files (changes from recent commits).

Shadow auto-approve: would not auto-approve because issues were found.

View guided diff | Re-trigger cubic

Comment thread torch_sim/state.py Resolved
Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>
Signed-off-by: Rhys Goodall <rhys.goodall@outlook.com>

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0 issues found across 1 file (changes from recent commits).

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Shadow auto-approve: would not auto-approve. Auto-approval skipped because this PR is from an external contributor.

View guided diff | Re-trigger cubic

@CompRhys
CompRhys merged commit 869fb71 into TorchSim:main Nov 18, 2026
63 of 65 checks passed
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.

2 participants