Skip to content
/ airelien.dev
Go back
Aurélien AMSELLEM

--kv-select, part 2: the prompt copying comes from greedy decoding, and a better cache cuts the gap by 28 to 70 %

Follow-up to the --kv-select post (Qwen3.8-27B at 242K on 24 GB). I looked for why the selection sometimes made the model copy a passage of the prompt: it is a greedy-decoding effect, absent from 90 sampled generations. Then I improved the block cache: it keeps the blocks that score best and prefetches the next ones. The gap with a full read drops by 36 % at 32 blocks and 70 % at 64, at no speed cost.

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:

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 readGenerationsPrompt copies
Whole cache300
--kv-select 32300
--kv-select 64300

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):

VariantMean KLTime per window
PR version (LRU cache)0.008846 / 54 ms
Eviction by score0.007845 / 53 ms
Eviction by score + prefetch of 16 blocks0.0066—
Eviction by score + prefetch of 64 blocks0.005647 / 55 ms
Blocks of 32 positions (budget 64, same cells)0.006843 / 54 ms
Blocks of 16 positions (budget 128)0.005250 / 62 ms
Exact Quest bound (radius weight 1)0.0176—

The two changes I kept:

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-selectKL, beforeKL, nowSame top token, before → now
160.01870.0135 (-28 %)93.7 % → 94.5 %
320.00880.0056 (-36 %)95.7 % → 96.6 %
640.00500.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

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).

Share this post on:

Comments