Repository navigation
qwen4_exp: sparse QSA attention + GQA support - #2112
Conversation
|
Watching this for a merge. This is definitely exciting as prefill is the main performance drag on my max spec 256 GB M3 Ultra studios. They currently get around 800-850 tok/sec stable median prefill using llama.cpp and Q8 (lower quants don't offer any perf boost since Q8 sits comfortably in memory). Decode hovers stably between 45-65 tok/sec and 55 tok/sec stable median. My llama.cpp build is about a month old and I am going to be updating and qualifying on the latest release ASAP so I can accurately judge the impact of this specific PR once it gets merged. |
|
Most of my workloads sit in the 90k - 175k tok range. |
|
Checking the stats posted, I am not sure why my cluster, without this patch, is already getting perf exceeding the optimized outcomes from the patch. My guess is I already have a highly optimized config for llama.cpp plus whatever optimizations llama.cpp itself offers (eg from the drafter). The drafter wouldn't help with prefill though and I'm already seeing prefill stable of 850 tok/sec at very high context (>128k) so will be interesting to see if I get similar multiplicative boost from this PR once it makes its way to llama.cpp or not... it's possible llama.cpp already has something similar integrated which may explain my current performance scenario as well (that's beyond me but I can have my AIs have a look for me). |
Description
This PR brings the work of #1997, #2013, #2027 and #2046 to QSA, used by Qwen-3.8 Flash Next.
The current QSA indexer in
qwen4_exppicks the top-k blocks for each query, but attention still runs over all keys, so compute and memory grow withO(L * N)instead ofO(L * top_k). Porting the changes from #2027 was not possible, as #2027 only added support for MLA (shared latent K = V) and did not support GQA.This PR adds support for GQA.
sparse_mla_attentionis renamed toindexed_attentionand supports both MLA as well as GQA. Each threadgroup computes one query and a group of up to 16 heads that share one KV head. Each selected row is still loaded once per group.For QSA, the block keys are computed once. A block key is the mean of its
rraw keys, with normalization and roping applied. It's stored in a cache, avoiding re-computation.Similarly, in decode, this patch only reads the picked rows instead of all previous ones.
DSA behavior is unchanged. GLM-5.3 is bit-identical to main and achieves same performance.
Performance gains
Setup
Prefill throughput (tok/s) for Qwen-3.8 Flash Next on M5 Ultra:
Prefill throughput (tok/s) for Qwen-3.8 Flash Next on M3 Ultra:
Script: qsa_sweep.py
Commands:
PYTHONPATH=. python qsa_sweep.py --out baseline.json # on mainPYTHONPATH=. python qsa_sweep.py --out patch.json # on this branchDecode throughput (tok/s) for Qwen-3.8 Flash Next on M5 Ultra:
Decode throughput (tok/s) for Qwen-3.8 Flash Next on M3 Ultra:
Script: Same as previous
Memory
Unit: Peak GB during the prefill chunk, on M5 Ultra:
Script: Same as previous
Tests & Accuracy:
☑️ I understand it is strictly prohibited to use AI to write PR description
AI usage disclosure: