Repository navigation
metal : add tensor API flash attention kernel for F16 KV - #29570
Conversation
|
Can this implementation also support |
@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). |
|
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. |
|
Could you also add |
@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 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. |
|
Thanks - interesting work. I'll try to do some additional testing on M5 too. cc @ggml-org/ggml-metal |
|
I haven't received my M6 Mac yet, so I ran llama.swiftui benchmark with an iPhone Air (A19 Pro), and it looks good:
UPDATE: M6 Mac mini result
|
There was a problem hiding this comment.
@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. |
|
Ok thanks. Actually, I am doing some tests in parallel for |
@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. |
) * 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)
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 intoggml-tensor.metallibforGGML_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 lacksGGML_METAL_HAS_BF16).-fa offvs master.Requirements