Skip to content

metal : add tensor API flash attention kernel for F16 KV - #29570

Merged
ggerganov merged 4 commits into
ggml-org:masterfrom
ethanhq:fa-tensor-api
Oct 2, 2026
Merged

ggerganov merged 4 commits into
ggml-org:masterfrom
ethanhq:fa-tensor-api

Conversation

@ethanhq

@ethanhq ethanhq commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Overview

Add a FLASH_ATTN_EXT kernel for M5 (tensor API) that computes QK^T and PV with mpp::tensor_ops::matmul2d, using Q = 32 queries per threadgroup (16 for head sizes 512 and 576) over 64-row KV blocks, and rescales the output only when the running max grows by more than 8.

It is used for F16 (or dequantized) K/V with DK == DV in {64, 128, 256, 512} or DK/DV = 192/128 or 576/512, KV length a multiple of 64, at least 20 queries, and ceil(n_queries/Q)*n_heads*head_size >= 8192 (2x the crossover measured on M5 Pro, smaller batches keep the existing kernel). Sinks, ALiBi and logit softcap are handled with function constants, as in the existing kernel. It is also built into ggml-tensor.metallib for GGML_METAL_EMBED_LIBRARY=OFF.

Additional information

M5 Pro (20-core GPU), master vs PR:

  • test-backend-ops -o FLASH_ATTN_EXT: 4958/4958; the new large-logit cases are the only ones that catch a missing mid-loop rescale, or a missing tanh with softcap (the existing softcap cases have tanh(x) ~ x). With EMBED=OFF all non-bf16 cases pass (bf16 crashes on master too, the pre-compiled library lacks GGML_METAL_HAS_BF16).
  • Qwen3.8-27B Q8_0, 8K context: mean KLD about 1e-6, PPL 5.8341 for both.
  • Head sizes 512, 576 and 192, and sinks/ALiBi/softcap: 1.6-2.6x on prefill shapes, none slower above the threshold. Cases without sinks/ALiBi/softcap run at the same speed as before they were added (4 alternating runs).
  • End to end, KLD vs master is at the noise level on bloom-560m and gemma-2-2b; on the MoE models below it is at the level of master -fa off vs master.
FLASH_ATTN_EXT perf, F16 KV master TFLOPS PR TFLOPS
hs=64 nh=8 nr23=[4,1] kv=16384 nb=4096 4.99 9.01
hs=128 nh=8 nr23=[4,1] kv=16384 nb=4096 5.13 9.92
hs=256 nh=4 nr23=[6,1] kv=4096 nb=512 5.21 10.52
hs=512 nh=4 nr23=[8,1] kv=4096 nb=512 5.20 10.22
hsk=576 hsv=512 nh=8 nr23=[4,1] kv=4096 nb=4096 5.17 12.96
hsk=192 hsv=128 nh=16 kv=16384 nb=512 3.84 8.87
hs=64 nh=8 nr23=[8,1] kv=16384 nb=512, sinks 4.44 8.95
hs=128 nh=32 kv=16384 nb=512, max_bias=8 4.15 9.74
hs=256 nh=8 nr23=[2,1] kv=16384 nb=512, softcap=50 4.24 9.44
llama-bench, Qwen3.8-27B Q8_0, -fa 1 master t/s PR t/s
pp512 383.3 385.8
pp512 @ d8192 295.7 340.0
pp512 @ d32768 174.3 252.9
pp32 @ d8192 (below the threshold) 107.2 106.2
tg32 9.18 9.02
llama-bench pp512, -fa 1 master t/s PR t/s
bloom-560m Q8_0 (ALiBi) @ d1024 13511 15201
gemma-2-2b-it Q8_0 (softcap) @ d4096 2429 3083
gpt-oss-20b MXFP4 (sinks) @ d4096 1446 1614
DeepSeek-V2-Lite-Chat Q4_K_M, no MLA (192/128) @ d4096 1599 2016
GLM-4.7-Flash Q4_K (576/512) @ d4096 501 812

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: YES, Claude Code assisted with writing the kernel and running the benchmarks.

@ethanhq
ethanhq requested review from a team and ggerganov as code owners September 28, 2026 11:33
@github-actions github-actions Bot added testing Everything test related ggml changes relating to the ggml tensor library for machine learning Apple Metal https://en.wikipedia.org/wiki/Metal_(API) labels Sep 28, 2026
@ggerganov ggerganov self-assigned this Sep 28, 2026
@ggerganov

Copy link
Copy Markdown
Member

Can this implementation also support DK == 512 && DV == 512? Also DK == 576 && DV == 512?

@ethanhq

ethanhq commented Sep 28, 2026

Copy link
Copy Markdown
Contributor Author

Can this implementation also support DK == 512 && DV == 512? Also DK == 576 && DV == 512?

@ggerganov Yes, both are supported now in 2733cfc. For DK = 512 and DK = 576 the kernel uses 16 queries per threadgroup instead of 32, otherwise the Q tile does not fit in the 32 KB of threadgroup memory. On M5 Pro the prefill FA is 1.6-2.6x faster than master for these head sizes (numbers in the description).

@ggerganov

Copy link
Copy Markdown
Member

Is there a fundamental barrier for supporting sinks and logit softcap with this implementation, or are they skipped for simplicity?

@ethanhq

