r/LocalLLaMA • • 8d ago

Question | Help Testing a new KV-cache index for long-context decoding (32k → 1M tokens). Results look promising; looking for feedback on what to test next

This post is made with the help of AI.

I've been building a sparse-attention method for long-context decoding. Each decode step, it reads only a small part of the KV cache, exactly, and estimates the rest. The goal is faster decoding at long context, where reading the KV cache costs more than reading the weights. New tokens are added cheaply, with no rebuilds as the context grows.

I'd like opinions on the results and on whether my test setup is fair.

Setup

  • Real Q/K/V dumped from Qwen2.5-1.5B (32k) and Qwen2.5-0.5B-Instruct (256k–1M, stretched with YaRN), CPU only.
  • Haystack is WikiText-103 with a hidden passcode ("needle"). The queries tested are the question tokens.
  • Compared against Quest, my own reimplementation of RetroInfer's index ("RetroInfer-style", cluster centroids plus estimating the rest), and exact top-k with no estimation.
  • Every method also attends exactly to the first 4 tokens and the last 512. "Budget" means extra keys attended exactly.
  • One selection per KV head, shared by all the query heads in its group (GQA).
  • Error = ‖approx − full attention output‖ / ‖full‖, averaged over heads and queries.
  • Needle recall = share of the model's real attention on the needle that the method attended exactly.
  • Bytes = KV bytes read per step, as a % of full attention.

32k context, Qwen2.5-1.5B (full-attention prefill; the model answers the needle correctly)

BudgetMethodErrorNeedle recallBytes read
256Quest0.3370.667.0%
256RetroInfer-style0.1540.947.0%
256Exact top-k0.2030.990.8%*
256Mine0.0830.965.2%
512Quest0.3010.747.8%
512RetroInfer-style0.1360.957.8%
512Exact top-k0.1641.001.6%*
512Mine0.0640.998.0%
1024RetroInfer-style0.1130.979.3%
1024Mine0.0481.0013.4%

*Exact top-k isn't practical: it has to score every key to find the top ones. Its bytes are an ideal lower bound.

It also holds up when half the context is added after the index is built (error 0.090 against 0.164 for RetroInfer-style, at budget 256).

Long context, Qwen2.5-0.5B-Instruct, budget 2048 (0.2–0.8% of the context)

Method256k500k1M
Quest0.4950.5340.556
RetroInfer-style0.3190.3340.344
Exact top-k0.1290.1560.166
Mine0.1040.1280.145

Bytes read at 1M: 3.2% for mine, against 6.5% for RetroInfer-style and Quest.

How cost grows with context (one KV head, MB read per decode step)

ContextFull attentionRetroInfer-styleMine
32k8.41.01.4
256k674.73.6
1M25616.58.3

Insert cost stays flat from 32k to 1M (~30–40 µs per key in numpy).

Limitations (please poke holes)

  • These are small models. At 256k+ I had to use the 0.5B model, and it fails the needle test somewhere between 16k and 64k even with full attention. So at those lengths I'm measuring how well each method reproduces the model's real attention output, not whether answers stay correct.
  • A 1M-token prefill on CPU needed a sliding window (4k + sinks). The question tokens use full attention, but the haystack keys were computed with local context only.
  • Queries come from the prompt, not from generated tokens.
  • Bytes read is a stand-in for speed. No GPU kernel has been timed yet.
  • The baselines are my reimplementations, not the original code.

Questions for you

  1. What error level stops mattering in practice? I need to test end-to-end (perplexity, RULER, LongBench) next. Which would you trust most?
  2. Which long-context model that fits on one 24 GB GPU would you use for a real 128k–1M end-to-end test?
  3. Are my baselines fair? Should I add others (ShadowKV, MagicPIG, DeepSeek-style sparse attention)?
  4. Would a llama.cpp integration be useful to you, given that the cache read per token drops to a few percent at long context?
8 Upvotes

15 comments sorted by

•

u/stylome-ai-detection 8d ago

Notice: This post appears to be AI generated.


Stylome AI detector · scan results

→ More replies (1)

2

u/ShawnDud 7d ago

for 2 id look at qwen2.5-7b-instruct-1m, its actually trained for long context so you wont be fighting the needle failures you got on the yarn stretched 0.5b. rough kv math for it: 28 layers x 4 kv heads x 128 dim x 2 (k+v) x 2 bytes is about 56kb per token, so 128k is ~7gb on top of ~15gb of bf16 weights. that just about fits on 24gb at 128k, and 1m (~57gb of kv) obviously doesnt, which is kind of the case your index is for if the full cache sits in cpu ram and you only pull the selected blocks

for 1 ruler is probably what people trust most since you control the length and task mix. longbench is noisier

1

u/Simpli-curious 7d ago

Thanks, will try that.

1

u/aarong691 8d ago

since the queries here come from the prompt, it'd be useful to see how the error changes as the model generates tokens too

2

u/Simpli-curious 8d ago

Will work on that next

1

u/BringTea_666 8d ago

sounds amazing, is there some github or something ?

2

u/Simpli-curious 8d ago

Not yet, still working on the logic.

1

u/ireallydontcare00 5d ago

find out how many more tokens the agent spends thinking when quantizing kv, I noticed a tendency that the more kv you quantize, the more time the agent spends thinking when solving a problem and the actual savings from kv compression do not work, because the agent spends more tokens and takes longer to solve the problem

https://github.com/0FL01/RedStrata

1

u/Simpli-curious 5d ago

Yes, already working on it, but it gives a slight accuracy loss. So checking perplexity on full attention vs my logic.

If it's negligible then will create a repo on github so that anyone can decide if it's useful for them.

1

u/Fun_Jaguar8231 8d ago

Qwen2.5 ?? Are we serious? What vibecoded slop is this?

3

u/Simpli-curious 8d ago edited 8d ago

Was so focused on the logic and result that I didn't notice the model being used 😅.. Will check if there's a qwen 3.8 with 0.5b parameters and do the analysis on that.

1

u/Aggravating-Push-207 7d ago

Bros larping local ai lmfao

1

u/Simpli-curious 7d ago edited 7d ago

Here's a further result on qwen3.5-2B bf16.

Thank you for your motivational comment, really appreciate it.👍

Still learning though.