Skip to content

Commit cd75ce5

Browse files
authored
Compute n_edges memory scalers in atom-bounded chunks (#612)
1 parent 489c8aa commit cd75ce5

2 files changed

Lines changed: 81 additions & 10 deletions

File tree

‎tests/test_autobatching.py‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,51 @@ def test_n_edges_scalers_batched(ar_double_sim_state: ts.SimState) -> None:
185185
assert all(v >= 0 for v in result)
186186

187187

188+
@pytest.fixture
189+
def mixed_many_sim_state(
190+
ar_supercell_sim_state: ts.SimState, si_sim_state: ts.SimState
191+
) -> ts.SimState:
192+
"""Batched state of alternating large and small periodic systems."""
193+
return ts.concatenate_states(
194+
[ar_supercell_sim_state, si_sim_state, si_sim_state, ar_supercell_sim_state],
195+
device=si_sim_state.device,
196+
)
197+
198+
199+
@pytest.mark.parametrize("max_atoms_per_chunk", [1, 16, 40, 10_000])
200+
def test_n_edges_scalers_chunked_matches_single_pass(
201+
mixed_many_sim_state: ts.SimState, max_atoms_per_chunk: int
202+
) -> None:
203+
"""Splitting the neighbor-list pass leaves every per-system edge count unchanged."""
204+
state = mixed_many_sim_state
205+
single = _n_edges_scalers(state, cutoff=5.0, max_atoms_per_chunk=state.n_atoms)
206+
chunked = _n_edges_scalers(state, cutoff=5.0, max_atoms_per_chunk=max_atoms_per_chunk)
207+
assert chunked == single
208+
209+
210+
@pytest.mark.parametrize("max_atoms_per_chunk", [1, 16, 40, 10_000])
211+
def test_n_edges_scalers_chunk_bounds(
212+
mixed_many_sim_state: ts.SimState,
213+
max_atoms_per_chunk: int,
214+
monkeypatch: pytest.MonkeyPatch,
215+
) -> None:
216+
"""Each pass holds whole systems and exceeds the bound only for a lone system."""
217+
passes: list[tuple[int, int]] = []
218+
real_nl = ts.autobatching.torchsim_nl
219+
220+
def recording_nl(**kwargs: Any) -> Any:
221+
passes.append((int(kwargs["system_idx"].max()) + 1, kwargs["positions"].shape[0]))
222+
return real_nl(**kwargs)
223+
224+
monkeypatch.setattr(ts.autobatching, "torchsim_nl", recording_nl)
225+
state = mixed_many_sim_state
226+
_n_edges_scalers(state, cutoff=5.0, max_atoms_per_chunk=max_atoms_per_chunk)
227+
228+
assert sum(n_sys for n_sys, _ in passes) == state.n_systems
229+
assert sum(n_at for _, n_at in passes) == state.n_atoms
230+
assert all(n_at <= max_atoms_per_chunk or n_sys == 1 for n_sys, n_at in passes)
231+
232+
188233
@pytest.mark.parametrize("items", [[], {}])
189234
def test_to_constant_volume_bins_empty_input(
190235
items: list[Any] | dict[int, float],

‎torch_sim/autobatching.py‎

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@
4242
# nvalchemiops kernels used by ORB v3) raise "Failed to allocate <n> bytes".
4343
DEFAULT_OOM_ERROR_MESSAGES = ("CUDA out of memory", "Failed to allocate")
4444

45+
# Atoms per neighbor-list pass when computing n_edges scalers. Alchemiops holds
46+
# fixed-width buffers of ~4.4 KiB per atom at a 6 A cutoff, so this caps the
47+
# pass at about 1 GiB however large the state is.
48+
N_EDGES_MAX_ATOMS_PER_CHUNK = 250_000
49+
4550

4651
def to_constant_volume_bins( # noqa: C901
4752
items: dict[int, float] | list[Any],
@@ -290,16 +295,37 @@ def determine_max_batch_size(
290295
torch.cuda.empty_cache()
291296

292297

293-
def _n_edges_scalers(state: SimState, cutoff: float) -> list[float]:
294-
"""Return per-system edge counts from the neighbor list as memory scalers."""
295-
_, system_mapping, _ = torchsim_nl(
296-
positions=state.positions,
297-
cell=state.cell,
298-
pbc=state.pbc,
299-
cutoff=cutoff,
300-
system_idx=state.system_idx,
301-
)
302-
return system_mapping.bincount(minlength=state.n_systems).float().tolist()
298+
def _n_edges_scalers(
299+
state: SimState,
300+
cutoff: float,
301+
max_atoms_per_chunk: int = N_EDGES_MAX_ATOMS_PER_CHUNK,
302+
) -> list[float]:
303+
"""Return per-system edge counts from the neighbor list as memory scalers.
304+
305+
The neighbor list runs over contiguous runs of systems holding at most
306+
``max_atoms_per_chunk`` atoms, so peak memory does not grow with the size of
307+
the state. A system larger than the bound runs alone. Edge counts do not
308+
depend on other systems, so the result equals a single pass over the state.
309+
"""
310+
n_atoms_per_system = state.n_atoms_per_system.tolist()
311+
n_systems = len(n_atoms_per_system)
312+
scalers: list[float] = []
313+
s0 = a0 = 0
314+
while s0 < n_systems:
315+
s1, a1 = s0 + 1, a0 + n_atoms_per_system[s0]
316+
while s1 < n_systems and a1 - a0 + n_atoms_per_system[s1] <= max_atoms_per_chunk:
317+
a1 += n_atoms_per_system[s1]
318+
s1 += 1
319+
_, system_mapping, _ = torchsim_nl(
320+
positions=state.positions[a0:a1],
321+
cell=state.cell[s0:s1],
322+
pbc=state.pbc,
323+
cutoff=cutoff,
324+
system_idx=state.system_idx[a0:a1] - s0,
325+
)
326+
scalers.extend(system_mapping.bincount(minlength=s1 - s0).float().tolist())
327+
s0, a0 = s1, a1
328+
return scalers
303329

304330

305331
def calculate_memory_scalers(

0 commit comments

Comments
 (0)