Skip to content

fix(autobatching): recognise Warp OOM in memory estimation and free cache before the real run#589

Open
niklashoelter wants to merge 1 commit into
TorchSim:mainfrom
niklashoelter:fix/autobatcher-warp-oom-and-cache-release
Open

fix(autobatching): recognise Warp OOM in memory estimation and free cache before the real run#589
niklashoelter wants to merge 1 commit into
TorchSim:mainfrom
niklashoelter:fix/autobatcher-warp-oom-and-cache-release

Conversation

@niklashoelter

Copy link
Copy Markdown
Contributor

Problem

InFlightAutoBatcher / BinningAutoBatcher estimate a safe batch size by replaying the model forward on geometrically-growing batches until it OOMs, then backing off. Two defects on that path make models whose neighbor lists run on Warp / nvalchemiops (e.g. ORB v3) crash instead of backing off:

  1. The Warp OOM isn't recognised. determine_max_batch_size only matched the substring "CUDA out of memory". Warp's allocator raises "Failed to allocate <n> bytes on device '…'", which doesn't match, so the OOM propagates and kills the run during memory estimation. In addition, the re-raise sat inside the for msg in oom_error_message loop, so passing a broadened list of signatures never worked — the first non-matching entry re-raised before later ones were checked.

  2. PyTorch's caching allocator starves the external Warp pool after estimation. Once (1) is fixed, estimation survives but the crash simply moves to the first real optimization forward pass, failing on a tiny (~2.5 KB) Warp allocation. The estimation forwards leave PyTorch's caching allocator holding almost all of device memory, and Warp allocates from a separate cudaMallocAsync pool that then has nothing left.

Concretely, ORB v3 conservative on a 4272-molecule batch reproducibly died with RuntimeError: Failed to allocate 2556 bytes on device 'cuda:N'.

Fix (torch_sim/autobatching.py)

  • Add DEFAULT_OOM_ERROR_MESSAGES = ("CUDA out of memory", "Failed to allocate") and use it as the default oom_error_message for determine_max_batch_size, InFlightAutoBatcher, and BinningAutoBatcher.
  • Re-raise only after checking all signatures (if any(msg in exc_str …): return safe then raise), so a multi-signature list actually works.
  • Release memory at the end of estimate_max_memory_scaler (shared by both batchers) with torch.cuda.synchronize() + torch.cuda.empty_cache(), guarded by torch.cuda.is_available(), so the real run isn't starved.

Tests

Adds regression tests in tests/test_autobatching.py: both PyTorch and Warp OOM wordings are recognised, and a genuine non-OOM error still propagates. pytest tests/test_autobatching.py → 38 passed; ruff clean.

Validation

ORB v3 conservative over a 4272-structure library on a dedicated GPU: 4/4 full runs complete 4272/4272 with the fix; the same code with the memory release disabled crashes with Failed to allocate 2556 bytes.

@niklashoelter niklashoelter force-pushed the fix/autobatcher-warp-oom-and-cache-release branch from c05b9d4 to cb2c240 Compare July 4, 2026 10:50
…he before real run

The autobatcher estimates a safe batch size by replaying the model forward
on geometrically-growing batches until it OOMs. Two defects on that path made
ORB v3 conservative (autograd forces -> ~2x memory) crash:

1. determine_max_batch_size only recognised the substring "CUDA out of
   memory". ORB's neighbor lists run on Warp/nvalchemiops, whose allocator
   raises "Failed to allocate <n> bytes", so the OOM was not caught and
   propagated during estimation. In addition the re-raise sat inside the
   message-matching loop, so a broadened list of signatures never worked:
   the first non-matching entry re-raised before later ones were checked.

   Fix: add DEFAULT_OOM_ERROR_MESSAGES = ("CUDA out of memory",
   "Failed to allocate"), use it as the default across
   determine_max_batch_size / InFlightAutoBatcher / BinningAutoBatcher, and
   only re-raise after checking all signatures (any(...)).

2. After estimation survives, PyTorch's caching allocator still holds most of
   the device memory, starving the separate Warp/cudaMallocAsync pool on the
   first real forward pass (fails on a tiny 2556-byte allocation). Release the
   cache at the end of estimate_max_memory_scaler (shared by both batchers)
   via synchronize() + empty_cache().

Adds regression tests covering both OOM wordings and confirming genuine
non-OOM errors still propagate.
@niklashoelter niklashoelter force-pushed the fix/autobatcher-warp-oom-and-cache-release branch from cb2c240 to b678610 Compare July 4, 2026 10:59
@niklashoelter niklashoelter marked this pull request as ready for review July 4, 2026 11:35
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.

1 participant