Repository navigation
perf(md): remove host-device syncs from the MD integrators - #650
Conversation
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.
There was a problem hiding this comment.
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
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>
There was a problem hiding this comment.
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
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>
There was a problem hiding this comment.
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
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
bincountandint(system_idx.max()), used to count systems or atoms per system. Since #621,state.n_systemscomes from the cell shape, so I replaced those with buffers of sizestate.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.state.n_atoms_per_systeminstead of their ownbincounts.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):(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:
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.pywith hand-computed values.test_calc_kinetic_energy_batched_systempassesn_systems=3for atoms in 2 systems and expects[3, 24, 0].test_calc_kt_batched_systemdoes the same forcalc_kTwithdof_per_system=[4, 12, 1]and expects[1.5, 4, 0], and also checks that a scalardof_per_systemstill broadcasts. I didn't add a test for the syncs themselves since CI runs on CPU (#371). I checked them on a GPU withset_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: