Skip to content

[Qwen3.8] Fix FP8 KV cache support in QSA - #36644

Closed
LingZ315 wants to merge 3 commits into
sgl-project:qwen4-main-squashedfrom
LingZ315:lz/fix-qsa-fp8-kv
Closed

LingZ315 wants to merge 3 commits into
sgl-project:qwen4-main-squashedfrom
LingZ315:lz/fix-qsa-fp8-kv

Conversation

@LingZ315

@LingZ315 LingZ315 commented Aug 27, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • support FP8 KV cache in QSA normal and chunked prefill by widening cached K/V to the query dtype and applying the per-layer KV descales
  • dequantize compacted FP8 cache into query-dtype scratch buffers for the FlashAttention fallback, while preserving FP8 cache buffers for the TRT-LLM decode path and passing its BMM descales
  • use per-layer scales when writing the KV cache without mutating the K/V tensors still consumed by initial prefill
  • keep the existing BF16 paths compile-time separated, and add focused coverage for FP8, non-unit scales, fallback, decode, and cache writes

Addresses the remaining prefill/chunk-prefill FP8 path related to #36545.

Context

With an FP8 KV cache, a long cached chunk-prefill request reaches the QSA Triton kernel with BF16 queries and FP8 cached keys/values. tl.dot then rejects the mixed input dtypes. PR #36806 fixes the exact-SM120 QSA sparse-decode routing and supersedes the broader SM12x routing proposed in #36556. That decode-path fix does not dequantize FP8 QSA cache entries consumed by normal or chunked prefill; this PR covers that orthogonal data path.

This PR is stacked on #36497 because the Qwen3.8/QSA implementation is not on main yet.

