Repository navigation
[not for merge, measured slower] Broadcast batches of one-row products through the row kernel - #69
Draft
jonathan308 wants to merge 1 commit into
Draft
jonathan308 wants to merge 1 commit into
jonathan308 wants to merge 1 commit into
Conversation
A batch of one-row products with one weight matrix (x of shape [..., 1, K] with contiguous rows, and the weights, scales and biases broadcast over the batch) ran the batched qmv, which reads the whole weight matrix again for every row. It now runs affine_qmv_rows, which reads the weights once for a tile of rows. Both give each row the bits that qmv gives the row alone, so no output bit changes. MLX_QMV_ROWS_BROADCAST (read once in each process, default 1) selects it; 0 keeps the batched qmv. affine_qmv_rows also serves 2 bits where qmv uses its fast kernel (K a multiple of 512): the expressions of load_vector and qdot for 2 bits, with one pack per thread as in qmv_fast_impl. The row-exact switch (MLX_QMV_ROW_EXACT) serves 2 bits on the same terms. The signed-Hadamard 2-bit linear layers of mlx-swift (HadamardQuantizedLinear) send their multi-row projections as such a batch to keep each row's bits, so their prefill and batched decode read the weights once per tile. tests/qmv_row_exact_tests.cpp compares the bytes of each row of broadcast batches of 2 to 19 rows (2, 4 and 8 bits, three group sizes, three float types, the fast and general kernels and qmv_quad) and of row-exact products with 2 bits with the same row alone. The qmv_rows_broadcast_off ctest entry runs the broadcast case with the batched qmv; Fork Tests runs it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
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.
What it tried
mlx-swift's signed-Hadamard 2-bit layers (
HadamardQuantizedLinear, Ternary Bonsai) send multi-row projections as a batch of one-row products:xof shape[rows, 1, K]against weights broadcast over the batch. The batchedqmvhandles that with the bits of each row alone. This PR sent such batches toaffine_qmv_rows(from #60, with 2 bits added), which reads the weights once per tile of rows, on by default (MLX_QMV_ROWS_BROADCAST).Measured
Bits. Every row equals the row alone with the route on and off (C++ case, 1,225 of 1,225 assertions each way, M5 Max and M5 Ultra GPUs). Engine digests over token ids and log-prob bits were identical on and off (Ternary Bonsai 2 27B, M5 Max, 30 requests).
Speed. Kernel time on the M5 Ultra,
bench_rows: 2 bits, group 128, float32x, N=17408 and K=5120, median ms; two passes each.qmv(today)qmvalready reuses weights from the GPU cache, since a 22 MB layer matrix fits: a row costs 0.25 ms alone and 0.019 ms in a batch of 256.Kept from this branch, if wanted
affine_qmv_rowswhereqmvuses its fast kernel. Proven bit-exact underMLX_QMV_ROW_EXACT=16: 3,826 of 3,826 assertions.AI usage
🤖 Generated with Claude Code