perf(md): remove host-device syncs from the MD integrators
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_systemusesindex_addinton_systemsentries instead ofbincount.get_number_of_degrees_of_freedomonly checks for non-positive DOF when there are constraints. Without constraints every system has at least one atom, so at least 3 DOF.calc_kTandcalc_kinetic_energyget an optionaln_systemsargument (old behaviour if it is not given).MDState.calc_kTand the Nose-Hoover NVT/NPT integrators pass it.- The NPT integrators use
state.n_atoms_per_systeminstead of their ownbincounts. - The MTK cell force looped over systems in Python with a
mask.any()per system. It is now a singlecalc_kinetic_energycall.
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.