DeepEP icon indicating copy to clipboard operation
DeepEP copied to clipboard

[HybridEP] Fabric (MNNVL) allocator leaks device memory on every buffer reallocation

Open jerryhong21 opened this issue 1 month ago • 1 comments

Summary

On systems where HybridEP uses the fabric (MNNVL) allocator (for example GB200/GB300 NVL72), physical device memory is never returned when a HybridEP buffer is freed. Each time HybridEPBuffer::update_buffer grows the buffer, the entire previous buffer set leaks. With variable-length batches, the leaks keep adding up until cuMemCreate fails.

Symptom

We hit this in MoE training with variable-length, long-context batches on GB300 NVL72, using HybridEP through Megatron-LM's flex dispatcher:

  • Device memory on one rank climbed stepwise from ~242 GiB to ~281 GiB over ~2h and never came back down.
  • PyTorch's allocator stats did not account for the growth, because this memory is allocated outside PyTorch.
  • The run then aborted mid-step:
CU error encountered at: file=.../csrc/hybrid_ep/allocator/allocator.cu line=91,
call='cuMemCreate(&handle, size, &fabric_prop_, 0)' Reason=out of memory.

Root cause

In csrc/hybrid_ep/allocator/allocator.cu, the fabric path never releases three handle references:

  • allocate never releases the handle from cuMemCreate (:91).
  • get_handle never releases the reference taken by cuMemRetainAllocationHandle before export (:124).
  • open_handle never releases the handle from cuMemImportFromShareableHandle (:139).

free retains and releases once, so its net effect on the count is zero. close_handle only unmaps. The CUDA driver frees physical memory only after every mapping is unmapped and every handle reference is released, so this memory stays allocated. Peers' imports also keep each exporter's memory pinned.

update_buffer calls release_buffer() and then allocate_buffer() whenever a call needs more capacity than the current reservation (hybrid_ep.cu, grow_buffer_config). With variable-length inputs, every new tokens-per-rank high (in alignment steps) therefore leaks a whole buffer set. Workloads where every step has the same shape allocate once and are not affected.

The cudaMalloc/IPC path, used when fabric is unavailable, is not affected. The legacy SharedMemoryAllocator in csrc/deep_ep.cpp has the same three sites.

Workarounds

  • Size the buffer up front for the maximum max_num_of_tokens_per_rank, so it never reallocates.
  • Or set USE_MNNVL=0 when the EP group fits within one NVLink node, so the cudaMalloc/IPC path is used.

Fix

PR #798 adds the missing cuMemRelease calls after map, export and import.

jerryhong21 avatar Oct 26 '26 23:10 jerryhong21


Reproduced and quantified on GB200 (4 ranks, sm_100), hybrid-ep @ 4e890fd.

What I see matches the report exactly. In ExtendedMemoryAllocator the three handle references are never released (allocate after cuMemMap, get_handle after cuMemExportToShareableHandle, open_handle after cuMemMap), and free's retain/release pair nets to zero so it cannot offset them. The same three omissions exist in SharedMemoryAllocator in csrc/deep_ep.cpp (malloc / get_mem_handle / open_mem_handle).

Measurement. I built the branch for sm_100 and repeatedly grew the buffer via update_template_config(num_of_tokens_per_rank=1024 … 32768), which triggers release_buffer() + allocate_buffer() each time. Driver-level free memory (torch.cuda.mem_get_info(), since this memory bypasses PyTorch's allocator):

stage free memory delta vs baseline
grow 1024 −0.05 GiB
grow 4096 −0.13 GiB
grow 16384 −0.35 GiB
grow 32768 −0.64 GiB
after destroying the buffer −0.63 GiB, never returned

So each reallocation permanently leaks the previous buffer set — same mechanism as your 242→281 GiB / 2h observation, just at a smaller scale.

With the six cuMemRelease calls added (3 in each allocator), the same loop returns to +0.00 GiB after the buffer is destroyed, and the growth-phase delta is just the live buffer size.

I noticed #798 already had this fix (+8 lines, same three call sites in both files) but was closed without merging, and #799 is still open. Is this being handled internally, or would you like a PR? I can open one against hybrid-ep with the patch plus the GB200 before/after numbers above (and I am happy to leave it as a review/verification only if you prefer to land it yourself).


YYuJLiu avatar Nov 06 '26 06:11 YYuJLiu