Skip to content

fix(memory): size DeepSeek V2 KV through the non-FP16 cache-mode decline #3153

Closed

Description

@inureyes

Problem / Background

kv_arch sizes the DeepSeek V2 KV cache as the absorbed MLA latent whenever mla::absorbed_enabled() holds and the KV cache is not INT8 (deepseek_v2_latent_layout, src/execution/kv_arch.rs:350-351). The runtime is stricter: MlaLatentCache::supports (src/lib/mlxcel-core/src/mla/cache.rs:215-228) wraps a cache as a latent only when mla::latent_layout_supports_mode (src/lib/mlxcel-core/src/mla/cache.rs:183-191) accepts the mode, and that function accepts KVCacheMode::Fp16 alone. DeepseekV2::forward (src/models/deepseek_v2.rs:453-460) and its batched path (src/models/deepseek_v2.rs:655-663) then keep decompressed per-head K/V for every other mode. A turbo-quant cache (Turbo4Asym, Turbo3Asym, Turbo4, Turbo4Delegated) is therefore sized as the 576-element latent while the run stores num_heads × (qk_nope + qk_rope + v_head_dim) per token per layer: on the V2-Lite fixture that is 1152 estimated bytes per token per layer against 10240 actually cached, an 8.9x undercount.

PR #3152 made absorbed MLA the DeepSeek V2 default on ROCm, so the absorbed branch is now the default there; the bug is reachable whenever a non-FP16 KV mode is selected. resolve_kv_cache_mode_for_model deliberately does not substitute for deepseek_v2 (src/lib/mlxcel-core/src/cache/turbo/allowlist.rs:195-217, pinned by deepseek_v2_is_not_treated_as_a_latent_family at :324-333), so --kv-cache-mode turbo4-asym and --cache-type-v turbo4 reach the runtime unchanged, and the --kv-bits disable at src/server/cli_input.rs:981-1000 fires only on a substitution this family never gets. The same mismatch exists on Metal and CUDA; only the default differs.

The undercount runs in the unsafe direction for a memory budget. recommend_model_kv_store_capacity_from_config derives entry_bytes from the estimate (src/server/prompt_cache/snapshot_sizing.rs:137), so the prompt-cache store is sized for a smaller entry than the runtime parks and evicts live snapshots. memory_left_after_model (src/server/prompt_cache/snapshot_sizing.rs:180-190) and the --estimate-memory preflights, which abort serve/generate on an over-budget run, read the same figure, so a configuration that will not fit passes the preflight and fails at allocation.

Current Behavior

  • estimate_kv_arch, estimate_kv_arch_from_config, estimate_kv_arch_unwindowed_from_config and estimate_kv_arch_inner (src/execution/kv_arch.rs:600-690) take int8_kv: bool; that one bool selects both the element width (elem_bytes, :130-134; sum_groups, :529) and the only latent decline (deepseek_v2_caches_latent, :358-360).
  • The estimate callers pass false unconditionally for the KV mode: src/server/prompt_cache/snapshot_sizing.rs:137 (KV store), :264 (snapshot store, unwindowed) and src/execution/kv_cache_advisor.rs:338 (classification behind the advice). src/execution/request_memory.rs:173 also passes false, but that is a Nemotron-H FP16 identity check and is not affected.
  • estimate_total_memory (src/execution/memory_estimate.rs:576-582) takes kv_dtype_int8: bool and forwards it at :591. The CLI preflights resolve the effective mode and then collapse it to a bool at src/commands/inspect.rs:97-100, src/commands/generate.rs:383-393 and src/commands/serve.rs:429-435.
  • mlxcel inspect already accepts the KV mode: InspectArgs flattens TurboKvCacheArgs (src/main.rs:985-987) and run_inspect resolves the effective mode (src/commands/inspect.rs:91-97). It needs no new flag; it is wrong today only because line 98 reduces the mode to kv_int8.

Proposed Solution

