2026-11-26 15:48 UTC

llama.cpp merges fused sparse-attention mask optimization

state: corroboratedheat: lowuncertainty: mediumrelevance: mediumllama-cpp sparse-attention inference-optimizationam17anGeorgi Gerganov

What is this?

llama.cpp merged PR #30640, adding a fused top-k attention-mask operation that replaces a sequence of operations with one op. The PR reports benchmark gains of about 0.2–6.8% on tested sparse-attention workloads, including QSA/k-pool models; a generalized version also covers DSA and DeepSeek V4 CSA mask paths, with existing-path fallback on backends that do not support the op. These are contributor-reported results on a limited set of models and hardware, not evidence of a uniform speedup.

Why it matters

A practical llama.cpp inference optimization, not a new model: the merged fused mask operation reduces sparse-attention bookkeeping and broadens one optimized path from QSA/k-pool models to DSA and DeepSeek V4 CSA. The reported gains are modest and vary substantially by test (about 0.2–6.8%); they may matter to users running these architectures, but should not be assumed for other models, hardware, or workloads.

Current assessment

llama.cpp merged PR #30640, fusing sparse-attention top-k mask construction across QSA/k-pool, DSA, and DeepSeek V4 CSA paths, with fallback on unsupported backends. The contributor reported roughly 0.2–6.8% gains on selected models and hardware, and reviewers checked semantics and backend tests; broad or guaranteed gains are not established. A separate related QSA change removes the kq_mask buffer, but discussion cautions that this is not the same as expert-cache savings or a general decode-speed improvement; benefits depend on workload, including potentially prefill and batch settings. Users of these paths should benchmark their own setup.

Measured heat

now 0 pts/hpeak 0 pts/hcomments 0/hpeers p0momentum: steady1 platformsage 860h
points/hour across evidence · reading as of 2026-11-27 02:23:58.509736+11:00 · deterministic, not a model opinion

How the heat travelled

10-21 19:34⭐ origin directly observedllama: create new fused op for QSA mask by am17an · Pull Request #30640 · ggml-org/llama.cpp
jacek2023 on r/LocalLLaMA
—
10-23 18:43first on r/LocalLLaMA · published · +47.2hqwen4exp : build the QSA selection without the kq_mask by sanmai · Pull Request #30965 · ggml-org/llama.cpp
jacek2023
—
10-21 19:34amplified on r/LocalLLaMAreddit.post.1xbx12a
jacek2023
peak 35 · 12 comments · 28% of case engagement
10-23 18:43amplified on r/LocalLLaMA 👑reddit.post.1xdmek0
jacek2023
peak 95 · 24 comments · 72% of case engagement
10-21 20:25our radar first saw it · +0.8hdiscovery anchor: reddit.post.1xbx12a—
pace: p76 vs 879 stories at the 720h mark (now 860h old) — ahead of nothing-threw-agent-failure-audit (1.0x), behind capy-flash-125b-moe-consumer-gpus (1.0x)

Evidence (2) — ⭐ canonical anchor

sourceobjectauthorscorecomments
🟠 reddit ⭐llama: create new fused op for QSA mask by am17an · Pull Request #30640 · ggml-org/llama.cpp
LocalLLaMA
Retrieved article excerpt

Open article · Retrieved 2026-10-21T20:28:28.776476+00:00

## Repository navigation



# llama: create new fused op for QSA mask - #30640

#30640

Merged

[am17an](https://github.com/am17an) merged 4 commits into

[master](https://github.com/ggml-org/llama.cpp/tree/master)ggml-org/llama.cpp:masterfrom 

[aman/cuda-kpool-mask](https://github.com/ggml-org/llama.cpp/tree/aman/cuda-kpool-mask)ggml-org/llama.cpp:aman/cuda-kpool-maskCopy head branch name to clipboard

Oct 21, 2026

Merged

## [llama: create new fused op for QSA mask](https://github.com/ggml-org/llama.cpp/pull/30640#top)#30640 [am17an](https://github.com/am17an) merged 4 commits into [master](https://github.com/ggml-org/llama.cpp/tree/master)ggml-org/llama.cpp:masterfrom [aman/cuda-kpool-mask](https://github.com/ggml-org/llama.cpp/tree/aman/cuda-kpool-mask)ggml-org/llama.cpp:aman/cuda-kpool-maskCopy head branch name to clipboard

## Conversation

[@am17an](https://github.com/am17an)

### @am17an **[am17an](https://github.com/am17an)** commented [Oct 16, 2026](https://github.com/ggml-org/llama.cpp/pull/30640#issue-5877661042) • edited Loading Uh oh! There was an error while loading. [Please reload this page](https://github.com/ggml-org/llama.cpp/pull/30640).

Copy link
 

Copy Markdown

Contributor

## Overview

Create a fused op for QSA scoring, fusing 25 ops into a single one.

| Test | RTX PRO 6000 (IQ4\_NL) | DGX Spark GB10 (UD-IQ4\_XS) |
| --- | --- | --- |
| pp2048 @ d0 | 4771 -> 5022 (+5.2%) | 1063 -> 1104 (+3.9%) |
| pp2048 @ d16384 | 4263 -> 4555 (+6.8%) | 947 -> 992 (+4.7%) |
| pp2048 @ d65536 | not run | 872 -> 923 (+5.9%) |
| tg64 @ d0 | 111.0 -> 114.9 (+3.5%) | 29.0 -> 29.2 (+0.6%) |
| tg64 @ d16384 | 105.1 -> 110.4 (+5.1%) | 27.7 -> 28.1 (+1.3%) |
| tg64 @ d65536 | not run | 25.8 -> 26.0 (+0.7%) |

Flash-Next, `-lzm on -fa 1 -ub 2048`, best of 2 alternating rounds. The d65536 rows were run once per round.

For GLM-5.3-next

| Test | GLM-5.3-Flash IQ2\_XXS, DGX Spark GB10 |
| --- | --- |
| pp2048 @ d0 | 535.7 -> 548.8 (+2.4%) |
| pp2048 @ d16384 | 405.9 -> 417.3 (+2.8%) |
| pp2048 @ d65536 | 369.0 -> 380.5 (+3.1%) |
| tg64 @ d0 | 20.3 -> 20.4 (+0.2%) |
| tg64 @ d16384 | 19.5 -> 19.6 (+0.8%) |
| tg64 @ d65536 | 18.2 -> 18.4 (+1.0%) |

## Additional information

## Requirements

- I have read and agree with the [contributing guidelines](https://github.com/ggml-org/llama.cpp/blob/master/CONTRIBUTING.md)
- AI usage disclosure: YES

[@am17an](https://github.com/am17an)

[am17an](https://github.com/am17an)
requested review from
a team,
[CISC](https://github.com/CISC),
[JohannesGaessler](https://github.com/JohannesGaessler) and
[ggerganov](https://github.com/ggerganov)
as [code owners](https://github.com/ggml-org/llama.cpp/blob/b6334e37e6ef286fc5091a41b6561b2113d915fb/CODEOWNERS#L62)
[October 16, 2026 03:12](https://github.com/ggml-org/llama.cpp/pull/30640#event-33403687698)

[@am17an](https://github.com/am17an)
[am17an](https://github.com/am17an)
changed the title
~~llama: create new fused op for QSA scoring~~
llama: create new fused op for QSA mask
[Oct 16, 2026](https://github.com/ggml-org/llama.cpp/pull/30640#event-33403699838)

[@github-actions](https://github.com/apps/github-actions)
[github-actions](https://github.com/apps/github-actions)
Bot
added
[model](https://github.com/ggml-org/llama.cpp/issues?q=state%3Aopen%20label%3Amodel)
Model specific
[testing](https://github.com/ggml-org/llama.cpp/issues?q=state%3Aopen%20label%3Atesting)
Everything test related
[ggml](https://github.com/ggml-org/llama.cpp/issues?q=state%3Aopen%20label%3Aggml)
changes relating to the ggml tensor library for machine learning
[CUDA](https://github.com/ggml-org/llama.cpp/issues?q=state%3Aopen%20label%3ACUDA)
Related to the CUDA backend
labels
[Oct 16, 2026](https://github.com/ggml-org/llama.cpp/pull/30640#event-33403968185)

[@am17an](https://github.com/am17an)

[am17an](https://github.com/am17an)
[force-pushed](https://github.com/ggml-org/llama.cpp/compare/ee0d2cc9574628f3e748065c5b3d644d39375ca0..8a86212329f2d07f76fb3ba3799436e19b4d2205)
the

aman/cuda-kpool-mask
branch
from
[`ee0d2cc`](https://github.com/ggml-org/llama.cpp/commit/ee0d2cc9574628f3e748065c5b3d644d39375ca0) to
[`8a86212`](https://github.com/ggml-org/llama.cpp/commit/8a86212329f2d07f76fb3ba3799436e19b4d2205)  [Compare](https://github.com/ggml-org/llama.cpp/compare/ee0d2cc9574628f3e748065c5b3d644d39375ca0..8a86212329f2d07f76fb3ba3799436e19b4d2205)
[October 16, 2026 10:44](https://github.com/ggml-org/llama.cpp/pull/30640#event-33436665321)

[am17an](https://github.com/am17an)
added 2 commits
[October 19, 2026 14:22](https://github.com/ggml-org/llama.cpp/pull/30640#commits-pushed-4ac7076)

[@am17an](https://github.com/am17an)

`llama: create new fused op for QSA scoring`

`4ac7076`

[@am17an](https://github.com/am17an)

`enroll glm5.3-next too`

`6459705`

[@sanmai](https://github.com/sanmai)

### **[sanmai](https://github.com/sanmai)** commented [Oct 20, 2026](https://github.com/ggml-org/llama.cpp/pull/30640#issuecomment-6304191629) • edited Loading Uh oh! There was an error while loading. [Please reload this page](https://github.com/ggml-org/llama.cpp/pull/30640).

Copy link
 

Copy Markdown

Contributor

|  |
| --- |
| Just chiming in to appreciate the work. After some follow-up tweaks ([#30815](https://github.com/ggml-org/llama.cpp/pull/30815)) I can say that I don't need Strata or variants of thereof anymore - llama.cpp is just as snappy with QFN UD-Q4\_K\_XL:  ``` prompt processing, n_tokens =  20480, progress = 0.72, t =  18.54 s / 1104.48 tokens per second prompt processing, n_tokens =  24377, progress = 0.86, t =  22.15 s / 1100.73 tokens per second prompt processing, n_tokens =  28435, progress = 1.00, t =  26.02 s / 1093.00 tokens per second prompt processing, n_tokens =  28469, progress = 1.00, t =  26.61 s / 1069.88 tokens per second   n_gen =    136, tg =  44.90 t/s, tg_3s =  45.23 t/s  n_gen =    286, tg =  47.38 t/s, tg_3s =  49.85 t/s  n_gen =    458, tg =  50.60 t/s, tg_3s =  57.03 t/s  n_gen =    600, tg =  49.71 t/s, tg_3s =  47.03 t/s ```  That's without MTP, on 5090 and with:  ``` -b 4096 -ub 4096 -cmoe --moe-cache-mib 18700 ```  Otherwise all recommended default settings, no greedy decoding shenanigans. |

[@am17an](https://github.com/am17an)

`tests : initialize the remaining tensors in test_kpool_mask`

 …

`2961d5e`

```
The harness sentinels were left uninitialized, so leftover memory could read as NaN and fail the NaN check.
```

[@am17an](https://github.com/am17an)

[am17an](https://github.com/am17an)
[force-pushed](https://github.com/ggml-org/llama.cpp/compare/8a86212329f2d07f76fb3ba3799436e19b4d2205..2961d5e5c7762a7696e72c42e75e04551676cc1f)
the

aman/cuda-kpool-mask
branch
from
[`8a86212`](https://github.com/ggml-org/llama.cpp/commit/8a86212329f2d07f76fb3ba3799436e19b4d2205) to
[`2961d5e`](https://github.com/ggml-org/llama.cpp/commit/2961d5e5c7762a7696e72c42e75e04551676cc1f)  [Compare](https://github.com/ggml-org/llama.cpp/compare/8a86212329f2d07f76fb3ba3799436e19b4d2205..2961d5e5c7762a7696e72c42e75e04551676cc1f)
[October 21, 2026 04:07](https://github.com/ggml-org/llama.cpp/pull/30640#event-33809850181)

[francisab](https://github.com/francisab)
added a commit
to francisab/llama.cpp
that referenced
this pull request
[Oct 21, 2026](https://github.com/ggml-org/llama.cpp/pull/30640#ref-commit-1754267)

[@francisab](https://github.com/francisab)

`rtx-spark : validate ggml-org#30640 and assess ggml-org#29948`

…

`1754267`

```
Assisted-by: GitHub Copilot
```

[@ggerganov](https://github.com/ggerganov)
[ggerganov](https://github.com/ggerganov)
self-assigned this
[Oct 21, 2026](https://github.com/ggml-org/llama.cpp/pull/30640#event-33813344749)

[@ServeurpersoCom](https://github.com/ServeurpersoCom)

[ServeurpersoCom](https://github.com/ServeurpersoCom)
self-requested a review
[October 21, 2026 11:22](https://github.com/ggml-org/llama.cpp/pull/30640#event-33839240634)

[ServeurpersoCom](https://github.com/ServeurpersoCom)

**[ServeurpersoCom](https://github.com/ServeurpersoCom)**
approved these changes
[Oct 21, 2026](https://github.com/ggml-org/llama.cpp/pull/30640#pullrequestreview-5599134757)

[View reviewed changes](https://github.com/ggml-org/llama.cpp/pull/30640/files/2961d5e5c7762a7696e72c42e75e04551676cc1f)

### @ServeurpersoCom **[ServeurpersoCom](https://github.com/ServeurpersoCom)** left a comment

Copy link
 

Copy Markdown

Contributor

There was a problem hiding this comment.

### Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. [Learn more](https://docs.github.com/articles/managing-disruptive-comments/#hiding-a-comment).

Went through the op against both unfused paths, same semantics: liveness from the score, sentinel cells skipped, kept cells carry the kq\_mask value. test-backend-ops is green on my RTX PRO 6000 (12/12, f16 and f32, multi-stream included) and the probe falls back cleanly on backends without the op. LGTM!

[ggerganov](https://github.com/ggerganov)

**[ggerganov](https://github.com/ggerganov)**
reviewed
[Oct 21, 2026](https://github.com/ggml-org/llama.cpp/pull/30640#pullrequestreview-5599191766)

[View reviewed changes](https://github.com/ggml-org/llama.cpp/pull/30640/files/2961d5e5c7762a7696e72c42e75e04551676cc1f)

Comment thread

[ggml/include/ggml.h](https://github.com/ggml-org/llama.cpp/pull/30640/files/2961d5e5c7762a7696e72c42e75e04551676cc1f#diff-7dea3e94fe52f756a218321acc77042d0a333fd3d7e4c35160920ce6e86cb400)

Outdated
 
 





Show resolved
 





Hide resolved

### Uh oh!

There was an error while loading. [Please reload this page](https://github.com/ggml-org/llama.cpp/pull/30640).

[ggerganov](https://github.com/ggerganov)

**[ggerganov](https://github.com/ggerganov)**
reviewed
[Oct 21, 2026](https://github.com/ggml-org/llama.cpp/pull/30640#pullrequestreview-5599206885)

[View reviewed changes](https://github.com/ggml-org/llama.cpp/pull/30640/files/2961d5e5c7762a7696e72c42e75e04551676cc1f)

Comment thread

[ggml/include/ggml.h](https://github.com/ggml-org/llama.cpp/pull/30640/files/2961d5e5c7762a7696e72c42e75e04551676cc1f#diff-7dea3e94fe52f756a218321acc77042d0a333fd3d7e4c35160920ce6e86cb400)

Outdated
 
 





Show resolved
 





Hide resolved

### Uh oh!

There was an error while loading. [Please reload this page](https://github.com/ggml-org/llama.cpp/pull/30640).

[@ServeurpersoCom](https://github.com/ServeurpersoCom)

`ggml: generalize the k-pool mask op into ggml_top_k_mask`

 …

`55672a5`

```
The k-pool mask op only covered the pooled QSA selection of qwen4exp
and glm5-next, while the DSA models and the DeepSeek V4 CSA build the
same top-k mask by hand with fill, set_rows and add.

ggml_top_k_mask(mask, top_k, score, blocks, extra) keeps the mask
cells of the live picks and the extra cells, and sets the rest to
-inf. Every row tensor is 4D in the layout of top_k, and the mask is
broadcast to those rows, so a selection can also be made per head.
score, blocks and extra are optional: without score every pick is
live, without blocks a pick is a cell, and extra holds the cells a
row keeps without a pick.

llm_graph_context::build_fused_top_k_mask views the row tensors in
the layout of kq_mask and registers the fused node. The k-pool
models, the DSA attention of build_attn and the DeepSeek V4 CSA mask
use it when the probe enables the op, and keep their current path on
backends without it.
```

[@ServeurpersoCom](https://github.com/ServeurpersoCom)

### **[ServeurpersoCom](https://github.com/ServeurpersoCom)** commented [Oct 21, 2026](https://github.com/ggml-org/llama.cpp/pull/30640#issuecomment-6316497496)

Copy link
 

Copy Markdown

Contributor

|  |
| --- |
| [@am17an](https://github.com/am17an) nice work on this one! I sketched a more generic version of the op on top of your branch, in case it helps: [55672a5](https://github.com/ggml-org/llama.cpp/commit/55672a5197a0da65513733004447d4ab18683e18)  The same "keep the mask cells picked by a top\_k, -inf elsewhere" pattern is built by hand t
jacek20233312
🟠 redditqwen4exp : build the QSA selection without the kq_mask by sanmai · Pull Request #30965 · ggml-org/llama.cpp
LocalLLaMA
jacek20239524

Interpretation history

Decision trace