Repository navigation
Prefill over a compressed TurboQuant cache through the fused SDPA kernel - #717
Merged
davidkoski merged 2 commits intoDec 3, 2026
Merged
Conversation
Member
|
Change looks good. Just needs a rebase to handle conflict with #704. Thank you! |
davidkoski
approved these changes
Dec 1, 2026
aleroot
force-pushed
the
alessio/turboquant-fused-prefill
branch
from
December 2, 2026 00:28
8233ce8 to
89675e0
Compare
Contributor
Author
Done, thanks |
Member
|
Hrm, another ci failure on main. |
Member
|
OK, #857 for those |
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
force-pushed
the
alessio/turboquant-fused-prefill
branch
from
December 3, 2026 00:20
c21e89c to
f2363f7
Compare
Member
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
TurboQuantKVCachestays raw through the first prefill and compresses on the first decode step. After that,attentionWithCacheUpdatesends every call throughcompressedAttention, and for query blocks longer than one token it took a slow path:turbo0v*) and affine-key (turbo8v*) schemes build the full[B, H, L, N]f32 score matrix, then run softmax and the compressed value sum.turbo4,turbo4v2, …) runturboFlashAttentionCausal, 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
QuantizedKVCachethat #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:
turbo0v4turbo8v4turbo4The new test
compressedPrefillChunkMatchesShortChunkschecks(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
xin the boxes that apply.pre-commit run --all-filesto format my code / installed pre-commit prior to committing changesAI usage
accurately describes the code changes.