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

Merged
ST
5d ago
+96-32

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):

integratormainthis PR
NVE, NVT Langevin00
NVT Nose-Hoover10
NVT CSVR80
NPT Langevin (iso/aniso)60
NPT Nose-Hoover isotropic (MTK)6 + 6 per system0
NPT C-rescale isotropic160
NPT C-rescale triclinic204

(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:

integrator8 systems32 systems
NVT Nose-Hoover5.08 vs 4.697.13 vs 4.67
MTK8.26 vs 6.7413.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.