Skip to content

feat: store a quantized MLA latent in the DeepSeekV3Attention families - #3172

Merged
inureyes merged 6 commits into
mainfrom
feature/issue-3160-v3-quantized-latent
Nov 19, 2026
Merged

inureyes merged 6 commits into
mainfrom
feature/issue-3160-v3-quantized-latent

Conversation

@inureyes

@inureyes inureyes commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

Summary

The eight DeepSeekV3Attention model types (deepseek_v3, kimi_k2, joyai_llm_flash, kimi_vl, kimi_k25, youtu, youtu_llm, youtu_vl) now write their MLA latent through MlaLatentCache, so --kv-cache-mode int8 and --kv-bits 8 keep the #3155 affine int8 latent (672 against 1152 bytes per token per layer) instead of being substituted with fp16. int8 only: 4-bit failed the #3155 gate.

  • mla::latent_pair: QUANTIZED_LATENT_FAMILIES and latent_pair_supports_mode, called by rule 1; the warning names int8 for these families. Turbo modes still resolve to fp16, and glm4_moe_lite and the other latent-pair families keep fp16. The list lives in a new file beside mla/cache.rs to keep that file under 500 lines.
  • Single-sequence forward and mla_latent_decode_rows store through store_latent_step. A quantized one-query step runs quantized_absorbed_attention (now public); multi-token steps dequantize once; fp16 caches take the unchanged KVCache::update_and_fetch. A selector over a quantized row panics naming the family. The batched helper now takes the family, the geometry and an explicit quantized_latent opt-in: only DeepSeekV3Attention passes true, so deepseek_v32, kimi_linear, glm4_moe_lite and kimi_k3 keep the per-token path even if an int8 cache reached them. The declined-view warning fires once per family.
  • Sizing (kv_arch, inspect, preflights, prompt-cache budget), the advisor (int8 at medium/long), the serve --kv-bits gate and the generate banner follow.
  • Registry kv_modes for DeepSeekV3, KimiVL, KimiK25, YoutuLLM and YoutuVLM now report fp16 and int8 (new KV_INT8), pinned by a test against stores_quantized_latent_pair; docs/supported-models.md updated to match.

Results (M3 Ultra, Moonlight-16B-A3B and Kimi-VL-A3B-Thinking)

