Add TurboQuant3/4 modes to quantized_scaled_dot_product_attention#3453
Closed
dedalien wants to merge 23 commits into
Closed
Add TurboQuant3/4 modes to quantized_scaled_dot_product_attention#3453dedalien wants to merge 23 commits into
dedalien wants to merge 23 commits into
Conversation
Add Affine dispatch entries for group_size=64 at bits={4,6,8} and
relax the validation in quantized_scaled_dot_product_attention.
This matches the default produced by mx.quantize(mode="affine") and
the kv_group_size=64 default used by mlx-lm, so users following the
MLX/mlx-lm conventions no longer hit an error when using fused
quantized attention.
Benchmarks (M4, B=1 H=32 D=128 Lq=1, affine 4-bit):
Context gs=32 fused gs=64 fused speedup
32K 50 us 41 us +22%
64K 95 us 82 us +16%
128K 176 us 152 us +16%
gs=64 is faster at long context because it has half the scale/bias
memory traffic.
Costs:
mlx.metallib: 128,161,428 -> 128,233,236 bytes (+0.056%)
libmlx.dylib: unchanged
Existing 10 test_quantized_sdpa* tests continue to pass (54 subtests).
Integrates TurboQuant (arXiv 2504.19874) into the generic quant-SDPA infrastructure from ml-explore#3026, rather than as a standalone kernel. New modes "turbo3" (3-bit) and "turbo4" (4-bit) use Lloyd-Max codebooks for the N(0,1) distribution of WHT-rotated, norm-normalised key coordinates. Codebooks are compile-time Metal constants; no runtime buffer needed. Memory layout: - K/V: uint32-packed bit indices, same PackReader<N> path as affine - k_scales/v_scales: per-vector float16 norms / sqrt(D) - group_size == head_dim (one scale per full D-element vector) - Queries pre-rotated by caller via WHT Changes: - primitives.h/cpp: TurboQuant3/TurboQuant4 in QuantizationMode enum, string parsing, quantization_mode_to_string explicit cases - fast.cpp: TurboQuant validation branch (no biases, float scales, group_size == head_dim, head_dim in {64,128,256}, GPU-only fallback) - quantized_utils.h: QuantMode enum extension, QuantConfig/Dequant specialisations, Lloyd-Max codebook constants - sdpa_vector.h: static_assert extended for bits=3; QUANT_SDPA_DISPATCH entries for D in {64,128,256} gated on if constexpr (D >= group_size) - scaled_dot_product_attention.cpp: quant_mode_to_int (TurboQuant3=4, TurboQuant4=5) - python/src/fast.cpp: turbo3/turbo4 in mode docstring Tested on Qwen3.6-27B (head_dim=256, 24Q/4KV, GQA=6), 24 GB unified memory Mac. Enables longer context generation on memory-constrained hardware by compressing the KV cache ~5x.
12 sub-cases: D in {64,128,256} x bits in {3,4} x B in {1,2},
each targeting a distinct Metal template instantiation.
Explicit bfloat16, error path coverage.
Collaborator
|
Before we merge the #3026 it is suggested to turn this into a PR that targets CC-Yeh's repo. |
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.
Integrates TurboQuant (arXiv 2504.19874) into the generic quant-SDPA infrastructure from #3026 (Dan Yeh), rather than as a standalone kernel.
New modes
"turbo3"(3-bit) and"turbo4"(4-bit) use Lloyd-Max codebooks for the N(0,1) distribution of WHT-rotated, norm-normalised key coordinates. Codebooks are compile-time Metal constants; no runtime buffer needed.Memory layout
PackReader<N>path as affinek_scales/v_scales: per-vector float16 norms / √Dgroup_size == head_dim(one scale per full D-element vector)Changes
primitives.h/cpp:TurboQuant3/TurboQuant4inQuantizationModeenum, string parsing,quantization_mode_to_stringexplicit casesfast.cpp: TurboQuant validation branch (no biases, float scales,group_size == head_dim,head_dim ∈ {64,128,256}, GPU-only fallback)quantized_utils.h:QuantModeenum extension,QuantConfig/Dequantspecialisations, Lloyd-Max codebook constantssdpa_vector.h:static_assertextended for bits=3;QUANT_SDPA_DISPATCHentries for D ∈ {64,128,256} gated onif constexpr (D >= group_size)scaled_dot_product_attention.cpp:quant_mode_to_int(TurboQuant3=4, TurboQuant4=5)python/src/fast.cpp:turbo3/turbo4in mode docstringTests
12 sub-cases: D ∈ {64,128,256} × bits ∈ {3,4} × B ∈ {1,2}, each targeting a distinct Metal template instantiation. Explicit bfloat16, error path coverage.
Implementation notes
This implements TurboQuant_mse (Algorithm 1). TurboQuant_prod (Algorithm 2,
which adds a 1-bit QJL residual correction on the MSE residual) is not
implemented; at 3–4 bits the inner product bias of TurboQuant_mse is
empirically negligible (paper Section 4.1, Fig. 1: bias converges to zero
above 2 bits). This matches existing community implementations (arozanov,
rachittshah, sharpner).
The random rotation uses a Walsh-Hadamard Transform (WHT) rather than the
full random Gaussian rotation from the paper. WHT runs in O(D log D) vs
O(D²), is fully vectorizable on GPU, and gives equivalent practical quality
on real LLM key vectors.
Tested on Qwen3.6-27B (head_dim=256, 24Q/4KV, GQA=6), 24 GB unified memory Mac. Enables longer context generation on memory-constrained hardware by compressing the KV cache ~5×.