Can sparse prefill speed up long prompts on Qwen3.8-27B and keep its quality?
Before a local model writes its first token, it has to process the whole prompt. With a 256k token prompt, Qwen3.8-27B spends about two and a half minutes on that prefill on an RTX 5090, even with the recent upstream llama.cpp we tested. We built block sparse prefill into our llama.cpp fork: each block of the prompt attends to the start of the context, its recent neighbours and the earlier pages most likely to matter, and skips the rest. Together with the page sparse decode from our Sparse Decode Bench note, one 256k request with a 1,000 token answer took 107 seconds through llama-server, against 172.5 seconds for the fastest dense llama.cpp we measured: 1.61 times faster. At 256k it stayed inside a RULER quality gate we fixed before the run, but only after a fix to our own checker that we made after seeing the data; under the rules as first registered, the quality test gives no decision.
Short answer
- End to end at 256k. On an RTX 5090, one request of 259,525 prompt tokens and 1,000 generated tokens took 107.0 seconds with sparse prefill and sparse decode, against 172.5 seconds for the fastest dense setup, upstream llama.cpp. Against our fork's own dense path it was 2.31 times faster.
- The gain grows with length. At 128k the same setup was 1.27 times faster on the RTX 5090, and 1.33 times on an RTX 4090 (speed only; no RULER ran on that card).
- Upstream got faster, and the gain held. Upstream's dense prefill had become much faster than our fork's base. In a development run after we rebased the sparse path onto it, our dense prefill matched upstream's and the sparse times stayed close to the earlier run's: 1.65 times faster at 256k and 1.28 at 128k.
- Quality at 256k. The combined setup lost 2.40 RULER points, with a 95% interval of −4.06 to −0.88, inside our gate of 3 points. That pass needs the checker fix made after the data; without it, the rule we registered gives no quality decision.
- Quality at 128k. With an f16 KV cache it cost 1.60 points with BF16 weights and 1.65 with UD-Q4_K_M.
- A lesson about thinking models. Our first BF16 run on raw RULER prompts ended without a decision. Its raw outputs showed Qwen3.8 sometimes starting to think, only to be cut off by RULER's short answer budget, and returning more empty answers. With the chat format and thinking switched off, BF16 and UD-Q4_K_M gave the same dense score on all 260 prompts (a check made after the data).
How it works
Qwen3.8-27B has 64 layers. 16 of them use full attention over a KV cache of the full length; the other 48 are Gated DeltaNet layers with a fixed size state. Both sparse methods act only on the 16 full attention layers.
- Sparse prefill. The prompt is processed in blocks of 64 tokens. For each block and KV head, attention keeps the first page of the KV cache (a page holds 64 cached tokens, or cells), the 512 cells just before the block, the block itself, and the best 10% of the other earlier pages, rounded up. Pages are scored with the Quest bound from per page key minima and maxima, normalised per query token, taking the maximum over the block's tokens and the query heads that share the KV head. The last 512 prompt tokens, where RULER puts the question, run with full attention. Nothing is trained.
- Sparse decode. As in the Sparse Decode Bench note: during generation each KV head reads the first page, the most recent tokens and the best scoring pages, within a budget of 27,648 cells.
The prototype gathers the selected cells through a CUDA sparse gather path in our fork, rather than a dedicated block sparse attention kernel, with a microbatch of 512 tokens (-ub 512). The page selection runs on the GPU as part of prefill. The end to end runs use a newer version of the gather that builds a list of the selected pages and converts only their cells when the KV cache is q8_0. On both timed prompts its logits were byte identical to the earlier path's for 32 steps. That checks the gather change on those inputs; quality comes from the separate RULER runs.
End to end speed
We timed one long request through llama-server: two public domain books from PG19 with a request to summarise them, 259,525 prompt tokens at 256k and 129,526 at 128k from a shorter part of the same text, then 1,000 generated tokens with greedy decoding. The model was unsloth's Qwen3.8-27B UD-Q4_K_M with a q8_0 KV cache; an f16 cache would not fit at 256k on a 32 GB card, by our estimate, and was not run there. Dense ran in five ways: our fork and upstream llama.cpp (commit c6a25fc62), each with and without MTP speculative decoding, and our fork with 2048 token microbatches. The fastest of them is the baseline. Each row ran twice in each of two blocks, and the table shows the lower block's speedup.
| GPU | Prompt | Fastest dense | Dense | Sparse prefill and decode | Speedup |
|---|---|---|---|---|---|
| RTX 5090 | 256k | upstream | 172.5 s | 107.0 s | 1.61x |
| RTX 5090 | 128k | upstream with MTP | 67.3 s | 53.0 s | 1.27x |
| RTX 4090, 330 W | 128k | upstream with MTP | 102.6 s | 77.2 s | 1.33x |
Times are the median of the two requests in the block that gave the speedup, prompt and generation together, measured at the client. On the RTX 4090, our fork with MTP was the fastest dense in the other block (102.4 seconds), with the same speedup.
- Most of the gain is prefill. At 256k the first token came after 92 seconds instead of about 145. Sparse prefill with dense decode was already 1.42 times faster end to end; sparse decode then took generation from about 37 tokens per second with upstream dense to 68.
- The speedup grew by 0.34, from 1.27 at 128k to 1.61 at 256k, more than the 0.3 we set in advance as the line for "grows with length".
- We had predicted 2.2 times at 256k and 1.55 at 128k, and missed both. We expected our fork's dense path to be the baseline, but, as we saw during the run, upstream's dense prefill had become much faster than our fork's base on this card: 145 seconds to the first token at 256k, against 218. Against our fork's own dense, an informative comparison, the request was 2.31 times faster.
- The RTX 4090 ran at a 330 W power limit instead of its stock 450 W. There, our fork and upstream were level at dense prefill, and the 1.33 times is inside the 1.10 to 1.35 we predicted.
- Doubling the decode budget to 55,296 cells cost little speed: 1.59 times at 256k.
Upstream moves fast, so we checked whether its faster dense path takes away our gain. We rebased the sparse path onto the same upstream commit and timed it again on another RTX 5090, one block with two repeats, as a development run rather than a preregistered test. Our fork's dense prefill now matched upstream's, 150 seconds to the first token at 256k, while the sparse times stayed within 0.6% of the earlier run at 256k and within 1.3% at 128k. Against upstream dense the combined setup was 1.65 times faster at 256k (177.3 to 107.3 seconds) and 1.28 times at 128k (68.7 seconds with MTP, to 53.5). Dense on this card was 2 to 3% slower than in the registered run, which lifts these ratios slightly. Our reading is that upstream's speedup is in its dense attention kernel, which the sparse path does not use.
Quality at 256k
We ran RULER at 256k, 13 tasks with 20 prompts each, from 222,523 to 261,600 tokens, in Qwen3.8's chat template with thinking switched off, with greedy decoding, UD-Q4_K_M weights and a q8_0 KV cache, on two RTX 5090s. The gate was the one we used at 128k: lose at most 3 points on average over the 13 tasks, with the lower end of the paired 95% interval at −6 or above.
| Configuration | Change against dense |
|---|---|
| Dense score | 95.26 |
| Sparse decode only | −1.73 (−3.27 to −0.46) |
| Sparse prefill only | −1.73 (−3.04 to −0.73) |
| Both, gated | −2.40 (−4.06 to −0.88) |
| Both, decode budget doubled | −1.88 (−3.37 to −0.50) |
Changes are RULER points against dense on the same 260 prompts, with 95% intervals. Only the gated row decides; the doubled budget was the registered fallback.
- The gated setup passed under amendment 4, the checker fix below, written after the data; the score thresholds did not change. Under the rules as first registered, the quality result is invalid and gives no decision. The loss is inside the −5 to +1 we predicted. It lost 16.5 points on common words extraction, 5 on each question answering task, 3.8 on the multi value needle task and 1 on variable tracking; the other eight tasks did not change.
- Dense scored 95.26, above the 70 to 92 we had predicted. No output was empty or contained thinking text.
- The pass needed a fix written after the data. One prompt was 256,002 tokens long, so its prefill ended with a microbatch of a single token. The engine runs that token with full attention and does not count it as a sparse step. Our count checker expected it to be counted, marked that unit of 10 prompts invalid and ended that card's run. We changed the checker to match the engine and checked the unit again from its saved outputs, without a rerun, and Codex reviewed the change. The speed results do not depend on it.
- Each half of the prompts ran on its own card. The first half alone lost 2.96 points, with a lower bound of −6.04, just past the line.
- Read after the data: sparse decode alone lost 0.38 points at 128k but 1.73 at 256k. Its budget is a fixed 27,648 cells, about 21% of a 128k context but about 11% of a 256k one, and with the budget doubled the combined loss fell from 2.40 to 1.88 points, a difference well inside the intervals. The 128k runs also used an f16 cache rather than q8_0, so this does not isolate the budget. A budget that grows with the context is untested.
Quality at 128k
We ran RULER at 128k, 13 tasks with 20 prompts each, in Qwen3.8's chat template with thinking switched off, with greedy decoding and RULER's own answer lengths. Weights were unsloth's BF16 and UD-Q4_K_M files, with an f16 KV cache (llama.cpp's default), on one RTX PRO 6000 Blackwell Workstation Edition with 96 GB. Every prompt ran dense, with each sparse method alone, and with both. The gate, fixed before the run: lose at most 3 points on average over the 13 tasks, with the lower end of the paired 95% bootstrap interval at −6 or above.
| Configuration | BF16 | UD-Q4_K_M |
|---|---|---|
| Dense score | 95.77 | 95.77 |
| Sparse decode only | −0.38 (−1.15 to 0.00) | −0.38 (−1.15 to 0.00) |
| Sparse prefill only | −1.21 (−2.17 to −0.58) | −1.27 (−2.25 to −0.60) |
| Both, gated | −1.60 (−3.25 to −0.15) | −1.65 (−3.25 to −0.17) |
Changes are RULER points against dense on the same 260 prompts, with 95% intervals. Only the last row was gated.
- Both precisions passed, as we predicted, and lost almost the same amount.
- The largest loss is on common words extraction, which counts words across the whole context: 7 task points at BF16 and 9 at UD-Q4_K_M. We predicted that too. It comes from sparse prefill: prefill alone scores the same on this task as both together, and decode alone keeps it at 100. Read after the data, 12 prompts at BF16 and 14 at UD-Q4_K_M dropped below 100 on this task, most of them finding 9 of 10 words.
- For the combined configuration, the other losses are 5 points on the second question answering task at both precisions, 5 on the first at UD-Q4_K_M, 5 on the third multi key needle task at BF16, and 3.75 (BF16) and 2.5 (UD-Q4_K_M) on the multi value needle task. Variable tracking stays at 100.
- Every one of the 2,080 outputs was a direct answer, with no
<think>text and no empty output. - Dense scored 95.77, above the 86 to 94 we had predicted.
Prefill alone at 128k
| GPU | Model and weights | Dense prefill | Sparse prefill | Speedup |
|---|---|---|---|---|
| RTX PRO 6000 | Qwen3.8-27B, BF16 | 49.8 s | 35.3 s | 1.41x |
| RTX PRO 6000 | Qwen3.8-27B, UD-Q4_K_M | 51.5 s | 37.4 s | 1.38x |
| RTX 5090 | Qwen3.8-27B, UD-Q4_K_M | 63.8 s | 40.9 s | 1.56x |
| RTX 5090 | Qwen3.6-35B-A3B, UD-Q4_K_M | 18.7 s | 14.1 s | 1.34x |
These earlier runs timed prefill only, not a full request with generation, with sparse prefill alone and an f16 KV cache on 7 RULER prompts of about 128k tokens per model, through our test tool with graph reuse off. Their dense baseline is our fork's base, not upstream llama.cpp. Times are medians; the speedup is the median of the per prompt ratios, so it can differ slightly from the ratio of the two times. The sparse time includes the page selection. Both runs were preregistered with a pass line of 1.20 times. Each was repeated in two blocks whose speedups agreed to within 0.003, and the table shows the lower block. The Qwen3.6-35B-A3B row is for comparison; this note's quality results are for Qwen3.8-27B.
- On the RTX 5090, Qwen3.8-27B came out faster than we predicted (1.05 to 1.45 times). Sparse prefill saves 36% of its prefill time, against 25% for the mixture of experts model.
- On the RTX PRO 6000, dense prefill was slightly faster with BF16 weights than with UD-Q4_K_M, and the speedup held at full precision: 1.41 times, inside our predicted 1.25 to 1.80. UD-Q4_K_M on that card, at 1.38 times, came in just below our predicted 1.40 to 1.75.
The end to end runs above used the newer gather path, with page lists and selective q8_0 conversion.
A lesson for RULER on thinking models
Our first BF16 run used RULER's raw prompts with no chat template, as our earlier runs on Qwen models had. It ended without a decision: the BF16 dense baseline fell below the floors we had set before the run, on variable tracking (88 against 90) and the first question answering task (25 against 50).
Reading the raw outputs afterwards pointed to a likely cause. With a raw prompt, Qwen3.8 decides on its own whether to open a <think> block, and RULER's answer budgets of 30 to 128 tokens often cut it off. 29 of 260 BF16 dense outputs were cut off thinking, and 19 were empty, 9 of them on the first question answering task where the floor failed. Our earlier UD-Q4_K_M run, with a q8_0 cache on an RTX 5090, had about as much cut off thinking (27) but only 4 empty outputs. Cut off outputs scored 57.2 and 37.0 on average, against 97.5 and 97.7 for direct answers, though these are subsets selected by output kind, and the BF16 direct answers include no question answering prompts. That earlier run passed its own gate, but part of its Qwen3.8 score measured whether the model chose to think within the budget, not retrieval alone.
With the chat format and thinking switched off, dense scored 95.77 at both precisions, against 85.46 for BF16 on raw prompts, and, read after the data, the two weight formats gave the same score on every one of the 260 prompts (identical tokens on 210). The raw prompt run also used other cards, drivers and power limits and a different order of work, so this comparison does not isolate the prompt format as the cause.
If you run RULER on a model that can think, read the raw outputs, not only the scores, and look for cut off thinking and empty answers. The chat template with thinking switched off is what worked for us; a longer answer budget with thinking on is another option we have not tested. In hindsight, the cut off thinking was already visible in our earlier UD-Q4_K_M run's raw outputs before we rented the cards for the BF16 run.
Limits
- Quality was measured on one model with 20 prompts per task, at two lengths, with an f16 cache at 128k and a q8_0 cache at 256k. RULER is close to saturated for this model: dense scores 100 on 11 of 13 tasks at 128k and 10 of 13 at 256k, where only a loss can show. At 256k, RULER's first question answering task puts the same documents in every prompt, by RULER's design; without that task the loss is 2.19 points. A harder benchmark or long generated answers could show losses this one cannot.
- Speed was measured on one RTX 5090 at a 500 W power limit (below its default) and one RTX 4090 at 330 W, with one model file, one book prompt per length, 1,000 generated tokens and one request at a time. The rebase result is a development run. The timed summaries were not scored for quality. No RULER ran on the RTX 4090, and our 128k quality runs used an f16 cache, while the 128k timings used q8_0.
- In the server, the full attention on the last 512 prompt tokens is set from the known prompt length by an environment variable: a prototype control, not a server feature. The prefill is a prototype path in a fork, CUDA only, not a dedicated kernel. It saves no VRAM: at 256k the sparse setup peaked at 26.7 GiB on the RTX 5090, against 25.0 GiB for upstream dense.
- Untested: thinking switched on, an f16 cache at 256k, a q8_0 cache with the chat format at 128k, other prefill fractions, windows and microbatch sizes, a decode budget that grows with the context, prompts with the question before the context, batched serving, prompt caching and multi turn use, and other models and cards.
How it was tested
Each registered test was written down and committed before its data, with its gate, predictions and the consequence of each outcome, and Codex reviewed the protocols before the runs and the decisions afterwards. The rebase measurement is a development run, not one of them.
The 256k runs used four rented cards in parallel: speed on an RTX 5090 and an RTX 4090, and RULER split across two RTX 5090s. Three amendments came before the data they cover: a lower RAM floor for the RTX 5090 boxes, whose offers had 30 GiB, and two relaunches of the RTX 4090 run after network failures during its setup. The fourth, the checker fix above, came after the data. Every setup gate passed, and every configuration in the two block speed comparison gave the same output in all four of its timed requests.
In the 128k quality run, one of the two rented cards produced corrupted outputs, runs of a single repeated token, in 13 of its 120 prefills, dense and sparse alike, which did not repeat on identical inputs. Its cause was not established. We excluded that card by an amendment written after its data existed and reran its half of the prompts on the other card, so the predicted completion on both cards only partly held. Two early checks, the format screen and the first task dense check, were read late for both formats, though all passed. A diagnostic process also shared the card with two accepted BF16 task units, which slowed down; their scores were all 100, but they were not rerun on their own.
The protocols, raw results and analysis code are in our research repository, which is not public yet. The work was built with a lot of help from Claude Code, and Codex for reviews and corrections. It is independent and has not been peer reviewed.