Skip to content

simd_qmm.install() has no callers; prefill runs stock mx.quantized_matmul while decode runs simd_qmm #97

Closed

Description

@christrade215

Environment: Apple M2 Max (30-core GPU, Metal 4), 32 GB, macOS 26.6.2, TensorFold v0.3.6.3 (1911880), mlx 0.32.2, serving Vontra/Qwen3.8-27B-MLX-4bit + z-lab/Qwen3.8-27B-DFlash2, --context 8192, TENSORFOLD_MEMORY_LIMIT_GB=25.

What I found

simd_qmm.install() (src/tensorfold/kernels/qwen/dense/v1/simd_qmm.py:546) is the only function that routes a model's nn.QuantizedLinear modules through simd_qmm globally. It has no callers:

grep -rn "simd_qmm" src/ --include='*.py' | grep -v 'dense/v1/simd_qmm.py' | grep install
# (no output)

grep -rn "QuantizedLinear.__call__" src/ --include='*.py'
# row_matmul.py:199  _draft_call   (drafted modules only, via _DRAFT_ATTR)
# simd_qmm.py:553    _call         (only from simd_qmm.install — uncalled)
# lane_qmm.py:387    _call         (M5 tensor units only)

On the M1–M4 row path, install_row_decoder() (families/qwen3_5/__init__.py:63) calls row_matmul.install(model, backend), which sets BACKEND and stacks weights but never patches nn.QuantizedLinear.__call__. route_drafter does patch it, but only flags the drafter's own modules, so target-model linears fall through to _draft_orig.

Net effect on this machine:

path runs
decode (row_forward → row_matmul.project) simd_qmm
lm_head (row_matmul.logits) simd_qmm
prefill body (families/qwen3_5/family.py:95, self.core(...)) stock mx.quantized_matmul

Measurements

Raw MLX on this box, group-64 4-bit, N=17408 K=5120, mean of 10 timed repetitions (mx.eval + mx.synchronize), run alongside an idle serving process holding ≈19 GiB of Metal memory:

rows (M) stock quantized_matmul simd_qmm simd / stock
16 2.20 ms 1.65 ms 1.33×
64 4.11 ms 4.88 ms 0.84×
256 10.17 ms 10.08 ms 1.01×
1024 31.50 ms 40.03 ms 0.79×
2048 62.24 ms 79.84 ms 0.78×
4096 124.60 ms 217.77 ms 0.57×

Stock plateaus at 5.85 TFLOP/s; simd_qmm at 4.57. bf16 mx.matmul reaches 8.26 TFLOP/s at the same shapes — 41% above the 4-bit path.

import mlx.core as mx
from tensorfold.kernels.qwen.dense.v1 import row_matmul

N, K, M = 17408, 5120, 2048
w = mx.random.normal((N, K)).astype(mx.bfloat16)
pw, s, b = mx.quantize(w, group_size=64, bits=4)
backend = row_matmul.simd_qmm_backend()
backend.prepare([(pw, s, b, 64, 4)])
x = mx.random.normal((M, K)).astype(mx.bfloat16)
# time backend(x, pw, s, b, 64, 4) against
# mx.quantized_matmul(x, pw, s, b, group_size=64, bits=4, transpose=True)

Questions

  1. Is simd_qmm.install() intentionally unused — superseded by the row_matmul + row_forward routing — or is a call site missing? Its docstring says "including serial calls", which reads as if it should be wired in.
  2. If it were wired in, prefill would regress ~22% at 2,048 rows, so I assume it should stay unwired. Either way a comment on the function would help.
  3. Is the stock-MLX prefill path a deliberate choice for bit consistency? family.py:96 says "a fresh prompt and a resumed one get the same chunks and bits", which suggests yes. If so, simd_qmm.install's docstring is the only thing misleading.

Observed throughput (context, not a complaint)

Cold prefill on this box, --context 8192, unique prompts: 623 → 91 tok/s, 1,828 → 81, 2,132 → 82, 3,978 → 72, 5,602 → 68. That is 3.9–5.1 TFLOP/s against the 5.85 measured ceiling, i.e. 67–95% of achievable. Decode is bandwidth-bound: 14.4 GB/token ÷ 52 ms/row = 277 GB/s of the M2 Max's 400.

The recipe's 1,834 tok/s (DGX Spark / NVFP4) and 423 tok/s (M5 Pro, tensor units) aren't comparable to the M1–M4 row path, so no doc change needed there — but a one-line M1–M4 prefill figure would have saved me an hour of chasing it.

Possibly related: this is a follow-up to #91 (the simd_qmm threadgroup crash), same file.

