Skip to content

Memory: fix leaks and allocation churn at large sizes - #17

Open
AdvaitTahilyani wants to merge 14 commits into
scale/01-lstm-deadlockfrom
scale/02-memory
Open

AdvaitTahilyani wants to merge 14 commits into
scale/01-lstm-deadlockfrom
scale/02-memory

Conversation

@AdvaitTahilyani

Copy link
Copy Markdown

Stacked on #16. Needs UIUC-PPL/charmtyles#8.

  • Flushed results drop their operands, and deletions reach every rank (fixes a device leak growing 10 → 24 GB in indexing_routines).
  • Large-buffer and free-list caches with per-GPU budgets; release-all and retry on OOM.
  • Async zero-fill on the compute stream; freed CUDA IPC blocks merge with their buddies.
  • CNUM_MEM_STATS=1 prints per-PE memory at barriers.

richardson_lucy 512³: 986 → 335 ms (flat across repeats); black_scholes_greeks on 4 A100s: 4.1 s → 125 ms.

Tested (full stack): CPU +p1/+p2 15/15 originals, 25/25 ports, 30/30 ops; A100 1 GPU 15/15, 25/25, 30/30 and 4 GPUs (one process per GPU) 15/15, 25/25.

🤖 Generated with Claude Code

AdvaitTahilyani and others added 9 commits November 2, 2026 17:23
A DAG's deletions were stored only for the ranks its nodes touch; the other
ranks got an empty DAG. An array whose deletion arrived with a DAG of another
rank was never freed: indexing_routines leaked every 2-D DIAG and GATHER result
and the 3-D arrays of each routine (+8 GB per repeat at -n 2000, out of device
memory on the second repeat at -n 4000). The empty DAGs now carry the
deletions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
One line per barrier on PE 0: device memory in use, live and retired arrays
(device and host-mirror bytes, free-list keys), cached message buffers, pinned
and IPC pools, pending per-node states and sends, stored DAGs, compiled kernels
and the host heap.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Message buffers above 64 MB and the executors' scratch buffers (alloc_buffer)
were cudaMalloc'ed and cudaFree'd per use, and the small-block pool stopped
caching at 1 GB. cudaMalloc of hundreds of MB got slower with every block the
process had freed: richardson_lucy 512^3 spent 49, 314 and 521 ms per repeat
in cudaMalloc (426 -> 925 ms steady). Large blocks are now cached by size in
2 MB steps; all cached blocks share a cap of 1/8 of device memory
(CNUM_DEVICE_CACHE_MB), and an allocation that fails releases the large blocks
and retries. free_buffer fences the device, as the cudaFree it replaces did.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
At most 4 retired arrays per key were kept and the rest freed, so DAGs with
more temporaries of a shape paid a cudaFree (a device sync) and a cudaMalloc
per array. On NVIDIA the free lists now keep up to 64 per key within 1/8 of
device memory per PE (CNUM_FREE_LIST_MB); a failed array allocation frees the
retired arrays and cached buffers and retries. Retired arrays drop their host
mirrors. Zero fills of new and reused arrays were a blocking Kokkos init or
deep_copy (device-wide fences); they are now a cudaMemsetAsync on the compute
stream, with the comm stream and the default instance ordered after it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Freed blocks went back to their own size class and were never merged, so the
1 GB of IPC slabs fragmented into small blocks and large messages fell back
to host staging, more of them each repeat (stencil -n 8192 on 4 GPUs:
12.9 -> 15.4 s). Free lists are ordered sets and a freed block is merged with
its free buddy up to a whole slab.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Budgets are fractions of the device split among the processes that share
  it (counted by the startup probe from PCI bus ids), not per process: small
  message blocks 1/16 (CNUM_COMM_CACHE_MB), large blocks 1/8
  (CNUM_DEVICE_CACHE_MB), retired arrays 1/8 (CNUM_FREE_LIST_MB, now a
  process-wide counter). The IPC slabs stay separate (in-flight messages,
  CNUM_GPU_IPC_MB).
- Small and large blocks no longer share one budget, so a full large cache
  does not make every small free a cudaFree; large blocks are evicted least
  recently freed first, only as many as needed.
- A failed allocation (message buffer or array) frees every cached block and
  all of this PE's retired arrays and retries once; a second failure aborts
  with the size and the free device memory. Message buffers are allocated
  outside the pool lock.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@adityapb adityapb left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: approve with nits.

This is a static review. Line numbers are at this PR's head.

I checked these and found them correct:

  • IPC buddy merge: alignment, no overlap, no double merge.
  • Deletions for empty DAGs.
  • Cached large comm blocks: every free happens after a fence, from a stream callback, or at send_complete.
  • OOM release: there is a fence before it.
  • Reuse: ordered correctly with the async zero-fill.
  1. [minor] In SMP builds the OOM release only reaches the calling PE's retired arrays, but the budget is per process.
    • Where: release_device_caches (backend.hpp:1116-1127) loops over local_partitions(), which is thread_local (:1105-1108).
    • Scenario: PE A holds the budget in retired arrays. PE B's allocation then fails, the retry frees only B's arrays, and it aborts in device_oom.
    • #25 makes this easier to hit.
    • Still at #44.
  2. Nits:
    • Budget timing: budgets are sized before the IPC probe sets gpu_sharers(). With 4 processes per GPU, the early caches are 4× too large and are never trimmed.
    • Free-path cost: comm_free calls getenv (through device_budget) under pool.mu on every free, and eviction in comm_free_large is O(n²).
    • SMP race: the retired-bytes cap check in retire_array races between PEs, so the cap can be overshot.

…bytes cap, cheaper pool

An out-of-memory release now walks every partition in the process (registry with a
lock; free_arrays has its own mutex), since the budget is per process. The retired
bytes cap reserves atomically. Budget env vars are read once; large-block eviction
is O(log n) through an age index; the caches are trimmed when the IPC probe lowers
the budgets.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@AdvaitTahilyani

Copy link
Copy Markdown
Author
  1. OOM release only reaches the calling PE's retired arrays in SMP: fixed in 2377512 (process-wide partition registry with a lock; release_device_caches walks every partition in the process, and free_arrays has its own mutex held by retire, reuse, release and the memory stats).
    2a. Budgets sized before the IPC probe: fixed in 2377512 (the probe now trims the comm pool and the retired arrays to the new budget when it lowers it).
    2b. getenv under pool.mu: fixed in 2377512 (each env var is read once into a BudgetKnob).
    2b. O(n^2) eviction in comm_free_large: fixed in 2377512 (age index, O(log n) per eviction).
    2c. SMP overshoot of the retired-bytes cap: fixed in 2377512 (the bytes are reserved with fetch_add before the push).

Validated: nc build and CPU regress at +p1 and +p2 (orig 15/15, ports 25/25, ops 26/26 each, no problems). The device code is not in the nc build: the GPU translation units touched (gpu_comm, partition_lifecycle, partition_get, executor_core) compile with the GPU flags, but no GPU run was done, so the SMP out-of-memory path is untested at runtime.

AdvaitTahilyani and others added 4 commits November 10, 2026 15:59
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

2 participants