Skip to content

feat(modelopt): refit native FP8 and NVFP4 KV caches - #5516

Draft
mxinO wants to merge 5 commits into
mainfrom
mxin/realquant-kv-refresh-20261201
Draft

mxinO wants to merge 5 commits into
mainfrom
mxin/realquant-kv-refresh-20261201

Conversation

@mxinO

@mxinO mxinO commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor
  • I, the PR author, have personally reviewed every line of this PR.

What and why

Follow up to #3945: add the NeMo RL consumer for ModelOpt's native FP8/NVFP4 KV caches, including KV-only recipes and combined weight+KV recipes.

ModelOpt remains responsible for quantization and scale semantics. Bridge emits Q/K/V scale companions in its existing QKV export groups. NeMo RL uses the existing IPC/collective refit stream and vLLM's native scale storage, validating coverage and preserving buffer addresses. Changed scales refresh cached attention coefficients and CUDA graphs after KV wake-up; fused attention/output quantization refreshes after every refit because it also caches weight-side scales.

flowchart LR
  A[ModelOpt recipe and Q/K/V scales] --> B[Bridge QKV export groups]
  B --> C[Existing NeMo RL refit transport]
  C --> D[vLLM native KV storage and scale buffers]
  D --> E[Refresh cached coefficients and graphs]
Loading

Depends on #5427: that PR bumps the Bridge pin to one that includes both Bridge PRs listed below. Main's current pin rejects KV quantizers, so this does not work end to end until #5427 merges. This PR adds no dependency upgrades, new tensor transport, attention kernels, or study-specific behavior. Learner P-QDQ remains deferred; standard attention is targeted, with MLA rejected.

Issues

Follow-up to #3945.

Convergence / perf / functionality proof

  • Fresh Codex and Claude role-based source reviews completed; confirmed findings addressed and retained-reviewer follow-ups clean. A full line-by-line audit and same-reviewer recheck also completed with no remaining actionable source findings.
  • Ruff 0.9.9 lint, import sorting and formatting pass. Scoped Pyrefly 1.4.0 passes for seven production modules. The expanded GRPO check reports the same four diagnostics before and after these fixes in the same runtime.
  • 158 targeted tests pass, with no skips: 47 native-marked KV/real-quant cases, 90 metadata/configuration/refit regressions, 15 refit-readiness cases and six existing safety-guard checks. Coverage includes two-rank Gloo error/change consensus, deferred KV wake-up failure propagation, native MXFP8 plus FP8-KV validation, and native registry-owner bindings. Runtime: vLLM 0.33.0, Torch 2.14.0+cu130, Transformers 5.16.1, FlashInfer 0.7.2.post1, ModelOpt 3c815f8 (0.49.0rc1.dev40). Scoped Pyrefly also passes in that container.
  • Fault injection confirmed the strengthened two-rank Gloo check rejects missing error propagation; the previous assertions incorrectly passed the same broken-consensus control. A separate mixed-NVFP4 dispatch check fails at the intended assertion when its metadata repair is removed.
  • Earlier tiny combined weight+KV training/resume checks covered calibrated and constant-amax FP8/NVFP4 on vLLM 0.29. Those results do not qualify vLLM 0.33.
  • Full KV-engine startup, native transport, attention CUDA-graph replay, combined training/resume, convergence and performance qualification on the current stack remain pending. Unit tests do not establish those results. Usage and limitations are documented in docs/guides/quantization-aware-rl.md.

Needs upstream changes

PR commands and CI
  • CI label, exactly one:
    • CI:Lfast: the default for most PRs. Runs a fast test subset.
    • CI:L1: dependency or container changes (pyproject.toml, uv.lock, Dockerfile) or broad core changes. Builds a new image and runs unit and functional tests.
    • CI:docs: docs-only changes. Runs doc tests only.
  • /ok to test <full-commit-sha>: start CI for that commit. Post again after each push.
  • /nightly-ci test-case=<test>: run NVIDIA's nightly CI for the PR head; eligible teams only.
  • /review: automated review comments. Add mode=strict for a stricter review, reset=true for a fresh full review.
  • /resolve-conflict: Autobot merges main into this PR and resolves the conflicts. PRs from forks must allow edits by maintainers.
  • Locally: /review-pr-team <pr-number> for a team review before you ask for human review.
  • Contributor guidelines

@mxinO mxinO added the CI:Lfast Runs a fast test suite and re-use nightly `main` container (but sync dependencies to PRs version) label Dec 2, 2026
@copy-pr-bot

copy-pr-bot Bot commented Dec 2, 2026

Copy link
Copy Markdown

Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

@svcnvidia-nemo-ci svcnvidia-nemo-ci added complexity:high Large or cross-cutting change. Set by Autobot PR classify (skills/pr-classify) convergence-risk:high Likely to change training results. Set by Autobot PR classify (skills/pr-classify) proof:functionality Needs proof that it works. Set by Autobot PR classify (skills/pr-classify) proof:convergence Needs convergence proof (W&B runs). Set by Autobot PR classify (skills/pr-classify) proof:perf Needs performance proof. Set by Autobot PR classify (skills/pr-classify) labels Dec 2, 2026
mxinO added 2 commits December 2, 2026 16:30
Propagate deferred cache-wake failures and keep failed synchronizations stale. Permit native MXFP8 with FP8 KV only when ModelOpt owns the attention-scale lifecycle, retaining the legacy safety guards. Cover the failure paths and native quantization registry bindings.

Signed-off-by: Meng Xin <[email protected]>
Require remote error propagation in the Gloo checks, cover both KV transport wrappers and their fatal-error state, and start the transfer-failure regression from current weights. Clarify simulated versus native KV recipes and native post-load scale validation.

Signed-off-by: Meng Xin <[email protected]>
@svcnvidia-nemo-ci svcnvidia-nemo-ci removed the proof:perf Needs performance proof. Set by Autobot PR classify (skills/pr-classify) label Dec 3, 2026
mxinO added 2 commits December 2, 2026 19:53
Remove the immediate-recapture fallback with no production caller. Use explicit callbacks in lifecycle tests and replace the unused NVFP4 metadata-field assertion with mixed-config attention-method selection.

Signed-off-by: Meng Xin <[email protected]>
@mxinO

mxinO commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

End-to-end KV-cache training check (with #5427's Bridge pin)

Setup

  • Image built from this PR head (a64db69f) + ci: bump MBridge to 1ec9ce8b0 #5427 (Bridge 1ec9ce8b0, transformers 5.21.1) + current main, plus the one-line transformers guard fix from ci: bump MBridge to 1ec9ce8b0 #5427 (comment).
  • Qwen3-8B-Base DAPO GRPO (run_grpo.py), NVFP4 W4A16 MLP weights in every arm, colocated CUDA-IPC refit (KV sleep/wake), 4×4 GB300.
  • 30 steps per arm, same seed and calibration. Only the K/V quantizer differs.

Results. vLLM starts with the native cache the policy config selects: kv_cache_dtype=fp8_e4m3 / nvfp4. Reward is the raw [0, 1] mean.

Arm (KV cache) Reward, steps 21–30 gen_kl_error 1–10 → 21–30 token_mult_prob_error median, 1–10 → 21–30
BF16 (control) 0.129 0.0010 → 0.0007 1.017 → 1.012
FP8, constant amax 0.121 0.0044 → 0.0027 1.093 → 1.035
FP8, calibrated 0.124 0.0043 → 0.0028 1.065 → 1.029
NVFP4, constant amax 0.077 0.0209 → 0.0175 7.30 → 8.72
NVFP4, calibrated – 0.0200 → ~0.013 48.0 (steps 1–10)
NVFP4 calibrated, fake quant (diagnostic, 10 steps) – 0.0115 1.77
  • FP8 KV works. Reward tracks the control. The gap is about 4× the control but stable, and it shrinks with training, so the scales refresh correctly across refits.
  • NVFP4 KV runs, refits and resumes, but its rollout does not match the trainer.
    • Resume: the calibrated run was cancelled after its step-15 checkpoint and resubmitted unchanged. gen_kl_error continues smoothly across the resume (0.018 → 0.019 → 0.013 by step 30).
    • Mismatch: the same recipe in fake-quant mode has a median token-probability ratio of 1.77; native NVFP4 storage has 7–48, and reward lags.

Root cause: a 6× scale-convention mismatch for NVFP4 KV.

  • Trainer. ModelOpt's fake NVFP4 uses a global scale of amax / (6 × 448) (NVFP4QTensor.get_weights_scaling_factor_2).
  • Export. ModelOpt exports KV cache scales as amax / 448 for both FP8 and NVFP4 (get_kv_cache_scaling_factor, "as the HF exporter writes them and vLLM / TensorRT-LLM read them"), and Bridge #6726 streams that value.
  • vLLM 0.33. FlashInferImpl.do_kv_cache_update passes layer._k_scale / _v_scale to FlashInfer's nvfp4_quantize_append_paged_kv_cache_with_slot_mapping as the global decode scale. The kernel (include/flashinfer/page.cuh, nvfp4_append_quantize_block) computes the per-block E4M3 scale as block_amax / (6 × global).
  • Effect. The native block scales sit 6× lower in the E4M3 range than the trainer's (max about 75 instead of 448), so they round differently from the trainer's simulation.

One-GPU check. I quantized the same K/V-shaped tensors with ModelOpt's TensorQuantizer (NVFP4, dynamic block-16) and with FlashInfer's native append kernel, then dequantized the cache:

Global scale passed to FlashInfer Native vs ModelOpt (K / V) Native vs original (K / V)
amax / 448 (today) 7.5% / 8.2–8.5% 9.5% / 9.5%
amax / (6 × 448) 0 / 0 (bit-exact) 9.5% / 9.5%

Constant and calibrated amax give the same picture. With constant amax, 44% of V block scales also land in E4M3's subnormal range today (2.9% with amax / 2688).

So amax / 448 gives about the same accuracy against the original, but it rounds differently from the trainer's simulation. Passing amax / (6 × 448) to the native NVFP4 KV path makes inference bit-identical to what the learner simulates.

Where the fix goes. The amax / 448 checkpoint convention is shared by ModelOpt, vLLM and TensorRT-LLM, so I would not change the export. The divide-by-6 belongs where the value becomes FlashInfer's NVFP4 global scale, either in vLLM's NVFP4 KV path (which would also fix plain HF-checkpoint inference) or, as a stopgap, when this PR writes the NVFP4 K/V scale buffers.

The fix has to cover both cache writes and decode reads, which use the same global scale. The FP8 KV path is unaffected and looks ready.

This branch has not been deployed

No deployments
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) complexity:high Large or cross-cutting change. Set by Autobot PR classify (skills/pr-classify) convergence-risk:high Likely to change training results. Set by Autobot PR classify (skills/pr-classify) proof:convergence Needs convergence proof (W&B runs). Set by Autobot PR classify (skills/pr-classify) proof:functionality Needs proof that it works. Set by Autobot PR classify (skills/pr-classify)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants