Skip to content

qmv_wide: residual small-m cost on larger shapes — m=4–8 still 1.5–2.7x of m=1 on M4 Pro #3839

Closed

Description

@abcgco

First: qmv_wide (#3764) is a clear win — thank you @jessegross. Measured on speculative-decoding shapes it cut the small-m penalty roughly in half versus 0.31.2, and it's also ~3x more accurate than the per-row qmv path against an fp32 ground truth (explicit mx.dequantize + matmul), which incidentally restored exact greedy spec==nospec token match on a 4-bit 8B model in vLLM's Metal plugin (vllm-project/vllm-metal#503).

This is a follow-up data point: on larger linears the small-m cost is still well above weights-bandwidth-flat, and it grows with m in exactly the range speculative decoding uses (m = K+1 = 2..8).

Measurements (mlx 0.32.0 wheel, M4 Pro 24 GB, macOS 15.6, bf16 activations, affine 4-bit gs=64, transposed; median of 40 synced calls; cost relative to m=1 — flat would be the bandwidth-bound ideal since the weight read is identical):

shape (K×N) m=2 m=3 m=4 m=6 m=8
4096×6144 (qkv-like) 1.25x 1.48x 1.14x 1.47x 1.73x
4096×4096 1.20x 1.35x 1.57x 1.99x 2.01x
4096×24576 (mlp gate+up) 1.03x 1.22x 1.47x 2.07x 2.69x
12288×4096 (mlp down) 1.18x 1.40x 1.52x 2.05x 2.46x

(For reference, 0.31.2 on the same shapes: 4096×24576 was 1.89x at m=4 and 3.44x at m=8 — so 0.32.0 recovers about half the gap. The non-monotonic m=2 vs m=4 on the qkv shape reproduces across runs; possibly a dispatch boundary.)

Why it matters in practice: spec-decode verification runs the whole target model at m=K+1. End-to-end in vllm-metal on a Qwen3-8B-4bit target at an 8k prefix, 0.32.0 already saves 14–18 ms/step at K=3; by these curves a further flat-small-m kernel would be worth roughly another ~10 ms/step at K=3, more at higher K.

Repro:

import time
import mlx.core as mx

SHAPES = [(4096, 6144), (4096, 4096), (4096, 24576), (12288, 4096)]

def timed(fn, reps=40):
    mx.eval(fn())
    ts = []
    for _ in range(reps):
        t0 = time.perf_counter()
        mx.eval(fn())
        ts.append((time.perf_counter() - t0) * 1000)
    return sorted(ts)[len(ts) // 2]

for d_in, d_out in SHAPES:
    w = mx.random.normal((d_out, d_in)).astype(mx.bfloat16)
    qw, sc, bi = mx.quantize(w, group_size=64, bits=4)
    base = None
    row = f"{d_in}x{d_out}:"
    for m in (1, 2, 3, 4, 6, 8):
        x = mx.random.normal((m, d_in)).astype(mx.bfloat16)
        t = timed(lambda x=x: mx.quantized_matmul(
            x, qw, sc, bi, transpose=True, group_size=64, bits=4))
        base = base or t
        row += f"  m={m}: {t:.3f} ms ({t / base:.2f}x)"
    print(row)

For what it's worth, before finding #3764 I prototyped a naive multi-row qmv variant (R rows staged in registers, one weight stream): correct but slower — register pressure kills occupancy — so the dequant-once tile approach is clearly the right family. Is further small-m tuning on your roadmap? Happy to benchmark candidate kernels on M4 Pro and validate end-to-end through vllm-metal's speculative decoding, where these shapes are the hot path.

Activity

  1. zcbenz commented on Aug 5, 2026

    @zcbenz
    Member

    Thanks for doing the benchmarking, usually the issues should be about a clear problem that should be fixed, or a clear path that we can follow to improve things, this issue does not seem to belong either so I'm closing it.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions