Repository navigation
Description
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_configandestimate_kv_arch_inner(src/execution/kv_arch.rs:600-690) takeint8_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
falseunconditionally for the KV mode:src/server/prompt_cache/snapshot_sizing.rs:137(KV store),:264(snapshot store, unwindowed) andsrc/execution/kv_cache_advisor.rs:338(classification behind the advice).src/execution/request_memory.rs:173also passesfalse, but that is a Nemotron-H FP16 identity check and is not affected. estimate_total_memory(src/execution/memory_estimate.rs:576-582) takeskv_dtype_int8: booland forwards it at:591. The CLI preflights resolve the effective mode and then collapse it to a bool atsrc/commands/inspect.rs:97-100,src/commands/generate.rs:383-393andsrc/commands/serve.rs:429-435.mlxcel inspectalready accepts the KV mode:InspectArgsflattensTurboKvCacheArgs(src/main.rs:985-987) andrun_inspectresolves 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 tokv_int8.
Proposed Solution
Make KVCacheMode the KV input of the estimator, so layout, element width and decline all derive from one value.
- In
src/execution/kv_arch.rs, replaceint8_kv: boolwithkv_mode: KVCacheModeinestimate_kv_arch,estimate_kv_arch_from_config,estimate_kv_arch_unwindowed_from_configandestimate_kv_arch_inner; deriveint8 = kv_mode == KVCacheMode::Int8forsum_groups, andv2_latent = deepseek_v2_caches_latent(kv_mode). - Redefine the rule as
deepseek_v2_latent_layout(absorbed: bool, mode: KVCacheMode) -> boolreturningabsorbed && mlxcel_core::mla::latent_layout_supports_mode(mode).is_ok(), withdeepseek_v2_caches_latent(mode)passingmla::absorbed_enabled(). Dropconst(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 toFp16keeps the estimate from drifting the next time a mode is added. - In
src/execution/memory_estimate.rs, changeestimate_total_memoryandkv_cache_bytes_per_tokento takekv_mode: KVCacheMode. KeepMemoryEstimate.kv_dtype_int8asmode == Int8and keep the banner label, soformat_estimateand the inspect JSON schema do not change. Keep the paged helpers (paged_block_bytes,resolve_paged_block_budget,resolve_paged_slab_blocks) onkv_dtype_int8: booland mapInt8/Fp16inside: the paged pool serves dense non-MLA families anddeepseek_v2does not opt into it. The fp16/int8 lanes ofInspectReport::from_estimate(src/execution/memory_estimate.rs:480-481) passFp16andInt8explicitly. - Pass the mode from each caller: the inspect/generate/serve preflights use the
kv_cache_modethey already resolved, replacing thekv_int8local;src/execution/request_memory.rs:173passesKVCacheMode::Fp16with a comment. - Prompt-cache sizing: add
kv_mode: KVCacheModetorecommend_model_snapshot_capacity,recommend_model_snapshot_capacity_from_config,..._from_config_with_entries,recommend_model_kv_store_capacityandrecommend_model_kv_store_capacity_from_config, and tomemory_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()whenconfig.batch_kv_quant.is_enabled()(src/lib/mlxcel-core/src/cache/batch_quant.rs:242,270), otherwiseconfig.kv_cache_mode(src/server/config.rs:1036). - Advisor: add
kv_modetoadvise_kv_cache_modes,advise_kv_cache_modes_from_config,arch_and_type_from_configandadvise_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 becomesMlaDecompressed, 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 atsrc/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 localmode == Fp16. - Constraints:
estimate_kv_arch*ispubin themlxcelcrate; the signature change is internal-only but updates every test call site, which should move mechanically toKVCacheMode::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 isrecommend_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, aspublic_estimate_follows_the_process_absorption_decisionalready does (src/execution/kv_arch.rs:963-976). - Error handling: no new failure path; the estimator discards the
Err(String)text fromlatent_layout_supports_mode.
Acceptance Criteria
-
deepseek_v2_latent_layouttakes aKVCacheModeand returns true only forFp16; no INT8-only decline remains insrc/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,Turbo4andTurbo4Delegatedeach 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 withKVCache::new_pagedoverPagedBlockPool::newandPagedSequenceState::newis declined byMlaLatentCache::supportswith the paged reason; the quantized-mode test at:431stays. - Snapshot sizing test:
recommend_model_kv_store_capacity_from_configon the V2-Lite config yields the decompressedentry_bytesunderTurbo4Asym, and the latent figure underFp16when absorption is on in this process; assert the relationship conditionally onmla::absorbed_enabled()and assert the two are equal when it is off. - Advisor test:
advise_kv_cache_modes_from_config(cfg, Turbo4Asym)classifiesdeepseek_v2asMlaDecompressedon any host. -
mlxcel inspect --jsonon a V2-Lite directory prints the decompressed per-token rate with--cache-type-v turbo4-asymand the latent rate with--kv-cache-mode fp16on an absorbed build, andkv_cache_modein 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 warningsManual 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
test(registry): check kv_modes against the host-independent rule
fix(memory): size the FP16 last layer under --kv-bits 8
docs(mla): describe the fp16 substitution for quantized deepseek_v2 KV
fix(memory): size DeepSeek V2 KV by the mode the runtime builds (labl…