Repository navigation
feat: store the absorbed MLA latent as affine int8 on DeepSeek V2 - #3168
Conversation
A quantized KV request on an absorbing deepseek_v2 used to decline the latent and store decompressed per-head K/V (5120 bytes per token per layer for int8 on V2-Lite), and #3154 substituted fp16 in the interim. An int8 cache now stores the latent itself as an MLX affine group-64 triple with fp16 rope keys, 672 bytes against 1152 for the fp16 latent. - Storage reuses the four buffers a per-head INT8 cache has (packed latent, k_pe, group scales, group biases), so trim, trim_front, gather, detach/adopt and the prompt-cache store carry it. Two hard-coded width-1 scale slices now read the width from the array. - MlaLatentCache::update_and_fetch returns a LatentWindow (Dense, or Quantized with bits and group size). A single query runs both contractions through quantized_matmul on the packed rows; a multi-token step and a cold prefill dequantize the window once. The fused kernel and split-KV are not tried on a quantized window. - The resolver keeps int8 on an absorbing deepseek_v2 and still substitutes fp16 for Turbo modes there and for every quantized mode on the latent-pair families. kv_arch sizes the quantized latent, which inspect, the memory preflights, the prompt-cache budget and the advisor read. --kv-group-size other than 64 fails server startup for such a model. - generate and mlxcel-bench-decode honour MLXCEL_TRACE_MLA_STATS=1, and logit_trace applies MLXCEL_TRACE_KV_CACHE_MODE to caches the model does not own. A 4-bit latent was measured and failed the trace gate, so int4 is not a mode and --kv-bits 4 uniform stays rejected. Validated with the core mla and cache tests, the sizing, CLI, prompt-cache and deepseek_v2 unit tests, the V2 greedy parity test, clippy and fmt. Refs #3155, #3158
Rewrite the MLA latent section of docs/turbo-kv-cache.md now that deepseek_v2 holds an affine int8 latent, and record there that the latent-pair families keep the fp16 substitution until #3160-#3163. Update docs/mla-absorbed-decode.md with the slot layout and the composed decode, document MLXCEL_TRACE_MLA_STATS, and add the CHANGELOG entry. docs/benchmark_results/mla-quantized-latent-m3ultra-2026-11-19.md holds the teacher-forced traces, decode throughput at 512, 2048 and 16384 tokens, the 16384 prefill peak and the end-to-end checks. int8 shows no decided disagreement and all top-1 disagreements within 0.109 logits; int4 flips top-1 at gaps up to 0.859 and is recorded as not shipped. CUDA and ROCm are marked not measured. Refs #3155
DeepSeek-VL2 runs a deepseek_v2 decoder that now stores an int8 latent at runtime, but the mode resolver, kv_arch and the advisor keyed on the top-level model_type (deepseek_vl_v2) and kv_arch never looked under language_config. A Turbo request therefore stayed Turbo and pushed the decoder onto the larger decompressed cache, and VL2 was not sized as a latent at all. The VL2 loader's language_config builder is now shared (loading::deepseek_vl2_language_config), and execution::config_fields gains kv_decoder_config / kv_decoder_model_type on top of the existing text_config rule. kv_arch sizes and the advisor classifies the decoder section; the CLI resolver keys the deepseek_v2 rule on the decoder type, so VL2 gets the fp16 latent substitution for Turbo, the 672 B int8 latent rate and the int8 advice. stores_latent_through_view no longer string-matches the VL2 wrapper names. kv_arch also sizes an absorbed V2 whose kv_lora_rank is not a multiple of 64 as decompressed per-head int8 under int8, matching MlaLatentCache::supports declining it, instead of the latent rate. examples/mla_absorbed_decode_bench matches on LatentWindow::Dense after update_and_fetch changed its return type, and fails loudly on a quantized window. Tests: new VL2 cases in kv_arch, the advisor and turbo_args; the rank-96 test asserts the decompressed layout. Refs #3155
Implementation Review SummaryIntent
Findings Addressed
Remaining Items
Verification
Notes: fp16 latent numerics are unchanged. The Tests run: clippy |
`quantized_latent_bytes_per_token` multiplied `kv_lora_rank` by the bit width unchecked. `execution::kv_arch` feeds it straight from `config.json`, so a crafted whole-group rank (for example 2^62) panicked in a debug build and wrapped to a small rate in release, undercounting the KV estimate that `inspect`, `memory_estimate` and the prompt-cache budget build on. The rate now returns `usize::MAX` on overflow or a zero group size and saturates the remaining terms, which turns a hostile config into an overestimate. Tests: `mla::quant_latent` saturation case and a `kv_arch` hostile-rank case; `cargo test -p mlxcel-core --lib mla:: cache::latent_quant cache::detach` and `cargo test -p mlxcel --lib` for kv_arch, memory_estimate, kv_cache_advisor and prompt_cache snapshot_sizing pass. Refs #3155
The one-query int8 latent step computed the rope score as `[B, H, 1, d] @ [B, 1, d, S]`, which broadcasts the single rope head so every query head reads the whole `k_pe` window. The score now puts the heads on the row axis (`[B, 1, H, d]`), one product per step. The trace behind 1024 is byte-identical to the measured int8 trace and the 4096 comparison reproduces the recorded figures, so the quality gate result stands. Decode on V2-Lite (M3 Ultra) goes from 127.5-128.5 to 129.5-130.1 tok/s behind 2048 and from 70.5-70.9 to 71.3 behind 16384. Folding the heads into the latent score as well measured 3% to 13% faster but moves one top-1 flip to a 0.219-logit gap, outside the gate's 0.11 near-tie bar, so it is recorded in the results doc and left out. Folding the value sum measured slower at every length. Refs #3155
Security and performance reviewNo CRITICAL or HIGH findings. pr-reviewer findings were not revisited. Fixed
Checked, no change needed
Open (needs a decision, not changed)
Tests: |
Summary
Phase 1 of #3158.
--kv-cache-mode int8(also--cache-type-k/v int8and--kv-bits 8) ondeepseek_v2under absorbed decode now stores the MLA latent as affine group-64 8-bit with fp16 rope keys: 672 bytes per token per layer on V2-Lite against 1152 for the fp16 latent and 5120 for the decompressed int8 cache.MlaLatentCache::update_and_fetchreturns aLatentWindow(Dense, orQuantizedcarrying bits and group size), the contract perf: Read an int8 MLA latent inside the fused Metal decode kernel #3156, feat: Evaluate Turbo WHT compression for the 512-dim MLA latent #3157 and feat: Store a quantized MLA latent in the DeepSeekV3Attention families #3160-feat: Store a quantized MLA latent in the DSA families (V3.2, GLM-5) #3163 build on. A single query runs the latent score and value sum throughquantized_matmulon the packed rows, and the rope score puts the query heads on the matmul row axis so thek_pewindow is read once per step. A multi-token step and a cold prefill dequantize the window once. The fused kernel and split-KV are never tried on a quantized window.trim,trim_front, gather, detach/adopt and the prompt-cache store carry the triple.int8), and the latent-pair families keep fp16, a decision recorded indocs/turbo-kv-cache.mdwith feat: Store a quantized MLA latent in the DeepSeekV3Attention families #3160-feat: Store a quantized MLA latent in the DSA families (V3.2, GLM-5) #3163 as follow-ups. DeepSeek-VL2 resolves its KV rules through itsdeepseek_v2language config (config_fields::kv_decoder_config).inspect, the memory preflights, the prompt-cache budget and the advisor use the 672-byte rate, which saturates tousize::MAXfor akv_lora_rankwhose byte count would overflow.--kv-group-sizeother than 64 fails server startup for such a model.int4 is not shipped
It failed the gate, so
int4is not a mode and--kv-bits 4uniform keeps its startup error.Measured (M3 Ultra, Metal, V2-Lite)
Decode tok/s at 512 / 2048 / 16384: fp16 latent 163.6 / 146.7 / 109.9, int8 latent 138.9 / 127.6 / 70.7 before the rope-score change and 129.5-130.1 / 71.3 at 2048 / 16384 after it, decompressed int8 121.0 / 88.5 / 25.4. The int8 latent decodes on the composed path (#3156 is the fused kernel). The rope-score change leaves the int8 trace byte-identical. 16384 prefill peak: 11.23 GB against 11.45 GB for fp16. Full rows, the measured single-query layouts and end-to-end checks:
docs/benchmark_results/mla-quantized-latent-m3ultra-2026-11-19.md.Not done
quantize_weights,quantized_matmulanddequantize.mlxcel servedoes not printMLXCEL_TRACE_MLA_STATS; the serve check is the prompt-cache store's byte count and cached-token reuse.Test plan
cargo test -p mlxcel-core --lib -- mla:: cache:: --test-threads=1cargo test -p mlxcel --libfor the sizing, advisor, CLI, prompt-cache andmodels::deepseek_v2modulescargo test --test deepseek_v2_greedy_parityon the real checkpoint (fp16 default unchanged)generate,inspectandserveruns with int8 on V2-Litemake verify-clippy(workspace, all targets,-D warnings),cargo fmt --checkCloses #3155