Make KVCacheMode the KV input of the estimator, so layout, element width and decline all derive from one value.

  1. In src/execution/kv_arch.rs, replace int8_kv: bool with kv_mode: KVCacheMode in estimate_kv_arch, estimate_kv_arch_from_config, estimate_kv_arch_unwindowed_from_config and estimate_kv_arch_inner; derive int8 = kv_mode == KVCacheMode::Int8 for sum_groups, and v2_latent = deepseek_v2_caches_latent(kv_mode).
  2. Redefine the rule as deepseek_v2_latent_layout(absorbed: bool, mode: KVCacheMode) -> bool returning absorbed && mlxcel_core::mla::latent_layout_supports_mode(mode).is_ok(), with deepseek_v2_caches_latent(mode) passing mla::absorbed_enabled(). Drop const (the shared rule builds an error string) and update the doc comment at :341-349, which currently names INT8 as the only decline. Calling the runtime rule instead of comparing to Fp16 keeps the estimate from drifting the next time a mode is added.
  3. In src/execution/memory_estimate.rs, change estimate_total_memory and kv_cache_bytes_per_token to take kv_mode: KVCacheMode. Keep MemoryEstimate.kv_dtype_int8 as mode == Int8 and keep the banner label, so format_estimate and the inspect JSON schema do not change. Keep the paged helpers (paged_block_bytes, resolve_paged_block_budget, resolve_paged_slab_blocks) on kv_dtype_int8: bool and map Int8/Fp16 inside: the paged pool serves dense non-MLA families and deepseek_v2 does not opt into it. The fp16/int8 lanes of InspectReport::from_estimate (src/execution/memory_estimate.rs:480-481) pass Fp16 and Int8 explicitly.
  4. Pass the mode from each caller: the inspect/generate/serve preflights use the kv_cache_mode they already resolved, replacing the kv_int8 local; src/execution/request_memory.rs:173 passes KVCacheMode::Fp16 with a comment.
  5. Prompt-cache sizing: add kv_mode: KVCacheMode to recommend_model_snapshot_capacity, recommend_model_snapshot_capacity_from_config, ..._from_config_with_entries, recommend_model_kv_store_capacity and recommend_model_kv_store_capacity_from_config, and to memory_left_after_model. resolve_prompt_cache_store (src/server/startup.rs:3412-3489) passes the mode the scheduler will really build: config.batch_kv_quant.base_mode() when config.batch_kv_quant.is_enabled() (src/lib/mlxcel-core/src/cache/batch_quant.rs:242,270), otherwise config.kv_cache_mode (src/server/config.rs:1036).
  6. Advisor: add kv_mode to advise_kv_cache_modes, advise_kv_cache_modes_from_config, arch_and_type_from_config and advise_quantization (src/execution/quant_advisor.rs:218), and pass generate's resolved mode at both call sites (src/commands/generate.rs:3105,3112). With a declining mode the classified kind becomes MlaDecompressed, which is the cache the run builds, so Medium/Long advice returns INT8 as for any decompressed MLA family instead of the "keep fp16 because a quantized mode turns absorption off" branch at src/execution/kv_cache_advisor.rs:219-236.

Rejected alternative: adding deepseek_v2 to MLA_LATENT_CACHE_FAMILIES would force FP16 and hide the mismatch, but it removes a working configuration (the allowlist test at :324-333 exists to prevent that) and leaves every future mode mis-sized.

Scope

In scope: src/execution/kv_arch.rs, src/execution/memory_estimate.rs, src/server/prompt_cache/snapshot_sizing.rs, src/execution/kv_cache_advisor.rs, src/execution/quant_advisor.rs, src/execution/request_memory.rs, src/server/startup.rs, src/commands/{inspect,generate,serve}.rs, and their tests.

Out of scope: modelling Turbo per-token compression in the byte model (Turbo modes keep the FP16 element width, which overestimates, the safe direction); enabling the paged decode backend for deepseek_v2; the MLXCEL_MLA_ABSORBED tri-state itself.

Implementation Notes

  • Reuse: take the decline rule from mlxcel_core::mla::latent_layout_supports_mode, never a local mode == Fp16.
  • Constraints: estimate_kv_arch* is pub in the mlxcel crate; the signature change is internal-only but updates every test call site, which should move mechanically to KVCacheMode::Fp16 / KVCacheMode::Int8.
  • Coverage gap to close, not widen: snapshot_family_is_model_aware (src/server/prompt_cache/snapshot_sizing.rs:335-372) excludes MLA families, so the reachable budget for DeepSeek V2 is recommend_model_kv_store_capacity. The snapshot entry points still take the mode because they call the same estimator.
  • Edge cases: on a backend where absorbed_enabled() is false (a CPU-only test host), FP16 and a Turbo mode must produce the same decompressed estimate; assert that conditionally, as public_estimate_follows_the_process_absorption_decision already does (src/execution/kv_arch.rs:963-976).
  • Error handling: no new failure path; the estimator discards the Err(String) text from latent_layout_supports_mode.

Acceptance Criteria

  • deepseek_v2_latent_layout takes a KVCacheMode and returns true only for Fp16; no INT8-only decline remains in src/execution/.
  • Unit test: FP16 with absorption enabled sizes the latent on the V2-Lite fixture (KvArchKind::MlaCompressed, 1152 bytes per token per layer).
  • Unit test: Int8, Turbo4Asym, Turbo3Asym, Turbo4 and Turbo4Delegated each size the decompressed layout (KvArchKind::MlaDecompressed) with absorption enabled.
  • Unit test: absorption declined sizes decompressed under FP16 (update the existing mla_v2_uses_decompressed_per_head_without_absorption).
  • Unit test in src/lib/mlxcel-core/src/mla/cache.rs: an FP16, paged-backed cache built with KVCache::new_paged over PagedBlockPool::new and PagedSequenceState::new is declined by MlaLatentCache::supports with the paged reason; the quantized-mode test at :431 stays.
  • Snapshot sizing test: recommend_model_kv_store_capacity_from_config on the V2-Lite config yields the decompressed entry_bytes under Turbo4Asym, and the latent figure under Fp16 when absorption is on in this process; assert the relationship conditionally on mla::absorbed_enabled() and assert the two are equal when it is off.
  • Advisor test: advise_kv_cache_modes_from_config(cfg, Turbo4Asym) classifies deepseek_v2 as MlaDecompressed on any host.
  • mlxcel inspect --json on a V2-Lite directory prints the decompressed per-token rate with --cache-type-v turbo4-asym and the latent rate with --kv-cache-mode fp16 on an absorbed build, and kv_cache_mode in the report names the mode actually used.
  • The estimates are reached from the real paths (serve startup KV-store budget and preflights, inspect, --recommend-quant), not exposed as unused helpers.

Verification

cargo test -p mlxcel --lib execution::kv_arch
cargo test -p mlxcel --lib execution::kv_cache_advisor
cargo test -p mlxcel --lib execution::memory_estimate
cargo test -p mlxcel --lib server::prompt_cache
cargo test -p mlxcel-core --lib mla::cache
cargo fmt --all -- --check
cargo clippy --workspace --all-targets -- -D warnings

Manual pass on a ROCm or CUDA build where absorption is the default: mlxcel inspect <deepseek-v2-lite> --max-tokens 32768 prints 972.0 MiB of KV with the default FP16 mode and 8.44 GiB with --cache-type-v turbo4-asym (both read 972.0 MiB before this change). A serve --cache-type-v turbo4-asym launch with the prompt cache enabled logs a larger recommended_kv_store_capacity than before, matching the decompressed entry.

Technical Considerations

The runtime answer is per cache instance but stable for the cache's life (src/lib/mlxcel-core/src/mla/cache.rs:205-214), and the server builds every sequence's cache from one resolved mode, so a single mode argument is faithful. PR #3152 carried no sizing change because the estimator already read absorbed_enabled(); that is sufficient only while the KV mode is FP16, which is why this is filed separately. Refs #3152, #3133, #3137.

Activity

  1. added
    status:ready
    type:bug
    priority:high
    area:core
    area:inference
    status:in-progress
    status:review
    and removed
    status:ready
    status:in-progress
    on Nov 18, 2026
  2. added
    status:done
    and removed
    status:review
    on Nov 18, 2026
  3. added a commit that references this issue on Nov 19, 2026
    9ede15d
  4. added this to the 0.9 milestone on Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:corearea:inferencepriority:highstatus:donetype:bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions