Skip to content

feat(vllm): fp32 LM head for all models via vLLM head_dtype - #5061

Merged
ZhiyuLi-Nvidia merged 5 commits into
mainfrom
nvegesna/vllm-fp32-lm-head-all-models
Nov 4, 2026
Merged

ZhiyuLi-Nvidia merged 5 commits into
mainfrom
nvegesna/vllm-fp32-lm-head-all-models

Conversation

@nvegesna-netizen

Copy link
Copy Markdown
Contributor

Reopens #4922, which GitHub closed when its source fork was detached. Same commits (head 1cf892c), now on an upstream branch. #4922 was approved by guyueh1.

What does this PR do ?

Makes policy.generation.vllm_cfg.fp32_lm_head work for every model, not only Nemotron-H. It now sets vLLM's
native 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=true on any non-Nemotron-H model only produces a warning, and
generation keeps the BF16 head. The pairing that validate_fp32_lm_head_config enforces therefore ends up
effectively 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, adds head_dtype="float32" to the engine's
    hf_overrides.
    • vLLM's LogitsProcessor then 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's logit_dtype=float32, which
      megatron_cfg.fp32_lm_head sets.
    • It works for any model whose lm_head is unquantized; the FP8 rollout path already excludes lm_head by
      default.
    • Unlike the old patch, it does not cast the lm_head weight to an FP32 copy on every step.
  • Removed: _patch_vllm_nemotron_h_fp32_lm_head, the NRL_VLLM_FP32_LM_HEAD env plumbing, and the
    non-Nemotron-H warning, together with the HF-config fetch that warning needed in Policy.__init__. Once
    head_dtype is set, vLLM never calls lm_head.forward or quant_method.apply, so the patch would be dead code.
  • Validation:
    • NRL_VLLM_FP32_LM_HEAD in vllm_cfg.env_vars is still rejected, now with "no longer read".
    • A head_dtype in policy.hf_config_overrides or generation.vllm_kwargs.hf_overrides is rejected, so the
      flag stays the single switch covered by the trainer-match check.
    • In the worker, a conflicting head_dtype override raises.
  • Tests:
    • New tests/unit/models/generation/test_vllm_fp32_lm_head.py, including a @pytest.mark.vllm test that pins
      vLLM's head_dtype contract.
    • Nemotron-H patch tests removed. The routed-experts-capture threading coverage they carried is kept as two
      smaller tests.
  • Functional test (tests/functional/vllm_fp32_lm_head.sh): now checks the numerics directly instead of a log
    line.
    • It uses logprobs_mode="raw_logits", so vLLM returns the sampled token's raw logit.
    • With the BF16 head (control), every value is BF16-representable. With the FP32 head, at least half must be off
      the BF16 grid.
    • It runs on Qwen3-0.6B (tied embeddings) and NVIDIA-Nemotron-3-Nano-4B (the previously supported path).

Usage

policy:
  megatron_cfg:
    fp32_lm_head: true
  generation:
    vllm_cfg:
      fp32_lm_head: ${policy.megatron_cfg.fp32_lm_head}  # already the default wiring in the example configs

Validation

Run on H200 ×2 with the locked environment (uv sync --locked --extra vllm --group test, vLLM 0.29.0), main at
4d464d5 vs this PR:

