Skip to content

feat(kernels): add 2/3-bit multi-row Metal quant matmul - #831

Draft
EmmetAVS wants to merge 2 commits into
cactus-compute:mainfrom
EmmetAVS:metal-lowbit-gemm
Draft

EmmetAVS wants to merge 2 commits into
cactus-compute:mainfrom
EmmetAVS:metal-lowbit-gemm

Conversation

@EmmetAVS

@EmmetAVS EmmetAVS commented Dec 3, 2026

Copy link
Copy Markdown

Problem

Follow-up to #828 (Notes). Related to #816.

cactus_metal_encode_quant_matmul_m only accepts 4-bit weights, since cq4_gemm_mma is the only multi-row quantized matmul kernel. This causes every multi-row CQ2/CQ3 matmul to fall back to CPU, so CQ2/CQ3 prefill on Metal runs at roughly CPU speed while CQ4 is about 2x faster.

Solution

Adds cq_gemm_mma_lowbit in cactus_kernels.metal. It uses the same tiling as cq4_gemm_mma. Only the weight decode differs, and it decodes 2/3-bit interleaved indices the same way cq_gemv_mr_lowbit does. bits is passed at buffer 10.

In cactus_metal_encode_quant_matmul_m, the new kernel is selected the same way the single-row encoder picks a low-bit kernel: lowbit = !fast && psoGmmaLow && quant_lowbit_eligible(W). Like psoGmrLow, the pipeline is optional and is not part of ok. The 4-bit path remains unchanged.

As a result, CQ2/CQ3 prefill no longer hits the CPU fallback in which #816 occurs, so Metal CQ2/CQ3 no longer diverge from CPU at the first token on main. #828 is still needed for the fallback path in general.

Results

Gemma 4 E2B, cactus benchmark --backend metal (1k prefill / 100 decode), M5 Pro, Low Power Mode off, median of 3 interleaved rounds compared to current main:

prefill before prefill after decode peak RAM
CQ2 971 tok/s 1794 tok/s unchanged (~60) 1.58 GB → 0.93 GB
CQ3 1078 tok/s 1853 tok/s unchanged (~97) 1.57 GB → 0.92 GB
CQ4 2097 tok/s 2096 tok/s unchanged unchanged

Verification

  • Extended SyntheticCQ::make_interleaved in test_matmul.cpp to pack 2/3-bit INTERLEAVED_4ROW, matching pack_indices_interleaved_4row_{2,3}bit in the converter. Added matmul_cq2_il / matmul_cq3_il (interleaved CPU matmul vs fp32 reference). Both tests fail when the interleave is deliberately broken.

  • Added test_cq_metal_gemm(bits), compares cactus_metal_encode_quant_matmul_m vs CPU cactus_quant_matmul for M = 32, 37 and 128 (exact tile, uneven length, full prefill chunk), K = 256, N = 80 (not a multiple of the 64-wide tile). Registered for 2, 3 and 4 bits. The 4-bit case covers cq4_gemm_mma, which previously did not have a kernel-level test.

    $ cactus test --suite matmul # before (encoder rejects 2/3-bit)
    ✗ FAIL │ matmul_cq2_metal_gemm
    ✗ FAIL │ matmul_cq3_metal_gemm

    $ cactus test --suite matmul # after
    ✓ PASS │ matmul_cq2_metal_gemm
    ✓ PASS │ matmul_cq3_metal_gemm
    ✓ PASS │ matmul_cq4_metal_gemm
    ✓ All 16 tests passed!

  • cactus test --suite metal_parity passes (47/47).

  • CQ2/CQ3 no longer diverge at token 0. The remaining late divergences (tokens 13–45) match the pattern CQ4 already shows.

Notes

  • CQ2/CQ3 prefill is still ~12–15% behind CQ4. Possibly caused by the heavier 3-bit/2-bit unpack, or the fused encoders gated on quant_fast_eligible only (4-bit). (Not addressed here).

Extend make_interleaved to pack 2-bit and 3-bit indices in the
INTERLEAVED_4ROW layout (matching pack_indices_interleaved_4row_{2,3}bit
and the Metal lowbit GEMV unpack). Add matmul_cq2_il / matmul_cq3_il.

Signed-off-by: Aarush Shrikanth <aarush.shrikanth@gmail.com>
Signed-off-by: Aarush Shrikanth <aarush.shrikanth@gmail.com>

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