Hi everyone!
Two days ago I introduced --kv-select: instead of reading the whole KV cache held in RAM for every token, anaelith reads only the blocks that matter for the current query. Qwen3.8-27B went from 8 to 77 t/s at 242K of context on the Olares One.
With one flaw, found while preparing the Basalt pull request: in free generation, the model sometimes copied a passage of the prompt word for word. One continuation in six, on long books. Never when reading the whole cache.
I spent two days on that flaw. Here is what I found, what did not work, and the improvement that goes into the PR.
The test bench
Every measurement in this post uses the same setup:
- Qwen3.8-27B (QUASAR NVFP4 pack) on anaelith, int8 KV cache,
--kv-resident 16384: only 16K tokens stay on the card, the rest is in RAM; - six books from the PG19 corpus, read to 100,000 or 200,000 tokens;
- teacher-forced: the book’s real continuation (2,047 tokens) is imposed and the model’s probabilities are compared with and without the selection (KL divergence, and how often the top token is the same);
- free generation: the model writes 400 tokens after the book, and I count the share of its reply found as-is in the prompt (16-token runs).
Lead 1: it is greedy decoding
My first free generations were greedy (temperature 0): at each step the model takes the most likely token. It is the usual setting to compare two engines, because it is supposed to be deterministic.
I redid them with sampling (temperature 0.7, top-p 0.8, the recommended settings for Qwen without reasoning), five different seeds, on the six books:
| Cache read | Generations | Prompt copies |
|---|---|---|
| Whole cache | 30 | 0 |
--kv-select 32 | 30 | 0 |
--kv-select 64 | 30 | 0 |
Zero copies in 90 generations. The copying is a greedy-decoding effect. In real use (an agent, a chat), most clients sample: reassuring, but it does not explain why the selection triggers it.
Lead 2: a copy feeds itself
I took the trajectories where the model copied and replayed them teacher-forced with the whole cache. Once the copy has started, the full read predicts it too: a perplexity of 1.05 to 1.09 on the copied text, meaning the model is nearly certain of the next token.
In other words, the selection does not “make” the copy from start to finish: it only changes where it starts. Once it is going, any read mode carries it on. That is why the fixes that only touched the read changed nothing.
A dead end: more blocks in the last layers
My best idea the day before: give the last 4 attention layers four times more blocks (128 instead of 32). Teacher-forced, it was as good as 64 blocks everywhere (KL 0.0047) for almost the same time. And a first run no longer copied.
Except that running exactly the same configuration again, greedy generation diverged from the 11th token and copied at 98 %. Greedy is not reproducible from one run to the next on this engine, and my “fix” was a lucky draw. On top of that, the app’s config no longer fit in VRAM. Dropped.
Lesson: a single greedy run proves nothing. Repeat it, or measure teacher-forced.
The improvement: a cache that keeps what scores well
If the copying cannot be tuned directly, the gap with the full read can. I tested several variants, all at 32 blocks per KV head (time per decode window, books at 200K / 100K):
| Variant | Mean KL | Time per window |
|---|---|---|
| PR version (LRU cache) | 0.0088 | 46 / 54 ms |
| Eviction by score | 0.0078 | 45 / 53 ms |
| Eviction by score + prefetch of 16 blocks | 0.0066 | — |
| Eviction by score + prefetch of 64 blocks | 0.0056 | 47 / 55 ms |
| Blocks of 32 positions (budget 64, same cells) | 0.0068 | 43 / 54 ms |
| Blocks of 16 positions (budget 128) | 0.0052 | 50 / 62 ms |
| Exact Quest bound (radius weight 1) | 0.0176 | — |
The two changes I kept:
- Eviction by score. Each layer’s block cache holds the picks of the last two windows. When it ran out of room, it used to drop the block required longest ago. Now it drops the one that scores lowest against the current window. An old block that is still relevant stays.
- Prefetch. At each window, besides the picked blocks, the kernel loads the best scored blocks nobody picked, as long as they score above the block they would replace. The cache fills with what matters instead of waiting for it to be requested.
Smaller blocks gain a little precision but cost scoring time; the exact Quest bound is clearly worse than the 0.25 weight I use.
Results on the final code
The prefetch becomes a setting, --kv-select-prefetch P, 2 × N by default (64 for --kv-select 32). Measured on the code pushed to the PR, against reading the whole cache:
--kv-select | KL, before | KL, now | Same top token, before → now |
|---|---|---|---|
| 16 | 0.0187 | 0.0135 (-28 %) | 93.7 % → 94.5 % |
| 32 | 0.0088 | 0.0056 (-36 %) | 95.7 % → 96.6 % |
| 64 | 0.0050 | 0.0015 (-70 %) | 96.7 % → 98.4 % |
Speed does not move: 46 / 54 ms per window at 200K / 100K with --kv-select 32, against 44 / 54 without prefetch.
The PR’s parity test (the CUDA kernel against a CPU model) goes from 96 to 116 checks: the CPU model reproduces eviction by score and the prefetch, slot by slot, over 65 windows.
And greedy copying? At 64 blocks, the book that used to copy no longer does (the reply loops on itself, which the full read also does in greedy). At 32 blocks, the other book still copies, at 98 %. It is not solved, and I wrote it as such in the PR.
What about the app?
Anaelith Qwen38 27B One stays at 1.0.4 for now: the first 98,304 tokens are read exactly, and the selection only serves the part past them. The improvement will reach the app with the next anaelith image.
The PR is up to date: jesdga95/basalt#1205, with the measurements, the variants left out and the greedy-copy limitation.
What I take away
- Before looking for a fix, check under which conditions the flaw shows up: here, only in greedy.
- A single greedy run is not proof, even on an engine believed to be deterministic.
- When a flaw cannot be fixed directly, reducing the measurable error is still worth it: a third to two thirds less KL, with no speed lost.
Enjoy!
Disclosure — All benchmarks in this post run on my own Olares One. If the content was useful and you’re considering one, ordering through this referral link gets you $300 off ($4,699 instead of $4,999), stackable with the $300 Black Friday discount that started November 17: $600 off in total ($4,399). I’m mentioning this out of transparency — and yes, incidentally, it helps keep the blog alive (hosting, domain, and the time I spend writing here).