Skip to content

fix: preserve fp32 router correction bias during refit - #3540

Merged
yuki-97 merged 3 commits into
mainfrom
jia/preserve-fp32-router-bias
Aug 26, 2026
Merged

yuki-97 merged 3 commits into
mainfrom
jia/preserve-fp32-router-bias

Conversation

@fujial-code

@fujial-code fujial-code commented Aug 7, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

Preserve the Nemotron-Nano V3 MoE e_score_correction_bias buffer in FP32 during DTensor policy refit instead of unconditionally casting it to the policy dtype (typically BF16).

The change keeps the refit payload and its metadata consistent:

  • dtensor_params_generator sends the router correction bias in its original FP32 dtype.
  • prepare_refit_info advertises FP32 for the same tensor.
  • Other refit tensors continue to use the configured policy dtype.

Why?

e_score_correction_bias is maintained in FP32, but the current refit path casts every tensor to self.dtype. For BF16 policies, that downcast can introduce coarse rounding in the router correction values and change expert routing between the trainer and generation model.

This prevents an avoidable FP32-to-BF16 precision loss for the router correction bias in the full-weight refit path.

Experimental context

The plot below shows the 50-step merged-LoRA run with the FP32 router-bias preservation enabled together with a stream fence.

train/gen_kl_error for the 50-step merged-LoRA run

train/gen_kl_error starts near the 0.002 scale, grows after approximately step 18, and reaches a transient peak of approximately 0.102 around step 26 before returning near its initial scale. The fix prevents the FP32 router correction state from being quantized during each refit, removing that source of precision loss from the merged full-weight sync path.

Issues

No linked issue.

Usage

No configuration change is required. FP32 preservation is selected only for tensors whose final fully qualified name is e_score_correction_bias and whose source dtype is FP32.

Testing

  • Added unit coverage for the transmitted tensor dtype.
  • Added unit coverage for matching refit metadata.
  • Verified that ordinary FP32 buffers are still converted to BF16.
  • git diff --check passes.
  • Python syntax compilation passes for the implementation and test files.
  • Targeted pytest was not run on the HSG login node because that environment does not provide torch or pytest; full Nano V3 regression remains pending.

Before this PR is ready for review

  • Followed the contribution conventions and signed off the commit.
  • Added focused unit tests.
  • Run the targeted unit tests in the NeMo-RL test environment.
  • Run the Nano V3 trainer-generation logprob/KL regression.

@copy-pr-bot

copy-pr-bot Bot commented Aug 7, 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.

@fujial-code

Copy link
Copy Markdown
Contributor Author

/ok to test 7a849d7

@fujial-code

Copy link
Copy Markdown
Contributor Author

/ok to test 7a849d7

@fujial-code
fujial-code force-pushed the jia/preserve-fp32-router-bias branch from 7a849d7 to f5984ab Compare August 17, 2026 08:01
@fujial-code fujial-code added the CI:L2 Run doctests, unit tests, functional tests, and convergence tests label Aug 17, 2026
@fujial-code

Copy link
Copy Markdown
Contributor Author

/ok to test f5984ab

@fujial-code fujial-code left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Self-review (/review-pr-team)

Reviewed the rebased two-file change at f5984ab954d97fc6ea460221e55bf68442ab0e31 with parallel RL/design, bug/test, comment/process, and independent devil's-advocate passes. No remaining correctness, mixed-dtype transport, or design findings were identified.

What was verified

  • The pinned Automodel implementation creates e_score_correction_bias in FP32 and documents that BF16 quantization can change expert routing.
  • The refit payload and state_dict_info metadata use the same dtype-selection helper after HF state-dict adaptation.
  • Static inspection found per-tensor dtype support in the IPC, collective, HTTP, and checkpoint-engine refit paths.

Findings resolved before staging this review

  • Updated the generator docstring to describe target_dtype as the default and document the FP32 router-bias exception.
  • Applied the Ruff formatting change reported by the previous CI run.
  • Rebasing onto current main leaves this PR one commit ahead with the same two-file scope.
  • Added the required CI:L2 label and reauthorized CI for the new SHA.

Verification status

  • git diff --check: passed.
  • DCO, title, copyright, and secrets checks: passed or running cleanly on the new SHA.
  • The full CI:L2 test matrix is currently queued/running. Local runtime tests were not claimed because the login node has neither uv nor a working GPU environment.
  • The existing 50-step plot combines router-FP32 preservation with a stream fence, so it should be treated as preliminary rather than an isolated causal A/B result.

No inline findings remain on the current head. The PR should stay in Draft until the targeted/runtime CI results are available.

Generated by Claude Code

@fujial-code
fujial-code marked this pull request as ready for review August 18, 2026 06:08
@fujial-code
fujial-code requested review from a team as code owners August 18, 2026 06:08
@fujial-code
fujial-code requested a review from yuki-97 August 18, 2026 06:08
@fujial-code

Copy link
Copy Markdown
Contributor Author

/ok to test d3974b2

yuekaizhang added a commit to mferrato/RL that referenced this pull request Aug 26, 2026
PR NVIDIA-NeMo#3540 already preserves the FP32 e_score_correction_bias MoE router
buffer during DTensor refit; this PR's more general
_keep_in_fp32_modules(_strict) mechanism duplicated that fix. Drop it
here to avoid overlapping/conflicting changes and let NVIDIA-NeMo#3540 land the
fix; this PR keeps only the non-contiguous top-p logits and
keep_top_k: null fixes.

This reverts commit a697d80 and
commit 9edaf9d.

Signed-off-by: Yuekai Zhang <[email protected]>

@yuki-97 yuki-97 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM! thanks for the fix! @fujial-code

@yuki-97 yuki-97 added CI:Lfast Runs a fast test suite and re-use nightly `main` container (but sync dependencies to PRs version) and removed CI:L2 Run doctests, unit tests, functional tests, and convergence tests labels Aug 26, 2026
@yuki-97

yuki-97 commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

/ok to test 8f5336c

@yuki-97
yuki-97 enabled auto-merge (squash) August 26, 2026 14:41
@yuki-97
yuki-97 merged commit 3cea9e1 into main Aug 26, 2026
141 of 149 checks passed
@yuki-97
yuki-97 deleted the jia/preserve-fp32-router-bias branch August 26, 2026 17:24
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)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants