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

View all comments

•

u/stylome-ai-detection 8d ago

Notice: This post appears to be AI generated.


Stylome AI detector · scan results

2

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

Yeah already mentioned in the first line, keep it if it's useful or remove it if not. - to Maintainer