Skip to content

feat: store the paged KV pool in FP8 E4M3 on CUDA - #3693

Draft
inureyes wants to merge 2 commits into
mainfrom
feature/issue-3666-fp8-paged-kv
Draft

inureyes wants to merge 2 commits into
mainfrom
feature/issue-3666-fp8-paged-kv

Conversation

@inureyes

@inureyes inureyes commented Dec 2, 2026

Copy link
Copy Markdown
Member

Summary

Stores the server's paged KV block pool as FP8 E4M3 on CUDA (sm_89+), selected by --cache-type-k fp8 --cache-type-v fp8 (alias fp8_e4m3) or automatically when the checkpoint declares kv_cache_quant_algo: FP8. Halves pool bytes per token, so the same budget holds twice the blocks. Part of epic #3671.

Closes #3666

Design

  • FP8 is a property of the pool (PagedKvDtype), not of a sequence. KVCacheMode::Fp8E4m3 maps to Fp16 for dense caches (dense_storage_mode()), so the pool-backing gates keyed on Fp16 still route sequences into the pool, and layers kept in dense caches stay f16.
  • The pinned MLX has no float8 dtype; the slabs hold uint8 E4M3 bytes written through mlx::core::to_fp8 and read through from_fp8, with one scalar scale per layer and side (1.0 unless the checkpoint ships k_proj.k_scale / v_proj.v_scale; values beyond +-448 saturate, as in vLLM).
  • Encoding happens once at the float entry points: write_prefill, the batched write_decode_rows (the perf: Batch per-row RoPE, KV write and norm launches in batched decode #3665 InplaceScatterRows path now moves E4M3 bytes), and a float block given to write_block. Bytes pass through unchanged, so copy-on-write forks and block transfers never re-encode.
  • gather_visible dequantizes to the layer's compute dtype before SDPA. Prefill attention uses the history from the pool plus the chunk just written at its computed precision.
  • Fused decode: the v2 partial kernel gets a CUDA fp8 port (mlxcel_paged_attention_v2_partial_fp8) that loads E4M3 bytes directly; the K scale folds into the softmax scale and the V scale multiplies the output. The v1 kernel and cascade have no fp8 port: v1 declines to the gather path and cascade falls back to the flat v2 launch. A one-time info line names the fp8 kernel when it runs.
  • Memory plan (perf: Plan CUDA server memory at startup with a per-layer-type KV pool #3664): WorkerPlanRequest.pool_one_byte makes pooled slabs, the block budget, and prompt-cache retained rows count one byte per element. Dense layers outside the pool are still planned at f16. The KV capacity log line carries kv_pool_dtype.
  • Shared K==V slab (perf: Plan CUDA server memory at startup with a per-layer-type KV pool #3664) is unaffected: Gemma 4's attention_k_eq_v layers store both K and V, each encoded with its own scale.

Default paths FP8 affects

  • Pool-backed families on CUDA (Llama, Qwen and the other dense families that use the pool by default): every pooled layer stores FP8 once the flag or the checkpoint selects it.
  • Gemma 4 on CUDA: unaffected by default. Since fix(cuda): default paged sliding-window KV off on CUDA #3676 (MLXCEL_PAGED_SLIDING unset) it decodes from dense per-sequence caches, which keep f16. FP8 applies only with MLXCEL_PAGED_SLIDING=1. This includes the Gemma 4 31B NVFP4 checkpoint: it is auto-detected as fp8 but, on the default path, its caches stay dense f16.
  • mlxcel generate, the chat REPL, IndicOCR and bench_decode keep dense caches and reject fp8 with an error naming the command.

How Metal stays unchanged

  • The explicit flag fails at startup on any device that is not CUDA sm_89+ (the message names the compute capability), and a WebUI load profile asking for fp8 is refused the same way.
  • A checkpoint that declares FP8 keeps f16 on Metal and older GPUs with an info line; no error, no behaviour change.
  • With fp8 never selected, the pool stays PagedKvDtype::Native, every new branch is a no-op (encode_kv / decode_kv return None), and the v2 kernel selection only takes the fp8 port when the slab dtype is uint8. The Metal kernel sources are not modified; a non-CUDA backend handed a uint8 pool throws rather than misreading it.

Verification

Narrow tests, server comparisons and numbers: see the status comment on this PR.

Deferred to the B200 follow-up measurement issue

  • Gemma 4 31B NVFP4 startup with the FP8 pool and its memory plan on B200.
  • Logit trace of FP8 against f16 on the 31B.
  • Serving table at c=1/8/32 on B200 and c=32 TPOT.

These need the 31B and B200 hardware; this PR validates on small models on GB10.

Add KVCacheMode::Fp8E4m3 (`--cache-type-k fp8 --cache-type-v fp8`, alias fp8_e4m3) as a property of the paged block pool: K and V slabs hold uint8 E4M3 bytes with a per-layer scale, the fused v2 decode kernel gets a CUDA fp8 port, the gather path dequantizes, and the memory plan counts one byte per pooled element. The server selects it from a checkpoint that declares kv_cache_quant_algo FP8 on CUDA sm_89+.

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: Store the paged KV pool in FP8 on CUDA

1 participant