Derive the qmv fast path K alignment from bits - #3965
Open
jlee5814 wants to merge 1 commit into
Open
Conversation
jlee5814
marked this pull request as ready for review
August 1, 2026 19:08
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.
Motivation
qmvandgather_qmvboth gate the fast kernels onK % 512 == 0, but 512 is not a free constant:qmv_fast_implsteps its K loop bywhich is 512 for bits in {2, 3, 4, 5} and 256 for bits in {6, 8}. At 6 and 8 bits the
dispatcher requires twice the alignment the kernel needs, so any K that is an
odd multiple of 256 takes the slow kernel unnecessarily.
The gate is shared across quantization modes:
fp_qmv_fast_implderives its K step the same way, so MXFP8 weights at 8 bits are affected identically.This has been true since the fast kernel landed; 6 bit was added later without revisiting the constant.
Modifications
Adds
qmv_fast_k_alignment(bits), using the existingget_pack_factorfrombackend/common/quantized.h, which CPU and CUDA already include. Both dispatch sites call it instead of the literal.Accuracy Tests
No new test needed:
test_qmvalready parametrizes bits 6 and 8 with a contraction dimension of 256, so roughly 48 subtests move from the slow kernel to the fast one under this change.python/tests/test_quantized.pypasses identically on both builds, 32 tests and 2896 subtests.MXFP8 at K=768 and K=1280 was checked separately against a dequantized reference on both builds since
test_qmvis affine only. Both newly eligible shapes change output within fp16 rounding; the already fast K=1024 control is unchanged.Speed Tests and Profiling
M4 Pro, N=2048, M=1, group_size=64, affine, fp16. Eight interleaved rounds per build, median, microseconds per call. The 4 bit rows are the control, since the alignment is unchanged there.
Coefficient of variation under 2.5% on both builds for every row above. Two further configurations, 4 bit at K=512 and K=768, were the first measured in each process and too noisy to report.
benchmarks/python/gather_qmm_bench.pyis unchanged within noise, as expected since its shapes are 4 bit and already 512 aligned.Shapes newly reaching the fast path pay a one time kernel compilation on first use, since these variants are not instantiated today. Cached thereafter.