Why Stream-K Attention Ran Slow on the RTX 5090: Lost K/V Sharing in L2, and Grouped Stream-K as the Fix
1. The symptom
llama.cpp issue #30784 reported something odd. On a long prompt, an RTX 5090 processed tokens more slowly than an RTX 4090. The models were ordinary: Qwen3.5-9B and a 27B Qwen model. The context was 128K tokens. The 5090 is the newer and larger card, so it should have won.
The reporter also found a workaround. The CUDA FlashAttention kernel in llama.cpp uses a scheduling scheme called stream-k, and it launches a fixed number of thread blocks. Forcing that number to 256, 384 or 512 instead of the default 340 made the 5090 37–40% faster at 128K.
This article explains why the default count is slow, why the obvious fix does not generalize, and what does. The short answer: the problem is not the block count itself. With 340 blocks, no two blocks ever read the same K/V data at the same time, so L2 cannot share anything and almost every byte comes from DRAM. A small change to how stream-k hands out work, grouped stream-k, restores the sharing on every card we tested where it was missing. It keeps the block count, or at most rounds it down to a multiple of 4. The fix is proposed upstream as llama.cpp PR #32459, called “the PR code” below.
2. Background: tiles, stream-k and fixup
2.1 What one attention call looks like
We use two models throughout:
| Model | Q heads | K/V heads | Head dim |
|---|---|---|---|
| Qwen3.5-9B Q4_K_M | 16 | 4 | 256 |
| Llama-3.1-8B-Instruct Q4_K_M | 32 | 8 | 128 |
The K/V cache is q8_0. The benchmark processes 512 new tokens (-ub 512) on top of a context of up to 131072 tokens (128K).
For one K/V head, attention is two matrix products. First S = Q × Kᵀ. For Qwen that is [2048 × 256] × [256 × 131072], where 2048 = 512 tokens × the 4 Q heads that share this K/V head. Then O = softmax(S) × V.
The kernel splits the output O into tiles. One tile is 16 tokens × the 4 Q heads of one K/V head. Each K/V head therefore has 512 / 16 = 32 tiles. Qwen has 4 × 32 = 128 tiles in total. Llama has 8 × 32 = 256. The kernel calls this number ntiles_dst; we write it as T.
Each tile must scan the whole K and V of its head. The 32 tiles of one head read exactly the same data.
In memory, one row of the K cache is one token, with all heads side by side in that row. One head’s data is therefore a vertical strip through all rows. Different heads never share bytes.
The kernel reads K/V in batches of 64 rows. At 128K, one tile is 2048 batches long.
One layer’s K plus V for Qwen at 128K is about 285 MB. The RTX 5090’s L2 cache is 96 MB.
2.2 Stream-k
The simple schedule gives each tile its own thread block. With few tiles and many SMs, that leaves the GPU half idle.
Stream-k does something else. It lays all the work out as one long queue: every tile, and inside every tile every K/V batch. It then cuts the queue into n equal pieces, one per thread block. n is the number of blocks the GPU can hold at once: blocks per SM × number of SMs. For these shapes the kernel fits 2 blocks per SM, so:
| GPU | SMs | n |
|---|---|---|
| RTX 5090 | 170 | 340 |
| RTX 4090 | 128 | 256 |
| RTX 5080 | 84 | 168 |
| RTX 5070 Ti | 70 | 140 |
| H100 SXM | 132 | 264 |
The 32 tiles of one head sit next to each other in the queue.
2.3 Fixup
An equal cut rarely falls on tile boundaries. A block may compute only part of a tile’s K/V range, and another block computes the rest. Their partial results must then be merged. This merge step is the fixup.
If n is a multiple of T, every tile is covered by the same number of blocks, and llama.cpp has a fast, regular merge path for that case. If not, it runs a general fixup kernel.
2.4 The existing rounding rule
llama.cpp already tries to avoid the general fixup. In ggml/src/ggml-cuda/fattn-common.cuh it rounds n down to a multiple of T, but only if this loses at most 5% of the blocks.
On the 5090 with Qwen, T = 128. Rounding 340 down to 256 loses 24.7%, so the rule keeps 340. On the 4090, 256 is already a multiple of 128, so nothing is lost. That is the difference between the two cards that the issue observed.
3. The obvious fix does not generalize
The obvious fix is to relax the 5% threshold, or to round to the nearest multiple as the issue proposed. We tried raising the threshold to 25%.
On the 5090 it works: 340 becomes 256 and prefill at 128K is 31% faster.
On an RTX 5080 it does the opposite. The 5080 has 168 blocks. Rounding to 128 loses 23.8%, almost the same fraction as the 5090’s 24.7%. The new rule rounds it, and the 5080 becomes 3.7% slower. A 5080 user in the issue thread had also reported that forcing 128 blocks was slower than the default.
Two cards with nearly identical rounding losses move in opposite directions. No percentage threshold can separate them. So rounding to a multiple of T cannot be the real mechanism. Something else changes when the block count changes.
4. It is not the fixup
The issue named the general fixup path as the slow part. A profile with Nsight Systems on the 5090 at 128K says otherwise:
| 340 blocks | 256 blocks | |
|---|---|---|
| Fixup kernel | 0.05 s | 0.02 s |
| Attention kernel | 9.74 s | 6.17 s |
The fixup is tiny either way. The attention kernel itself is what slows down.
Nsight Compute shows where the time goes. The L2 hit rate of the attention kernel at 128K:
| Blocks | L2 hit rate |
|---|---|
| 340 | 10% |
| 256 | 93% |
| 168 | 52% |
At 340 blocks almost nothing hits in L2. The question becomes: why does the block count decide whether blocks share cached data?
5. Which blocks read the same K/V at the same moment
5.1 Start positions
Measure each block’s start in units of one tile’s K/V length. Block i starts at position i × T / n. If all blocks run at the same speed, the K/V position a block reads at any moment is the fractional part of its start plus the common progress. Two blocks read the same K/V batch at the same time only if their starts have the same fractional part.
How many distinct fractional parts are there? Write T/n in lowest terms as a/b, with a and b coprime.
- At most b. Every start is a multiple of 1/b, so the fractional part is one of 0, 1/b, …, (b−1)/b.
- Exactly b. It is enough to show that blocks 0 to b−1 all land on different positions. Suppose two of them, blocks j and k with j < k, land on the same position. Then their starts differ by a whole number of tiles. That difference is k × a/b − j × a/b = (k−j) × a/b. For it to be a whole number, b must divide (k−j) × a. b shares no factor with a, so b must divide k−j. But k−j is at least 1 and at most b−1, and no such number is a multiple of b. So no two of the first b blocks share a position, and they fill all b positions. A small example: if T/n = 2/5, blocks 0 to 4 start at 0, 0.4, 0.8, 1.2 and 1.6 tiles. The fractional parts are 0, 0.4, 0.8, 0.2 and 0.6: five different positions.
- The pattern repeats every b blocks. Moving b blocks further along adds b × T/n = b × a/b = a to the start. a is a whole number, so block i+b starts exactly a tiles after block i and has the same fractional part. So blocks 0 to b−1 take each of the b positions once, blocks b to 2b−1 take them once more in the same order, and so on. The n blocks make n/b such rounds, so each position holds exactly n/b blocks. Example: with n = 340 and T = 128, a/b = 32/85. Block 85 starts at 85 × 128/340 = 32, exactly 32 tiles after block 0, so both start at the beginning of a tile. The 340 blocks make 4 rounds of 85, so each of the 85 positions holds 4 blocks.
- In terms of the gcd. Reducing T/n to lowest terms divides both by g = gcd(n, T). So there are b = n/g positions with g blocks each.
Some examples with T = 128:
| n | T/n in lowest terms | positions × blocks per position |
|---|---|---|
| 256 | 1/2 | 2 × 128 |
| 340 | 32/85 | 85 × 4 |
| 336 | 8/21 | 21 × 16 |
| 168 | 16/21 | 21 × 8 |
The derivation above treats starts as exact fractions such as 32/85 of a tile. The kernel cannot start in the middle of a batch. It counts the queue in batches and rounds each start down to a whole batch. Does that rounding change the count of positions? At 128K it does not, for two reasons.
- Blocks that share a position stay together. At 128K a tile is 2048 batches, so the queue for Qwen is 128 × 2048 = 262144 batches. Block 1 starts at 262144/340 = 771.01 batches and is rounded to 771. Block 86, in the same position one round later, starts at 66307.01 and is rounded to 66307. That is 32 whole tiles (65536 batches) plus the same 771. The exact starts differ by a whole number of tiles, and rounding down cuts the same fraction off both, so they still land on the same batch.
- Blocks at different positions stay apart. Two different positions are at least 1/b of a tile apart, that is at least 2048/b batches. With b at most 340, that is at least 6 batches. Rounding moves a start by less than one batch, so two different positions can never round to the same batch.
So the rounded starts keep exactly b positions with g blocks each, as long as a tile has at least b batches.
At short context that condition can fail. At 4K a tile is only 4096/64 = 64 batches, fewer than the 85 positions of 340 blocks, so some positions round to the same batch and merge. But then one layer’s K/V is only about 2176 × 4096 ≈ 8.9 MB, and it fits in L2 whatever the blocks do. Measured short-context speeds are indeed the same for all block counts.
5.2 Several K/V heads
Same position is not enough. The blocks must also be in the same K/V head, because different heads never share bytes.
Picture the T tiles laid end to end and coloured by head. Each colour is a run of T/H consecutive tiles, where H is the number of K/V heads times the number of sequences. By section 5.1, the g blocks at one position are blocks i, i+b, i+2b, …, one from each round. How far apart are they in the queue? Each block takes T/n tiles of work, so block numbers b apart start b × T/n tiles apart. Since b = n/g, that is (n/g) × T/n = T/g tiles. The g blocks therefore sit T/g tiles apart, and g steps of T/g cover exactly the T tiles of the queue. They are spread evenly along the whole queue, one every T/g tiles.
Two examples with Qwen (T = 128, 32 tiles per head):
- 340 blocks: g = 4, b = 85. Blocks 0, 85, 170 and 255 start at tiles 0, 32, 64 and 96, one every 128/4 = 32 tiles. A head is also 32 tiles long, so each of the four lands in a different head.
- 336 blocks: g = 16, b = 21. Blocks 0, 21, 42, … start at tiles 0, 8, 16, …, 120, one every 128/16 = 8 tiles. Each 32-tile head holds four of them.
The number of them that land inside one colour is:
s = (T/H) ÷ (T/g) = g / H, and s = 1 when g < H.
These s blocks read identical bytes at the same time. DRAM serves the data once for all of them, so the share of reads that go to DRAM is about 1/s.
| Model | T | H | g = 4 | g = 8 | g = 16 |
|---|---|---|---|---|---|
| Qwen | 128 | 4 | s = 1 | s = 2 | s = 4 |
| Llama | 256 | 8 | s = 1 | s = 1 | s = 2 |
At 340 blocks, g = gcd(340, 128) = 4, so s = 1 for both models. Every block reads its own copy of K/V. Nothing is shared.
5.3 A prediction, made before measuring
The model makes a testable prediction. For Qwen, g = 8 already gives sharing. For Llama, with twice as many K/V heads, it does not; sharing should start only at g = 16.
We wrote this prediction down before running the test. The result on the 5090 at 128K:
- 168 and 328 blocks (g = 8) were fast on Qwen but as slow as 164 and 340 on Llama.
- 144 and 336 blocks (g = 16) were 40% and 54% faster on Llama.
The threshold moved exactly as predicted.
6. A second kind of reuse: across time
Blocks of the same head that start close together can also reuse data with a delay. A later block may read a batch that an earlier block loaded a moment ago and that is still in L2. Whether it hits depends on the reuse distance: how much other data passes through L2 between the two reads. Roughly, that is the gap between the two starts, times the batches loaded per step, times the bytes per batch.
We wrote a simulator that replays every block’s reads in source order through an LRU cache. It splits hits into “same moment” and “later”. At 128K it agrees exactly with the s model of section 5. There is no reuse across time there, because the K/V is far larger than L2.
At medium context it matters. Llama’s head dimension is 128, so each batch is half the bytes of a Qwen batch, and L2 holds twice as many batches. Only Llama keeps this reuse at 32K–64K.
The cleanest pair is Llama at 328 and 340 blocks. Llama has T = 256 tiles, 8 heads and 32 tiles per head. Both counts have s = 1, so neither shares data at the same moment. Any difference between them must come from reuse across time.
How close the nearest neighbour in the same head is. Reuse across time needs two blocks in the same head whose positions inside a tile are close. Say block B is d batches behind block A. B then reads every batch d steps after A did.
Think of one tile as a dial with marks on it. A block’s position is the mark it starts on.
- 340 blocks: the dial has 85 marks, each 1/85 of a tile. Each block gets 256/340 = 64/85 of a tile of work, so the next block’s start moves 64 marks on. Four blocks move 256 marks, which is 3 full turns plus 1 mark. So blocks 0, 4, 8, 12, … land on marks 0, 1, 2, 3, …, one mark further every 4 blocks. Blocks 1, 5, 9, … climb the same way from mark 64, and so do the blocks starting with 2 and 3. Head 0 (tiles 0–31) holds 43 blocks, and they form 4 runs of consecutive marks: 0–10, 22–31, 43–53 and 64–74. Inside a run, neighbouring blocks are 1 mark apart, that is 1/85 of a tile. For example, block 0 starts at the beginning of tile 0 and block 4 starts 1/85 into tile 3: same head, 1/85 of a tile apart.
- 328 blocks: the dial has only 41 marks, each 1/41 of a tile. The 41 blocks of head 0 take each of the 41 marks once, so neighbours are 1 mark, 1/41 of a tile, apart. For example, block 0 is on mark 0, and its nearest neighbour, block 9 (starting 1/41 into tile 7), is on mark 1.
In both cases the nearest neighbour is 1 mark away. But a mark is 1/85 of a tile at 340 blocks and 1/41 at 328, about twice as large. So within a head, the nearest neighbour is about twice as far behind with 328 blocks.
Why distance decides the hit. While B is d steps behind A, every block reads d new batches, and with s = 1 every block reads its own data. So the data that passes through L2 between A’s read and B’s read is about d × n × the bytes of one batch. One Llama batch of one head is 64 rows × 128 dims × 1.0625 bytes × 2 (K and V) = 17408 bytes. At 48K a tile is 768 batches.
| Nearest gap at 48K | Data through L2 in between | |
|---|---|---|
| 340 blocks | 768/85 ≈ 9 batches | 9 × 340 × 17408 ≈ 53 MB |
| 328 blocks | 768/41 ≈ 19 batches | 19 × 328 × 17408 ≈ 108 MB |
The L2 is 96 MB nominally and behaves like about 64 MB in our fit. 53 MB fits, so B still finds A’s data in L2. 108 MB does not, so B goes to DRAM again. This is a rough estimate. It only counts the nearest neighbour and treats L2 as an ideal LRU. But the order is clear.
Measured. At 48K and 64K, 328 blocks are 32–33% slower than 340. The simulator gives the same order. At 128K the gap grows to 24 and 50 batches, about 142 MB and 286 MB, both beyond L2. That is why there is no reuse across time at 128K, and why only the s model matters there.
A limit: an ideal 96 MB LRU overestimates this reuse; about 64 MB matches the data. The real L2 is not an ideal LRU, and its drop-off is gentler than the simulator’s.
7. Counter evidence: it is DRAM traffic
Two checks confirm that L2 misses are real DRAM traffic.
First, dram__bytes_read cannot be read under WSL2, but fbpa__dram_read_bytes can. DRAM fills equal L2 misses, and the bytes match. At 160 blocks, for example: 66.5M missed sectors, 67.0M fills, 2.14 GB read from DRAM.
Second, every block count requests the same 16.7 GB from L2. What changes is how much of it has to come from DRAM: 90–97% at g = 4, about half at g = 8, about a third at g = 16. Up to g = 8, kernel time is proportional to DRAM bytes, at 1.05–1.35 TB/s (the peak is 1.79 TB/s). Fast and slow counts are both limited by DRAM. The slow ones simply move twice the bytes.
164 and 328 blocks make a clean pair here. Both have 41 start positions. 164 has g = 4 and an L2 hit rate of 3.6%. 328 has g = 8 and a hit rate of 49%, and its kernel time is half.
8. Two blocks on one SM do not run at the same speed
Sharing needs the blocks of a group to stay in step. To check that, we logged each block’s SM id, start time and end time with globaltimer.
With 2 blocks per SM, the two blocks on one SM are blocks i and i + (number of SMs). On the 5090 that is i and i + 170. They start together. But the lower-numbered block, the one launched first, finishes first. That held for 99–100% of such pairs at 320, 336 and 328 blocks, typically 0.5–1.7 ms earlier. The scheduler appears to favour the older block. The one exception was 340 blocks with s = 1, where the kernel is completely stuck on DRAM and the gap is only 14 µs.
This matters for any fix that puts two co-resident blocks into one sharing group: the group gets pulled apart. In today’s queue order, blocks i and i + 170 are 64 or more tiles apart, always in different heads. They never share, so the groups stay together. When we tried interleaving the K/V heads to create sharing, it put exactly such pairs into the same group, and the groups fell apart:
| Setup | Time spread inside a sharing group (median) |
|---|---|
| 160 blocks (1 per SM) | 0.6% |
| Today’s order, 336 / 328 blocks (2 per SM) | 2.6% / 0.6% |
| Today’s order, 320 blocks (20 SMs hold only 1) | 14.7% |
| K/V heads interleaved, 340 / 336 / 328 blocks | 17.6% / 51% / 39% |
The age priority is something we observed, not something a vendor document or a hardware counter confirms.
9. Three fixes compared
We tried three ways to restore sharing on the 5090:
| Fix | How sharing arises | Llama 32K / 48K | 128K | Problem |
|---|---|---|---|---|
| Choose n with gcd(n, T) ≥ 2 × H | depends on n | −3.0% / −3.4% | Qwen +40%, Llama +58% | sparser starts lose reuse across time; some SMs idle |
| Interleave K/V heads | depends on g and H | −3.9% / −3.0% | Qwen +33%, Llama +57% | co-resident blocks land in one group and drift apart |
| Grouped stream-k, m = 4 | s ≥ m by construction | +1.0% / +5.5% | Qwen +41.3%, Llama +74.7% | groups should line up with the SM count |
The first two rows were measured on prototype builds. The grouped row is the PR code (section 11.1).
Only grouped stream-k gains at 32K and 48K as well as at 128K.
10. Grouped stream-k
10.1 Design
The design has three steps.
Group the blocks. Every m consecutive blocks form a group. Block i is in group ⌊i/m⌋, and its member number inside the group is r = i mod m.
Split the tiles into m sub-queues. Cut the tiles, in order, into runs of m consecutive tiles. Since m divides the number of tiles per head, a run never crosses from one head into the next. Each run has m slots, numbered 0 to m−1. Sub-queue r is made of slot r of every run, in order. Run k’s slot r is tile k × m + r: k full runs come before it, and it is the r-th tile inside its own run. So sub-queue 0 holds tiles 0, m, 2m, …, sub-queue 1 holds tiles 1, m+1, 2m+1, …, and so on. All m sub-queues have the same length.
Each member works only on its own sub-queue. Member r of every group works on sub-queue r. Each sub-queue is cut evenly into as many pieces as there are groups, at the same points in every sub-queue. Group j takes piece j of each sub-queue, one piece per member. So the m members of a group cover the same stretch of their own sub-queues: the same run and the same K/V batches, each in a different tile of that run.
In ungrouped stream-k, neighbouring blocks line up one after another along the queue and start at different points. In grouped stream-k, the m blocks of a group stand side by side on m neighbouring tiles and move forward together.
10.2 Why s ≥ m
Any point in a sub-queue can be described by two numbers: which tile of the sub-queue it is in (the k-th), and which K/V batch of that tile it is at. Sub-queue r’s k-th tile is slot r of run k.
The m members of a group start at the same point of their own sub-queues, because every sub-queue is cut at the same places. They also move at the same pace, one batch per step. So at every step they are at the same k and the same batch.
- Same k means the same head. The members are in slots 0 to m−1 of the same run k. A run never crosses into another head, so all m members are in the same head.
- Same batch means the same rows of K/V. Inside that head, they all read the same 64 rows.
Same head and same rows mean the same bytes. So at every step, all m members read the same K/V data.
When they reach the end of a tile, they all move on to run k+1 at the same step. Run k+1 may belong to the next head, but the members still go there together, so they stay in the same head as each other.
This gives at least m blocks reading the same data. Different groups that happen to line up with each other share even more, which is why the bound is “at least m”.
This holds for any block count, any GPU and any model. It no longer depends on gcd(n, T).
10.3 Co-resident blocks stay apart
Members of a group are consecutive blocks, so the first wave puts them on different SMs. The two blocks on one SM are i and i + (number of SMs). As long as the SM count is at least m, they are in different groups.
We checked every configuration we tried, pair by pair: the 5090, 5080, 5070 Ti, H100 and 4090 shapes, both models, m = 2 and m = 4. No co-resident pair starts in the same head at the same position. If some configuration ever lined up, it could be detected at launch and shifted, for example by rotating member numbers.
There is one exception in our traces. On the 5090, group 42 is blocks 168–171. Blocks 168 and 169 are in the first residency slot and 170 and 171 in the second, because 170 is not a multiple of 4. This was the only group that drifted (about 3.6 ms against 4.75 ms). Aligning groups with the SM count would remove it; we have not implemented that yet.
10.4 When to group
On the H100, the existing rule already rounds 264 blocks down to 256 (a 3% loss), so s = 32 for both models. The model predicts no gain from grouping, and we measured −0.4% to −1.1% at every depth.
So grouping should apply only when it can help. The PR code (#32459) groups, with m = 4, only when all three of these hold:
- The split is fractional. The block count neither divides the tile count nor is a multiple of it, so some blocks start inside a tile.
- 4 divides the block count and the number of tiles per K/V head. Groups need whole sets of 4 blocks, and a run of 4 tiles must not cross into the next head.
- The default sharing is below m. gcd(n, T) < 4 × H, that is, s < m.
The H100’s 256 blocks are a multiple of its 128 or 256 tiles, so the first condition already keeps it on the existing path. The 5090 (s = 1), the 5070 Ti (s = 1) and the 5080 (s = 1 for Llama, s = 2 for Qwen) group.
If the block count is not a multiple of 4, the code rounds it down to one. It does this only when the split is fractional, and only within the same 5% loss limit as the existing rounding rule. 340, 168 and 140 are already multiples of 4, so on these cards the block count does not change.
10.5 Implementation notes
- Both places in the kernel that decode a work index first map the sub-queue unit back to the global tile.
- The general fixup splits the work the same way as the kernel. It walks back to the same member’s previous block, i − m, instead of i − 1.
- The uniform fixup path is unchanged. It is used when the block count divides the tile count, and in that case the code does not group.
- m is a compile-time constant (4). The kernel and the fixup each compute the group size from the launch shape with the same function, so they always agree. An earlier attempt passed m through a host-set
__device__variable and failed: for some shapes, the kernel and the host code are in different compilation units. The kernel read 1, the fixup used 2, and the correctness test failed. - The kernel computes the group size and member number again each time it forms a tile start, instead of keeping them for the whole tile loop. Keeping them made the kernel 1.3–2.9% slower than master even for shapes that do not group. Computing them at each use brings that cost down to at most 0.6%.
- On HIP and MUSA the group size is always 1. Only NVIDIA GPUs group.
11. Results
11.1 RTX 5090, by context depth
All numbers are prefill tokens per second from
llama-bench -m <model> -ngl 99 -fa 1 -ctk q8_0 -ctv q8_0 -b 2048 -ub 512 -p 512 -n 0 -d 0,8192,32768,49152,131072 -r 6
Each record has 6 samples. The first (warm-up) is dropped and the median of the other 5 is taken. Master and the change alternate over three rounds, and the rounds are averaged. Each binary loads its own libraries, checked with ldd. Both builds are on llama.cpp master a946eb541. The change in this table is the first version of the PR code. The final version only changes where the kernel computes the group size; its kernel timings are within 0.5% of the first version, and section 11.2 compares the two on whole runs.
| Model | Depth | master | grouped | Speedup | Per round |
|---|---|---|---|---|---|
| Qwen3.5-9B | 0 | 13286 | 13223 | 0.995 | 1.005 0.994 0.987 |
| Qwen3.5-9B | 8K | 12274 | 12067 | 0.983 | 0.961 0.999 0.989 |
| Qwen3.5-9B | 32K | 9317 | 9852 | 1.057 | 1.061 1.058 1.053 |
| Qwen3.5-9B | 48K | 7214 | 8743 | 1.212 | 1.215 1.211 1.209 |
| Qwen3.5-9B | 128K | 3910 | 5525 | 1.413 | 1.415 1.411 1.413 |
| Llama-3.1-8B | 0 | 14453 | 14330 | 0.991 | 0.983 0.991 1.000 |
| Llama-3.1-8B | 8K | 11111 | 11080 | 0.997 | 0.993 0.996 1.002 |
| Llama-3.1-8B | 32K | 6324 | 6386 | 1.010 | 1.004 1.016 1.009 |
| Llama-3.1-8B | 48K | 4738 | 4996 | 1.055 | 1.045 1.061 1.058 |
| Llama-3.1-8B | 128K | 1316 | 2299 | 1.747 | 1.754 1.745 1.742 |
At 128K, Llama is 1.747× and Qwen 1.413×, close to the prototype (1.775 and 1.426). At 32K and 48K the gain is 1.0% to 21.2%.
At depth 0 and 8K the averages are 0.983 to 0.997. Single rounds fall on both sides of 1, from 0.961 to 1.005. Inside one record, the samples spread by up to 10.6% at depth 0. So these four cells cannot resolve a difference of about 1% in either direction. Grouping does apply there: a debug build shows 340 blocks on 256 tiles for Llama at depths 0 and 8K.
The prototype was also traced at 128K. The share of L2 reads that go to DRAM fell from about 90% to 23.6% for Qwen (the simulator predicts 25%) and to 17.7% for Llama. 72 of 85 groups for Qwen and 81 of 85 for Llama stayed within 5% of each other in time. At every depth from 8K to 128K, the prototype matched or beat the best hand-picked block count.
11.2 The maintainer’s test matrix
The CUDA maintainer measured an earlier stream-k change (llama.cpp PR 29435) with long prompts, large microbatches and small models. The first version of that change lost 6–8% on an RTX 5090 in this matrix. We ran the same matrix on the PR code, with both f16 and q8_0 K/V:
llama-bench -m <model> -ngl 99 -fa 1 -ctk <kv> -ctv <kv> -b 8192 -ub 1024,2048,4096,8192 -p 65536 -n 0 -r 3
Each record has 3 samples. The first is dropped and the median of the other 2 is taken. Three builds (master, the first PR version, the final PR version) rotate over three rounds. The models are public GGUF files: Llama 3.2 1B (Q4_0), gemma 2 2B (Q4_K_M), Llama 3.1 8B (Q4_K_M), Qwen3.5 9B (Q4_K_M) and gpt-oss 20B (MXFP4). A debug build shows that all 20 (model, microbatch) pairs group, always with 340 blocks, on 128 to 8192 tiles.
Final PR version over master, each cell as f16 / q8_0:
| Model | ub 1024 | ub 2048 | ub 4096 | ub 8192 |
|---|---|---|---|---|
| gemma 2 2B | 1.150 / 1.157 | 1.091 / 1.113 | 1.060 / 1.069 | 1.043 / 1.049 |
| Llama 3.1 8B | 1.034 / 1.031 | 1.027 / 1.026 | 1.095 / 1.097 | 1.033 / 1.033 |
| Qwen3.5 9B | 1.064 / 1.085 | 1.048 / 1.062 | 1.037 / 1.044 | 1.027 / 1.026 |
| gpt-oss 20B | 1.025 / 1.018 | 1.041 / 1.030 | 1.041 / 1.037 | 1.051 / 1.049 |
| Llama 3.2 1B | 0.982 / 0.988 | 0.975 / 0.981 | 0.997 / 1.000 | 0.995 / 1.003 |
All 32 cells of the four larger models are faster, by 1.8% to 15.7%.
Llama 3.2 1B is the exception. At microbatch 1024 and 2048 it is 1.2% to 2.5% slower. At 4096 and 8192 it is within 0.5% of master. Nsight Compute shows that grouping lowers its L2 hit rate, from 94.5% to 87.4%. Llama 3.1 8B at microbatch 1024 has the same tile layout (512 tiles, 64 per K/V head, 8 K/V heads, 340 blocks) and is 3% faster. So the block and tile counts alone do not tell the two cases apart, and we have no rule yet that predicts the loss. It is within the 5% loss that the existing block-count rule already accepts.
The final version over the first PR version is 0.995 to 1.018 per cell. It helps gpt-oss 20B by 0.4% to 1.8% and is within 0.5% on the other four models.
11.3 Correctness and other checks
- Correctness.
test-backend-ops -o FLASH_ATTN_EXT -b CUDA0passes 4426/4426 on master and on the final PR code (RTX 5090). With a debug print, 28 of the 3871 stream-k launches in that test take the grouped path, with 340 and 168 blocks. The prototype passed 4100/4100 on every card, on the older master it was built on. - AMD. Grouping is off on HIP. We ran upstream’s compile-only
hip-quality-check(ROCm 7.2.1, gfx908), and gfx942 also compiles with-Werror. 1268 of the 1284 FlashAttention kernels use the same number of VGPRs as master; the others differ by −9 to +6. We did not run on AMD hardware. - MUSA. The group size is always 1. Not tested; we have no MUSA hardware.
- Group size (prototype). At 128K on the 5090, m = 2 is about 6% slower than m = 4 (Qwen −6.4%, Llama −5.7%). m = 8 is within 1% of m = 4.
- Scope. On NVIDIA, only Ada (compute capability 8.9) and newer use stream-k here. An RTX 3090 does not, and its speed is unchanged (prototype).
11.4 Cards not used in the design (prototype)
The RTX 5080, RTX 5070 Ti and H100 were not used while designing the fix. They test whether it generalizes. They were measured on the prototype, not on the PR code. The prototype grouped whenever s < m; the PR code adds the conditions of section 10.4. For these shapes both rules make the same decision on all three cards: the 5080 and 5070 Ti group, the H100 does not. But the PR code itself has not been timed on them.
| GPU (default blocks) | Model | 8K | 32K | 64K | 128K |
|---|---|---|---|---|---|
| RTX 5080 (168) | Qwen | 1.000 | 1.026 | 1.046 | 1.069 |
| RTX 5080 (168) | Llama | 1.001 | 1.545 | 1.729 | 1.828 |
| RTX 5070 Ti (140) | Qwen | 1.000 | 1.181 | 1.280 | 1.404 |
| RTX 5070 Ti (140) | Llama | 0.994 | 1.093 | 1.545 | 1.614 |
| H100 (256) | Qwen | 1.002 | 0.992 | 0.995 | 0.989 |
| H100 (256) | Llama | 0.994 | 0.996 | 0.993 | 0.990 |
The 5080’s gain on Qwen is modest because its 168 blocks already have s = 2. On Llama it starts from s = 1. The H100 row is the case that section 10.4 leaves on the existing path.
12. Prior art
We reached grouped stream-k on our own, from the measurements above. The same grouping already exists elsewhere, and any write-up of this fix must cite it.
- CUTLASS 3.x stream-K GEMM scheduler.
tile_scheduler_params.hdivides stream-K units into G groups to preserve L2 locality, so that the first unit of each group computes the same K range.sm90_tile_scheduler_stream_k.hppsets group = linear index mod G and tile = tile in group × G + group: G neighbouring CTAs walk G neighbouring tiles in lockstep. This is the same construction. In CUTLASS, G is at most 8 and must divide the unit count, so sharing still depends on divisibility. CUTLASS also hands only the last one or two waves to stream-K. - The Stream-K paper (Osama et al., 2023, arXiv 2301.03598), section 5.2. It notes that the even split’s offsets can prevent reuse across CTAs: when the grid size does not divide the tile count, CTAs start at different k offsets. Its remedy is to give stream-K only a remainder of one or two tiles.
What this work adds:
- a quantitative sharing model for attention with several K/V heads, s = gcd(n, T)/H, and its confirmed advance prediction (Llama’s threshold moving from g = 8 to g = 16);
- the observation that, of two blocks on one SM, the older one runs faster, which pulls apart sharing groups that contain such a pair;
- the trade-off between sharing at the same moment and reuse across time;
- an evaluation at attention scale: the prototype on two models and four stream-k cards, three of them held out, and the PR code on the RTX 5090 across five models.