Repository navigation
feat(vllm): fp32 LM head for all models via vLLM head_dtype - #4922
nvegesna-netizen wants to merge 4 commits into
Conversation
vllm_cfg.fp32_lm_head now sets vLLM's native hf_overrides head_dtype="float32" (vllm-project/vllm#48390, in the pinned 0.29.0), which runs the lm_head GEMM on the bf16 operands with an fp32 output for any model whose lm_head is unquantized. That is the same GEMM as Megatron's logit_dtype=float32 (policy.megatron_cfg.fp32_lm_head), so the matched pair now removes the head's bf16 rounding from the generation/training logprob mismatch for every model, not only Nemotron-H. With head_dtype set, vLLM's LogitsProcessor no longer calls lm_head.forward or quant_method.apply, so the Nemotron-H source patch would be dead code; it is removed together with its env-var plumbing and the non-Nemotron-H warning. NRL_VLLM_FP32_LM_HEAD in vllm_cfg.env_vars and a head_dtype in the hf overrides are rejected so the flag stays the single, validated switch. The functional test now checks the result instead of a log line: with logprobs_mode=raw_logits the sampled token's raw logit is bf16-representable with the bf16 head (control) and off the bf16 grid with the fp32 head, on Qwen3-0.6B (tied embeddings) and Nemotron-3-Nano-4B. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Nitin Vegesna <[email protected]>
nvegesna-netizen
left a comment
There was a problem hiding this comment.
Self-review by a review team: RL code, vLLM upstream, tests, bug scan, design, PR history, and a devil's-advocate pass that challenged every finding. No finding reached the bar for an ask. Below is what was checked, then two notes for later.
Checked against the pinned code
- vLLM side. The pinned vLLM 0.29.0 contains vllm-project/vllm#48390. With
head_dtype="float32",LogitsProcessor._apply_headruns the head GEMM on the bf16 operands with an fp32 output (torch.mm(..., out_dtype=self.head_dtype)). The logits stay fp32 through the TP gather and the sampler, for bothlogprobs_modevalues. A quantizedlm_headraises when the engine starts, so it cannot fail silently. - Megatron side.
megatron_cfg.fp32_lm_headsetslogit_dtype = torch.float32, and Megatron-Core's_linear_forwardalso runs bf16 operands with an fp32 output. The precision matches vLLM; the kernels differ, so the logits are close but not bit-for-bit equal. - Every vLLM worker gets the setting. The sync, async and both quantized workers build their engine through
_load_model, which calls_apply_fp32_lm_headafter_merge_fp8_kwargs. The async worker reuses it (vllm_worker_async.py). Later steps only addquantization_config, sohead_dtypeis kept. - Every model NeMo-RL runs. All 244
compute_logitsdefinitions in vLLM at the pinned commit were checked. Every model NeMo-RL uses goes through_apply_head, including tied embeddings, VLM wrappers and MTP/EAGLE draft heads. - No leftovers. Nothing in the repo still refers to the removed Nemotron-H patch, its env plumbing or its helpers.
- The legacy env-var rejection is needed. Two recipes on
super-v3.5-posttrainingsetNRL_VLLM_FP32_LM_HEAD: '1'(reasoning_teacher.yaml,student_rlvr.yaml). When they reach main, the "no longer read" error stops them from silently running a bf16 head. - The functional check is sound. With
raw_logits, a bf16 head can only give values on the bf16 grid, even after soft-capping and scaling; an fp32 head gives almost none. The>= halfthreshold is far from flaky, and the bf16 control run catches a check that silently does nothing. The test runs in L1, on every merge group and push to main, so a vLLM bump that stops honoringhead_dtype, or a refactor that drops the_apply_fp32_lm_headcall, cannot merge unnoticed. - Lint. ruff and ruff-format pass on the changed files.
Notes for later (not asks for this PR)
- Dynamo, SGLang and TRT-LLM still get a one-sided head. With
megatron_cfg.fp32_lm_head=trueon those backends,validate_fp32_lm_head_configreturns early, and Dynamo only shows its generic "vllm_cfg keys ignored by backend='dynamo'" warning. This dates from #4096, and the PR description lists it under "Not covered".- A follow-up can map
vllm_cfg.fp32_lm_headinto Dynamo'shf_overridesand run the trainer-match check forbackend="dynamo". - Do not move the
head_dtyperejection above that early return: todaypolicy.hf_config_overrides.head_dtype, which is copied into Dynamo'shf_overrides, is Dynamo's only way to get an fp32 head.
- A follow-up can map
- Rebase note for #4518. #4518 touches 4 of the same files; 3 conflict, and
nemo_rl/models/policy/utils.pymerges cleanly. Whichever PR lands second should:- in
nemo_rl/models/generation/vllm/config.py, keep this PR'svllm_fp32_lm_head_enabledname with #4518'sfp32_lm_head_enabled(...)body andFp32LmHeadSettingtype; - move #4518's two new cases in
test_vllm_patches.pyintotests/unit/models/generation/test_vllm_fp32_lm_head.pyunder the new name, since the old names are gone and that file would fail to load.
- in
Not verified in this review: nothing ran on a GPU or on a real vLLM install. The GPU results are the H200/GH200 runs in the description.
Generated by Claude Code
Resolve the conflict in nemo_rl/models/policy/utils.py: keep main's LoRA refit helpers (#3964) and drop the _NEMOTRON_H_* constants, which this branch no longer uses. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Nitin Vegesna <[email protected]>
|
/ok to test e64fb77 |
@guyueh1, there was an error processing your request: See the following link for more information: https://docs.gha-runners.nvidia.com/cpr/e/2/ |
|
@nvegesna-netizen this is a nice cleanup overall, but the vllm head_dtype=float32 and the current NemotronH patch have a difference:
My understanding is, the two are numerically same if the |
|
/ok to test e64fb77 |
|
Thanks, good question. Short answer: the context manager isn't needed, and the I checked on a B200 (torch 2.13.0+cu130, TE 2.18.0) with Qwen3-8B head shapes (hidden size 4096, vocab 151,936), batch sizes 1–2048:
These comparisons use equal batch sizes on both sides. In real runs the trainer and vLLM batch tokens differently, and cuBLAS may then pick different kernels, so individual logits can differ in the last FP32 bits. Precision stays the same. |
|
sounds good |
|
/ok to test 52ae9fc |
|
/ok to test 52ae9fc |
|
/ok to test 1cf892c |
|
Thank you @nvegesna-netizen. Awesome and clean PR. LGTM! |
Pull request was closed
What does this PR do ?
Makes
policy.generation.vllm_cfg.fp32_lm_headwork for every model, not only Nemotron-H. It now sets vLLM'snative
hf_overrides={"head_dtype": "float32"}(vllm-project/vllm#48390,in the pinned vLLM 0.29.0) and removes the Nemotron-H-only source patch.
Issues
Follow-up to #4096. Today
vllm_cfg.fp32_lm_head=trueon any non-Nemotron-H model only produces a warning, andgeneration keeps the BF16 head. The pairing that
validate_fp32_lm_head_configenforces therefore ends upeffectively one-sided for Qwen, Llama and other models, which its own error message says makes the mismatch worse.
Changes
vllm_worker._apply_fp32_lm_head: when the flag is set, addshead_dtype="float32"to the engine'shf_overrides.LogitsProcessorthen runs the lm_head GEMM on the BF16 operands with an FP32 output(
torch.mm(..., out_dtype=float32)). That is the same GEMM as Megatron'slogit_dtype=float32, whichmegatron_cfg.fp32_lm_headsets.lm_headbydefault.
_patch_vllm_nemotron_h_fp32_lm_head, theNRL_VLLM_FP32_LM_HEADenv plumbing, and thenon-Nemotron-H warning, together with the HF-config fetch that warning needed in
Policy.__init__. Oncehead_dtypeis set, vLLM never callslm_head.forwardorquant_method.apply, so the patch would be dead code.NRL_VLLM_FP32_LM_HEADinvllm_cfg.env_varsis still rejected, now with "no longer read".head_dtypeinpolicy.hf_config_overridesorgeneration.vllm_kwargs.hf_overridesis rejected, so theflag stays the single switch covered by the trainer-match check.
head_dtypeoverride raises.tests/unit/models/generation/test_vllm_fp32_lm_head.py, including a@pytest.mark.vllmtest that pinsvLLM's
head_dtypecontract.smaller tests.
tests/functional/vllm_fp32_lm_head.sh): now checks the numerics directly instead of a logline.
logprobs_mode="raw_logits", so vLLM returns the sampled token's raw logit.the BF16 grid.
Usage
Validation
Run on H200 ×2 with the locked environment (
uv sync --locked --extra vllm --group test, vLLM 0.29.0), main at4d464d5 vs this PR:
test_vllm_patches.py,test_policy_validation.py,test_vllm_fp8_hf_overrides.py,test_vllm_fp32_lm_head.py(new)--vllm-onlyQwen/Qwen3-0.6B(tied embeddings)nvidia/NVIDIA-Nemotron-3-Nano-4B-BF16_apply_fp32_lm_headforced to a no-opfp32 head looks bf16-rounded)meta-llama/Llama-3.2-1B) because therunner had no HF token.
--vllm-onlycountstays at 16: the new
head_dtypecontract test replaces the Nemotron-H source-anchor test.tensor_parallel_size=2,enforce_eager=False(FULL_AND_PIECEWISEgraphs captured).raw_logits: fp32 head 64/64 off the bf16 grid, bf16 head 0/64, on both models.processed_logprobs(what training consumes): every matched greedy token differs between the bf16 and fp32heads (42/42 Qwen, 51/51 Nemotron; mean |Δ| ≈ 0.013–0.015). So the fp32 logits reach
log_softmax; vLLM'ssampler only upcasts.
NEMO_RL_PY_EXECUTABLES_SYSTEM=1, so all actors share one venv, andwith
deep_ep/deep_gemmnot installed. Neither is used by these models.Why this matters: on Qwen3-8B GRPO in NeMo-RL (
grpo_math_8B_megatron, nightly 3cde458 with vLLM 0.29.0, oneGB200 node per arm, 3 steps, seed 42, plus a replicate), an FP32 LM head on both sides cut
gen_kl_error74%(7.5e-4 → 2.0e-4; the replicate is within 2%) and
token_mult_prob_error − 1by 49%. Megatron's FP32 head alonegives only −37%. Setting
policy.generation.vllm_kwargs.hf_overrides.head_dtypeby hand does not work in GRPO:grpo.pyoverwritesvllm_kwargs["hf_overrides"]withpolicy.hf_config_overrides(ppo.py and distillation.py dothe same), so the override never reaches vLLM. (The two-sided run above patched that line to merge the dicts.) This
PR sets
head_dtypeinside the vLLM worker, after that overwrite. Today a two-sided setup only works forNemotron-H, because NeMo-RL rejects one-sided configs; this PR makes it reachable for other models.
Before your PR is "Ready for review"
Pre checks:
Additional Information
fp32_lm_headand touchvllm/config.pyandpolicy/utils.py. Whichever lands second needs a small rebase.VllmGenerationWorker, so theynever had the FP32 head and still do not.
🤖 Generated with Claude Code