Skip to content

metal: add gemv_wide for fp16/bf16 matmuls of a few rows - #3888

Merged
angeloskath merged 3 commits into
ml-explore:mainfrom
jessegross:gemv-wide
Jul 22, 2026
Merged

angeloskath merged 3 commits into
ml-explore:mainfrom
jessegross:gemv-wide

Conversation

@jessegross

Copy link
Copy Markdown
Contributor

fp16/bf16 products of a few rows against a large transposed weight matrix
(x @ w.T with M = 2..15) route to GEMM tiles that pad M to 64 rows
and leave much of the memory bandwidth unused.

gemv_wide streams each block of weight rows once and applies it to up
to five input vectors held in registers; chunks beyond the grid loop
inside one threadgroup, so vocab-width outputs also stream the matrix
once. It covers Matmul, AddMM, and GatherMM, batched or sliced, on
the M3 generation and later.

Speedups are per-kernel GPU time against the stock routing, with
uncached weights. The shapes come from a qwen3.6 deployment (the
layers a quantized model keeps in bf16).

in_proj_a/b, [M, 2048] x [2048, 32]:

M M3 Ultra M4 Pro M5 Max
2 3.0x 2.3x 6.0x
4 2.3x 1.8x 4.7x
8 2.3x 1.7x 4.8x

lm_head, [M, 2048] x [2048, 248320]:

M M3 Ultra M4 Pro M5 Max
2 1.7x 2.3x 1.4x
4 1.7x 2.2x 1.4x
8 1.8x 2.1x 1.3x

jessegross and others added 3 commits July 21, 2026 17:19
fp16/bf16 products of a few rows against a large transposed weight matrix
(x @ w.T with M = 2..15) route to GEMM tiles that pad M to 64 rows
and leave much of the memory bandwidth unused.

gemv_wide streams each block of weight rows once and applies it to up
to five input vectors held in registers; chunks beyond the grid loop
inside one threadgroup, so vocab-width outputs also stream the matrix
once. It covers Matmul, AddMM, and GatherMM, batched or sliced, on
the M3 generation and later.

Speedups are per-kernel GPU time against the stock routing, with
uncached weights. The shapes come from a qwen3.6 deployment (the
layers a quantized model keeps in bf16).

in_proj_a/b, [M, 2048] x [2048, 32]:

  M    M3 Ultra  M4 Pro  M5 Max
  2       3.0x    2.3x    6.0x
  4       2.3x    1.8x    4.7x
  8       2.3x    1.7x    4.8x

lm_head, [M, 2048] x [2048, 248320]:

  M    M3 Ultra  M4 Pro  M5 Max
  2       1.7x    2.3x    1.4x
  4       1.7x    2.2x    1.4x
  8       1.8x    2.1x    1.3x

@angeloskath angeloskath left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

@angeloskath
angeloskath merged commit 0ebcee8 into ml-explore:main Jul 22, 2026
28 checks passed
@jessegross
jessegross deleted the gemv-wide branch July 22, 2026 16:54
@BrewTestBot BrewTestBot mentioned this pull request Aug 18, 2026
1 task done
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.

2 participants