verl icon indicating copy to clipboard operation
verl copied to clipboard

[RFC] To resolve CPU memory peak in checkpointing process: migration to megatron dist_ckpt?

Open ETOgaosion opened this issue 1 year ago • 2 comments

To resolve CPU memory peak in checkpointing process

Recently there are several reports about the existence of CPU Memory Peaks in Megatron model saving process when training larger scale of models.

Reports from issue: #1915 , #1964 (original problem is related to vllm memory leak, but still there are reports of OOM issues)

Current implementation

There are four components to save in current checkpoint_manager:

  • model weights
  • optimizer states
  • extra states:
  • rng states
  • optimizer scheduler (lr scheduler)
  1. For model weights, every rank in data parallel rank 0 shall save its model shards. And theoretically each model parallel rank saves its own.

https://github.com/volcengine/verl/blob/a1a152ee4ad93c1511ebdfb53df6687e272e2e76/verl/utils/checkpoint/megatron_checkpoint_manager.py#L253-L258

But context parallel shall also be considered, only cp rank 0 is needed to save. This is the first optimization.

  1. Secondly, as we default enable zero-1 optimizer:

https://github.com/volcengine/verl/blob/a1a152ee4ad93c1511ebdfb53df6687e272e2e76/verl/trainer/config/ppo_megatron_trainer.yaml#L91

https://github.com/volcengine/verl/blob/a1a152ee4ad93c1511ebdfb53df6687e272e2e76/verl/utils/megatron_utils.py#L210

Thus theoretically, every rank shall save its own ChainedOptimizer with its parameter shards which is also sharded by data parallel. But actually it turns out that saving process does not work like this, still dp rank 0 saves all optimizer states, we will discuss this later.

  1. Extra states are relatively small and we don't expand here.

Megatron checkpointing implementation

Every rank will generate all its model/optimizer/extra state_dict in a huge one.

https://github.com/NVIDIA/Megatron-LM/blob/0595ef2b0c93f8d61f473c9f99f9ff73803ff919/megatron/training/checkpointing.py#L428

In generate_state_dict, for model part, verl and megatron's difference is that megatron calls sharded_state_dict() while verl only use state_dict(). Then most states tensors use make_sharded_tensors_for_checkpoint. For tensor parallel operators' weights, turn to make_tp_sharded_tensor_for_checkpoint, while for other tensors, go to make_sharded_tensor_for_checkpoint, in megatron/core(/transformer)/utils.py.

The core implementation of sharded tensor is the class ShardedTensor in megatron/core/dist_checkpointing/mapping.py.

And for short, it construts every rank's own model shards different from any other tp and dp ranks.

For optimizer, verl uses the legacy saving method: save_parameter_state(), but megatron also uses sharded_state_dict().

The save_parameter_state() is very bad, still collects all states:

https://github.com/NVIDIA/Megatron-LM/blob/0595ef2b0c93f8d61f473c9f99f9ff73803ff919/megatron/core/optimizer/optimizer.py#L1222-L1232

Thus calling sharded_state_dict() is the only way to use distributed saved optimizer, navigating to sharded_param_state_fs_model_space in megatron/core/optimizer/distrib_optimizer.py. The main effect of this function is replace the original tensors with sharded tensors, because distributed optimizer naturally split between data parallel ranks.

Migration steps

From above analysis and the reference from megatron/training/checkpointing.py, it's relatively easy to fix and improve current checkpointing function. As a calculation, for <tp, cp, dp, pp> (expert consider its own tp and dp), the migration can bring with a speedup proportional to dp * cp.

The model merger process needs to take load checkpointing as a reference.

Confusing points

  1. It is noticable that although every rank in a machine starts together for checkpoint saving, at this time all models or optimizers have already been offloaded to CPU and caused no error. Theoretically, there should not be extra CPU memory copy and shall directly produce a disk saving action without OOM risks. Are there extra memory copy actions?

Maybe a cpu memory profiling tool is needed for further understanding.

ETOgaosion avatar Jun 12 '25 10:06 ETOgaosion

@ETOgaosion Hi, any updates? When using generate_state_dict with sharded_state_dict, It also has a large memory peak when save ckpts with Qwen3-23b as follow:

Image

why so large CPU Memory Usage?

qbei-i avatar Mar 04 '26 09:03 qbei-i

English:

This RFC appears to be addressed by the follow-up checkpointing change. PR #5705 was merged on 2026-03-23 and changed the Megatron optimizer distributed-checkpoint default from fully-reshardable to dp-reshardable, specifically to avoid high CPU memory usage during checkpoint saving. The current main branch keeps dist_ckpt_optim_fully_reshardable: False, and the checkpoint documentation now warns that dist_ckpt_optim_fully_reshardable=True gathers optimizer states on data-parallel rank 0 and may increase CPU memory usage or cause OOM for large models.

For remaining host memory leak or peak-memory reproductions, #5665 is the more specific tracking issue. In that thread, a maintainer pointed to #5705 as the fix, and a collaborator later reported that they had not observed OOM or memory leakage after #5705. If there is still a separate migration scope beyond this merged fix, please reopen or file a focused follow-up with the remaining gap.

中文:

这个 RFC 看起来已经被后续 checkpointing 变更处理。PR #5705 于 2026-03-23 合并,将 Megatron optimizer distributed-checkpoint 的默认格式从 fully-reshardable 改为 dp-reshardable,目的就是避免 checkpoint 保存阶段的高 CPU 内存占用。当前 main 分支中 dist_ckpt_optim_fully_reshardable: False,checkpoint 文档也已经说明:当 dist_ckpt_optim_fully_reshardable=True 时,保存 optimizer checkpoint 需要在 data-parallel rank 0 聚合 optimizer states,可能增加 CPU 内存使用并在大模型上导致 OOM。

如果仍然有 host memory leak 或 peak memory 的复现,#5665 是更具体的跟踪项。该线程中维护者已指向 #5705 作为修复,后续协作者也反馈 #5705 后没有再观察到 OOM 或 memory leakage。如果这里仍有超出该修复之外的独立迁移范围,建议重新打开或提交一个聚焦的 follow-up,说明剩余差距。

This response was generated by a scheduled Codex GPT5.5 issue processing task.

Kirrito-k423 avatar Jul 08 '26 01:07 Kirrito-k423