Skip to content

Port upstream affine qmv_mma for 3 to 16 rows on Metal (performance) - #64

Merged
jjang-ai merged 4 commits into
osaurus-ai:mainfrom
rcfa:apple/r3-affine-qmv
Nov 28, 2026
Merged

jjang-ai merged 4 commits into
osaurus-ai:mainfrom
rcfa:apple/r3-affine-qmv

Conversation

@rcfa

@rcfa rcfa commented Nov 26, 2026

Copy link
Copy Markdown

Branch apple/r3-affine-qmv, 4 commits on origin/main 23f364e.

This is a performance port, not a bug fix. It brings Apple's affine_qmv_mma Metal kernel, which multiplies 3 to 16
rows by an affine-quantized matrix with simdgroup matrix instructions, and its routing, from ml-explore/mlx main:
ml-explore#4787 (older than round 3; the round's ml-explore#4860, ml-explore#4890 and ml-explore#4827 patch it), ml-explore#4860, ml-explore#4890 and ml-explore#4827. Each commit is a
git cherry-pick -x of the upstream commit, with the upstream author kept and the (cherry picked from commit …) line
recording provenance. The kernel template is Apple's unchanged; the kernel instantiations and the routing are ported
into this fork's quantized.metal and quantized.cpp, which differ from Apple's. Each commit message notes where it
differs from Apple's.

Why we push it: on origin/main, affine quantized matmul with 3 to 16 rows runs the vector kernels. On an M4 Max this
branch runs 4 to 16 rows up to 2.48 times as fast at LLM layer shapes, with no loss of accuracy (see Validation). The
port keeps the fork's own quantized paths (mixed bf16/f16 metadata, exact F32 q6 rows, 1-bit affine) on the kernels
they use today.

Included, in upstream order:

Upstream Commit Here What it fixes
ml-explore#4787 ba8c005 1807ba7 Performance: on GPUs without NAX, affine quantized matmul with 3 to 16 rows ran the vector kernels (qmv, qmv_wide). Adds the affine_qmv_mma kernel (4 and 8 bits) and routes affine to it from 3 rows (below GPU generation 15) or 4 rows (generation 15 and later) for large layers (N, K ≥ 512, N·K ≥ 2^21, N % 16 = 0; not on Ultras). Adds test_affine_qmm_few_rows. Here without the NAX branch, only for scales and biases of x's type, with instantiations in this fork's scheme.
ml-explore#4860 6d38ff9 8f34f40 affine_qmv_mma read only 4 and 8 bits. The kernel now reads 2 to 8 bits (not 7), the fork instantiates 2, 3, 5 and 6 bits too, and on generation 15 and later it takes layers from N·K ≥ 2^20. Here the fork's exact F32 q6 rows (exact_q6_f32_rows) stay on the single-row kernels.
ml-explore#4890 5cabdf5 a64128a M1 and M2 Ultra never took affine_qmv_mma (every Ultra kept the vector kernels); they now take it from 4 rows. M3 Ultra and later still keep the vector kernels.
ml-explore#4827 8f578df 9caa7bb M3 Ultra and later now take affine_qmv_mma for large layers only (N, K ≥ 4096). Here without the 4-bit affine qmv_nax rows from 4 rows, which need ml-explore#4781.

Not included here: ml-explore/mlx PR 4781 (affine qmv_nax and the fp staged split-K) and every NAX branch of these
commits: only an M5 can validate them. Without PR 4781, use_qmv_mma returns false for affine on NAX GPUs, so an M5
keeps today's path. Apple's later NAX tuning of the fp rules is not taken either. The other round-3 commits are in
apple/r3-core, apple/r3-metal, apple/r3-metal-sdpa-vjp, apple/r3-cuda, apple/r3-python-docs-build and
apple/fixes-2. Also left out of the batch, by ml-explore/mlx PR number: 4887, 4883, 4903, 4801, 4734, 4647, and the
window-size part of 4927.

Conflict resolutions

All four commits are ports: origin/main has no affine qmv_mma, and its use_qmv_mma returned false for affine.

  • Kernel template (kernels/quantized.h): Apple's, unchanged in [Metal] Add affine qmv_mma for 3-16 rows ml-explore/mlx#4787 and [Metal] Add 2, 3, 5 and 6 bits to affine qmv_mma ml-explore/mlx#4860.
  • Instantiations (kernels/quantized.metal), in this fork's scheme:
    affine_qmv_mma_<type>_gs_<gs>_b_<bits>_wk<wk>_bn<bn>_tm<tm>, tiles (wk, bn, tm) = (4, 16, 1), (4, 16, 2) and
    (4, 32, 1), bits 2, 3, 4, 5, 6 and 8, group sizes 32, 64 and 128, float, float16_t and bfloat16_t: 162 kernels.
  • use_qmv_mma(x, w, scales, biases, mode, group_size, bits, M, N, K, d): affine on NAX returns false; affine needs
    scales and biases of x's dtype (the fork's mixed bf16/f16 metadata path stays on qmv); exact_q6_f32_rows()
    returns false (one predicate, now also used by the vector dispatch); then Apple's rule (minimum rows 3, or 4 on
    generation 15 and later and on Ultras; bits 2 to 8 except 7; N·K ≥ 2^20 on generation 15 and later, else 2^21;
    M3 Ultra and later only from 4096 × 4096). Bits ≥ 2 also keep 1-bit affine off this kernel. The fp rules are the
    fork's, unchanged. The caller's M >= 6 is now min_rows (6 for fp).
  • qmv_mma(): binds the biases when present (QuantizedMatmul has 4 inputs only for affine; global scales go
    through QQMatmul). wk and bn as Apple's: affine wk 4 everywhere without NAX; bn 32 below generation 15 when tm is 1
    and N % 32 = 0.
  • Tests: test_affine_qmm_few_rows and the rename of test_qmm_few_rows to test_fp_qmm_few_rows as Apple's.

Overlaps

None with the batch's other branches (merge-tree). osaurus-ai has no competing work on this: the
qmv-multirow-experiment branch (opt-in VMLX_QMV_MR) and the one-bit audit branch are unrelated.

Validation

Compared with main (23f364e), built and run the same way:

  • Linux arm64 (container vmlx-arm64, clang Release, Highway), C++ suite on the CPU (no Metal: a build check).

  • macOS (M4 Max, Release, Metal on), C++ suite (tests/tests) on the CPU and on Metal.

  • Python (macOS, Python 3.14.5), on the GPU and with DEVICE=cpu: the 16 test modules the batch touches, among
    them this branch's test_quantized, on main and on the integration tree 63ac1f9df (origin/main with all batch
    branches merged, this one included).

  • Benchmark (M4 Max, quiet machine): quantized_matmul with transposed affine weights, bfloat16, group size 64,
    4 and 8 bits, M = 1 to 16, at 5 layer shapes (N × K: 4096 × 4096, 14336 × 4096, 4096 × 14336, 1024 × 4096,
    2048 × 2048). Each cell evaluates 32 calls together, median of 7 rounds; main 23f364e against this branch at
    9caa7bb. Accuracy against x @ dequantize(w).T in float32.

  • Linux: 412 of 412 cases pass, as main.

  • macOS: 390 of 390 cases pass on the CPU and on Metal, as main.

  • Python: test_affine_qmm_few_rows (bits × group size × dtype × M = 3, 8, 9) passes on Metal. No test that passes on
    main fails on the integration tree. GPU: main fails 3 tests, the integration tree 1 (test_input_dilation_grad,
    as main): test_qmm_mid_m and test_qmm_splitk_precision fail on main and pass on the integration tree;
    apple/fixes-2 alone fixes them (its own test_quantized run). CPU: both fail the same 4 (GPU-only tests that do
    not skip on the CPU, and the Winograd budget). All new tests of the batch pass: 27 on the GPU, 85 on the CPU (the
    rest skip). main's CPU run stops at test_reduce: test_init_reduce_large_output (from ml-explore/mlx PR 4437)
    crashes it with SIGSEGV, the overflow that this batch's fix/reduce-int64-loops fixes. So test_reduce,
    test_threads, test_vmap and test_zero_copy have no main CPU result; on the integration tree none of their tests fails on
    the CPU.

  • Benchmark, speed of this branch over main:

    M Kernel Speed-up over main
    1 to 3 the same kernels on both (affine starts at 4 rows on generation 15) noise, about ±10 to 15 % (the first cell also warms up)
    4 to 5 affine_qmv_mma 0.93 to 1.50 times
    6 to 8 affine_qmv_mma 1.26 to 2.32 times
    9 to 16 affine_qmv_mma 1.18 to 2.48 times

    Over 5 shapes × 2 bit widths. For example, 4-bit 14336 × 4096 at M = 16: 287 µs to 148 µs; 4096 × 4096 at
    M = 13: 109 µs to 48 µs. At M = 4 and 5 some cells are slightly slower (down to 0.93 times). Accuracy: no cell is worse than main (largest error, 4-bit: 8.7e-3 against
    9.2e-3; 8-bit: 4.0e-3 against 9.2e-3). Timed one call per eval instead, an eval overhead of about 170 to 200 µs
    hides most of the gain: 1.3 to 1.9 times, on the large layers only.

  • Not run: a check of the kernel each shape selects (beyond the timings); M1, M2, M3 and Ultra GPUs; NAX GPUs; a
    model-level decode or prefill A/B.

Validation of the batch in shared

This batch's PRs but the last (fix/float-floor-divide, which came out of this validation), merged together on
main 23f364e with the two overlap resolutions (apple/fixes-2 and apple/r3-core in
python/tests/test_quantized.py; apple/r3-core and apple/fixes-3 in mlx/allocator.h), are mlx 8de1a51; vmlx-swift 31458a0d3 builds on it (osaurus-ai/vmlx-swift main bbd237750 with our open vmlx-swift
patches). Built and run, compared with main:

  • macOS (M4 Max, Release, Metal on): C++ suite 410 of 410 on the CPU and on Metal. vmlx-swift builds for macOS 14.0;
    its suite (swift test --no-parallel, 1046 tests) fails no test that passes on its main.
  • Linux arm64 and amd64 (containers, clang Release, Highway): C++ suite 431 of 431 on both (main 412 of 412).
    vmlx-swift's Linux CI steps pass: VMLXLinuxTests 37 of 37, the CPU kernel tests 61 of 61 at both thread settings.
  • Linux, the CI-equivalent Python run (osaurus-ai's Linux job, built and run the same way: GCC, Debug, Highway,
    DEVICE=cpu, python/tests module by module, torch 2.15 for the CPU). arm64: main 881 pass, 92 skip, 2 fail;
    the batch 906 pass, 96 skip, 2 fail (tree c43a37eec, which differs from 8de1a51 only in a C++ test). amd64: main
    882 pass, 92 skip, 1 fail; the batch 906 pass, 96 skip, 1 fail, 1 error. No test that passes on main fails on the batch. The failures are
    main's own: test_compile_sigmoid_matches_eager (bfloat16: the compiled CPU sigmoid is less accurate than the
    eager Highway kernel) and, on arm64 only, bfloat16 test_torch_conv_2D, where torch 2.15's own aarch64 bfloat16
    conv2d is off by up to 8.8 against float64 (MLX is within 0.06). test_async_eval_error_in_synchronize failed once on amd64; it fails about 1 run in 5 there on main too: the error it expects from synchronize can come from async_eval (the same code is on ml-explore/mlx main).
  • Three tests of main fail at random, on every branch alike, because they draw unseeded inputs or race:
    test_double.TestDouble.test_unary_ops in about 8% of runs (float32 erf is accurate to 1.5e-7 absolute, which
    misses rtol 1e-5 near 0 against the fork's accurate float64 erf); test_ops.TestOps.test_divmod in about 1%
    (the float16 quotient is off by one when a / b rounds up to an integer, e.g. 73.625 / 1.248 = 58.99 gives 59;
    ml-explore/mlx main has the same code); and, on amd64, test_async_eval_error_in_synchronize (above). This batch's
    fix/float-floor-divide fixes the second; the first gets a PR of its own.
  • osaurus-ai/mlx's GitHub Actions have not run since 2026-10-08, so this PR shows no checks; the runs above follow
    its Linux and macOS jobs.
  • This branch alone, the same Linux Python run (arm64): 881 pass, 93 skip, 2 fail: test_compile_sigmoid_matches_eager, test_torch_conv_2D as on main.

🤖 Generated with Claude Code

ahuzmeza and others added 4 commits November 24, 2026 07:16
Co-authored-by: Cheng <git@zcbenz.com>
osaurus-ai/mlx does not have the affine qmv_nax kernels (ml-explore#4781), so affine stays off this path on
NAX GPUs. Affine qmv_mma also needs scales and biases of the input type: the fork's mixed
bf16/f16 metadata path stays on qmv. The instantiations follow this fork's kernel names.
(cherry picked from commit ba8c005)
osaurus-ai/mlx also keeps the fork's exact F32 q6 rows (exact_q6_f32_rows) on the single-row
kernels, whose reduction order they depend on.
(cherry picked from commit 6d38ff9)
…#4890)

Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com>
(cherry picked from commit 5cabdf5)
osaurus-ai/mlx takes the non-NAX part only. The 4-bit affine_qmv_nax rows need ml-explore#4781.
(cherry picked from commit 8f578df)
@jjang-ai
jjang-ai merged commit 46f355a into osaurus-ai:main Nov 28, 2026
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.

6 participants