Activity

  1. christrade215 commented on Sep 29, 2026

    @christrade215
    Author

    Follow-up: does a wider representation buy prefill speed? (motivated by #94, where widening 2-bit → 4-bit gained 44% on a Bonsai 2 model)

    Same box (M2 Max, 32 GB, mlx 0.32.2), same shapes as the table above, mean of 10 timed reps:

    gate/up — N=17408, K=5120

    rows (M) 4-bit 8-bit bf16
    64 3.66 ms (3.11 TFLOP/s) 4.86 ms (2.35) 4.06 ms (2.81)
    256 12.05 ms (3.79) 8.11 ms (5.62) 5.99 ms (7.62)
    1024 31.30 ms (5.83) 30.78 ms (5.93) 22.68 ms (8.05)
    2048 62.22 ms (5.87) 61.42 ms (5.94) 44.19 ms (8.26)

    down — N=5120, K=17408

    rows (M) 4-bit 8-bit bf16
    64 2.50 ms (4.56) 2.43 ms (4.69) 1.89 ms (6.02)
    256 8.43 ms (5.41) 8.29 ms (5.50) 6.85 ms (6.66)
    1024 31.69 ms (5.76) 33.49 ms (5.45) 30.04 ms (6.08)
    2048 97.97 ms (3.73) 89.83 ms (4.06) 66.36 ms (5.50)

    At prefill's chunk size (2,048 rows):

    • 8-bit is ~equal to 4-bit — +1.2% (gate/up), +8.8% (down). Not worth doubling the weights.
    • bf16 is +41% / +47% — the only representation that moves the needle.

    Memory, per the same shapes' packed bytes (0.0415 / 0.0830 / 0.1660 GiB for that one weight):

    • 4-bit target ≈ 12 GiB (15.7 GiB "weights kept resident" at startup minus a ~3.6 GiB drafter)
    • bf16 target ≈ 48 GiB — 4×, against a 25 GiB budget on a 32 GB machine

    So the widening lever that #94 exploits is closed here: #94's model starts at 2-bit and widens to where a 4-bit checkpoint already sits, whereas moving 4-bit → bf16 doesn't fit. 4-bit → 8-bit would fit but buys ~1%.

    That closes the question this issue left open — there's no cheaper-but-faster representation reachable from this box. The stock-MLX 4-bit prefill path at 67–95% of the measured 5.85 TFLOP/s ceiling looks like the right call.

    Benchmark script
    import time
    import mlx.core as mx
    
    N, K = 17408, 5120
    MS = (64, 256, 1024, 2048)
    
    def timed(fn, reps=10):
        mx.eval(fn()); mx.synchronize()
        t0 = time.perf_counter()
        for _ in range(reps):
            mx.eval(fn())
        mx.synchronize()
        return (time.perf_counter() - t0) / reps
    
    w = mx.random.normal((N, K)).astype(mx.bfloat16)
    for bits in (4, 8):
        pw, s, b = mx.quantize(w, group_size=64, bits=bits)
        for M in MS:
            x = mx.random.normal((M, K)).astype(mx.bfloat16)
            t = timed(lambda x=x, pw=pw, s=s, b=b, bits=bits:
                      mx.quantized_matmul(x, pw, s, b, group_size=64, bits=bits, transpose=True))
            print(bits, M, t * 1e3, 2 * M * N * K / t / 1e12)
    for M in MS:
        x = mx.random.normal((M, K)).astype(mx.bfloat16)
        t = timed(lambda x=x: mx.matmul(x, w.T))
        print("bf16", M, t * 1e3, 2 * M * N * K / t / 1e12)
  2. ashhart commented on Sep 30, 2026

    @ashhart
    Owner

    Thanks for tracing this so carefully, and for the timings, which are the kind we would have had to take on trust. You
    were right that install() had no callers: it was left over from before the row decoder, which calls simd_qmm
    directly for decode's 1 to 16 rows, where your table has it 1.33x faster than MLX's quantized_matmul. Prefill
    staying on quantized_matmul is the right call by the same table (0.78x at 2,048 rows, 0.57x at 4,096), so nothing
    routed through the patch either way, and in 0.6.0 the 67 lines that made it look that way are gone: install(), its
    QuantizedLinear.__call__ patch, _first, enabled and _ORIG. Nothing changes at runtime, so no measurement was
    needed; the suites that cover that path passed before the deletion. Your bf16 figure is the useful follow-on: 8.26
    TFLOP/s for mx.matmul against 5.85 for the stock 4-bit path at the same shape. We would not answer that by
    dequantizing the 4-bit weights to bf16, because that rounds the weights differently from the quantized path and would
    break a resumed prompt equalling a fresh one, so a faster prompt matmul has to keep the bits. Your timings are the
    scoreboard for any attempt at 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