Repository navigation
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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(aliasfp8_e4m3) or automatically when the checkpoint declareskv_cache_quant_algo: FP8. Halves pool bytes per token, so the same budget holds twice the blocks. Part of epic #3671.Closes #3666
Design
PagedKvDtype), not of a sequence.KVCacheMode::Fp8E4m3maps toFp16for dense caches (dense_storage_mode()), so the pool-backing gates keyed onFp16still route sequences into the pool, and layers kept in dense caches stay f16.uint8E4M3 bytes written throughmlx::core::to_fp8and read throughfrom_fp8, with one scalar scale per layer and side (1.0 unless the checkpoint shipsk_proj.k_scale/v_proj.v_scale; values beyond +-448 saturate, as in vLLM).write_prefill, the batchedwrite_decode_rows(the perf: Batch per-row RoPE, KV write and norm launches in batched decode #3665InplaceScatterRowspath now moves E4M3 bytes), and a float block given towrite_block. Bytes pass through unchanged, so copy-on-write forks and block transfers never re-encode.gather_visibledequantizes 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.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.WorkerPlanRequest.pool_one_bytemakes 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. TheKV capacitylog line carrieskv_pool_dtype.attention_k_eq_vlayers store both K and V, each encoded with its own scale.Default paths FP8 affects
MLXCEL_PAGED_SLIDINGunset) it decodes from dense per-sequence caches, which keep f16. FP8 applies only withMLXCEL_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 andbench_decodekeep dense caches and rejectfp8with an error naming the command.How Metal stays unchanged
PagedKvDtype::Native, every new branch is a no-op (encode_kv/decode_kvreturnNone), and the v2 kernel selection only takes the fp8 port when the slab dtype isuint8. The Metal kernel sources are not modified; a non-CUDA backend handed auint8pool 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
These need the 31B and B200 hardware; this PR validates on small models on GB10.