Add TurboQuant KV cache compression with native Metal SDPA kernel#3328
Open
arozanov wants to merge 5 commits intoml-explore:mainfrom
Open
Add TurboQuant KV cache compression with native Metal SDPA kernel#3328arozanov wants to merge 5 commits intoml-explore:mainfrom
arozanov wants to merge 5 commits intoml-explore:mainfrom
Conversation
Adds TurboQuant (arXiv 2504.19874) as a new quantization mode for KV cache compression in MLX core. Changes: - QuantizationMode::TurboQuant enum + string conversion - sdpa_vector_turbo Metal kernel: reads bit-packed uint32 K indices with codebook dequant, pre-rotated query optimization (no WHT in inner loop). Instantiated for fp16/bf16 x 64/128 dim x 3/4 bit. - C++ dispatch function sdpa_vector_turbo() in SDPA backend - Python binding mx.fast.turboquant_sdpa() - CMake fix: removed -sdk macosx from xcrun metal invocation (Metal Toolchain installed via xcodebuild -downloadComponent) Status: Metal kernel compiled and instantiated. C++ dispatch ready. Python binding exposed. Currently falls back to regular SDPA — full native dispatch needs TurboQuantSDPA Primitive subclass to wire eval_gpu to the turbo kernel.
- TurboQuantSDPA primitive class in fast_primitives.h - eval_gpu() routes to sdpa_vector_turbo Metal kernel - Full pipeline: Python mx.fast.turboquant_sdpa() → C++ → Metal - Pre-rotated query: no WHT butterfly in attention inner loop - Kernel reads bit-packed uint32 K indices + codebook directly
Native Metal kernel benchmarks: 256 tokens: 0.83x standard SDPA 1K tokens: 0.71x (turbo faster) 4K tokens: 0.49x (turbo 2x faster) TurboQuant reads 3-bit packed data = less memory bandwidth than fp16.
Native Metal kernel benchmarks (28 query heads, 4 KV heads, D=128): 256 tokens: 0.8x (overhead) 1K tokens: 1.5x faster 4K tokens: 1.5x faster 8K tokens: 2.0x faster 16K tokens: 4.9x faster TurboQuant kernel stays at ~0.1ms regardless of context length. Apple SDPA grows linearly with context (memory bandwidth limited). Changes: - Proper buffer allocation with donation in eval_gpu - Contiguous copy handling - CPU fallback for non-GPU paths
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
Adds TurboQuant (arXiv 2504.19874) as a native Metal SDPA kernel for KV cache compression.
QuantizationMode::TurboQuantsdpa_vector_turboMetal kernel: reads 3-bit packed K indices with codebook dequantTurboQuantSDPAprimitive with full eval_gpu dispatchmx.fast.turboquant_sdpa()Benchmarks (M4 Pro 48GB, 28 query heads, 4 KV heads, D=128):
TurboQuant reads 3-bit packed data (4.6x less memory bandwidth than fp16). Kernel time constant at ~0.1ms regardless of context length.
Related: standalone package at https://github.com/arozanov/turboquant-mlx and mlx-lm PR at ml-explore/mlx-lm#1067
Checklist
pre-commit run --all-filesto format my code / installed pre-commit prior to committing changes