Validation

  • pre-commit run --from-ref 73a255206f --to-ref d2c8b42cb1: all hooks passed

  • existing QSA GPU suite on RTX 5090 (SM120): 52 passed

  • new focused QSA FP8 KV GPU suite on RTX 5090: 7 passed

    • normal prefill and cached chunk prefill against a dequantized reference
    • non-unit K/V descales
    • BF16 regression path
    • FlashAttention fallback compaction and native TRT-LLM decode scaling
    • scaled cache writes preserve the original prefill tensors
  • real checkpoint smoke test: RadixArk/Qwen3.8-Flash-Next-NVFP4, TP=8 on 8x RTX 5090, --kv-cache-dtype fp8_e4m3

    • FP8 K/V cache allocated successfully
    • decode CUDA Graphs captured for batch sizes 1, 2, 4, and 8
    • WikiText-103 50k-token request after a 45k-token prewarm: 44,992 cached tokens (89.984%) and successful generation
    • 8 concurrent distinct-suffix requests and an 8-concurrent cached replay both completed; the server reported an 8-request decode batch with CUDA Graph enabled
  • fresh current-base RED/GREEN revalidation (full setup and results) on qwen4-main-squashed 99c9362 (including fix(qsa): route exact SM120 to FlashInfer sparse decode #36806), 4x RTX 5090, TP=4, and real WikiText-103 token IDs

    • RED: a 45,000-token prewarm failed in _sparse_gqa_chunk_prefill at tl.dot(q_values, keys) with Unsupported rhs dtype fp8e4nv
    • GREEN: the same prewarm passed; a follow-up 50,000-token request reused 44,992 cached prompt tokens (89.984%) and generated successfully

The original TP=8 end-to-end worktree additionally carried #36556. The fresh TP=4 RED/GREEN revalidation instead used the current base 99c9362, including #36806. Both sides of each A/B carried the same unrelated ModelOpt NVFP4 MoE TP-padding loader fix required to load this particular exported checkpoint; none of those auxiliary changes are included in this PR. The checkpoint exposes unit/default KV scales, while non-unit scale behavior is covered by the focused kernel tests above.

Accuracy

Full official GSM8K A/B (python -m sglang.test.run_eval, all 1,314 examples, Chat API, thinking disabled, temperature 0, max 512 output tokens, concurrency 8) on the same patched source, checkpoint, and TP=8 host; only --kv-cache-dtype changes:

Metric BF16 KV FP8 KV FP8 delta
Correct / 1,314 1,265 1,261 -4
Accuracy 96.2709% 95.9665% -0.3044 pp
Output throughput (tok/s) 437.24 437.84 +0.14%

The paired result agrees on 1,298/1,314 examples (98.78%): 1,255 are correct under both KV dtypes, 43 are wrong under both, 10 are BF16-only correct, and 6 are FP8-only correct. A two-sided exact McNemar test on the 16 discordant examples gives p=0.45, so this run does not show a statistically significant task-accuracy difference. The focused GPU tests separately compare normal and cached chunk-prefill FP8 outputs against a dequantized reference with non-unit K/V scales.

Serving performance

Controlled A/B on the same patched source, model, 8x RTX 5090 host, and exact WikiText-103 token-ID prompts; each setting contains 16 requests and only --kv-cache-dtype changes:

Workload / metric BF16 KV FP8 KV FP8 delta
1k input / 1k output, c=8, output tok/s 764.50 764.44 -0.01%
50k input / 400 output, 89.98% cache hit, c=1, output tok/s 99.79 100.25 +0.46%
same c=1, TTFT p50 / p90 (ms) 928 / 1,001 925 / 961 -0.28% / -3.95%
50k input / 400 output, 89.98% cache hit, c=8, output tok/s 255.21 257.25 +0.80%
same c=8, TTFT p50 / p90 (ms) 4,299 / 8,751 4,284 / 8,684 -0.37% / -0.76%

The same long-context BF16 workload before vs. after this patch was -0.44% at c=1 and +1.17% at c=8, showing no measurable BF16 regression. FP8 increased allocated KV capacity per GPU from 762,496 to 1,440,320 tokens (+88.9%). The unpatched 50k / 90%-hit FP8 case crashes in the QSA mixed-dtype dot before producing a comparable result, so these numbers are intended as a regression guardrail rather than a broad speedup claim.

Integration note

Commit d2c8b42cb1 applies cleanly to the current qwen4-main-squashed head 99c9362 (including #36806). The controlled RED/GREEN revalidation above reproduces the failure on the base and passes with this commit. The full changed-file pre-commit suite passes, and the four files changed here pass the same hooks.


CI States

Latest PR Test (Base): ❌ Run #34829312782
Latest PR Test (Extra): ❌ Run #34829312532
Latest PR Test (AMD ROCm 10): ❌ Run #34829312674

@LingZ315

Copy link
Copy Markdown
Contributor Author

Fresh RED/GREEN revalidation on the current qwen4-main-squashed head (99c9362, including #36806), 2026-08-30.

Environment: 4x RTX 5090 (SM120), TP=4, RadixArk/Qwen3.8-Flash-Next-NVFP4, --kv-cache-dtype fp8_e4m3, page size 128, chunked prefill size 1024, context length 65,536, and real WikiText-103 token IDs. CUDA Graph was disabled to isolate the prefill path. Both worktrees carried the same unrelated ModelOpt MoE TP-padding loader fix required to load this checkpoint; the only QSA behavior difference was d2c8b42.

RED — 99c9362 without #36644

  • The server became healthy and a 128-input / 16-output request succeeded, confirming that fix(qsa): route exact SM120 to FlashInfer sparse decode #36806 fixes the earlier SM120 startup/decode-routing symptom.
  • After a cache flush, a 45,000-token WikiText prewarm entered QSA chunked prefill and all four ranks failed in _sparse_gqa_chunk_prefill at tl.dot(q_values, keys) with:
    AssertionError: Unsupported rhs dtype fp8e4nv
    
  • This occurs during the prewarm itself because later 1,024-token chunks read K/V written by earlier chunks from the FP8 cache.

GREEN — identical stack plus d2c8b42

  • The 128/16 request succeeded.
  • The 45,000-token WikiText prewarm succeeded.
  • A follow-up 50,000-token request reported 44,992 cached prompt tokens (89.984% hit) and successfully generated 32 tokens.

Therefore #36806 fixes the launch/decode-routing failure, but does not cover cached QSA chunk-prefill. The FP8-KV issue addressed by this PR remains reproducible on the current Qwen base, and this patch fixes it under a controlled A/B.

Copy link
Copy Markdown

@Qiaolin-Yu @JustinTong0323, could you please take a look when you have a chance? The issue is still reproducible on the latest Qwen integration head, and the RED/GREEN result above isolates the remaining FP8 KV cached chunk-prefill path. Since this PR is stacked on #36497, guidance on the preferred integration/rebase path would also be appreciated.

@Xia-Weiwen Xia-Weiwen mentioned this pull request Aug 31, 2026
3 of 5 tasks
@SSHdotCodes

Copy link
Copy Markdown

Independent validation of this design on RTX PRO 6000 Blackwell (SM120, 96 GB): I've been running an equivalent patch in production (widen-on-load inside _sparse_gqa_chunk_prefill, query-dtype scratch for the FA fallback's compacted cache; unit scales, since this checkpoint ships no KV scales) against RadixArk/Qwen3.8-Flash-Next-NVFP4 with --kv-cache-dtype fp8_e4m3 --max-total-tokens 524288 and NEXTN MTP + rejection sampling.

Quality, measured as teacher-forced input logprobs FP8-vs-BF16 and judged against the same server's BF16-vs-BF16 rerun noise floor (the deployment is not bit-deterministic, so raw diffs mean nothing without the floor):

probe FP8 vs BF16 BF16 noise floor
29.5k-token ctx: p50 abs dlogprob / top-1 agreement 0.00018 / 99.15% 0.00039 / 99.19%
113.8k-token ctx: p50 / top-1 0.00002 / 99.24% —
needle retrieval @129K tokens pass pass
1024-token degeneration 0/48 0/12

Single-stream decode is within noise of the BF16 pool, and two concurrent 248k-token requests (95% pool usage) both stay coherent — the halved pool bytes are what make two full-262K-context requests fit on 96 GB at all.

One data point in favor of the in-kernel widen this PR uses: my first attempt widened the chunked-prefill full-context gather at the torch level instead, and at ~113k context the extra query-dtype transient OOM'd the card mid-prefill. Widening on load keeps the gather at FP8 bandwidth (half the previous BF16 transient) and is what makes long-context chunked prefill fit.

@bradsmithmba

Copy link
Copy Markdown

Data point from GB10 (SM121), which is not in the PR's validation matrix: 2x NVIDIA DGX Spark, TP2/EP2 over a ConnectX-7 200G direct link, the official Qwen/Qwen3.8-Flash-Next-FP8 checkpoint (not NVFP4), image lmsysorg/sglang:qwen38flashnext (sglang 0.0.0.dev1+g593134d17, torch 2.13.0+cu130), NEXTN speculative decoding (3 steps, 4 draft tokens).

Before the patch, --kv-cache-dtype fp8_e4m3 allocated the pool and then failed at CUDA graph capture with ValueError: unsupported SM121 QSA call: expected BF16 D=256, 12:1 GQA, TP1 24Q/2KV or TP2 12Q/1KV, bs<=128.

With the PR applied (git apply against g593134d17, clean):

  • FP8 KV allocates, graph capture completes, decode works. KV pool 178,624 tokens at 65,536 context and 242,944 tokens at 131,072 context (0.90 static memory fraction) against 114,688 to 124,480 under BF16 at 65,536.
  • Needle recall correct at 16,817, 101,066, and 125,596 prompt tokens, with --chunked-prefill-size 2048 so the chunk-prefill path is exercised. No token-id-0 output in any test.
  • Throughput unchanged within noise: 35.7 to 37.0 tok/s single-stream, 92.8 tok/s aggregate at 4 streams, TTFT 0.15 to 0.19 s (512-token completions, thinking off), against 34.7 to 38.8 single and 88 to 98.5 aggregate with BF16 KV at 65K.
  • The checkpoint ships no KV scales, so the descales run at the 1.0 default and the startup warning fires. Correctness held, but I have not run a quality eval.

This is the paged-attention fallback path (the FlashInfer/trtllm decode route stays excluded for SM121 after #36806). Dockerfile, launch flags, and notes: https://github.com/bradsmithmba/dgx-spark-qwen38-flash-next-fp8

@github-actions

github-actions Bot commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

Thanks @LingZ315. Closing this because you have 7 PRs open and none updated in 7 days, over our soft cap of 5 for idle PRs.

Reopen the two or three you most want reviewed - a short active set moves faster than a long idle one.

Some directories moved recently, so an older branch may need retargeting:
sgl-kernel/ -> python/sglang/kernels/aot/, python/sglang/jit_kernel/
-> python/sglang/kernels/jit/, docs/ -> docs/docs/ (.mdx),
bench_serving.py -> benchmark/serving.py, test/srt/ -> test/registered/.

@github-actions github-actions Bot closed this Nov 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants