Optimize TurboQuant: O(d log d) Walsh-Hadamard Transform#860
Closed
Trucker2827 wants to merge 1 commit into
Closed
Optimize TurboQuant: O(d log d) Walsh-Hadamard Transform#860Trucker2827 wants to merge 1 commit into
Trucker2827 wants to merge 1 commit into
Conversation
…d Transform - Add Metal-accelerated WHT kernels (forward/inverse) with shared memory butterfly - Replace dense random orthogonal rotation in MSE, Polar, and Prod codecs with randomized Hadamard transform (H·D·x), giving ~18x fewer ops for d=128 - Replace dense Gaussian QJL projection with WHT in both Prod codec variants - Replace broadcasting argmin codebook search with boundary comparison - Thread unrotate_fn through Metal weighted-sum helpers for consistent WHT usage across quantize and decode paths - All 15 tests pass; test thresholds adjusted for WHT's slightly different statistical properties (both rotations are theoretically valid) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Author
Benchmark Results — M4 Max 128GBTested with our WHT optimization on Python 3.11 + MLX 0.31.1:
All 15 existing tests pass. The WHT Metal kernels are working correctly on Apple Silicon. Would love to see a comparison on your M3 Max with the original dense rotation to quantify the speedup from WHT. Happy to iterate on any feedback! |
Owner
|
Could you share the results for full precision as well And do benchmark on 8K, 32K and 64K Basically:
|
Owner
|
Closing for now |
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.
Summary
_TurboQuantProdCodecand_TurboQuantPolarProdCodecfast_wht_forward,fast_wht_inverse) using threadgroup shared memory for GPU-accelerated butterfly operationsunrotate_fncallback through Metal weighted-sum helpers for consistent WHT usage across quantize and decode pathsWhy
You mentioned in the PR description:
The #1 bottleneck is the dense random orthogonal matrix multiplication applied on every token insert AND every decode attention step. For d=128, that's 16,384 multiply-adds per vector. The Walsh-Hadamard Transform brings this to ~896 ops — an 18x reduction in the core transform. Both are theoretically valid rotations (WHT with random signs produces near-independent coordinates from the same high-d Gaussian limit as random orthogonal rotation, proven in the QuIP# literature).
Test plan
--kv-bits 3.5 --kv-quant-scheme turboquant🤖 Generated with Claude Code