Retrieved article excerpt
Open article · Retrieved 2026-10-25T10:27:53.102687+00:00
## Repository navigation
# CUDA: fuse FFN gate/up matmuls and GLU in MMQ with interleaved gate/up rows - #29948
#29948
Merged
[ggerganov](https://github.com/ggerganov) merged 14 commits into
[ggml-org:master](https://github.com/ggml-org/llama.cpp/tree/master)ggml-org/llama.cpp:masterfrom
[anujj:cuda-mmq-fused-gate-up-interleaved](https://github.com/anujj/llama.cpp/tree/cuda-mmq-fused-gate-up-interleaved)anujj/llama.cpp:cuda-mmq-fused-gate-up-interleavedCopy head branch name to clipboard
Oct 25, 2026
Merged
## [CUDA: fuse FFN gate/up matmuls and GLU in MMQ with interleaved gate/up rows](https://github.com/ggml-org/llama.cpp/pull/29948#top)#29948 [ggerganov](https://github.com/ggerganov) merged 14 commits into [ggml-org:master](https://github.com/ggml-org/llama.cpp/tree/master)ggml-org/llama.cpp:masterfrom [anujj:cuda-mmq-fused-gate-up-interleaved](https://github.com/anujj/llama.cpp/tree/cuda-mmq-fused-gate-up-interleaved)anujj/llama.cpp:cuda-mmq-fused-gate-up-interleavedCopy head branch name to clipboard
## Conversation
[@anujj](https://github.com/anujj)
### @anujj **[anujj](https://github.com/anujj)** commented [Oct 4, 2026](https://github.com/ggml-org/llama.cpp/pull/29948#issue-5699726794)
Copy link
Copy Markdown
Contributor
Extends the gate/up + GLU fusion, which today only covers small batches (MMVQ/MMVF), to prompt-processing batch
sizes on the MMQ path.
Alternative to [#28702](https://github.com/ggml-org/llama.cpp/pull/28702) implementing [@am17an](https://github.com/am17an)'s suggestion there: keep **one accumulator** and apply the GLU in the write-back instead of carrying a second accumulator tile for the gate as discussed here: [#28702 (comment)](https://github.com/ggml-org/llama.cpp/pull/28702#issuecomment-5887166026)
For prompt processing, the gate and up `MUL_MAT`s of a dense FFN and the following `GLU` run as a single `mul_mat_q` launch. The small-batch MMVQ/MMVF fusion, other backends and the weight layout are unchanged.
### Implimentation
- The loaders interleave up and gate rows in chunks of 8, so each thread holds the up and gate value of the same output element (rows `r` and `r + 8` of the MMA tile). Chunks of 8 rather than 16 also cover the one-tile-per-warp configs.
- MMA loop, tile config and `sum[]` are unchanged; the write-back applies the GLU from registers and writes 64 output rows per tile.
- Q4\_K, Q5\_K, Q6\_K, Q8\_0, IQ4\_XS on NVIDIA Turing+; SWIGLU, GEGLU, SWIGLU\_OAI, SWIGLU\_CLAMP.
Please note: I have a plan to validate and enable it for the other GGML also.
### Performance
DGX Spark (GB10), `llama-bench -ngl 999 -fa on -p 16384 -n 0 -b 2048 -ub 2048 -ctk f16 -ctv f16 -r 5`, tok/s:
| model | type | master | PR | |
| --- | --- | --- | --- | --- |
| Qwen3.8-27B | Q4\_K\_M | 840.1 | 899.9 | +7.1% |
| | Q5\_K\_M | 840.1 | 880.6 | +4.8% |
| | Q6\_K | 751.2 | 785.8 | +4.6% |
| | Q8\_0 | 876.2 | 940.1 | +7.3% |
| | IQ4\_XS | 882.4 | 948.0 | +7.4% |
| Qwen3.6-27B | Q4\_K\_M | 816.6 | 865.6 | +6.0% |
| | Q5\_K\_M | 800.0 | 851.1 | +6.4% |
| | Q6\_K | 720.8 | 770.4 | +6.9% |
| | Q8\_0 | 876.9 | 940.2 | +7.2% |
| | IQ4\_XS | 882.2 | 948.5 | +7.5% |
| Llama-3.1-8B | Q4\_K\_M | 2749.3 | 3000.6 | +9.1% |
| | Q5\_K\_M | 2699.4 | 2960.3 | +9.7% |
| | Q6\_K | 2392.2 | 2616.0 | +9.4% |
| | IQ4\_XS | 2933.9 | 3249.0 | +10.7% |
RTX 5090, `llama-bench -ngl 999 -fa on -p 16384 -n 0 -b 2048 -ub 2048 -ctk f16 -ctv f16 -r 5`, tok/s:
| model | type | master | PR | |
| --- | --- | --- | --- | --- |
| Qwen3.8-27B | Q4\_K\_M | 3688.4 | 3790.9 | +2.8% |
| | Q5\_K\_M | 3589.5 | 3659.6 | +2.0% |
| | Q6\_K | 3220.5 | 3281.0 | +1.9% |
| | Q8\_0 | 3617.3 | 3766.9 | +4.1% |
| | IQ4\_XS | 3796.2 | 3926.8 | +3.4% |
| Qwen3.6-27B | Q4\_K\_M | 3567.9 | 3660.3 | +2.6% |
| | Q5\_K\_M | 3429.8 | 3525.3 | +2.8% |
| | Q6\_K | 3105.2 | 3187.8 | +2.7% |
| | Q8\_0 | 3628.7 | 3788.4 | +4.4% |
| | IQ4\_XS | 3785.6 | 3915.4 | +3.4% |
| Llama-3.1-8B | Q4\_K\_M | 12071.5 | 12625.9 | +4.6% |
| | Q5\_K\_M | 11823.0 | 12297.3 | +4.0% |
| | Q6\_K | 10513.1 | 10928.6 | +4.0% |
| | IQ4\_XS | 12997.6 | 13603.3 | +4.7% |
### Correctness
- `test-backend-ops`: 19 new `MUL_MAT_VEC_FUSION` cases hit the MMQ path (129 rows with bounds checks at one and two MMA tiles per warp, 256 rows without, GEGLU / SWIGLU\_OAI / SWIGLU\_CLAMP, batched). Full suite passes on GB10; the MUL\_MAT suites also pass on an RTX 5090.
- Perplexity (wiki.test, 8 chunks) is bit-identical with `GGML_CUDA_DISABLE_FUSION=1` for all five types, also with `-ot ffn_gate=CPU`.
## 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 - Paired with claude
[@github-actions](https://github.com/apps/github-actions)
[github-actions](https://github.com/apps/github-actions)
Bot
added
[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 4, 2026](https://github.com/ggml-org/llama.cpp/pull/29948#event-32445052262)
[@anujj](https://github.com/anujj)
### **[anujj](https://github.com/anujj)** commented [Oct 4, 2026](https://github.com/ggml-org/llama.cpp/pull/29948#issuecomment-5980522212)
Copy link
Copy Markdown
Contributor
Author
| |
| --- |
| Adding [@am17an](https://github.com/am17an) and [@JohannesGaessler](https://github.com/JohannesGaessler) for review |
[@anujj](https://github.com/anujj)
### **[anujj](https://github.com/anujj)** commented [Oct 8, 2026](https://github.com/ggml-org/llama.cpp/pull/29948#issuecomment-6057828971)
Copy link
Copy Markdown
Contributor
Author
| |
| --- |
| Adding [@am17an](https://github.com/am17an) and [@JohannesGaessler](https://github.com/JohannesGaessler) for review gentle reminder for the review |
[@JohannesGaessler](https://github.com/JohannesGaessler)
### **[JohannesGaessler](https://github.com/JohannesGaessler)** commented [Oct 8, 2026](https://github.com/ggml-org/llama.cpp/pull/29948#issuecomment-6059005274)
Copy link
Copy Markdown
Contributor
| |
| --- |
| Sorry, I hadn't prioritized looking at this PR for review because it is still marked as a draft. |
[@anujj](https://github.com/anujj)
[anujj](https://github.com/anujj)
marked this pull request as ready for review
[October 8, 2026 12:05](https://github.com/ggml-org/llama.cpp/pull/29948#event-32785007145)
[@anujj](https://github.com/anujj)
[anujj](https://github.com/anujj)
requested review from
a team and
[ggerganov](https://github.com/ggerganov)
as [code owners](https://github.com/ggml-org/llama.cpp/blob/c811cb8f0ac91b8ac72a32f970bdd45037f20da7/CODEOWNERS#L62)
[October 8, 2026 12:05](https://github.com/ggml-org/llama.cpp/pull/29948#event-32785007996)
[JohannesGaessler](https://github.com/JohannesGaessler)
**[JohannesGaessler](https://github.com/JohannesGaessler)**
reviewed
[Oct 8, 2026](https://github.com/ggml-org/llama.cpp/pull/29948#pullrequestreview-5456001198)
[View reviewed changes](https://github.com/ggml-org/llama.cpp/pull/29948/files)
Comment thread
[ggml/src/ggml-cuda/ggml-cuda.cu](https://github.com/ggml-org/llama.cpp/pull/29948/files#diff-cb2761994492f7839320b81765a6fc2a23c180e3ddfd4a51d6b0f618f8d76a69)
Outdated
Comment on lines
+1880
to
+1887
| | | |
| --- | --- | --- |
| | | if (up->op != GGML\_OP\_MUL\_MAT || !ggml\_cuda\_mmq\_fusion\_supported(x\_up->type) || y->type != GGML\_TYPE\_F32 || |
| | | ggml\_get\_op\_params\_i32(up, 1) != GGML\_HINT\_NONE || ggml\_get\_op\_params\_i32(gate, 1) != GGML\_HINT\_NONE) { |
| | | return false; |
| | | } |
| | | |
| | | if (!ggml\_cuda\_should\_fuse\_mul\_mat(up, gate, glu)) { |
| | | return false; |
| | | } |
### @JohannesGaessler **[JohannesGaessler](https://github.com/JohannesGaessler)** [Oct 8, 2026](https://github.com/ggml-org/llama.cpp/pull/29948#discussion_r4218452869)
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).
I'm not sure this is logically correct. You *can* fuse matrices with the Hadamard hind, but whether you *should* is a different question. So the correct place to have this check would be in `ggml_cuda_should_fuse_mul_mat_q`.
### @anujj **[anujj](https://github.com/anujj)** [Oct 9, 2026](https://github.com/ggml-org/llama.cpp/pull/29948#discussion_r4230058585)
Copy link
Copy Markdown
Contributor
Author
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).
Moved the hint check into ggml\_cuda\_should\_fuse\_mul\_mat\_q. If I understand currently it does not trigger here: the hint is only set on the KV-cache rotation matmuls (F32 src0, not followed by a GLU), while the fused path only takes quantized gate/up weights.
Comment thread
[ggml/src/ggml-cuda/mmq.cu](https://github.com/ggml-org/llama.cpp/pull/29948/files#diff-b2fe862fcd5119199ae59ea13d1b6a46e0d23e41e727e39d90913f828a5ff66b)
Show resolved
Hide resolved
### Uh oh!
There was an error while loading. [Please reload this page](https://github.com/ggml-org/llama.cpp/pull/29948).
Comment thread
[ggml/src/ggml-cuda/mmq.cu](https://github.com/ggml-org/llama.cpp/pull/29948/files#diff-b2fe862fcd5119199ae59ea13d1b6a46e0d23e41e727e39d90913f828a5ff66b)
Show resolved
Hide resolved
### Uh oh!
There was an error while loading. [Please reload this page](https://github.com/ggml-org/llama.cpp/pull/29948).
Comment thread
[ggml/src/ggml-cuda/mmq.cuh](https://github.com/ggml-org/llama.cpp/pull/29948/files#diff-f591a6af9587b282030c7387e32a880973e68370ee6ee3918bd5cd008d1fb89d)
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/29948).
Comment thread
[ggml/src/ggml-cuda/mmq.cuh](https://github.com/ggml-org/llama.cpp/pull/29948/files#diff-f591a6af9587b282030c7387e32a880973e68370ee6ee3918bd5cd008d1fb89d)
Outdated
Comment on lines
+1106
to
+1107
| | | |
| --- | --- | --- |
| | | const int jt = has\_fusion ? blockIdx.x : blockIdx.y; |
| | | const int it = has\_fusion ? blockIdx.y : blockIdx.x; |
### @JohannesGaessler **[JohannesGaessler](https://github.com/JohannesGaessler)** [Oct 8, 2026](https://github.com/ggml-org/llama.cpp/pull/29948#discussion_r4218754446)
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).
Why are you swapping `blockIdx.x` and `blockIdx.y`?
### @anujj **[anujj](https://github.com/anujj)** [Oct 9, 2026](https://github.com/ggml-org/llama.cpp/pull/29948#discussion_r4230200704) • edited Loading Uh oh! There was an error while loading. [Please reload this page](https://github.com/ggml-org/llama.cpp/pull/29948).
Copy link
Copy Markdown
Contributor
Author
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).
The fused kernel cannot use stream-k, so it runs the xy tiling with the column tiles innermost, the same order as the stream-k path