Skip to content

[not for merge, measured slower] Broadcast batches of one-row products through the row kernel - #69

Draft
jonathan308 wants to merge 1 commit into
perf/qmv-row-exactfrom
perf/qmv-rows-broadcast
Draft

jonathan308 wants to merge 1 commit into
perf/qmv-row-exactfrom
perf/qmv-rows-broadcast

Conversation

@jonathan308

@jonathan308 jonathan308 commented Nov 14, 2026 •

Copy link
Copy Markdown

Not for merge: measured, this route is not faster. Bits are equal, but on the M5 Ultra the existing batched qmv is as fast or faster at every row count. Kept as a draft for the record; close it unless the owner wants the 2-bit row-exact support on its own.

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: x of shape [rows, 1, K] against weights broadcast over the batch. The batched qmv handles that with the bits of each row alone. This PR sent such batches to affine_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, float32 x, N=17408 and K=5120, median ms; two passes each.

Rows Batched qmv (today) This PR's kernel (2 outputs per thread) Weights converted once per tile, 4 outputs, halves
8 0.33-0.35 0.39-0.70 0.39-0.52
64 1.36-1.39 1.56-1.59 1.38-1.40
256 4.76-4.77 6.57 4.94-4.96
  • The batched qmv already 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.
  • At large batches it is bound by its exact per-row arithmetic. Converting each tile's weights once (two later variants, bit-exact in every test case, patches kept off this branch) brought the kernel level with today's, not past it.
  • On the M5 Max the engine was too noisy to time: one-stream decode, which this route does not touch, moved 1.6x between runs.

Kept from this branch, if wanted

  • 2 bits in affine_qmv_rows where qmv uses its fast kernel. Proven bit-exact under MLX_QMV_ROW_EXACT=16: 3,826 of 3,826 assertions.

AI usage

  • AI usage disclosure: Claude Code (Claude Opus 5.5) wrote the code, the tests and this description, and ran the measurements, at the request of the repository's maintainer.

🤖 Generated with Claude Code

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>
@jonathan308 jonathan308 changed the title Read the weights once per tile for broadcast batches of one-row products (same bits), and 2 bits in the row kernel [not for merge, measured slower] Broadcast batches of one-row products through the row kernel Nov 14, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant