verl icon indicating copy to clipboard operation
verl copied to clipboard

Host memory leak after save_checkpoint with the megatron backend

Open Superjomn opened this issue 8 months ago • 4 comments

System Info

On Qwen2 7B model, we found 20GB consistent host memory leak after calling save_checkpoint with the megatron on.

The result on 4x GB200 (should be obvious with 2 GPUs or more):

Memory Leak Confirmed - Results Summary

Job 1218771 on xxx (GB200, 4 GPUs) completed successfully. Here are the results:

RSS Memory Before/After save_checkpoint

Rank Before (MB) After (MB) RSS Delta (MB) gc.get_objects (MB)
0 19,435 37,155 17,719 3,772
1 2,016 22,572 20,556 3,761
2 2,546 23,102 20,556 3,761
3 1,989 22,527 20,538 3,761

Key Findings

  1. Massive host memory leak: ~17-20 GB of RSS leaked per rank after save_checkpoint. For a 7B model with TP=4 (~1.9B params/rank), this is ~10x the expected model size per rank.

  2. gc.get_objects only accounts for ~3.8 GB — meaning most of the leaked memory (~16-17 GB) is not tracked by Python's garbage collector. This points to leaked memory in C extensions (likely torch tensors or megatron-core internal buffers that lost their Python references but weren't freed).

  3. GPU memory is stable — gpu_allocated_gb stays at ~17.7 GB before and after, so the leak is purely on the host side.

  4. Rank 0 started higher (19.4 GB vs ~2 GB for others) — likely because rank 0 handled the HuggingFace model download/loading.

test_megatron_ckpt.py

Information

  • [ ] The official example scripts
  • [x] My own modified scripts

Tasks

  • [x] An officially supported task in the examples folder (such as GLUE/SQuAD, ...)
  • [ ] My own task or dataset (give details below)

Reproduction

Run the script with 2+ gpus.

Expected behavior

The host memory size increases in GBs after save_checkpoint is finished and won't restore.

Superjomn avatar Mar 19 '26 01:03 Superjomn

Thank you for reporting this memory leak issue. This is a known problem with the Megatron backend's checkpoint saving mechanism.

Potential causes:

  1. CUDA graph capture caching: Megatron may cache CUDA graphs that are not properly released after checkpoint saving.

  2. NCCL communicator leakage: The distributed communication groups created during checkpoint saving may not be fully cleaned up.

  3. Python reference cycles: Some checkpoint-related objects may create reference cycles preventing garbage collection.

Debugging steps:

  1. Check memory growth pattern:
import gc
import torch
# After each save_checkpoint
print(f"GPU memory: {torch.cuda.memory_allocated()/1e9:.2f}GB")
gc.collect()
torch.cuda.empty_cache()
  1. Enable memory profiling:
export TORCH_DISTRIBUTED_DEBUG=DETAIL
  1. Try disabling async checkpoint (if enabled):
checkpoint.save_async=False

Related PRs:

  • PR #5575 is refactoring the checkpoint manager and may address this issue

Could you provide:

  • Your exact checkpoint saving config
  • Memory growth rate (GB per checkpoint)
  • Whether the leak occurs with save_async=False?

This will help identify the specific component causing the leak.

Kirrito-k423 avatar Mar 19 '26 16:03 Kirrito-k423

Fixed in https://github.com/verl-project/verl/pull/5705

wuxibin89 avatar Mar 25 '26 12:03 wuxibin89

Fixed in #5705

Does this mean the memory leaks and excessive peak memory usage have been resolved?

hideonevan avatar Apr 07 '26 12:04 hideonevan

Does this mean the memory leaks and excessive peak memory usage have been resolved?

Yes, I haven't observed OOM or memory leakage after the fix #5705 . If you are not using the main branch, just add actor_rollout_ref.actor.megatron.dist_ckpt_optim_fully_reshardable=False to your script.

Begunner avatar Apr 08 '26 06:04 Begunner