Repository navigation
metal : few-row MMA kernels for MUL_MAT_ID and more src0 types - #30047
pratiknarola-t wants to merge 2 commits into
Conversation
|
Can you split the MUL_MAT kernels in a separate PR so we can merge them quickly? It would make it simpler to review the MUL_MAT_ID changes. |
28c7cac to
c1d2e51
Compare
|
Please rebase on the |
Below 32 tokens MUL_MAT_ID runs one mat-vec per token and expert slot, and from 32 tokens kernel_mul_mm_id, which pads every expert to 32 rows. With many experts a batch of 32 to 192 tokens gives each expert only a few of them, so mul_mm_id computes mostly padding. The new kernel_mul_mv_mma_id_* kernels run over the entry tiles that kernel_mul_mm_id_map0 emits, one expert with up to 32 token rows each: a threadgroup reads its expert's weights once per 8 rows. Duplicate ids reach them through the same prepass. They share the bodies of the few-row MUL_MAT kernels, so they take the same types. The kernel follows the tokens per expert, n_tokens*n_used/n_expert. Below 2 (0.5 for rows of up to 512 weights) the mat-vec kernel runs, also past 32 tokens, as it reads at about memory bandwidth there. Above that the MMA kernel runs, past 32 tokens only up to 6 tokens per expert (4 for weights of 2 bytes or more), and the mat-mat kernel past that and from 256 tokens (128 for short rows). test-backend-ops perf -o MUL_MAT_ID on M3 Ultra, speedup over master (two interleaved runs each): 256 experts with 8 used and q4_K (Qwen3.6-35B-A3B) 1.43x to 2.40x at 32 to 192 tokens for 512x2048, and 1.11x to 1.49x at 24 to 96 tokens for 2048x512. At 16 tokens the 2048x512 shape is 0.95x (q6_K 0.91x) with the uniform random ids of the test, and 1.33x (q6_K 1.27x) with 24 sets of expert ids captured from Qwen3.6-35B-A3B, whose router puts 16 tokens on 63 experts instead of 102. The 128/8, 128/4, 64/8, 32/4 and 8/2 shapes over q4_0, q4_K, q6_K, q8_0, iq2_xs, iq4_xs, mxfp4, f16, bf16 and f32 gain 1.02x to 3.10x wherever the kernel changes. Where both pick the same kernel, which is master's code, the speedup spreads 0.91x to 1.06x; two builds of master spread 0.95x to 1.07x on the same cases. Qwen3.6-35B-A3B Q4_K_M on M3 Ultra: llama-batched-bench -npp 512 -ntg 32 tg at 16 sequences 346.8 -> 365.5 t/s, 1 to 8 sequences within 0.5% and pp unchanged; llama-perplexity -ub 64 over 40 chunks 5.8659 -> 5.8620; serial decode and the DFlash-2 verify step unchanged. Qwen3.8-27B Q4_0 (dense) is unchanged.
c1d2e51 to
c82eed8
Compare
|
/bot review |
Automated code reviewReview of the MUL_MAT_ID Metal MMA kernels PR (c82eed8). Scope check: follow-up to #29869 with a linked discussion, single-purpose (Metal MUL_MAT_ID kernel selection + kernels), perf data included, AI usage disclosed. No quick-reject issues. I checked the kernel logic in detail (entry-tile reads, duplicate-id handling, dst indexing, shmem reduction, barriers) and found the core implementation correct: the clamp-then-discard in Will slow the review: (point 1) (point 2) (point 3) Nits: (point 4) (point 5) The new block adds ~630 MUL_MAT_ID eval cases (all_types x 7 token counts x 3 shapes, plus the second block). Shapes are tiny, but this roughly doubles the MUL_MAT_ID section on every backend in CI - worth confirming the suite runtime delta is acceptable on the slower runners. The heuristic thresholds being documented with measurement provenance ("measured on an M3 Ultra", the 0.95x regression cases disclosed) is good practice and helps future tuning. This review was generated automatically by pi coding agent using |
|
This change is a bit difficult to justify. The range of batch sizes between Also, the thresholds might be overfitted for M3 Ultra because I don't see consistent improvements on M2 Ultra and M5 Max: CMAKE_OPTS="-DGGML_BLAS=OFF" scripts/compare-commits.sh master pr/30047 llama-bench -hf ggml-org/Qwen3.8-Flash-Next-GGUF:IQ4_NL -ub 2048 -b 2048 -d 4096 -p 16,24,32,48,64,96,128,192,256 -n 0 -t 1 -r 3
So for now, my preference is to hold off merging. I was mostly interested in small-batch mul_mat_id that would lead to similar benefits for speculative decoding like the previous PR. But I understand that the simdgroup-based multiplication does not simply apply to the indirect tiles. |
|
Data point from an Apple M1 Pro (16-core GPU, 32 GB, macOS 26.5.2; no tensor API), since the open question is whether the thresholds hold outside M3 Ultra. On this chip I see gains in the pp32–pp96 range where M2 Ultra / M5 Max regressed, and no regression anywhere. Builds: this PR ( Model: 1. Your sweep:
(The first base pp16 run was a cold outlier, 88 ± 38; the value above is the warm repeat.) 2. The op:
3. Speculative decoding (what I was hoping for too):
So on this MoE the MTP-sized verify (≤ 4 rows) doesn't move, as the n = 1–8 rows of the op table suggest; the gain shows up when the n-gram drafts get long (copy), which lands in the 32–64 range. One machine, one model, so take it as a single data point. Raw logs are available if useful. |
|
Data point from an Apple M4 (base Mac mini, 10-core GPU, 24 GB, macOS 26.3.1), on the small-batch speculative-verify question. Builds: this PR ( 1. Where the small-batch cost comes from. I used a standalone ggml harness on the Qwen3-30B-A3B gate/up expert shape: 128 experts, 8 used, m=768, k=2048, q4_0/q4_K/q8_0. In the test, all
2. What this means for speculative decoding. With fully shared experts, the reuse gap at n=4 is 1 − 1888/3343 ≈ 44% of the q4_K Scope and limits:
|
Overview
Follow-up to #29869, for this question.
Rebased on master after #30065 and #30373. The MMA kernels now read the entry tiles of the new
kernel_mul_mm_id_map0, so duplicate ids take the same path, and this PR no longer changes the prepass.kernel_mul_mm_idpads every expert to 32.kernel_mul_mv_mma_id_*runs one threadgroup per entry tile ofkernel_mul_mm_id_map0(one expert, up to 32 token rows) and reads the expert's weight tile once per 8 rows.n_tokens*n_used/n_expert). Below 2 (0.5 when K <= 512) it uses the mat-vec kernel and above that the MMA kernel. It switches tomul_mm_idpast 6 tokens per expert (4 for F32, F16 and BF16) and from 256 tokens (128 when K <= 512).Additional information
Apple M3 Ultra (60-core GPU), macOS 15.7.9, against master 4413a19. Speedups are the time on master divided by the time with this PR, from two interleaved runs of each build.
test-backend-ops perf -o MUL_MAT_IDon the Qwen3.6-35B-A3B expert shapes (256 experts, 8 used, Q4_K):The down projection at 16 tokens (0.5 tokens per expert) is the one case that runs slower in
test-backend-ops: 0.95x with Q4_K and 0.91x with Q6_K. The test draws uniform random expert ids, which put 16 tokens on 102 experts on average. The router of Qwen3.6-35B-A3B puts them on 63, measured over 16 tokens 32 apart in a wikitext prompt. Replaying 24 such captured id sets, the same shape runs 1.33x (1.06x to 1.46x over the sets) with Q4_K and 1.27x (0.99x to 1.43x) with Q6_K. In the model, 16 sequences decode 5.4% faster (table below).On the 128/8, 128/4, 64/8, 32/4 and 8/2 expert shapes with Q4_0, Q4_K, Q6_K, Q8_0, IQ2_XS, IQ4_XS, MXFP4, F16, BF16 and F32, the speedup is 1.02x to 3.10x wherever the kernel changes. Where both builds run the same kernel, which is master's code, it stays between 0.91x and 1.06x. Two builds of master differ by 0.95x to 1.07x on the same cases, and the 0.91x case (F32, 32 experts, 4 used, 4 tokens) runs the same
kernel_mul_mv_id_f32_f32_4pipeline on both builds.llama-batched-bench -npp 512 -ntg 32with Qwen3.6-35B-A3B Q4_K_M, text generation. Each build ran three times in a row, and the table shows the mean of runs 2 and 3, after the shader cache was warm:At 16 sequences the down projection gets 0.5 tokens per expert, which runs on the new kernel. From 1 to 8 sequences both builds run the mat-vec kernel. Prompt processing is the same on both builds, and Qwen3.8-27B Q4_0 (dense) is within 0.2% at every batch size.
llama-perplexity -ub 64over 40 chunks, where the expert mat-muls get 2 tokens per expert and run on the new kernel: 5.8659 on master, 5.8620 with this PR.Tests:
test-backend-ops -o MUL_MAT,MUL_MAT_ID -b MTL0: 3900/3900 on the M3 Ultra. On an Apple M5 with the tensor API on and off (GGML_METAL_TENSOR_DISABLE=1): MUL_MAT and MUL_MAT_ADD 2615/2615, MUL_MAT_ID 1453/1453. The duplicate-id cases from vulkan: handle mul_mat_id duplicates in the prepass rather than looping #29998 run on the new kernels at 9 and 16 tokens.Requirements