The llama.cpp team merged a patch on November 17 that changes GPU data read order. It speeds up processing of additional input when a long conversation context already exists.
In the PR author's test, an RTX 5090 with Qwen3.8-27B Q4_K_M processed a new 512-token prompt with 128K of existing context 1.41x faster than the baseline branch. The author attributed this to better reuse of L2 cache, fast memory shared by GPU blocks, reducing repeated GPU memory reads.
Results
Table: Prompt processing speed by context depth
Conditions: llama.cpp PR #32407 measurements; 1 NVIDIA GeForce RTX 5090; Qwen3.8-27B-UD-Q4_K_M.gguf; n_prompt 512; n_gen 0. n_depth is existing context length; PR branch versus Master; t/s values are reproduced unchanged.
Unit: t/s
| n_depth (tokens) | Master | PR | Speedup |
|---|---|---|---|
| 131072 | 1290.10833 | 1822.36027 | 1.41 |
| 65536 | 2030.31282 | 2527.97409 | 1.25 |
| 32768 | 2931.96118 | 3142.20539 | 1.07 |
| 16384 | 3562.18311 | 3573.59336 | 1.00 |
| 8192 | 3854.83184 | 3850.97547 | 1.00 |
| 4096 | 3974.20194 | 4015.85501 | 1.01 |
| 2048 | 4079.23891 | 4077.58068 | 1.00 |
| 1024 | 4103.79141 | 4123.60737 | 1.00 |
| 512 | 4114.42862 | 4136.20591 | 1.01 |
| 0 | 4300.67808 | 4323.0908 | 1.01 |
Gains grow with context length. At 16K or below, the relative difference from Master was at most about 1.05%; it becomes noticeable at 32K and widens beyond that. Both builds perform similarly when processing only new tokens without context.
Measurement method
The PR provides this reproduction command: llama-bench -m Qwen3.8-27B-UD-Q4_K_M.gguf -d 131072,65536,32768,16384,8192,4096,2048,1024,512,0 -n 0. The -d, n-depth option specifies existing context length, while -n 0 disables generation, leaving only prompt processing. With no repetition option specified, llama-bench used its default of 5 runs.
The test used a single NVIDIA GeForce RTX 5090, with the model type listed as qwen35 27B Q4_K - Medium and n_prompt set to 512. It compared master and the PR branch in the same repository.
Why the difference occurs
The PR author identified stream-K, which splits tile processing across multiple blocks, as the cause. Blocks sharing the same K/V cache, which stores attention keys and values, read different tiles at the same time. The author said this reduces L2 cache reuse and can cause the same bytes to be read from GPU memory repeatedly.
Flash attention divides attention computation into small tiles and reuses intermediate results to reduce memory traffic. The patch rotates the K/V tile read order in FP16 MMA flash attention, which uses half-precision matrix multiplication units. It makes blocks processed together read the same K/V block, enabling sharing in L2.
Repository issue #30784 reports long-context slowdowns. It covers llama.cpp b11674, CUDA 13.4.59, driver 595.91.07, and Linux, using Qwen3.5-9B and Qwen3.8-27B. It reports that the RTX 5090 was slower than the RTX 4090 at prefill, or prompt processing, with long accumulated contexts. The 5090 was faster in all token-generation tests and about 20% faster with short contexts. With flash attention disabled, it was 1.6x faster at 128K, but fell behind only on the FA, or flash attention, path.
The issue author suspected the block count calculation in this configuration. With the FA kernel running 2 blocks per SM, a GPU compute unit, stream-K uses twice the SM count: 256 on the 4090 and 340 on the 5090. For the 9B and 27B model shapes discussed, the 27B tile count is 256. Since 340 is not a multiple of either 128 or 256, the author suspected that some blocks cross tile boundaries and take the generic cleanup path.
The issue author tested Qwen3.8-27B UD-Q4_K_M with q8_0 KV cache, pp512, and 128K-depth prefill. The 5090 reached 1224 t/s versus 1403 t/s on the 4090. Setting the block count to 256 raised it to 1704 t/s, while the issue's proposed patch reached 1710 t/s.
The author also reported results from a local llama-server build with the issue's proposed patch. Processing an approximately 98K-token prompt with Qwen3.8-27B Q6_K fell from 51.3 seconds to 44.7 seconds.
A comment on the issue reported tests on an RTX 5080 with 84 SMs, CUDA 13.3, and a native Windows build. It used Qwen3.5-9B-Q4_K_M with pp512 and q8_0 KV at 128K depth. Forcing 256 blocks was 24% slower than the default 168 blocks, with only one 128K measurement per configuration. The issue remains open.
Another approach appeared in the repository the same day. PR #32459 proposes grouped stream-K to preserve K/V sharing in L2. It references issue #30784 and has not yet been merged.
Choosing whether to upgrade
A new build may be worthwhile for workloads that append new prompts to existing context. The author recorded clear gains with long accumulated contexts. Improvements for batch input that supplies a long prompt all at once require separate measurement. Applying the change requires a new CUDA build; it entered official builds starting with release b12477.
These figures come from a single RTX 5090 and one 27B model, so the same results cannot be assumed for other GPUs or models. The author observed regressions when applying rotation to single-stage kernels. Rotation was therefore limited to multi-stage kernels, which prefetch K/V in multiple stages. This multi-stage loading works only when cp.async, the GPU asynchronous copy instruction, is available on NVIDIA Ampere or newer architectures.
Even fast compute hardware can process long inputs slowly when read order prevents cache reuse, as shown by the author's 1.41x speedup after changing read order on an RTX 5090 at 128K context.