ethanhq commented Sep 28, 2026

Copy link
Copy Markdown
Contributor Author

Is there a fundamental barrier for supporting sinks and logit softcap with this implementation, or are they skipped for simplicity?

@ggerganov No fundamental barrier, they were skipped in the first version for simplicity. I'll add them in this PR and post the numbers after testing.

@ggerganov

Copy link
Copy Markdown
Member

Could you also add DK == 192 && DV == 128?

@ethanhq

ethanhq commented Sep 29, 2026

Copy link
Copy Markdown
Contributor Author

Could you also add DK == 192 && DV == 128?

@ggerganov Added sinks and logit softcap in 2a83277, and DK = 192, DV = 128 in 7f0d4d5. On M5 Pro the tensor kernel is 1.6-2.5x faster than the existing one on prefill shapes with these, and shapes that were already on the tensor kernel keep the same speed.

pp512 at 4K depth vs master: gemma-2-2b (softcap) +27%, gpt-oss-20b (sinks) +12%, DeepSeek-V2-Lite without MLA (192/128) +26%. KLD vs master is at the noise level on gemma-2-2b, and on the MoE models about the same as master -fa off vs master.

I also added a softcap test with large logits. The existing softcap cases have small scores where tanh(x) ~ x, so they pass even without the tanh.

@ggerganov

Copy link
Copy Markdown
Member

Thanks - interesting work. I'll try to do some additional testing on M5 too.

cc @ggml-org/ggml-metal

@jhen0409

jhen0409 commented Sep 30, 2026 •

Copy link
Copy Markdown
Member

I haven't received my M6 Mac ​​yet, so I ran llama.swiftui benchmark with an iPhone Air (A19 Pro), and it looks good:

Qwen 3.5 4B Q4_0 master (t/s) PR (t/s)
pp512 @ d0 489.23 493.17
pp512 @ d2048 394.81 422.31
pp512 @ d4096 320.43 363.17
pp512 @ d8192 115.41 137.12
pp512 @ d16384 125.68 168.00
tg32 @ d0 20.20 20.29
tg32 @ d4096 19.54 19.45

UPDATE: M6 Mac mini result

Qwen 3.5 4B Q4_0 master (t/s) PR (t/s)
pp512 @ d0 1313.67 1344.89
pp512 @ d2048 1185.70 1279.47
pp512 @ d4096 1079.66 1195.30
pp512 @ d8192 904.30 1110.96
pp512 @ d16384 682.30 921.17
tg32 @ d0 47.38 47.38
tg32 @ d4096 44.52 44.19

@ggerganov ggerganov 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.

@ethanhq If you are interested, you can try to add full BF16 support for this kernel. The idea is when running with -ctk bf16 -ctv bf16 to use a BF16 variant of the kernel that does the multiplication using BF16 += BF16 x BF16. I expect that this path will be even faster on modern chips compared to the existing F32 += F16 x F16.

@ggerganov
ggerganov merged commit 926862e into ggml-org:master Oct 2, 2026
32 of 35 checks passed
@ethanhq

ethanhq commented Oct 2, 2026

Copy link
Copy Markdown
Contributor Author

@ethanhq If you are interested, you can try to add full BF16 support for this kernel. The idea is when running with -ctk bf16 -ctv bf16 to use a BF16 variant of the kernel that does the multiplication using BF16 += BF16 x BF16. I expect that this path will be even faster on modern chips compared to the existing F32 += F16 x F16.

@ggerganov Thanks for the suggestion! I’ll explore a BF16 variant as a follow-up and see how it compares in performance and accuracy.

@ggerganov

ggerganov commented Oct 2, 2026 •

Copy link
Copy Markdown
Member

Ok thanks. Actually, I am doing some tests in parallel for BF16 += BF16 x BF16 in mul_mat and my hypothesis that it is faster than F32 += F16 x F16 (that we currently use) does not seem to be correct. The speed is pretty much the same. Still, it would be worth it to support the exact BF16 path since in theory it should be a tiny bit more accurate.

@ethanhq

ethanhq commented Oct 2, 2026

Copy link
Copy Markdown
Contributor Author

Ok thanks. Actually, I am doing some tests in parallel for BF16 += BF16 x BF16 in mul_mat and my hypothesis that it is faster than F32 += F16 x F16 (that we currently use) does not seem to be correct. The speed is pretty much the same. Still, it would be worth it to support the exact BF16 path since in theory it should be a tiny bit more accurate.

@ggerganov Thanks for the update! I’ve been on vacation the past few days and haven’t had a chance to look into this yet. Once I’m back, I’ll help with testing and work on BF16 support.

@ggerganov ggerganov added the highlight Changes that will be highlighted in the next release notes label Oct 2, 2026
edwardyoon pushed a commit to edwardyoon/focus-llama that referenced this pull request Oct 8, 2026
)

* metal : add tensor API flash attention kernel for F16 KV

* cont : add tensor FA kernels for DK=DV=512 and DK=576, DV=512

* cont : support attention sinks, ALiBi and logit softcap in the tensor FA kernel

* cont : add tensor FA kernel for DK=192, DV=128

(cherry picked from commit 926862e)
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) ggml changes relating to the ggml tensor library for machine learning highlight Changes that will be highlighted in the next release notes testing Everything test related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants