Skip to content

Prefill over a compressed TurboQuant cache through the fused SDPA kernel - #717

Merged
davidkoski merged 2 commits into
ml-explore:mainfrom
aleroot:alessio/turboquant-fused-prefill
Dec 3, 2026
Merged

davidkoski merged 2 commits into
ml-explore:mainfrom
aleroot:alessio/turboquant-fused-prefill

Conversation

@aleroot

@aleroot aleroot commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

Proposed changes

Another chapter in my fight against prefill slowness on Apple Silicon. This time it's TurboQuant, and it's the worst case I've found so far: with a compressed cache, the second turn of a chat is 4–9× slower than with a plain fp16 cache.

A TurboQuantKVCache stays raw through the first prefill and compresses on the first decode step. After that, attentionWithCacheUpdate sends every call through compressedAttention, and for query blocks longer than one token it took a slow path:

  • Raw-key (turbo0v*) and affine-key (turbo8v*) schemes build the full [B, H, L, N] f32 score matrix, then run softmax and the compressed value sum.
  • Symmetric schemes (turbo4, turbo4v2, …) run turboFlashAttentionCausal, which is slower still.

So every follow-up message, tool result or pasted document gets prefilled at a fraction of normal speed, and it uses gigabytes of temporary scores.

For chunks of 16 or more queries, this change decodes the cached keys and values in their rotated basis, runs MLXFast.scaledDotProductAttention, and inverse-rotates only the output. The rotations are orthogonal, so the result matches the compressed-domain kernels. Decode and short speculative-verify chunks (L < 16) keep the existing kernels, which are faster there.

This change is independent of #703 and #704: #704 leaves TurboQuant on the convert-later path. It also stacks with #703, because TurboQuant's boundary layers use the 8-bit QuantizedKVCache that #703 speeds up.

Measurements

M4 Pro (24 GB, base model), Qwen3-14B-4bit, prefill step 512, 4k-token second turn over an 8k-token compressed history:

Scheme Before (main) After (this PR) Change
fp16 cache (reference) 21.8 s — —
turbo0v4 92–99 s 23–26 s ~4× faster
turbo8v4 92 s 23–28 s ~3.5× faster
turbo4 198 s 26–28 s ~7× faster
Peak MLX memory during the turn 13.8–16.9 GB 11.1–11.9 GB up to 5 GB less

The new test compressedPrefillChunkMatchesShortChunks checks (0,4), (8,4), (4,4) and (4,2). It confirms that one 32-token chunk on the fused path matches four 8-token chunks on the old kernels.

Checklist

Put an x in the boxes that apply.

  • I have read the CONTRIBUTING document
  • I have run pre-commit run --all-files to format my code / installed pre-commit prior to committing changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the necessary documentation (if needed)

AI usage

  • I have read this PR description in full and approve it as my own, and it
    accurately describes the code changes.
  • AI usage disclosure:

@davidkoski

Copy link
Copy Markdown
Member

Change looks good. Just needs a rebase to handle conflict with #704. Thank you!

@aleroot
aleroot force-pushed the alessio/turboquant-fused-prefill branch from 8233ce8 to 89675e0 Compare December 2, 2026 00:28
@aleroot

aleroot commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

Change looks good. Just needs a rebase to handle conflict with #704. Thank you!

Done, thanks

@davidkoski

Copy link
Copy Markdown
Member

Hrm, another ci failure on main.

@davidkoski

Copy link
Copy Markdown
Member

OK, #857 for those

@aleroot

aleroot commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

Probably we should consider something like: #791 ?

A TurboQuantKVCache stays raw through the first prefill and compresses on
the first decode step. After that, every multi-token prompt (the next turn
of a chat, a tool result) goes through compressedAttention. For L > 1 the
raw-key and affine-key schemes built the full [B, H, L, N] f32 score matrix,
and the symmetric schemes ran turboFlashAttentionCausal, which was slower
still.

For chunks of 16 or more queries, decode the keys and values in their
rotated basis, run MLXFast.scaledDotProductAttention, and inverse-rotate
only the output. The rotations are orthogonal, so the result matches the
compressed-domain kernels. Shorter chunks (decode, speculative verify) keep
the existing kernels, which win below 16 queries.

Measured on M4 Pro with Qwen3-14B-4bit, 4k second turn over an 8k
compressed history (fp16 cache: 21.8 s):
- turbo0v4: 92-99 s -> 23-26 s
- turbo8v4: 92 s -> 23-28 s
- turbo4: 198 s -> 26-28 s
- MLX peak during the turn: 13.8-16.9 -> 11.1-11.9 GB
- single layer, L = 512 over 8k: 12.7-17.6x faster, peak 0.8-1.5 -> 0.23-0.35 GB
On M5 GPUs MLX runs float32 matmuls as TF32, so the fused SDPA kernel and
the compressed-domain kernels round differently. The two attention paths
then differ by 7e-4 to 1.5e-3 relative RMS, and the fixed 1e-4 bound
fails on these machines.

Use MatmulPrecision.tolerance: 1e-4 stays the bound with IEEE float32,
5e-3 applies with TF32. A missing causal mask (0.2) and a 1% scale error
(1.2e-2) still fail.
@davidkoski
davidkoski force-pushed the alessio/turboquant-fused-prefill branch from c21e89c to f2363f7 Compare December 3, 2026 00:20
@davidkoski

Copy link
Copy Markdown
Member

Probably we should consider something like: #791 ?

Yeah, I was thinking the same thing :-)

Rebased to pick up main with the merged #858

@davidkoski
davidkoski merged commit 4ead844 into ml-explore:main Dec 3, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants