Skip to content

fix: fix optimizer step working on random initialized model weights - #2372

Merged
terrykong merged 7 commits into
NVIDIA-NeMo:mainfrom
pengdurice:peng-offload-optimizer-bf16-fix-v1
Jul 21, 2026
Merged

terrykong merged 7 commits into
NVIDIA-NeMo:mainfrom
pengdurice:peng-offload-optimizer-bf16-fix-v1

Conversation

@pengdurice

@pengdurice pengdurice commented Apr 29, 2026 •

Copy link
Copy Markdown
Contributor

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.sh
loss values ~10 which is logprobs of random prediction of ~100K vocab

Image

When calling with
NRL_DISABLE_FP32_SYNC_FIX=0 bash tests/test_suites/llm/sft-qwen2.5-0.5b-1n2g-megatron-cpuoffload.sh

Image

Note:

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:

  • Make sure you read and followed Contributor guidelines
  • Did you write any new necessary tests?
  • Did you run the unit tests and functional tests locally? Visit our Testing Guide for how to run tests
  • Did you add or update any necessary documentation? Visit our Document Development Guide for how to write, build and test the docs.

Additional Information

  • ...

@copy-pr-bot

copy-pr-bot Bot commented Apr 29, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@pengdurice pengdurice changed the title fix: fix the issue that the optimizer step works on random initialized model weights when cpu offloading + distributed optimizer + precision = bf16 + using a hf checkpoint fix: fix the issue that the optimizer step works on random initialized model weights in a less frequent combination Apr 30, 2026
@pengdurice pengdurice changed the title fix: fix the issue that the optimizer step works on random initialized model weights in a less frequent combination fix: fix the issue that the optimizer step works on random initialized model weights Apr 30, 2026
@pengdurice pengdurice changed the title fix: fix the issue that the optimizer step works on random initialized model weights fix: fix optimizer step working on random initialized model weights Apr 30, 2026
@pengdurice
pengdurice marked this pull request as ready for review April 30, 2026 19:16
@pengdurice
pengdurice requested review from a team as code owners April 30, 2026 19:16
@pengdurice
pengdurice marked this pull request as draft April 30, 2026 19:16
@pengdurice
pengdurice marked this pull request as ready for review April 30, 2026 19:17
@svcnvidia-nemo-ci svcnvidia-nemo-ci added the waiting-on-maintainers Waiting on maintainers to respond label May 2, 2026
@pengdurice

Copy link
Copy Markdown
Contributor Author

@terrykong, Hi Terry, feel free to take a look at the issue (#2371) and this PR when you get a chance!

@svcnvidia-nemo-ci svcnvidia-nemo-ci added waiting-on-maintainers Waiting on maintainers to respond and removed waiting-on-maintainers Waiting on maintainers to respond labels May 8, 2026
@pengdurice pengdurice mentioned this pull request May 13, 2026
2 of 4 tasks

@terrykong terrykong left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cuichenx @yaoyu-33 could you review?

@terrykong
terrykong requested review from cuichenx and yaoyu-33 May 20, 2026 07:19
@svcnvidia-nemo-ci svcnvidia-nemo-ci added waiting-on-maintainers Waiting on maintainers to respond and removed waiting-on-maintainers Waiting on maintainers to respond labels May 20, 2026
@pengdurice

Copy link
Copy Markdown
Contributor Author

@cuichenx @yaoyu-33 , hi nice to meet you folks, this is Peng. This PR is to resolve an optimizer working on random model weight issue. Would you please review it? thank you!

@svcnvidia-nemo-ci svcnvidia-nemo-ci added waiting-on-maintainers Waiting on maintainers to respond and removed waiting-on-maintainers Waiting on maintainers to respond labels May 27, 2026
cuichenx
cuichenx previously approved these changes May 29, 2026

@cuichenx cuichenx left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@pengdurice

Copy link
Copy Markdown
Contributor Author

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

@RayenTian

Copy link
Copy Markdown
Contributor

Thanks for tracking this down and the really clear root-cause writeup, @pengdurice
Would you please rebase the branch, and then we can trigger CI.

@svcnvidia-nemo-ci svcnvidia-nemo-ci added waiting-on-customer Waiting on the original author to respond and removed waiting-on-maintainers Waiting on maintainers to respond labels Jun 25, 2026
@pengdurice
pengdurice force-pushed the peng-offload-optimizer-bf16-fix-v1 branch from 6c3f989 to 49a9bc3 Compare June 25, 2026 16:38
@pengdurice

Copy link
Copy Markdown
Contributor Author

Thanks for tracking this down and the really clear root-cause writeup, @pengdurice Would you please rebase the branch, and then we can trigger CI.

just rebased, thank you!

@svcnvidia-nemo-ci svcnvidia-nemo-ci added waiting-on-maintainers Waiting on maintainers to respond and removed waiting-on-customer Waiting on the original author to respond labels Jun 25, 2026
@pengdurice

Copy link
Copy Markdown
Contributor Author

Thanks for tracking this down and the really clear root-cause writeup, @pengdurice Would you please rebase the branch, and then we can trigger CI.

just rebased, thank you!

@RayenTian , would you mind give it a CICD run when you get a chance? thank you!

@svcnvidia-nemo-ci svcnvidia-nemo-ci removed the waiting-on-maintainers Waiting on maintainers to respond label Jul 1, 2026
@RayenTian

Copy link
Copy Markdown
Contributor

/ok to test 9611687

Signed-off-by: pengdurice <[email protected]>
@pengdurice

pengdurice commented Jul 2, 2026 •

Copy link
Copy Markdown
Contributor Author

/ok to test 9611687

the lint check failed and I just fixed it, would you mind run cicd again? thank you! @RayenTian

@RayenTian

Copy link
Copy Markdown
Contributor

/ok to test 7c12b8c

@pengdurice

Copy link
Copy Markdown
Contributor Author

/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!

@svcnvidia-nemo-ci svcnvidia-nemo-ci added the waiting-on-maintainers Waiting on maintainers to respond label Jul 5, 2026
@pengdurice

Copy link
Copy Markdown
Contributor Author

/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!

@RayenTian

Copy link
Copy Markdown
Contributor

Hi, @pengdurice. Sorry for late reply. It seems a flaky failed. Re-trigger CI now.

@RayenTian

Copy link
Copy Markdown
Contributor

/ok to test 87c062c

@pengdurice

Copy link
Copy Markdown
Contributor Author

Hi, @pengdurice. Sorry for late reply. It seems a flaky failed. Re-trigger CI now.

@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!

@RayenTian

Copy link
Copy Markdown
Contributor

@terrykong , would you please help take a look to this PR and merge it if it's okay?

@svcnvidia-nemo-ci svcnvidia-nemo-ci removed the waiting-on-maintainers Waiting on maintainers to respond label Jul 21, 2026
@terrykong
terrykong enabled auto-merge (squash) July 21, 2026 06:37
@terrykong
terrykong merged commit 30b24ce into NVIDIA-NeMo:main Jul 21, 2026
78 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CI:Lfast Runs a fast test suite and re-use nightly `main` container (but sync dependencies to PRs version) community-request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants