Repository navigation
Description
Problem
While running the #354 KV-cache throughput sweeps, Turbo4Asym (fp16-K + 4bit-V, the advisory --recommend-quant suggestion for medium/long context) decode throughput is catastrophically slow: 0.13-0.24x fp16, scaling worse with context.
Measured on M1 Ultra, qwen2.5-7b-4bit at ~4k context, greedy, decode tok/s:
| mode | decode tok/s | vs fp16 |
|---|---|---|
| fp16 | 99 | 1.00x |
| int8 | 77 | 0.78x |
| turbo4-asym | 13 (default) -> 24 (max sparse-V threshold) | 0.13-0.24x |
| turbo4-delegated | 79 | 0.80x |
So Turbo4Asym is the slowest mode while Turbo4Delegated and int8 are fast. The bench_kv_cache.sh acceptance gates expect turbo4-asym decode >= 0.95x fp16; the reality is far below that (these gates were never validated, per #327).
Root cause
Turbo4Asym decode is hardwired to the sparse-V weighted-sum path (KVCache::update_and_sparse_v_attention -> turbo::sparse_v::attention_sparse_v_turbo4) in the model dispatch (src/models/qwen3.rs around line 177 and src/models/llama3.rs around line 218: if l == 1 && cache.sparse_v_available() { update_and_sparse_v_attention } else { ... }, where sparse_v_available() is matches!(mode, Turbo4Asym)). The sparse-V path computes a post-softmax alive-mask, sparse-gathers V, and weighted-sums it; at the default threshold 1e-6 almost nothing is skipped, so it pays the full V-dequant cost plus the masking/gather overhead. Raising MLXCEL_SPARSE_V_THRESHOLD to 0.1 only reaches 0.24x (the masking/gather overhead dominates, not the skip count).
Symmetric Turbo4 and Turbo4Delegated both already have a dequant-first SDPA path (KVCache::update_and_turbo4_dequant_sdpa_attention / update_and_turbo4_delegated_attention, both default-on) that dequantizes V transiently and runs native MLX SDPA, reaching 0.79-0.80x fp16. Turbo4Asym is the only Turbo mode with no such path.
Not a #353 regression: turbo4-asym is equally slow at MLX_MAX_OPS_PER_BUFFER=40 (12.2 tok/s) and 1000 (13.6 tok/s).
Proposed fix
Give Turbo4Asym decode a dequant-then-SDPA path: dequantize the 4bit-V to fp16 transiently and run native SDPA with the fp16-K, mirroring int8 / Turbo4 / Turbo4Delegated. Make it the default (it is both more accurate than the sparse-V approximation and ~3-6x faster), keeping the sparse-V path reachable behind an env flag for A/B and fallback. Update every model call site that routes Turbo4Asym through sparse-V (qwen3, llama3, and any others; confirm which model handles qwen2.5 since it was slow in the sweep).
Acceptance criteria
- Turbo4Asym decode reaches roughly int8 throughput (>= 0.7x fp16) at 4k/16k/32k on M1 Ultra.
- Output is numerically correct: parity with the exact dequant + SDPA reference (the lossy sparse-V approximation is replaced by exact dequant + native SDPA, so quality should improve or match).
- The sparse-V path remains reachable via an env flag (A/B / fallback), default off-the-hot-path.
- No regression to Turbo4 / Turbo4Delegated / int8 / fp16 decode.
Found while running #354; the advisor's Turbo4Asym recommendation depends on this fix.
Activity
perf(cache): route Turbo4Asym decode through dequant-SDPA (lablup#369)
perf(cache): route Turbo4Asym decode through dequant-SDPA (lablup#369)