Skip to content

fix(memory): size the DeepSeek V2 KV cache the way the runtime lays it out - #3137

Merged
inureyes merged 1 commit into
mainfrom
fix/deepseek-v2-kv-sizing-latent
Nov 16, 2026
Merged

inureyes merged 1 commit into
mainfrom
fix/deepseek-v2-kv-sizing-latent

Conversation

@inureyes

Copy link
Copy Markdown
Member

Summary

Since #3131 and #3136, DeepSeek V2 caches the MLA latent by default wherever the fused Metal kernel runs, but kv_arch still sized every deepseek_v2 config as the decompressed per-head cache: 10240 bytes per token per layer on DeepSeek-V2-Lite against the 1152 actually cached (8.9x). The figure feeds the prompt-cache snapshot budget, mlxcel inspect / --estimate-memory and the KV cache advisor. This is the sizing item of #3133, done before the release so the new default and its estimates ship together.

  • kv_arch resolves the deepseek_v2 layout with the loader's own decision: latent when mla::absorbed_enabled() holds and the KV cache is FP16 (deepseek_v2_latent_layout), decompressed under MLXCEL_MLA_ABSORBED=0, on a backend without the fused kernel, or with an int8 cache (latent_layout_supports_mode refuses it at runtime). The paged-cache decline cannot reach deepseek_v2, which does not opt into the paged decode backend.
  • The always-latent families in MLA_LATENT_CACHE_FAMILIES (GLM-4.7-Flash, Kimi Linear, Kimi K2, ...) were also sized per head; they now size the latent.
  • The advisor no longer recommends int8 KV for an absorbing DeepSeek V2: int8 turns absorption off and returns to the decompressed cache, about 4x the fp16 latent. Without absorption the int8 advice is unchanged.
  • CHANGELOG: the Known Issues entry that still said V2 keeps the decompressed cache by default now describes CUDA and ROCm only, and a Fixed entry covers the sizing.

Validation

  • execution:: and server::prompt_cache:: lib tests pass on Metal (406), including new tests for the latent and decompressed sizes, the int8 decline, the layout rule, the process-level decision, the latent-pair families and the advisor rule. The old mla_v2_uses_decompressed_per_head becomes mla_v2_uses_decompressed_per_head_without_absorption. clippy --lib --tests -D warnings and fmt pass.
  • mlxcel inspect on the published DeepSeek-V2-Lite-Chat-4bit-mlx config.json at 32768 tokens: KV cache 8.44 GiB before, 972.0 MiB after (M3 Ultra, absorbed by default; 31104 instead of 276480 bytes per token), 8.44 GiB with MLXCEL_MLA_ABSORBED=0, 4.22 GiB (decompressed) with an int8 cache.

Not done here: the CUDA/ROCm measurements in #3133, which decide whether the default (and with it the sizing) changes on those backends.

Refs #3133

…t out

Since #3131 DeepSeek V2 caches the MLA latent by default where the fused Metal kernel runs, but kv_arch still sized every deepseek_v2 config as the decompressed per-head cache (10240 against 1152 bytes per token per layer on V2-Lite, 8.9x). kv_arch now follows the loader's decision: latent when absorbed decode is selected and the KV cache is FP16, decompressed otherwise. The always-latent MLA families are sized as the latent too, and the KV cache advisor no longer advises int8 for an absorbing V2, since int8 turns absorption off. The CHANGELOG Known Issues entry that still called the decompressed cache the V2 default now covers CUDA and ROCm only.

execution:: and server::prompt_cache:: lib tests pass on Metal; mlxcel inspect on the V2-Lite config at 32K tokens drops from 8.44 GiB to 972.0 MiB of KV.

Refs #3133
@inureyes inureyes added type:bug Bug fixes, error corrections, or issue resolutions priority:medium Medium priority status:review Under review area:models Model architectures, weights, loading, metadata labels Nov 16, 2026
@inureyes
inureyes merged commit be70b63 into main Nov 16, 2026
32 checks passed
@inureyes
inureyes deleted the fix/deepseek-v2-kv-sizing-latent branch November 16, 2026 21:37
@inureyes inureyes added status:done Completed and removed status:review Under review labels Nov 16, 2026
@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:models Model architectures, weights, loading, metadata priority:medium Medium priority status:done Completed type:bug Bug fixes, error corrections, or issue resolutions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant