You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
perf(models): remove the fixed per-step cost of absorbed MLA decode #3121
#3102 (PR #3118) kept DeepSeek V2's decompressed MLA cache as the default because absorbed decode was 9.9% slower at pp512/tg128 (180.7 against 200.6 tok/s on an M5 Max, deepseek-v2-lite-chat-4bit-mlx), although it is 57% faster behind a 16K prompt (111.2 against 70.9 tok/s) and peaks 6.7 GB lower at 28K with 2048-token chunks. The crossover is near 3K tokens of context. The measurement suggests a fixed per-step cost of about 0.55 ms in the absorbed path at short context.
DeepSeek V3, V3.2, Kimi-VL and Kimi-K2 always take the absorbed path, so any fixed decode cost there applies to every decode step of those models too.
Acceptance Criteria
Profile one absorbed decode step at 512 tokens of context against the decompressed step (per-op GPU time, e.g. with the mlxcel-gpu-profiling approach), and post where the extra ~0.55 ms goes.
Remove or shrink the dominant fixed cost (for example quantized or fused folded projections, or a fused absorbed attention), with output unchanged or the teacher-forced trace reported if arithmetic changes.
Re-measure pp512/tg128 and the 2048- and 16K-context decode for deepseek-v2-lite in both layouts, plus one always-absorbed model if a checkpoint fits (kimi-vl-a3b-thinking-4bit). If absorbed decode now holds within a few percent at short context, revisit the V2 default per docs/mla-absorbed-decode.md.
Profile of one decode step at 512 tokens of context, DeepSeek-V2-Lite geometry (16 heads, rank 512, rope 64, nope/v 128), 27 chained layers, M5 Max. Method: an in-context ablation in MLX (each layer's output feeds the next layer's projections so the graph is serial like a real step; graph built first, only eval timed; median of 150 evals, best of three interleaved rounds; GPU lock held). Each row removes one component, so the delta is that component's marginal cost per token. Per-shader GPU time from a .gputrace was not used: the capture replay is impractical at this size, and the ablation answers the same question.
step variant (S=512)
ms/token
marginal cost
absorbed, as on main (dense f16 folds, rope mask + SDPA)
The folds: 0.36 ms. Two batched f16 GEMVs over 113 MB of dequantized W_UK/W_UV per token; unabsorb_output (N=128 per head) runs at about 230 GB/s, absorb_queries at about 385 GB/s. Against the decompressed step's single 4-bit kv_b_proj GEMV, that is the ~0.3 ms difference between the two no-attention rows.
The attention block: the 512-wide head with an array mask is not fused by MLX, so the step runs the rope-score matmul, the scale, the score matmul, the mask add, the softmax and the value matmul as separate small kernels (about 34 us per layer at 512 keys, latency rather than bytes: it reads 0.6 MB per layer). Two other compositions (GQA-row score matmul, addmm) and forcing MLX's head-dim-512 vector kernel with the mask (MLX_SDPA_D512_MIN_KL=0) did not help.
Fixes, in order of the measured cost:
Fold the quantized kv_b_proj without dequantizing. Its packed rows are grouped along the rank axis, which is the output axis of the query contraction and the input axis of the output one, so both run as batched quantized_matmul (transpose false and true) on the checkpoint's own planes: 30 MiB instead of 113 MiB, same quantized weights as the decompressed path. Ablation: 2.06 to 1.83 ms.
A fused Metal kernel for unmasked single-query absorbed attention: both score halves, an online softmax and the latent-value sum, split over two key halves plus a merge. Prototype against the composed block alone (27 layers): 1.12 to 0.66 ms at 512 keys, 1.49 to 1.12 at 2048, 3.88 to 3.26 at 8192, slower from 16384 (each head re-reads the window), so the composed path keeps windows above 8192.
End-to-end numbers on the real checkpoint follow in the PR.
Activity
inureyes commentedon Nov 16, 2026
Profile of one decode step at 512 tokens of context, DeepSeek-V2-Lite geometry (16 heads, rank 512, rope 64, nope/v 128), 27 chained layers, M5 Max. Method: an in-context ablation in MLX (each layer's output feeds the next layer's projections so the graph is serial like a real step; graph built first, only
evaltimed; median of 150 evals, best of three interleaved rounds; GPU lock held). Each row removes one component, so the delta is that component's marginal cost per token. Per-shader GPU time from a.gputracewas not used: the capture replay is impractical at this size, and the ablation answers the same question.absorb_queriesunabsorb_outputkv_b_proj+ fused vector SDPA)Where the extra time goes:
W_UK/W_UVper token;unabsorb_output(N=128 per head) runs at about 230 GB/s,absorb_queriesat about 385 GB/s. Against the decompressed step's single 4-bitkv_b_projGEMV, that is the ~0.3 ms difference between the two no-attention rows.addmm) and forcing MLX's head-dim-512 vector kernel with the mask (MLX_SDPA_D512_MIN_KL=0) did not help.Fixes, in order of the measured cost:
kv_b_projwithout dequantizing. Its packed rows are grouped along the rank axis, which is the output axis of the query contraction and the input axis of the output one, so both run as batchedquantized_matmul(transpose false and true) on the checkpoint's own planes: 30 MiB instead of 113 MiB, same quantized weights as the decompressed path. Ablation: 2.06 to 1.83 ms.End-to-end numbers on the real checkpoint follow in the PR.
perf(mla): quantized folds and a fused kernel for absorbed decode (la…
perf(mla): absorb DeepSeek V2 decode by default where the fused kerne…