Skip to content

metal : per-device tuned mul_mm tiles for every src0 type - #29743

Draft
forforever73 wants to merge 5 commits into
ggml-org:masterfrom
forforever73:mul-mm-tile-rehome
Draft

forforever73 wants to merge 5 commits into
ggml-org:masterfrom
forforever73:mul-mm-tile-rehome

Conversation

@forforever73

Copy link
Copy Markdown
Contributor

Overview

Draft. Opening this early to get feedback on the structure; the tables are still provisional (see TODO).

kernel_mul_mm always runs a 64x32 tile, which leaves most of the GPU idle on small batches. #25377 hand-picks 64x8 for q4_0; this PR makes the tile a measured, per-device choice for every src0 type.

  • One kernel_mul_mm with a tunable tile: NR1 is a template argument (8/16/32), NR0 a function constant. 64x32 stays the default, existing host names are unchanged, and 52 instantiations are added (26 src0 types x nr1 {8,16}).
  • Two tables, both generated by ggml-metal-tuning mul-mm:
    • mm_tile_tuned_table picks (nr0, nr1) from the src0 type, output features and ne11. This is the pp16-pp64 gain.
    • mm_tile_ne11_mm_min_table lowers the mul_mv_ext -> mul_mm switch point inside ne11 2-8, so those batches run a small mm tile instead of mul_mv_ext. This, together with the tile, is the pp5-pp8 gain.
  • Unchanged: ne11 >= 256, batched mul_mat (ne12*ne13 > 1), mul_mm_id, the tensor-API path, and any device without rows.
  • test-backend-ops -o MUL_MAT gains a tile slice that checks every legal tile for every src0 type against the CPU.

Results (M4 Max)

llama-bench -p 5,6,7,8,16,32,64,128,256,512 -n 0 -r 10 -fa on, PR / master (8df332de1) t/s:

model pp5 pp6 pp7 pp8 pp16 pp32 pp64 pp128 pp256 pp512
Llama-3.2-1B F16 1.009 1.018 1.167 1.203 1.148 1.119 1.079 0.999 0.991 0.997
Llama-3.2-1B Q4_0 1.325 1.329 1.304 1.353 1.336 1.136 1.047 1.008 1.000 0.999
Llama-3.1-8B Q4_K_M 1.374 1.377 1.926 2.026 1.329 1.056 1.012 1.002 0.998 0.999
Qwen3.6-27B Q8_0 1.366 1.389 1.837 1.959 1.441 1.115 1.010 1.005 1.000 1.000
Llama-3.1-70B Q4_K_M 2.083 2.095 2.747 2.927 1.469 1.004 1.005 0.999 0.999 0.999

50 points: min 0.991, max 2.93, geomean 1.246.
The lowest point, 1B F16 pp256, measures 0.995 on an interleaved re-run. At ne11 >= 256 the table is not consulted, so both builds run the same 64x32 tile there.

Tests: MUL_MAT 1269/1269 (tile slice 495 run / 0 failed / 36 unsupported), MUL_MAT_ID 867/867.

Binary and compile cost:

master PR
default.metallib 14.37 MB 15.07 MB (+4.8%)
mul_mm.metal host names 111 163
mul_mm.metal offline compile 0.74 s 1.03 s
EMBED cold start, all libraries (4 runs) 8.06 s 8.25 s

Sweep cost: 26 types, 9199 cells, about 23 h on M4 Max, split into one shard per type (--dtype).

TODO

  • Key the tables by SKU or by GPU family.
  • Decide whether the 9 types without a mul_mv_ext kernel (iq2_xxs, iq2_xs, iq3_xxs, iq3_s, iq2_s, iq1_s, iq1_m, iq4_xs, tq2_0) get switch-point rows. Their rival would be mul_mv, and the ne00 % 128 condition would then need to apply only to mv_ext types.
  • Re-sweep all 26 types on the final kernel, replace the tables and redo the numbers above. The current rows were measured before d362e82.
  • Rows for devices other than M4 Max.

Requirements

  • I have read and agree with the contributing guidelines.
  • AI usage disclosure: Yes, claude was used for code and test.

@ggerganov ggerganov self-assigned this Sep 30, 2026
@github-actions github-actions Bot added documentation Improvements or additions to documentation testing Everything test related examples ggml changes relating to the ggml tensor library for machine learning Apple Metal https://en.wikipedia.org/wiki/Metal_(API) labels Sep 30, 2026
@ggerganov

ggerganov commented Oct 3, 2026 •

Copy link
Copy Markdown
Member

I am thinking that probably for the BS range of [2..16] we should have a separate set of kernels as proposed in #29869, that will replace the mat_vec_ext kernels.

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

Apple Metal https://en.wikipedia.org/wiki/Metal_(API) documentation Improvements or additions to documentation examples ggml changes relating to the ggml tensor library for machine learning testing Everything test related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants