Repository navigation
Closed
Labels
area:coremlxcel-core: MLX FFI, primitives, KV cache, layerspriority:mediumMedium prioritystatus:doneCompletedtype:enhancementNew features, capabilities, or significant additions
Milestone
Description
Part of #52
Goal
Estimate KV-cache memory from config + requested context length, replacing the flat KV_CACHE_HEADROOM_GB = 2 constant in recommend_quantization.
Why
The 2 GB constant (src/lib/mlxcel-core/src/hardware.rs:436) is correct only by coincidence for ~8B / 8K-context models; it is badly wrong for long context or layer-heavy models. KV cache is exactly computable once context length is fixed.
Scope / implementation
- Add
kv_cache_bytes(num_layers, num_kv_heads, head_dim, elem_bytes, ctx_len, batch) -> u64plus a config-driven wrapper. - Formula:
num_layers × 2 (K,V) × num_kv_heads × head_dim × elem_bytes × ctx_len × batch. - Account for mlxcel's 256-token pre-allocation rounding (round
ctx_lenup to the next multiple of 256) so the estimate matches the actual reservation — see the KV buffer pre-alloc insrc/lib/mlxcel-core/src/cache.rs/src/lib/mlxcel-core/src/layers.rs. - Honor KV dtype / TurboQuant flags: INT8 KV (
--cache-type-k,--cache-type-v,--kv-cache-mode) ≈ half the bytes; setelem_bytesaccordingly (seesrc/cli/turbo_args.rs).
Integration (required for completion)
- Replace the flat constant in
recommend_quantization()with the computed KV figure, parameterized by context/batch/KV-dtype. - Feed the same function into the unified estimator (sub-issue D) so
inspect/ preflight report a real KV number, not a constant.
Acceptance criteria
- Tests for dense (MHA), GQA (kv_heads < heads), long context (e.g. 128K), and INT8 KV (half) cases; 256-token rounding verified.
recommend_quantizationno longer uses a hardcoded headroom; its fit decision changes appropriately for long-context requests.
Metadata
Metadata
Assignees
Labels
area:coremlxcel-core: MLX FFI, primitives, KV cache, layerspriority:mediumMedium prioritystatus:doneCompletedtype:enhancementNew features, capabilities, or significant additions
Activity
feat: unified memory estimator with `mlxcel inspect` and preflight (#67)
feat: unified memory estimator with `mlxcel inspect` and preflight (l…