Repository navigation
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Follow-up to #828 (Notes). Related to #816.
cactus_metal_encode_quant_matmul_monly accepts 4-bit weights, sincecq4_gemm_mmais 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_lowbitincactus_kernels.metal. It uses the same tiling ascq4_gemm_mma. Only the weight decode differs, and it decodes 2/3-bit interleaved indices the same waycq_gemv_mr_lowbitdoes.bitsis 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). LikepsoGmrLow, the pipeline is optional and is not part ofok. 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:Verification
Extended
SyntheticCQ::make_interleavedintest_matmul.cppto pack 2/3-bitINTERLEAVED_4ROW, matchingpack_indices_interleaved_4row_{2,3}bitin the converter. Addedmatmul_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), comparescactus_metal_encode_quant_matmul_mvs CPUcactus_quant_matmulfor 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 coverscq4_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_paritypasses (47/47).CQ2/CQ3 no longer diverge at token 0. The remaining late divergences (tokens 13–45) match the pattern CQ4 already shows.
Notes
quant_fast_eligibleonly (4-bit). (Not addressed here).