Since #3156 merged into this branch, a one-query int8 step over a window of up to 2048 keys runs on the fused quantized kernel and longer windows run the composed path. Measured on the merged tree:

  • Trace behind 1024, int8 against the fp16 latent: 0 decided disagreements on both. Moonlight has 7 / 512 top-1 flips with two outside the 0.11 near-tie bar (0.117 and 0.328), ppl +0.76%; Kimi-VL has 12 / 512 with none outside the bar, ppl +0.99%. Every int8 decode step is absorbed_fused. The fp16 trace is byte-identical to the recorded one.
  • Behind 4096 (composed path, unchanged by perf: Read an int8 MLA latent inside the fused Metal decode kernel #3156): 0 decided disagreements, every flip inside the bar.
  • The near-tie misses were reviewed and accepted for merge on the basis of 0 decided disagreements.
  • Decode, int8 vs fp16 latent: Moonlight -10.4% at 512 (fused, +5.7% over the pre-perf: Read an int8 MLA latent inside the fused Metal decode kernel #3156 int8) and -13.3% at 2048; Kimi-VL -9.8% at 512 (+6.5%) and -13.6% at 2048. Behind 8064 (Moonlight, capped by its 8192 context) and 16384 (Kimi-VL) the composed path measured -7.9% and -35.8%.
  • Runtime prompt-cache nbytes equal the inspect rate under both flags.
  • CUDA and ROCm not measured (hosts unreachable).

Details: docs/benchmark_results/mla-quantized-latent-v3-m3ultra-2026-11-19.md.

Test plan

  • cargo test -p mlxcel --lib on models::deepseek_v3, mla_batched, youtu_vl_lm, glm4_moe_lite, kimi_linear, kimi_k3, deepseek_v32, cli::turbo_args, execution::, server::prompt_cache, server::cli_input, vision::encoders::kimi_vl (766 passed)
  • cargo test -p mlxcel-core --lib on mla::, cache::turbo::allowlist, cache::latent_quant (101 passed)
  • cargo fmt --check, cargo clippy -p mlxcel-core/-p mlxcel --lib --tests -D warnings
  • After review fixes: models::deepseek_v3 (26), models::registry (21), models::mla_batched (2), execution::kv_arch (47), mlxcel-core mla::latent_pair (4), plus fmt and clippy
  • generate / serve / inspect / logit_trace / bench runs on both checkpoints
  • After merging main (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): make verify-clippy, mlxcel-core mla:: (90), mlxcel deepseek_v3, mla_batched, registry, kv_arch, kv_cache_advisor (139); trace behind 1024 and decode at 512 / 2048 re-run on both checkpoints

Closes #3160

The eight model types that run DeepSeekV3Attention (deepseek_v3, kimi_k2, joyai_llm_flash, kimi_vl, kimi_k25, youtu, youtu_llm, youtu_vl) cached their latent pair through KVCache::update_and_fetch directly, so rule 1 substituted every quantized request with fp16. They now write the pair through MlaLatentCache, and --kv-cache-mode int8 / --kv-bits 8 keep the #3155 affine group-64 latent (672 against 1152 bytes per token per layer at rank 512).

- mla::latent_pair adds QUANTIZED_LATENT_FAMILIES and latent_pair_supports_mode; rule 1 calls it and its warning names int8 for these families. Turbo modes still resolve to fp16; the other latent-pair families keep fp16.
- DeepSeekV3Attention and the shared batched helper store each step through store_latent_step. A quantized one-query step runs the composed quantized decode; multi-token steps dequantize once; fp16 caches take the unchanged path. A key selector over a quantized row panics naming the family.
- kv_arch, the advisor, inspect, the serve kv-bits gate and the generate banner use the quantized rate and mode.

M3 Ultra, Moonlight-16B-A3B and Kimi-VL-A3B-Thinking: 0 decided disagreements behind 1024 and 4096 (one flip per checkpoint behind 1024 at 0.117 and 0.180, outside the 0.11 near-tie bar), fp16 trace byte-identical to 43f1c8d, decode 15 to 17% slower than the fp16 latent at 512 and 2048 and 36% at 16384.

Closes #3160
@inureyes inureyes added status:review Under review type:enhancement New features, capabilities, or significant additions priority:medium Medium priority area:models Model architectures, weights, loading, metadata area:core mlxcel-core: MLX FFI, primitives, KV cache, layers area:inference Generation, sampling, decoding (incl. speculative, DRY) labels Nov 19, 2026
@inureyes

Copy link
Copy Markdown
Member Author

Implementation Review Summary

Intent

Store the #3155 affine int8 MLA latent on the eight DeepSeekV3Attention model types (single-sequence, batched, prefill, prompt cache, sizing, advisor), int8 only, fp16 numerics unchanged.

Findings Addressed

None needed a fix (no CRITICAL or HIGH findings).

Remaining Items

  • mla_latent_decode_rows now routes any int8 row cache through the latent view, including glm4_moe_lite, kimi_linear, kimi_k3 and deepseek_v32, whose single-sequence forwards still write per-token rows through KVCache::update_and_fetch. Rule 1 and the serve --kv-bits gate keep those caches fp16, so it is unreachable today, but a resolver bypass would mix two layouts in one cache (and panic on deepseek_v32 through the selector assert). Gating store_latent_step on an opt-in from the caller would make it fail-closed. (MEDIUM)
  • The results doc outcome line and docs/turbo-kv-cache.md call the 0.180 Kimi-VL flip "just outside" the 0.11 bar; it is 64% over it, beyond the 0.109 fp16 kernel noise floor, and the CHANGELOG entry does not mention the near-tie miss at all. The PR body ("Decision needed"), the results doc's Near-tie bar section and the unchecked issue item are accurate. (MEDIUM)
  • The advisor now recommends int8 at medium and long context for these families before the near-tie decision is made; if the bar is held, the advisor row and rule 1 change need to be reverted together. (MEDIUM, tied to the open decision)
  • INT8_LAYER_TOL = 5e-2 is about 1.7x the measured 2.9% on the ramp fixture, so it is justified, but every fp16-relative check mixes codec error with wiring error. The batched-vs-sequential check (1e-5) compares two callers of the same composed decode, so a shared wiring mistake would only be caught by the 5e-2 bound. The logits-level 2e-2 check and the real-checkpoint traces cover this in practice. (LOW)
  • Multi-token steps over a quantized window (chunked prefill, any multi-token verify) dequantize the whole window each step: correct and specified, but O(window) per chunk. (LOW)

Verification

  • All stated requirements implemented (trace near-tie item left open for a decision, as the issue records)
  • No placeholder/mock code remaining
  • Integrated into project code flow (rule 1, kv_arch/inspect, advisor, serve --kv-bits gate, generate banner, batched decode, prompt-cache restore)
  • Project conventions followed
  • Existing modules reused where applicable (MlaLatentCache::wrap/update_and_fetch, LatentWindow::into_dense, quantized_absorbed_attention, quantized_latent_bytes_per_token; no parallel codec)
  • No unintended structural changes (fp16 path calls the same KVCache::update_and_fetch; output projection only moved into a helper)
  • Tests pass (cargo fmt --check, cargo clippy -p mlxcel-core -p mlxcel --lib --tests -D warnings, and the deepseek_v3 incl. latent_tests, mla_batched, kv_arch, kv_cache_advisor, cli::turbo_args, serve kv_bits, mla::latent_pair, allowlist selectors on Metal)

@inureyes

Copy link
Copy Markdown
Member Author

Security and performance review

No CRITICAL or HIGH findings, so no fixes were committed.

Checked:

  • Prompt-cache restore of a quantized latent: no KV tensors are read from disk. slot_persist stores only tokens and identity, and the prompt cache moves DetachedKVCache handles within one process, carrying the cache's own mode. bits comes from KVCacheMode::latent_affine_bits and group_size from LATENT_QUANT_GROUP_SIZE, never from stored data. a_restored_int8_latent_decodes_identically passes.
  • Panics reachable from request input: none. A cache is int8 only after resolve_effective_kv_cache_mode (and disable_batch_kv_quant_for_family for --kv-bits) runs at startup, and both force fp16 on every latent-pair family outside QUANTIZED_LATENT_FAMILIES. That makes the select.is_none() assert reachable only through the library or dev-tool engines that set the mode directly. The ungroupable-rank fallback keeps one layout for the cache's whole life and decodes (an_ungroupable_rank_falls_back_to_per_token_rows).
  • Sizing: quantized_latent_bytes_per_token saturates, and classify, bytes_per_token and fp16_last_layer_extra use saturating arithmetic throughout. The rank-512 rate matches runtime nbytes (672 B).
  • fp16 decode: store_latent_step on an fp16 cache calls KVCache::update_and_fetch unchanged and into_dense moves the Dense arrays without a copy, so fp16 numerics and kernel dispatch are unchanged.

Remaining items (not fixed, below the auto-fix threshold):

  • MEDIUM (defense in depth, already with the finalizer): store_latent_step decides on cache.mode alone. If an int8 cache ever reached glm4_moe_lite, kimi_linear, kimi_k3 or deepseek_v32 (for example through an embedder of DirectEngine), single-token forwards would write per-token int8 rows while batched decode writes the affine triple into the same cache, and that fails in MLX at the next concat. Keying the view on the family (the opt-in) closes this.
  • LOW: the ungroupable-rank fallback accepts int8 and stores per-token rows with one scale per row of the latent, the quality case rule 1 previously refused. It is warned once and no shipping checkpoint has such a rank.
  • LOW: the warn-once Once in warn_latent_view_declined is process-wide, so a second family that declines gets no warning.
  • LOW: the registry kv_modes for DeepSeekV3, KimiVL, KimiK25, YoutuLLM and YoutuVLM still advertise fp16 only, while the runtime now keeps int8. This under-advertises the capability and does not cause a fault.

…llers

store_latent_step and mla_latent_decode_rows chose the latent view from the cache mode alone, so glm4_moe_lite, kimi_linear, kimi_k3 and deepseek_v32 would have taken the quantized latent under an int8 cache although they are outside mla::QUANTIZED_LATENT_FAMILIES. Both helpers now take an explicit quantized_latent flag; only DeepSeekV3Attention passes true, and the other callers keep the per-token path. fp16 caches never reach the view, so their behavior is unchanged. A new test checks that an int8 cache gets a dense window without the flag and a quantized one with it.

The warn-once guard for a declined latent view is now per family instead of process-wide.

The registry kv_modes for deepseek_v3, kimi_vl, kimi_k25, youtu_llm and youtu_vlm now list fp16 and int8 to match the runtime, with a test that pins the list against QUANTIZED_LATENT_FAMILIES. The results doc, turbo-kv-cache.md, supported-models.md and the CHANGELOG now state that the 0.117 and 0.180 top-1 flips behind 1024 tokens are outside the 0.11 near-tie bar and remain open; no measured number changed.

Validation: cargo test -p mlxcel --lib models::deepseek_v3:: (26 passed), models::registry (21), models::mla_batched (2), execution::kv_arch (47), cargo test -p mlxcel-core --lib mla::latent_pair (4), all serial under the GPU lock; cargo clippy -p mlxcel -p mlxcel-core --lib --tests -D warnings and cargo fmt --check clean.

Refs #3160
@inureyes inureyes added status:done Completed and removed status:review Under review labels Nov 19, 2026
…-quantized-latent

# Conflicts:
#	CHANGELOG.md
#	docs/environment-variables.md
#3156 merged into this branch, so an unmasked int8 decode step on the DeepSeekV3Attention families now runs the fused kernel's quantized variant up to 2048 keys instead of the composed path. The #3160 rows were measured before that.

- Results doc: add an "After #3156 merged" section with the trace behind 1024 and decode at 512 and 2048 on Moonlight and Kimi-VL, re-measured on 9af1ce4, with path counters; keep the original rows as the pre-#3156 record. Behind 1024 every int8 step is absorbed_fused, decided disagreements stay 0, Moonlight has flips at 0.117 and 0.328 above the near-tie bar and Kimi-VL none. int8 decode is about 10% below fp16 at 512 (was about 17%) and unchanged at 2048, which stays composed.
- turbo-kv-cache.md, environment-variables.md, the #3160 CHANGELOG entry, the advisor text and comments in deepseek_v3.rs, mla_batched.rs and mla/decode.rs no longer say these families skip the fused kernel.

The fp16 trace behind 1024 is byte-identical to the recorded one on both checkpoints.

Refs #3160, #3156
@inureyes
inureyes marked this pull request as ready for review November 19, 2026 11:15
@inureyes
inureyes merged commit 3f5679a into main Nov 19, 2026
32 checks passed
@inureyes
inureyes deleted the feature/issue-3160-v3-quantized-latent branch November 19, 2026 11:31
inureyes added a commit that referenced this pull request Nov 19, 2026
…3181)

#3172 made deepseek_v3 keep int8 KV as a quantized MLA latent, so the load-profile test that expected int8 to be refused on a deepseek_v3 checkpoint failed: the profile no longer sees a substitution. The two refusal assertions now use turbo4-asym, which DeepSeek V3 still substitutes with fp16, and a new assertion pins that an int8 profile is accepted and keeps KVCacheMode::Int8.

Validated on ROCm gfx1151: cargo test --features rocm --lib server::webui::load_profile passes (4 tests).

Closes #3180
@inureyes inureyes added this to the 0.9 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core mlxcel-core: MLX FFI, primitives, KV cache, layers area:inference Generation, sampling, decoding (incl. speculative, DRY) area:models Model architectures, weights, loading, metadata priority:medium Medium priority status:done Completed type:enhancement New features, capabilities, or significant additions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: Store a quantized MLA latent in the DeepSeekV3Attention families

1 participant