Repository navigation
Raise qmv batch limit for large matrices on M5-class GPUs - #3791
Conversation
|
Friendly bump — this is a small, low-risk change (gen-17 large-matrix |
|
Ran the small/mid bucket sweep I offered above, on the same M5 Max (g17s). TL;DR: I don't think small (18) or mid (12) should move — the large-bucket change here is the only one with a measurable effect. Method: I added a bench-only runtime override to 1. Isolated per-op microbench (4-bit affine, group_size 64; min of 60 reps, 2 trials): small (1024², 2048²) and mid (4096², 2880×4096, 4096×2880) shapes read vector ≈ qmm within ~1–2% across M=1–24 (median vec/qmm 0.99–1.03). But as a sanity check I included the large 8192×8192 shape as a positive control, and the microbench fails it — it reads M=12 as a tie (vec/qmm ≈ 1.00), not the ~18% win this PR measured whole-model. So an isolated single-matmul benchmark is too insensitive to resolve this; the large-bucket win looks like a whole-model/system effect (split-K launch cost amortized across the layer chain with a warm cache), not an isolated-kernel property. 2. Whole-model A/B (matching this PR's own methodology) on
Median force-vec/force-qmm over M≥12 = 0.997 — within noise across the whole speculative-verify window; the shipped default tracks both. So for a real small/mid-dominated model the limit choice doesn't materially affect decode/verify throughput. Net: keeping small at 18 and mid at 12 (the inherited fallback values) looks right for gen-17 — no data to justify changing them, which is consistent with this PR only touching the large bucket. Happy to share the harness or run other shapes if useful. |
|
@pierre427 this looks great. I will try to get some numbers from more architectures to in-a-way redo this heuristic and then merge. |
|
#3584 can also use some similar heuristics. |
|
Some cross-model numbers for the heuristic redo — four model/shape classes, all Method: same whole-model methodology as this PR — force every quantized matmul inline int get_qmv_batch_limit(int D, int O, metal::Device& d) {
if (const char* qmv_env = std::getenv("MLX_QMV_LIMIT")) {
return std::atoi(qmv_env); // bench-only: force the crossover
}
...
The shape: on the dense 70Bs force-qmm is ~flat in M (batched GEMM) while One number to flag directly: at M=12 on the same K2-V2-70B-4bit, this run reads Reading for the heuristic, including one point that cuts against this PR's
The harness, raw log, and the override patch are available, and I can run other |
|
Re-confirmed on M5 / mlx 0.32.0 — the M=11–16 verify band is the right target. A separate speculative-decoding session this week (DFlash block-verify) independently reproduced the Extra datum from the same run: below B=4 a fixed ~800 µs kernel-launch floor dominates (B=1 ≈ 807 µs/tok, ~3.2× over a prior shape-extrapolated estimate), so the cost step isn't a smooth curve — it's a launch-floor region (B<4), a plateau (B=5–14/15), and a recovery (B≥16). Net: the shape-dependence is real and stable on gen-17 hardware, and a verify-shape-aware kernel-selection path (of which this qmv-limit raise is the first concrete step) directly buys back the multi-candidate verification band. |
1ac2fba to
c173afa
Compare
|
I did some benchmarking on M5 Max. The script I used for profiling: import mlx.core as mx
import time
import statistics
# (K, N)
Ms = [8, 10, 11, 12, 13, 14, 15, 16]
shapes = [(16384, 16384), (16384, 28672), (16384, 250112)]
def timed(fn, reps=20):
mx.eval(fn())
ts = []
for _ in range(reps):
t0 = time.perf_counter()
mx.eval(fn())
ts.append(time.perf_counter() - t0)
return sorted(ts)[int(0.95*len(ts))]*1000
for M in Ms:
for N, K in shapes:
bits = 4
w = mx.random.normal((N, K)).astype(mx.bfloat16)
qw, sc, bi = mx.quantize(w, group_size=64, bits=bits)
row = f"{M}x{N}x{K} : "
x = mx.random.normal((M, K)).astype(mx.bfloat16)
t = timed(lambda x=x: mx.quantized_matmul(
x, qw, sc, bi, transpose=True, group_size=64, bits=bits))
row += f"{t:.3f} ms"
print(row)For large shapes: raw datakernel time:
For small shapes: raw datakernel time:
Note that the data for small shapes are rather variant that every time I benchmark there would be some abnormal data. So with my data I would do: if (arch_gen >= 17 && arch_size != 'd') {
if (D <= 2048 && O <= 2048) {
return 33;
} else if (D <= 4096 && O <= 4096) {
return 25;
} else {
return 13;
}
}Though I think for small shapes this tuning would probably not have too much difference. I will also do some benchmarking on M4 too. |
|
Benchmark data on M3 Max: For large shapes: raw dataget_qmv_batch_limit returns 10: 8x16384x16384 : 1.320 ms get_qmv_batch_limit returns 18: 8x16384x16384 : 1.381 ms kernel time: get_qmv_batch_limit returning 10 vs returning 16
For small shapes: raw dataget_qmv_batch_limit returns 10: 10x2048x2048 : 0.442 ms get_qmv_batch_limit returns 33: 10x2048x2048 : 0.451 ms kernel time: get_qmv_batch_limit returning 10 vs returning 33
|
On g17 hardware the qmv_wide kernels stay ahead of the qmm path well past
the generic non-'d' fallback limit of 10. Measured on M5 Max (g17s) with
affine 4-bit/8-bit weights (group_size 64) at D=8192,
O in {1024, 8192, 28672, 250112}: qmv_wide is ~18% faster at M=12
(241.5ms vs 294.6ms whole-model forward on a 70B) and still ahead at M=16.
Raises the large-matrix limit to 16 for gen>=17 non-'d' GPUs; small and
mid shapes keep the existing fallback values. Mitigates the small-M
speculative-decoding verification cost discussed in ml-explore#3553.
c173afa to
c98f677
Compare
zcbenz
left a comment
There was a problem hiding this comment.
I updated the code according to the data I got, though for small shapes I think it is rather hard to get accurate numbers and this is not likely to make a difference in practice.
Proposed changes
get_qmv_batch_limitcurrently sends gen-17 (M5-class) GPUs down the genericnon-
'd'fallback, which caps the qmv/qmv_wide path at M<10 for largematrices. Measured on an M5 Max (
applegpu_g17s), theqmv_widekernels stayahead of the
qmmpath well past that limit on large shapes. This PR raisesthe large-matrix limit to 16 for
arch_gen >= 17non-'d'devices; small andmid shapes keep the existing fallback values (no data to justify changing
them).
This mitigates the small-M quantized-matmul cost step discussed in #3553 for
the M=11–16 band, which matters for speculative decoding: verification of
multi-candidate / tree / long prompt-lookup proposals lands exactly in that
window. #2031 notes these limits are empirical and machine-dependent; gen-17
hardware postdates the tunings there.
Measurements
Apple M5 Max, 128GB (
applegpu_g17s), macOS 26. Whole-model forward cost vsrow count M against a warm 400-token KV cache, min of 6 reps. Model: 70B dense
llama-arch (LLM360 K2-V2), affine 4-bit, group_size 64 (D=8192; O spans 1024 /
8192 / 28672 / 250112 across layers).
Same model at affine 8-bit: M=12 326.7 → 304.1 (−7%).
For reference, the 0.31.2 release (no qmv_wide kernels) costs 298.5 ms at
M=12 on the same 4-bit model, and end-to-end speculative decoding on this
hardware improves from ~18 tok/s (release) to 24.5 tok/s (main + k=3 chain
with a 0.6B draft), with the M=11–16 window enabling wider verification
schemes this change dispatches to the faster path.
Repro
Happy to run additional shapes/configs on this hardware if useful for tuning
the small/mid buckets too.