Skip to content

qwen4_exp: sparse QSA attention + GQA support - #2112

Merged
zcbenz merged 1 commit into
ml-explore:mainfrom
vinceclifford:qsa-sparse-attention
Dec 1, 2026
Merged

zcbenz merged 1 commit into
ml-explore:mainfrom
vinceclifford:qsa-sparse-attention

Conversation

@vinceclifford

Copy link
Copy Markdown
Contributor

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_exp picks the top-k blocks for each query, but attention still runs over all keys, so compute and memory grow with O(L * N) instead of O(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_attention is renamed to indexed_attention and 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 r raw 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

  • M3 Ultra and M5 Ultra, 512GB, MLX 0.32.3, mlx-community/Qwen3.8-Flash-Next-4bit
  • Baseline main at 1bf8da0 (Fuse the DSA indexer's head sum  #2046), Patch with default settings
  • One warmup, then 3 timed runs per point for prefill and decode (mean)

Prefill throughput (tok/s) for Qwen-3.8 Flash Next on M5 Ultra:

context pp512 baseline pp512 patch speedup pp1024 baseline pp1024 patch speedup pp2048 baseline pp2048 patch speedup
0 2110.8 2110.4 1.00x 2702.2 2672.9 0.99x 3022.9 3037.8 1.00x
4K 1962.8 2040.5 1.04x 2456.2 2577.3 1.05x 2763.3 2931.6 1.06x
16K 1625.5 2009.1 1.24x 2043.6 2529.6 1.24x 2286.3 2868.6 1.25x
32K 1315.1 1960.1 1.49x 1667.9 2436.1 1.46x 1851.3 2738.3 1.48x
64K 951.5 1845.0 1.94x 1195.9 2294.0 1.92x 1325.2 2579.9 1.95x
128K 589.7 1689.5 2.86x 767.6 2070.9 2.70x 825.6 2274.0 2.75x

Prefill throughput (tok/s) for Qwen-3.8 Flash Next on M3 Ultra:

context pp512 baseline pp512 patch speedup pp1024 baseline pp1024 patch speedup pp2048 baseline pp2048 patch speedup
0 1059.7 1053.8 0.99x 1182.9 1176.4 0.99x 1257.4 1251.0 0.99x
4K 979.4 1010.3 1.03x 1078.8 1134.9 1.05x 1125.8 1208.2 1.07x
16K 806.3 995.8 1.24x 876.0 1122.7 1.28x 901.2 1190.4 1.32x
32K 649.2 978.0 1.51x 690.4 1096.4 1.59x 698.3 1162.4 1.66x
64K 452.2 946.4 2.09x 458.6 1057.4 2.31x 439.6 1121.0 2.55x
128K 274.2 876.2 3.20x 261.3 983.9 3.76x 259.5 1041.9 4.02x

Script: qsa_sweep.py
Commands:

  • PYTHONPATH=. python qsa_sweep.py --out baseline.json # on main
  • PYTHONPATH=. python qsa_sweep.py --out patch.json # on this branch

Decode throughput (tok/s) for Qwen-3.8 Flash Next on M5 Ultra:

context tg512 baseline tg512 patch speedup tg1024 baseline tg1024 patch speedup tg2048 baseline tg2048 patch speedup
0 47.5 47.8 1.01x 47.3 47.7 1.01x 47.1 47.7 1.01x
4K 43.9 44.2 1.01x 43.9 44.2 1.01x 43.9 44.2 1.01x
16K 42.3 43.4 1.03x 42.3 43.5 1.03x 42.3 43.5 1.03x
32K 41.1 43.1 1.05x 41.0 43.1 1.05x 41.0 43.2 1.05x
64K 38.5 42.5 1.10x 38.6 42.5 1.10x 38.6 42.6 1.10x
128K 34.8 41.4 1.19x 34.8 41.5 1.19x 34.8 41.6 1.20x

Decode throughput (tok/s) for Qwen-3.8 Flash Next on M3 Ultra:

context tg512 baseline tg512 patch speedup tg1024 baseline tg1024 patch speedup tg2048 baseline tg2048 patch speedup
0 39.8 39.9 1.00x 39.7 39.8 1.00x 39.5 39.7 1.01x
4K 36.6 36.5 1.00x 36.6 36.6 1.00x 36.5 36.6 1.00x
16K 35.1 36.0 1.02x 35.1 36.0 1.03x 35.1 36.0 1.03x
32K 33.8 35.6 1.05x 33.7 35.6 1.06x 33.6 35.6 1.06x
64K 31.0 35.1 1.13x 30.9 35.1 1.14x 31.0 35.2 1.14x
128K 27.4 34.1 1.24x 27.4 34.2 1.25x 27.4 34.2 1.25x

Script: Same as previous

Memory

Unit: Peak GB during the prefill chunk, on M5 Ultra:

context pp512 baseline pp512 patch pp1024 baseline pp1024 patch pp2048 baseline pp2048 patch
0 112.1 112.0 113.2 112.9 115.2 114.4
4K 112.2 112.2 113.0 113.0 114.5 114.5
16K 112.9 112.7 113.2 113.4 114.8 114.9
32K 113.6 113.3 113.7 113.8 115.3 115.4
64K 115.5 114.4 114.9 115.2 116.4 116.4
128K 119.1 116.6 117.4 117.9 119.9 118.5

Script: Same as previous

Tests & Accuracy:

  • 8 new tests, all of which fail without this change
  • No new failures in the entire test suite
  • KLD (main || patch) on 131072 tokens of real text: mean 0.0104, top-1 agreement: 96.7%

☑️ I understand it is strictly prohibited to use AI to write PR description
AI usage disclosure:

  • Claude Code helped write the optimization and run the benchmark suites.

@marksmeltzer

marksmeltzer commented Nov 28, 2026 •

Copy link
Copy Markdown

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.

@marksmeltzer

Copy link
Copy Markdown

Most of my workloads sit in the 90k - 175k tok range.

@marksmeltzer

Copy link
Copy Markdown

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).

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.

3 participants