Skip to content

OOM during refit in mcore path when using larger bucket sizes #630

Open

Description

@yfw

Using a larger bucket size for refit by setting a higher percentage of available bytes here (e.g. 0.4) results in OOM for DSv2-Lite model.

Sample command to reproduce (after increasing bucket size to 40%):

uv run python examples/run_grpo_math.py --config=examples/configs/grpo_math_1B_megatron.yaml grpo.val_batch_size=2 policy.model_name=deepseek-ai/DeepSeek-V2-Lite-Chat cluster.gpus_per_node=8 policy.megatron_cfg.pipeline_model_parallel_size=4 policy.megatron_cfg.num_layers_in_first_pipeline_stage=7 policy.megatron_cfg.num_layers_in_last_pipeline_stage=6  policy.refit_buffer_size_gb=8 policy.max_total_sequence_length=1024 checkpointing.enabled=False checkpointing.save_period=5  grpo.val_period=-1 grpo.max_val_samples=16 grpo.val_batch_size=4 checkpointing.keep_top_k=100 checkpointing.checkpoint_dir=results/dsv2-lite-testsave grpo.val_at_start=False grpo.max_val_samples=16 policy.megatron_cfg.expert_model_parallel_size=2 policy.megatron_cfg.apply_rope_fusion=False

This seems to happen because we use double the memory when packing the params. We may need to multiply the size_in_bytes of each param by 2 in prepare_weights_for_ipc.

Activity

  1. guyueh1 commented on Jul 9, 2025

    @guyueh1
    Contributor

    why did we propose this knob "setting a higher percentage of available byte"? Ideally it should be completely automatic for non-expert users, we need a logic to set the default bucket size; and this logic should be aware if we are using packing or not and choose accordingly; for expert users they can use this knob to override our default setting

  2. guyueh1 commented on Jul 14, 2025

    @guyueh1
    Contributor

    I did some investigation and two issues came up:

    1. after refit we do not delete the _held_gather_buffer in time, this memory is held for the rest of the step. !664 can remove this part of the overhead and shouldn't change functionality
    2. although during refit, the peak memory usage is static memory + 2x bucket size, which means in theory we can use bucket size as 50% of the free memory size, but in reality, the reserved memory size is much larger than allocated memory size in megatron process, leading to OOM . A workaround is to use invoke torch.cuda.empty_cache() more often, see in this commit

    An additional comment: although currently we first gather a bucket of full parameter then copy it to the ipc buffer, leading to 2x bucket size memory usage, we can change this logic to copy full param to ipc buffer as soon as all-gather finish, so that our memory usage is still 1x bucket size; this relies on #638 which calculates tensor metadata ahead of time, so i can create a PR after 638 is stablized.

  3. yuki-97 commented on Aug 18, 2025

    @yuki-97
    Contributor

    I think we need to improve the 2x bucket size memory usage to 1x, this may bring some refit speedup.

    more groups lead to slower refit
    with an interesting run which has different refit time itself, I found the speed of refit strongly related to the number of group, and the number of group is determined by the memory.

    step 1: 76s
    [Refit] Split 1718 keys into 321 groups
    step2: 110s
    [Refit] Split 1718 keys into 640 groups
    step9: 85s
    [Refit] Split 1718 keys into 380 groups
    

    https://wandb.ai/nvidia/grpo-dev-yukih-mcore/runs/zx2d6fz3/logs
    Image

    TP32DP8 slower than TP64DP4 may also because of this
    I have some test on vllm TP64DP4 and TP32DP8, the generation is faster but the refit is slower.

    the reason I think is that the vLLM model on each GPU of TP32DP8 is about 2x than TP64DP4, then the remaining memory for refit will be much less, then we need more groups in refit process.
    Image

    I also tested with non-colocated, which won't be affected by memory in refit time. the refit time of TP32DP8 and TP64DP4 only has small difference compared to the colocated one, so I believe the slowdown of colocated case is mainly caused by the memory.
    Image

  4. guyueh1 commented on Aug 18, 2025

    @guyueh1
    Contributor

    @YUki-666
    Current 2x memory overhead is because get_weights_ipc_handles has the following steps

    1. call gather_params on the key list (1x memory )
    2. call megatron_to_hf_converter on the megatron param list (in-place)
    3. create ipc buffer and copy hf param list (1x memory)
      Could we break these into fine-grained pipeline, for every key in the list perform step 1-3, so that the peak memory should just be slightly larger than 1x buffer size?
  5. yuki-97 commented on Aug 19, 2025

    @yuki-97
    Contributor

    thanks @guyueh1 , this implementation to reduce peak memory sounds good!

  6. guyueh1 commented on Aug 19, 2025

    @guyueh1
    Contributor

    @YUki-666 I would suggest to wait for #905 to merge or directly work on top of #905, becauese Bridge refactor will replace most of the existing refit code

  7. assigned and unassigned on Aug 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions