Skip to content

[rl] vLLM generator: fp8 KV cache through FA4's fp8 forward, MTP verify steps included - #6479

Draft
liangel-02 wants to merge 3 commits into
liangel-02/mtp-full-graphs-prefillfrom
liangel-02/vllm-fp8-kv-cache
Draft

liangel-02 wants to merge 3 commits into
liangel-02/mtp-full-graphs-prefillfrom
liangel-02/vllm-fp8-kv-cache

Conversation

@liangel-02

Copy link
Copy Markdown
Contributor

Supersedes #6365 (originally by felipemello1).

Summary (human-written)

Agent details

Needs retesting on B200 (SM100): every number below is from one GB300 (SM103). FA4's split heuristics are tuned per GPU, so B200 timings can differ.

Stack, bottom to top:

  1. "[rl] GDN builder: stage steps with drafts in fixed buffers"
  2. "[rl] MTP: replay the verify CUDA graph only when every row verifies"
  3. "[rl] GDN layer: run steps with drafts from TorchTitan's builder"
  4. "[rl] MTP speculative decoding in the vLLM generator"
  5. "[rl] MTP verify steps: run verify rows on FA4's decode kernel"
  6. "[rl] MTP on GDN models: FULL CUDA graphs for steps with prefill"
  7. this PR

The stack lands after the vLLM 0.33 PRs ("[rl] generator: vLLM 0.33 API fixes and TP padding in the model wrapper", "[rl] Install vLLM 0.33.0 on the torch nightly"). "[rl] generator: run on vLLM's model runner V2 (vLLM 0.34)" rebases on it.

TLDR: VLLMEngine.Config.kv_cache_dtype = "fp8" stores K and V in e4m3 and runs FA4's fp8 forward for decode and prefill rows. "auto" (bf16) stays the default and keeps the trainer's kernel. Qwen3.8-27B on one GB300: 2x the KV tokens (5.81M vs 2.90M), and 1,043 vs 902 output tok/s at 32 sessions with 27k-135k contexts. With fp8 SwapAB decode (an open FA4 PR), the same code gives 1,170. MTP drafts work with fp8 too: each verify step is one FA4 varlen call.

With kv_cache_dtype fp8, K and V take 1 byte per value instead of 2 and run FA4's fp8 forward, MTP verify steps included as one varlen call, so one GB300 engine holds 5.81M KV tokens instead of 2.90M, for 1,043 output tok/s vs 902 with bf16 and 1,170 with FA4's fp8 SwapAB PR.

kv_cache_dtype = "fp8"
  -> EngineArgs.kv_cache_dtype = "fp8": vLLM writes K/V in e4m3 (scale 1) and quantizes the query
  -> TorchTitanVarlenInnerAttentionImpl views the cache as e4m3
  -> paged_varlen_attention_out sees key_cache.dtype == float8_e4m3fn -> FA4 _flash_attn_fwd
     (bf16 keeps torch's varlen attention, the trainer's kernel)

Problem

  1. The KV cache caps how much context an engine holds: Qwen3.8-27B fits 2.90M bf16 tokens on one GB300 (gpu memory 0.85).
  2. Long agent loads fill it. 46 sessions with 27k-135k contexts and an 80 GiB CPU KV offload: KV sits at 89-92%, requests wait for KV (5.8 on average), and the engine gives 788 output tok/s.
  3. vLLM has an fp8 KV cache, but TorchTitan's attention impl asserts "FP8 KV cache not supported yet.".
  4. vLLM's fp8 check in FlashAttentionBackend is about vLLM's own FlashAttention build, which has no fp8 kernel at head dim 256. TorchTitan's backend subclasses it, so the check would refuse fp8 too.

Solution

  1. kv_cache_dtype: Literal["auto", "fp8"], passed to vLLM's EngineArgs. vLLM's cache write needed no change.
  2. The impl views the cache as e4m3. Every scale is 1: VLLMInnerAttention builds vLLM's Attention without a quant_config.
  3. paged_varlen_attention_out picks the fp8 path from the cache dtype. It calls FA4's private _flash_attn_fwd with the arguments torch's FA4 bridge passes. num_splits is 1 under deterministic algorithms, as torch's bridge sets it for the bf16 call.
  4. supports_combination hides fp8 from FlashAttentionBackend's check, and refuses a non-SM100 GPU with a reason. The impl passes "auto" to FlashAttentionImpl.__init__ for the same reason.
  5. Refused with a ValueError that says how to fix it:
    • fp8 with debug.batch_invariant: the trainer runs bf16.
    • fp8 on a model with sliding-window layers (gpt-oss, muse_glimmer, muse_spark): untested. gpt-oss is also the only model with attention sinks (out_transform), so this covers sinks too.
    • fp8 with tensor_parallel_degree > 1: untested.
    • fp8 on a model with MLA layers (DeepSeek-V3, Kimi K3): untested. Their K and V head dims differ, and vLLM runs the attention backend TorchTitan passes for them unchecked.
    • Not refused here: fp8 on a flex-attention model. vLLM refuses it at engine start.
  6. fp8 MTP verify steps make one FA4 varlen call with max_seqlen_q = 1 + k, at any row count. They skip the verify-steps PR's decode call + fp32 tail, which has no fp8 path.
    • On FA4 4.0.0b40, this call runs FA4's standard fp8 kernel: 0.686 ms per layer at 26 rows.
    • With FA4's open token-mode PR, the same call runs SwapAB with no change here: 0.441 ms (table below).
  7. Unchanged: "auto" runs the same code as before. The FA4 floor stays at 4.0.0b38, which has the fp8 forward and every argument the call passes.

Results

Numerics: not bitwise vs bf16 KV: K, V and the query are stored in e4m3, and FA4 rounds P to e4m3 before P @ V.
  Kernel, rel L2:
    fp8 FA4 vs bf16 FA4 on the same e4m3 values,          1.5e-3 decode rows, 4.3e-3 prefill rows
      engine-like inputs (long contexts, some large keys)
    fp8 FA4 vs an fp32 reference on the dequantized       ~0.02 (bar: 0.05)
      values, the GPU test's flat random attention
  A CUDA graph replayed after the KV lengths change is bitwise equal to eager.
  Trainer-vs-generator logprob gap: the trainer scores 34,816 generated tokens, temperature 1, 27k-78k contexts.
                      mean |diff|   k3 KL     tokens with ratio outside [0.2, 5]
    bf16 KV           0.0096        1.05e-3   4
    fp8 KV            0.0217        3.66e-3   19
    stock vLLM bf16   0.0157        1.16e-3   4
    stock vLLM fp8    0.0252        4.07e-3   17
  fp8 raises the gap 2.3x by mean and 3.5x by k3, a bit below stock vLLM's fp8.
Qwen3.8-27B, one GB300, TP1, gpu memory 0.85, FULL CUDA graphs, prefix caching, max_num_batched_tokens 16384,
32 sessions sharing one 27k prompt + own histories (27k-135k), 180 s window, one run per arm:
                                                  bf16 KV   fp8 KV   fp8 / bf16
  KV tokens                                       2.90M     5.81M    2.00x
  output tok/s, FA4 4.0.0b40                      902       1,043    1.16x
    (fp8 decode on FA4's standard kernel)
  output tok/s, FA4 with fp8 SwapAB (open PR)     902       1,170    1.30x
fp8 MTP-3 verify call (rows of 4 tokens, causal), one Qwen3.8-27B attention layer (24 q heads, 4 KV heads,
hd256, 128-token pages), one GB300, CUDA-graph replay, warm, median of 30, ms:
requests, layout                                standard fp8 kernel    FA4 token-mode PR
                                                (FA4 4.0.0b40's path)  (SwapAB)
26, 3 shared prompts (27k / 55k / 77k), 69k     0.686                  0.441
40, one shared 27k prompt, 27k-135k             1.430                  0.909

When KV is full (46 sessions, 80 GiB CPU KV offload, FA4 with fp8 SwapAB): fp8 gives 1,303 output tok/s with KV at 51-54% and no request waiting, vs 788 for bf16 with KV at 89-92%. Not a controlled pair: fp8 ran at gpu memory 0.9, bf16 at 0.85.

Where fp8 does not gain: on FA4 4.0.0b40, SwapAB takes only bf16/fp16, so fp8 decode runs FA4's standard kernel, which gets almost nothing from L2 on shared prompt pages. One layer, 26 requests in 3 groups sharing 27k / 55k / 77k prompts: 0.685 ms in fp8 vs 0.581 ms for bf16 SwapAB. With the FA4 fp8 SwapAB PR: 0.346 ms.

NOTE:

  • The engine and drift numbers ran on a development branch that also carries generator changes not in this stack, and the pre-merge version of main's CPU KV offload connector. That branch passed the layer's q, k, v scales (all 1.0) to FA4. This PR passes none, which FA4 treats as 1. One run per arm. Not re-measured on this stack.
  • The one-call fp8 verify path has no throughput or logprob run. An engine probe ran it at this head: Qwen3.5-0.8B, fp8 KV, k = 3, FULL, one GB300, 8 requests (4 arriving next to verify rows), all finished. Earlier fp8 + MTP-3 runs used another verify path (each row's tokens packed as query heads): 1,634 output tok/s at 32 sessions (fp8 alone 1,170; stock vLLM fp8 + MTP-3: 1,634).
  • Prefix-cache reuse gets coarser. A hybrid model's KV block grows from 896 to 1,664 tokens, and a prefix hit ends on a block boundary, so each agent turn recomputes ~400 more prompt tokens (2,053 vs 1,679 in the earlier run). TODO at the site.
  • fp8 with main's CPU KV offload connector ran in the 46-session engine runs above, but no test covers it: main's offload GPU test (multi-GPU) runs bf16 KV only.
  • Two open PRs edit vllm/attention/attention.py; the one that lands second rebases: the vLLM V2 model runner PR and the ROCm paged-KV gather PR.

You may ask: why FA4's private _flash_attn_fwd? torch's varlen attention takes no fp8 inputs, and FA4's public flash_attn_varlen_func has no out= to write into vLLM's output buffer. An open PyTorch PR adds fp8 descales to varlen_attn. TODO at the site to switch to torch's call once it lands.

Follow up / TODO (each has a # TODO at its site)

  • Calibrated per-layer KV scales: with scale 1, values above 448 clip and values below 2^-6 lose precision. They need a descales argument in paged_varlen_attention_out. The gain is not measured.
  • Raise the FA4 floor to the first release with fp8 SwapAB token mode (an open FA4 PR).
  • Call torch's varlen_attn_out once the open PyTorch PR lands, instead of FA4's private call.
  • Stop hiding fp8 from FlashAttentionBackend's check if this backend stops subclassing it.
  • fp8 with tensor parallelism, with sliding windows and with MLA: test, then lift the refusals.
  • Coarser prefix reuse: try vLLM's prefix_match_unit or a smaller mamba_block_size.

Test plan

pytest tests/rl/unit_tests/cpu
pytest tests/rl/unit_tests/gpu/test_vllm_verify_attention.py   # 1 GPU (GB300); also in the B200 RL workflow
pre-commit run --files <changed files>
RL CPU suite   793 passed, 0 failed, 8 skipped on the pinned vLLM (1.0.0.dev20261107; PR 6: 788 passed), main merged
               in. New: test_fp8_kv_cache_refusals (4 cases) and kv_cache_dtype as a TorchTitan-owned engine arg.
GPU            test_vllm_verify_attention.py: 6 passed (42 s). auto and fp8 on verify, few-row and mixed batches
               through `forward` (vLLM's uint8 storage, e4m3 query) against an fp32 reference on the dequantized
               values. The uniform fp8 steps assert one FA4 call with max_seqlen_q == 4.
               Also at this head: test_gdn_graph_metadata.py 38 passed (31 s); test_gdn_cudagraph.py (with the
               fused-kernel PR) 5 passed (497 s).
pre-commit     all hooks pass (pyrefly and lychee skipped)

…fy steps included

VLLMEngine.Config.kv_cache_dtype="fp8" stores K/V in e4m3 at scale 1 and runs FA4's fp8
forward; paged_varlen_attention_out picks it from the cache dtype. MTP verify steps make
one varlen call. Refused: debug.batch_invariant, tensor parallelism, sliding windows, and
GPUs outside SM100.
…-kv-cache

# Conflicts:
#	torchtitan/rl/generator/vllm/engine.py

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

ciflow/rl CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants