You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
OOM during refit in mcore path when using larger bucket sizes #630
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.
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
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
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.
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
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.
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.
@YUki-666
Current 2x memory overhead is because get_weights_ipc_handles has the following steps
call gather_params on the key list (1x memory )
call megatron_to_hf_converter on the megatron param list (in-place)
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?
@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
Activity
guyueh1 commentedon Jul 9, 2025
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
guyueh1 commentedon Jul 14, 2025
I did some investigation and two issues came up:
_held_gather_bufferin time, this memory is held for the rest of the step. !664 can remove this part of the overhead and shouldn't change functionalityAn 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.
yuki-97 commentedon Aug 18, 2025
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.
https://wandb.ai/nvidia/grpo-dev-yukih-mcore/runs/zx2d6fz3/logs

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.

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.

guyueh1 commentedon Aug 18, 2025
@YUki-666
Current 2x memory overhead is because get_weights_ipc_handles has the following steps
gather_paramson the key 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?
yuki-97 commentedon Aug 19, 2025
thanks @guyueh1 , this implementation to reduce peak memory sounds good!
guyueh1 commentedon Aug 19, 2025
@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