Repository navigation
fix: fix optimizer step working on random initialized model weights - #2372
Conversation
|
@terrykong, Hi Terry, feel free to take a look at the issue (#2371) and this PR when you get a chance! |
There was a problem hiding this comment.
Thanks — LGTM from a Bridge perspective. Same bug shape exists in Megatron-Bridge (HF → mcore init + optimizer_cpu_offload=True + distributed optimizer + BF16 hits the same stale FP32 master copies in HybridDeviceOptimizer), so I'll mirror this workaround on the Bridge side.
The root cause looks like it's in mcore — reload_model_params() only walks level 1 of HybridDeviceOptimizer and leaves levels 2/3 (CPU clones, FP32 working copy) at random init. Might be worth filing an mcore issue so the fix lands upstream and both RL and Bridge can drop their copies of this helper eventually — the workaround reaches into a few private attrs (_get_model_param_range_map, gpu_params_map_cpu_copy, param_to_fp32_param), so it's a bit fragile to carry long-term.
|
Thanks for the review @cuichenx! This is approved now but the workflows are still awaiting maintainer approval ("10 workflows awaiting approval"). Could a maintainer approve the CI runs (or /ok to test)? thanks! cc @terrykong |
|
Thanks for tracking this down and the really clear root-cause writeup, @pengdurice |
Signed-off-by: pengdurice <[email protected]>
Signed-off-by: pengdurice <[email protected]>
Signed-off-by: pengdurice <[email protected]>
Signed-off-by: pengdurice <[email protected]>
6c3f989 to
49a9bc3
Compare
just rebased, thank you! |
@RayenTian , would you mind give it a CICD run when you get a chance? thank you! |
|
/ok to test 9611687 |
Signed-off-by: pengdurice <[email protected]>
the lint check failed and I just fixed it, would you mind run cicd again? thank you! @RayenTian |
|
/ok to test 7c12b8c |
@RayenTian , thank you for triggering the CI. there is one job failed, but I believe that's irrelevant to my code change and it is due to the flaky test itself, would you mind take a look? thank you! |
@RayenTian , friendly reminder of this thread;-) Or let me know if I can do anything! |
|
Hi, @pengdurice. Sorry for late reply. It seems a flaky failed. Re-trigger CI now. |
|
/ok to test 87c062c |
@RayenTian , thank you! now there is the test coverage issue, however it doesn't seem to come from my PR change: it is likely introduced by other code changed then merged to this branch. Would you mind take a look? Thank you! |
|
@terrykong , would you please help take a look to this PR and merge it if it's okay? |
What does this PR do ?
Fix the bug of optimizing on random initialized model weights when a very very corner case combination is used when cpu offloading + distributed optimizer + precision = bf16 + using a hf checkpoint
Issues
#2371
run:
NRL_DISABLE_FP32_SYNC_FIX=1 bash tests/test_suites/llm/sft-qwen2.5-0.5b-1n2g-megatron-cpuoffload.shloss values ~10 which is logprobs of random prediction of ~100K vocab
When calling with
NRL_DISABLE_FP32_SYNC_FIX=0 bash tests/test_suites/llm/sft-qwen2.5-0.5b-1n2g-megatron-cpuoffload.shNote:
Some of the redundant debug codes can be cleaned up later, feel free to use them for reproducing the errors and fixes first.
Usage
NA
What is the bug
When cpu offload + using bf16 + distributed optimizer is enabled and model is initialized from a checkpoints (i.e. fine tuning), a hidden bug manifests. The reason is the bf16 cpu copy and the fp32 cpu copy for the hybrid device optimizer are not correctly flushed by the imported model checkpoints, i.e. they remain random initialized values; and later then the optimizer's step() works on them.
The fix
Force copying the hf checkpoint's model weights to these random initialized copies
Before your PR is "Ready for review"
Pre checks:
Additional Information