Check main this PR
Unit: test_vllm_patches.py, test_policy_validation.py, test_vllm_fp8_hf_overrides.py, test_vllm_fp32_lm_head.py (new) 68 passed, 21 errors 62 passed, 21 errors
Unit, --vllm-only 16 passed 16 passed
Functional, Qwen/Qwen3-0.6B (tied embeddings) — PASS: 16/16 sampled raw logits off the bf16 grid (bf16 head: 0/16)
Functional, nvidia/NVIDIA-Nemotron-3-Nano-4B-BF16 — PASS: 16/16 off the bf16 grid (bf16 head: 0/16)
Mutation: _apply_fp32_lm_head forced to a no-op — functional check FAILS as intended (fp32 head looks bf16-rounded)
  • The 21 errors are the same tests on both sides. All are gated-repo 401s (meta-llama/Llama-3.2-1B) because the
    runner had no HF token.
  • Test-count change: 23 Nemotron-H patch tests are removed and 17 new tests are added. The --vllm-only count
    stays at 16: the new head_dtype contract test replaces the Nemotron-H source-anchor test.
  • TP=2 with CUDA graphs (a separate check, not part of this PR's tests): GH200, tensor_parallel_size=2,
    enforce_eager=False (FULL_AND_PIECEWISE graphs 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 fp32
      heads (42/42 Qwen, 51/51 Nemotron; mean |Δ| ≈ 0.013–0.015). So the fp32 logits reach log_softmax; vLLM's
      sampler only upcasts.
  • Environment: the functional test ran with NEMO_RL_PY_EXECUTABLES_SYSTEM=1, so all actors share one venv, and
    with deep_ep/deep_gemm not 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, one
GB200 node per arm, 3 steps, seed 42, plus a replicate), an FP32 LM head on both sides cut gen_kl_error 74%
(7.5e-4 → 2.0e-4; the replicate is within 2%) and token_mult_prob_error − 1 by 49%. Megatron's FP32 head alone
gives only −37%. Setting policy.generation.vllm_kwargs.hf_overrides.head_dtype by hand does not work in GRPO:
grpo.py overwrites vllm_kwargs["hf_overrides"] with policy.hf_config_overrides (ppo.py and distillation.py do
the same), so the override never reaches vLLM. (The two-sided run above patched that line to merge the dicts.) This
PR sets head_dtype inside the vLLM worker, after that overwrite. Today a two-sided setup only works for
Nemotron-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:

  • 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? (on an H200 node; see Validation)
  • Did you add or update any necessary documentation? (config docstrings and example-config comments; no docs page mentions the flag)

Additional Information

🤖 Generated with Claude Code

nvegesna-netizen and others added 4 commits October 25, 2026 15:41
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]>
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]>
@nvegesna-netizen nvegesna-netizen added the CI:L2 Run doctests, unit tests, functional tests, and convergence tests label Nov 4, 2026
@nvegesna-netizen nvegesna-netizen self-assigned this Nov 4, 2026
@copy-pr-bot

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

@nvegesna-netizen

Copy link
Copy Markdown
Contributor Author

/ok to test c1f4981

@ZhiyuLi-Nvidia
ZhiyuLi-Nvidia enabled auto-merge (squash) November 4, 2026 00:54
@ZhiyuLi-Nvidia
ZhiyuLi-Nvidia merged commit c6bc34c into main Nov 4, 2026
580 of 599 checks passed
@ZhiyuLi-Nvidia
ZhiyuLi-Nvidia deleted the nvegesna/vllm-fp32-lm-head-all-models branch November 4, 2026 22:03
isomap added a commit to isomap/RL that referenced this pull request Nov 5, 2026
Brings in NVIDIA-NeMo#5061 (vLLM fp32 LM head via head_dtype), NVIDIA-NeMo#5058 and NVIDIA-NeMo#4825. No
conflicts; NVIDIA-NeMo#5061 changes the fp32_lm_head comment next to this branch's
logprobs_mode comment in vllm/config.py.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Hayate Iso <[email protected]>
yeyu-nvidia added a commit to yeyu-nvidia/RL that referenced this pull request Nov 25, 2026
Conflict resolutions:
- vllm_quant_backend.py / vllm_modelopt.py: keep NVIDIA-NeMo#3945 native real-quant;
  NVIDIA-NeMo#5153 only ported the legacy nemo_modelopt path, which NVIDIA-NeMo#3945 removes.
- prepare_nccl_reshard_refit_info: keep the draft-aware manifest, add
  NVIDIA-NeMo#5153 follow_source_reshard_path.
- _finish_train_step_body: keep the CP-aware draft count reductions, add
  NVIDIA-NeMo#5153 in-loss sequence-filter survivor counts.
- RADIO final LayerNorm patch: NVIDIA-NeMo#5153 0.33 anchors, but load through the
  param weight_loader hook so native refit tracks it (38cd96a).
- Drop the stale _patch_vllm_nemotron_h_fp32_lm_head (superseded upstream
  by NVIDIA-NeMo#5061; referenced an undefined constant, never called).
- Real-quant suites: grep quantization=modelopt only (0.33 dropped the
  Detected ModelOpt NVFP4 checkpoint line).
- uv.lock: NVIDIA-NeMo#5153, to be re-locked.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Ye Yu <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CI:L2 Run doctests, unit tests, functional tests, and convergence tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants