MarginKV
Storage-efficient KV cache reuse for decoder-only reranking
A reranker reads every candidate document again for every query. MarginKV makes Qwen3-Reranker-0.6B read the document first, so the document's KV cache is built once and reused across queries, then measures how far that stored cache can be compressed (quantization, token pruning, both) before ranking quality drops, whether loading it from disk beats recomputing it, and how much of it a real serving stack actually reuses.
Setup: BEIR SciFact and NFCorpus, BM25 top 100, top 20 reranked, documents truncated to 256 tokens, nDCG@10 and MRR@10 with paired bootstrap CIs. Decision rules were committed before the runs.
Table 1: does document-first ordering cost quality?
| System | SciFact nDCG@10 / MRR@10 | NFCorpus nDCG@10 / MRR@10 |
|---|---|---|
| BM25 first stage, top 100 | 0.6791 / 0.6431 | 0.3187 / 0.5245 |
| Original query-first Qwen3-Reranker-0.6B as released | 0.7555 / 0.7345 | 0.3485 / 0.5662 |
| Query-first LoRA (control) same MS MARCO training, original order | 0.7617 / 0.7396 | 0.3504 / 0.5721 |
| Doc-first zero-shot document first, no training | 0.2855 / 0.2219 | 0.2350 / 0.3705 |
| Doc-first LoRA document first, KV reusable across queries | 0.7504 / 0.7239 | 0.3543 / 0.5879 |
Length-matched retrain (ablation)
| System | SciFact nDCG@10 / MRR@10 | NFCorpus nDCG@10 / MRR@10 |
|---|---|---|
| Original query-first Qwen3-Reranker-0.6B as released | 0.7555 / 0.7345 | 0.3485 / 0.5662 |
| Query-first LoRA trained on MS MARCO passages (Phase 4) | 0.7617 / 0.7396 | 0.3504 / 0.5721 |
| Doc-first LoRA MS MARCO passages (Phase 4); the model used everywhere else | 0.7504 / 0.7239 | 0.3543 / 0.5879 |
| Query-first LoRA, long docs MS MARCO documents, first 256 tokens | 0.7349 / 0.7057 | 0.3484 / 0.5767 |
| Doc-first LoRA, long docs MS MARCO documents, first 256 tokens | 0.7300 / 0.7017 | 0.3507 / 0.5873 |
| Paired difference, nDCG@10 | SciFact Δ [95% CI] | NFCorpus Δ [95% CI] |
|---|---|---|
| Doc-first − query-first, Phase 4 (order gate) | −0.0113 [−0.0243, +0.0014] | +0.0039 [−0.0021, +0.0099] |
| Doc-first − query-first, long docs | −0.0049 [−0.0181, +0.0089] | +0.0023 [−0.0044, +0.0086] |
| Gap change (pre-registered primary) | +0.0063 [−0.0087, +0.0218] not shown | −0.0016 [−0.0080, +0.0045] not shown |
| Doc-first: long docs − Phase 4 | −0.0204 [−0.0336, −0.0080] | −0.0036 [−0.0091, +0.0017] |
| Query-first: long docs − Phase 4 | −0.0267 [−0.0426, −0.0118] | −0.0020 [−0.0066, +0.0026] |
The Phase 4 row is recomputed with the same 10,000-resample bootstrap as the long-doc rows, so its CI differs slightly from Table 1's, which comes from the original evaluation run (1,000 resamples).
Code and README on GitHubFull resultsModel on Hugging FaceLoRA adapterv1.0 release
Storage vs. quality
Each document's KV cache is stored once and reused for every query, so its size on disk is the cost. Each point is one way to store it: quantized, pruned to a share of its tokens, or both. Packed size per document (real files: codes, scales, headers) against the change in nDCG@10 relative to the uncompressed BF16 cache, with a 95% paired bootstrap CI.
9 points below this view, drawn as triangles at the edge (lowest: Attention (b) 25%, −0.1637). Use "All points" to see them.
| Setting | MiB/doc | vs BF16 | SciFact Δ nDCG@10 [95% CI] | NFCorpus Δ nDCG@10 [95% CI] |
|---|---|---|---|---|
| Quantization | ||||
| BF16 uncompressed baseline, shared prefix stored once | 26.40 | 1.00x | 0.7506 | 0.3538 |
| FP8 per-tensor e4m3, one scale per tensor | 13.20 | 2.00x | −0.0013 [−0.0042, +0.0006] Within 1 point | −0.0006 [−0.0017, +0.0006] No measurable loss |
| FP8 per-head e4m3, one scale per KV head | 13.20 | 2.00x | −0.0015 [−0.0043, −0.0001] Within 1 point | +0.0004 [−0.0010, +0.0020] No measurable loss |
| INT8 per-token symmetric, one scale per head and token | 13.41 | 1.97x | −0.0002 [−0.0008, +0.0004] No measurable loss | −0.0001 [−0.0015, +0.0013] No measurable loss |
| INT4 KIVI keys per channel, values per token, groups of 32 | 8.33 | 3.17x | +0.0010 [−0.0007, +0.0038] Within 1 point | −0.0009 [−0.0027, +0.0007] Within 1 point |
| INT4 per-token keys and values per token, groups of 32 (diagnostic) | 8.25 | 3.20x | −0.0966 [−0.1200, −0.0742] Loss | −0.0434 [−0.0550, −0.0323] Loss |
| Pruning | ||||
| Uniform 25% evenly spaced doc tokens; kept tokens stored in BF16 | 6.59 | 4.01x | −0.0617 [−0.0861, −0.0379] Loss | −0.0194 [−0.0282, −0.0108] Loss |
| Uniform 50% evenly spaced doc tokens; kept tokens stored in BF16 | 13.24 | 1.99x | −0.0122 [−0.0277, +0.0029] Loss | −0.0080 [−0.0134, −0.0028] Within 1 point |
| Uniform 75% evenly spaced doc tokens; kept tokens stored in BF16 | 19.82 | 1.33x | −0.0014 [−0.0096, +0.0063] Within 1 point | −0.0006 [−0.0041, +0.0030] Within 1 point |
| Boundary 25% first 4 + most recent tokens (StreamingLLM-style); kept tokens stored in BF16 | 6.59 | 4.01x | −0.1488 [−0.1797, −0.1181] Loss | −0.0504 [−0.0649, −0.0370] Loss |
| Boundary 50% first 4 + most recent tokens (StreamingLLM-style); kept tokens stored in BF16 | 13.24 | 1.99x | −0.0822 [−0.1080, −0.0568] Loss | −0.0314 [−0.0430, −0.0206] Loss |
| Boundary 75% first 4 + most recent tokens (StreamingLLM-style); kept tokens stored in BF16 | 19.82 | 1.33x | −0.0310 [−0.0487, −0.0140] Loss | −0.0161 [−0.0235, −0.0090] Loss |
| Attention (a) 25% attention the token receives from later doc tokens, length-normalized; kept tokens stored in BF16 | 6.59 | 4.01x | −0.0875 [−0.1136, −0.0625] Loss | −0.0385 [−0.0506, −0.0268] Loss |
| Attention (a) 50% attention the token receives from later doc tokens, length-normalized; kept tokens stored in BF16 | 13.24 | 1.99x | −0.0294 [−0.0463, −0.0129] Loss | −0.0138 [−0.0199, −0.0079] Loss |
| Attention (a) 75% attention the token receives from later doc tokens, length-normalized; kept tokens stored in BF16 | 19.82 | 1.33x | −0.0092 [−0.0205, +0.0014] Within 1 point | −0.0063 [−0.0101, −0.0028] Within 1 point |
| Attention (b) 25% (a) weighted by absolute position; kept tokens stored in BF16 | 6.59 | 4.01x | −0.1637 [−0.1939, −0.1340] Loss | −0.0604 [−0.0742, −0.0468] Loss |
| Attention (b) 50% (a) weighted by absolute position; kept tokens stored in BF16 | 13.24 | 1.99x | −0.0416 [−0.0626, −0.0212] Loss | −0.0231 [−0.0311, −0.0153] Loss |
| Attention (b) 75% (a) weighted by absolute position; kept tokens stored in BF16 | 19.82 | 1.33x | −0.0101 [−0.0214, +0.0007] Loss | −0.0091 [−0.0135, −0.0049] Within 1 point |
| Random 25% one seed per document; kept tokens stored in BF16 | 6.59 | 4.01x | −0.0818 [−0.1089, −0.0558] Loss | −0.0287 [−0.0389, −0.0186] Loss |
| Random 50% one seed per document; kept tokens stored in BF16 | 13.24 | 1.99x | −0.0146 [−0.0308, +0.0011] Loss | −0.0079 [−0.0147, −0.0013] Within 1 point |
| Random 75% one seed per document; kept tokens stored in BF16 | 19.82 | 1.33x | −0.0009 [−0.0088, +0.0067] Within 1 point | +0.0002 [−0.0044, +0.0048] Within 1 point |
| H2O-style 25% accumulated attention; diagnostic, never selected; kept tokens stored in BF16 | 6.59 | 4.01x | −0.0383 [−0.0571, −0.0206] Loss | −0.0113 [−0.0178, −0.0047] Loss |
| H2O-style 50% accumulated attention; diagnostic, never selected; kept tokens stored in BF16 | 13.24 | 1.99x | −0.0125 [−0.0256, −0.0009] Loss | −0.0028 [−0.0070, +0.0011] Within 1 point |
| H2O-style 75% accumulated attention; diagnostic, never selected; kept tokens stored in BF16 | 19.82 | 1.33x | −0.0049 [−0.0125, +0.0025] Within 1 point | −0.0014 [−0.0040, +0.0010] Within 1 point |
| Pruning + quantization | ||||
| Uniform 75% + FP8 per-tensor prune first, then quantize the kept tokens | 9.91 | 2.66x | −0.0029 [−0.0112, +0.0046] Within 1 point | −0.0020 [−0.0058, +0.0017] Within 1 point |
| Uniform 75% + INT4 KIVI prune first, then quantize the kept tokens | 6.27 | 4.21x | −0.0028 [−0.0110, +0.0048] Within 1 point | −0.0001 [−0.0039, +0.0037] Within 1 point |
MiB/doc and ratios are SciFact means (NFCorpus agrees closely). BF16 row shows absolute nDCG@10. Pruning points on the chart are spread ±0.625 MiB around their real size so the methods at one retention don't overlap; the tooltip and table show the real size. Pruned tokens are removed from the stored cache; the shared system and instruction prefix is stored once per corpus and never pruned.
Query explorer: what each cache does to one ranking
Every number above is an average over hundreds of queries. Here is one query at a time: its 20 BM25 candidates, and the rank each stored-cache format gives them. Arrows are the move against BF16; ranks 1 to 10 (bold) are the ones nDCG@10 counts. Click a column header to order the rows by that format.
Loading SciFact queries…
Storage calculator
How much disk does a document KV store need? Pick a corpus size and an average document length. Sizes come from the exact packed file format (codes, scales, headers), with the shared system and instruction prefix stored once per corpus.
| Format | Per token | Per doc | Corpus total | Measured files | vs naive | |
|---|---|---|---|---|---|---|
| Naive BF16 prefix repeated in every doc | 112 KiB | 34.4 MB | 111 GB 103 GiB | 1.00x | ||
| BF16 uncompressed baseline, shared prefix stored once | 112 KiB | 27.7 MB | 89.0 GB 82.9 GiB | 82.9 GiB fit exact | 1.24x | |
| FP8 per-tensor e4m3, one scale per tensor | 56.0 KiB | 13.8 MB | 44.5 GB 41.5 GiB | 41.5 GiB fit exact | 2.49x | |
| FP8 per-head e4m3, one scale per KV head | 56.0 KiB | 13.8 MB | 44.5 GB 41.5 GiB | 41.5 GiB fit exact | 2.49x | |
| INT8 per-token symmetric, one scale per head and token | 56.9 KiB | 14.1 MB | 45.2 GB 42.1 GiB | 42.1 GiB fit exact | 2.45x | |
| INT4 KIVI keys per channel, values per token, groups of 32 | 35.0 KiB | 8.70 MB | 28.0 GB 26.1 GiB | 26.2 GiB fit −0.37% | 3.96x | |
| INT4 per-token keys and values per token, groups of 32 (diagnostic) | 35.0 KiB | 8.65 MB | 27.8 GB 25.9 GiB | 25.9 GiB fit exact | 3.98x | |
| With 75% pruning (keeps 181 of 241.34 doc tokens) | ||||||
| 75% pruning, BF16 keep 75% of doc tokens (any method), stored in BF16 | 84.0 KiB | 20.8 MB | 66.8 GB 62.2 GiB | 62.2 GiB fit −0.11% | 1.66x | |
| 75% pruning + FP8 per-tensor prune, then quantize the kept tokens | 42.0 KiB | 10.4 MB | 33.4 GB 31.1 GiB | 31.1 GiB fit −0.11% | 3.32x | |
| 75% pruning + INT4 KIVI prune, then quantize the kept tokens | 26.2 KiB | 6.53 MB | 21.0 GB 19.6 GiB | 19.7 GiB fit −0.80% | 5.28x | |
Disk: loading a stored cache vs recomputing it
A KV store only pays off if reading a document's cache back is cheaper than computing it again. Each bar is a cold load from disk (page cache evicted first) on NVIDIA GeForce RTX 4090, over 1,000 documents, split into its stages. The dashed line is recomputing the same document's KV with a forward pass.
| Format | MiB/doc | Cold p50 (p95) ms | Warm p50 ms | Read / decode ms (p50) | Cold MB/s p50 | Cold / recompute |
|---|---|---|---|---|---|---|
| BF16 uncompressed baseline, shared prefix stored once | 26.35 | 57.4 (64.4) | 42.7 | 34.2 / 0.8 | 495 | 1.67x |
| FP8 per-tensor e4m3, one scale per tensor | 13.18 | 21.7 (24.4) | 9.9 | 14.0 / 4.0 | 665 | 0.63x |
| FP8 per-head e4m3, one scale per KV head | 13.18 | 21.6 (24.0) | 9.7 | 14.1 / 3.9 | 665 | 0.63x |
| INT8 per-token symmetric, one scale per head and token | 13.38 | 21.6 (22.8) | 9.6 | 14.2 / 3.8 | 682 | 0.63x |
| INT4 KIVI keys per channel, values per token, groups of 32 | 8.32 | 24.5 (27.5) | 16.7 | 9.0 / 13.1 | 368 | 0.71x |
| INT4 per-token keys and values per token, groups of 32 (diagnostic) | 8.24 | 22.5 (23.6) | 14.8 | 8.9 / 11.3 | 403 | 0.65x |
| 75% pruning + FP8 per-tensor prune, then quantize the kept tokens | 9.89 | 17.0 (18.2) | 8.0 | 10.5 / 3.9 | 641 | 0.49x |
| 75% pruning + INT4 KIVI prune, then quantize the kept tokens | 6.26 | 21.5 (22.8) | 15.4 | 7.1 / 12.4 | 320 | 0.62x |
Sizes are the means of the benchmarked documents. One rented GPU's container disk and our own Python loader (two CPU copies per file, unfused INT4 dequant); a zero-copy or GPU-direct loader would change the absolute numbers and possibly the INT4 vs FP8 order. Recompute was measured for this 0.6B model at about 300 tokens.
Serving: does the stored cache actually get reused?
The real BEIR query stream, replayed in order through vLLM 0.34.0 with prefix caching on one RTX 4090: every query in file order, each with its 20 BM25 candidates. Many documents come back for later queries. The question is how much of that a real cache turns into reuse.
First bar: share of requests whose document appeared earlier in the stream. Other bars: share of document tokens served from the prefix cache (measured runs: cold cache, concurrency 16).
| Setting | KV cache | SciFact: prompt hit / doc reuse | NFCorpus: prompt hit / doc reuse | nDCG@10 | c=1 p50 ms |
|---|---|---|---|---|---|
| Query-first (original model) | 178k tok | 18.3% / 0.0% | 19.8% / 0.9% | 0.7561 / 0.3490 | 17.4 / 15.8 |
| Doc-first, BF16 KV | 178k tok | 15.9% / 16.9% 44% of unlimited cache | 28.6% / 28.6% 58% of unlimited cache | 0.7507 / 0.3549 | 15.6 / 15.7 |
| Doc-first, FP8 KV | 341k tok | 22.5% / 23.7% 62% of unlimited cache | 36.9% / 36.8% 75% of unlimited cache | 0.7476 / 0.3543 | 15.7 / 16.1 |
Prompt hit: share of all prompt tokens served from cache. Doc reuse: share of document tokens. nDCG@10 and latency as SciFact / NFCorpus. Query-first uses the original model, doc-first the LoRA; each was checked against its own Hugging Face reference. High-concurrency throughput was noisy run to run, so settings are not ranked on it.
Limitations
- One model (Qwen3-Reranker-0.6B), two BEIR datasets, one seed.
- Documents are truncated to 256 tokens and only the BM25 top 20 is reranked, so absolute scores sit below published full-length numbers. The storage calculator's sizes hold for any length; quality was only measured up to 256 tokens.
- Every doc-first and compression result depends on the LoRA adaptation. The doc-first LoRA misses the pre-registered order gate on SciFact by a small margin (CI includes zero); it is reported as is. The length-matched retrain changed negatives, sources and labels along with length, both of its arms got worse on SciFact, and the gap change was not shown, so the cause stays open. One attempt per arm.
- Quantization quality comes from fake quantization (quantize, dequantize, score). Quality and stored bytes are measured; attention speed on compressed caches is not. Keys are quantized after RoPE. The shared system and instruction prefix stays BF16 and is stored once.
- Quality was measured with each document's own copy of the shared prefix; storage assumes one shared copy. The score effect is several times below bf16 noise.
- Pruning importance comes only from the document's own attention (the query is never seen, by design), averaged over all heads and layers into one kept set shared across layers; learned or per-layer selection may do better. Random pruning uses one seed. The early-token finding is post hoc.
- Combined compression covers one pruning setting (uniform 75%) and two formats. Its interaction CIs are a few thousandths wide, so smaller compounding can't be ruled out. "Smallest within one point" is relative to the settings measured.
- Disk numbers come from one rented GPU's container disk and our own Python loader (two CPU copies per file, unfused INT4 dequant). A zero-copy or GPU-direct loader and a fused kernel would change the absolute numbers and possibly the INT4 vs FP8 order. Recompute was measured for this 0.6B model at about 300 tokens; the balance depends on model size and document length.
- Serving numbers come from one rented RTX 4090, vLLM 0.34.0 defaults (64-token blocks), one run per configuration, replaying BEIR test queries in file order with no arrival times. It is a benchmark, not a production deployment. High-concurrency throughput is noisy run to run, so settings are not ranked on it.
- vLLM's FP8 KV used default scales (no calibration); its score error is larger than the calibrated FP8 in the quantization study.
- The query-first serving baseline is the original model; the doc-first settings use the LoRA. Each is compared to its own Hugging Face reference.
- The released weights were trained on MS MARCO, which is licensed for non-commercial research.