Read each document once, keep its KV cache, reuse it for every query. MarginKV makes Qwen3-Reranker score with the document first, then measures how far that reusable document KV can be compressed, served and loaded from disk before ranking quality drops: 4.2x smaller within one nDCG@10 point.
Results explorer: marginkv.vercel.app | Model: LoRA · merged
The explorer is an interactive view of the results: storage vs. quality for every format and pruning setting, a per-query explorer, the storage calculator, disk load vs. recompute, serving reuse, and the length-matched retrain. Every number on it is read from results/ by scripts/14_export_app_data.py; the per-query data comes from scripts/14b_export_explorer.py, which checks each format's mean against the same metrics.
A decoder-only reranker can read each document once and keep its KV cache "in the margins," then reuse it for every query that retrieves that document. This repo measures four things: what document-first scoring costs in ranking quality, how far the stored document KV can be compressed (fewer bits, fewer tokens, or both) before ranking quality drops, whether the reuse pays off when the reranker is served on real traffic, and whether loading a stored KV from disk beats recomputing it.
Status: complete. Document-first adaptation, cache correctness, KV quantization with packed storage, query-independent token pruning, combined pruning + quantization, prefix-cache serving on the real query stream, cold-disk load benchmarks, a length-matched LoRA retrain, and the released weights. Follow-ups are listed in the Roadmap.
- Qwen3-Reranker-0.6B fails document-first zero-shot. On SciFact, nDCG@10 drops from 0.756 to 0.286. A LoRA trained on MS MARCO in the doc-first order brings it back to 0.750 (SciFact) and 0.354 (NFCorpus): within 0.011 of a query-first LoRA control trained the same way and 0.005 of the original model on SciFact, and above both on NFCorpus.
- The cached path is exact. Scoring on a stored document KV matches full-prompt scoring to 2.4e-5 log-odds in fp32.
- INT4 (KIVI-style) cuts stored document KV 3.2x with no meaningful quality change: 8.33 MiB per doc instead of 26.4 MiB, with nDCG@10 +0.0010 on SciFact and −0.0009 on NFCorpus. INT8 (1.97x) shows no measurable loss on either dataset. All sizes are measured from packed files on disk that round-trip bit-exactly.
- The quantization axis matters more than the bit width. The same INT4 budget with per-token keys loses 0.04 to 0.10 nDCG@10. Qwen3's post-RoPE keys have outlier channels, so keys need per-channel scales.
- Dropping tokens is a much weaker lever than dropping bits. Only 75% token retention (1.33x) stays within one point. At the same ~13 MiB per doc, FP8 and INT8 lose at most 0.0015 nDCG@10 while 50% pruning loses 0.008 to 0.08 (the five main methods), and INT4 at 8.3 MiB matches the best pruning setting, which needs 19.8 MiB. Importance scores from the document's own attention do no better than random, because they favor the end of the document; keeping its start matters more (a post-hoc finding).
- Bits and tokens combine. Dropping 25% of document tokens and quantizing the rest to INT4 KIVI stores 6.27 MiB per doc (4.2x) within one point on both datasets (−0.0028 / −0.0001), with no sign that the two losses compound. At about the same size, pruning alone loses 0.02 to 0.06.
- In real traffic, cache capacity decides whether reuse pays off. Served with vLLM on the BEIR query stream, 46 to 62% of documents repeat, but LRU eviction and 64-token blocks leave doc-first serving only 0.16 / 0.29 of prompt tokens from cache. FP8 KV doubles capacity and lifts that to 0.23 / 0.37 at −0.003 / −0.001 nDCG@10. A simple block-level cache simulator predicts every measured hit rate.
- A stored KV is only worth loading from disk once it's compressed. On an RTX 4090, a BF16 document KV takes 57 ms to load cold vs 34 ms to recompute. Every compressed format loads in 17 to 25 ms (0.49 to 0.71x of recompute), with pruning + FP8 fastest. INT4 is the smallest but loads slower than FP8 and INT8, because its unfused decode costs 11 to 13 ms.
- Training on long documents didn't explain the SciFact gap. A length-matched retrain narrowed doc-first's gap to −0.005 and both arms pass the order gate, but only because both lost 0.02 to 0.03 nDCG@10 on SciFact. The pre-registered test of the gap change is not shown.
Every setting measured, as Δ nDCG@10 against the BF16 cache with 95% CIs (shaded band: the ±0.002 equivalence margin; dashed line: one point). Pruning methods at one retention share one size and are spread ±0.6 MiB so their CIs don't overlap. Made by scripts/13_make_plots.py from results/metrics/combined_summary.json.
- Q1. Can Qwen3-Reranker-0.6B score with the document before the query, so the document's KV is query-independent and reusable, without losing ranking quality?
- Q2. Given that model, how much can the stored document KV be compressed (quantization, query-independent token pruning, or both) before ranking quality drops?
HyperRAG (An et al., 2025) established document-side KV reuse for decoder-only rerankers and discussed low-bit KV quantization, but did not evaluate quantized KV. Most KV compression work targets generation, where the cache lives for one request. Here the cache is query-independent and reused across many queries, so the question is storage: bytes per document vs. ranking quality. AdaptCache (2025) is the closest storage-side work, for generation serving.
Every experiment uses the same fixed inputs:
- Data: BEIR SciFact test (300 queries, 5,183 docs) and NFCorpus test (323 queries, 3,633 docs), from pinned Hugging Face mirrors with per-file sha256. Counts match BEIR's published stats.
- First stage: BM25 top 100 (bm25s, k1 1.2, b 0.75), saved once. Recall@100: 0.913 / 0.247.
- Reranking: top 20 reranked, then BM25 ranks 21 to 100. Metrics are nDCG@10 and MRR@10.
- Tokenization: done once and saved. Docs are truncated to 256 tokens. The query-first prompt matches the model card's tokenization on every pair.
- Document-first prompt:
[system + instruction][document] | [query][yes/no suffix]. Everything before|is cached. The score is log-odds of "yes" vs "no" at the last position, computed in fp32. - Hardware: quality experiments run on an M3 MacBook Air (MPS, bf16). LoRA training ($2.13), the vLLM serving study ($1.76), and the disk benchmarks plus the length-matched retrain ($4.29) ran on rented RTX 4090s, $8.18 in total.
nDCG@10 / MRR@10, full prompt, identical tokens for every system.
| System | SciFact | NFCorpus |
|---|---|---|
| BM25 | 0.6791 / 0.6431 | 0.3187 / 0.5245 |
| Random top-20 order (100 reps) | 0.1996 / 0.1360 | 0.1769 / 0.2797 |
| bge-reranker-v2-m3 | 0.7281 / 0.7026 | 0.3333 / 0.5654 |
| Qwen3-Reranker-0.6B, query-first (original) | 0.7555 / 0.7345 | 0.3485 / 0.5662 |
| Query-first LoRA (control) | 0.7617 / 0.7396 | 0.3504 / 0.5721 |
| Document-first, zero-shot | 0.2855 / 0.2219 | 0.2350 / 0.3705 |
| Document-first LoRA | 0.7504 / 0.7239 | 0.3543 / 0.5879 |
LoRA setup: r 16 on all 7 projections, listwise softmax loss over 1 positive + 7 negatives, 12k MS MARCO queries, 1,500 steps. Both arms are trained identically, and the checkpoint is chosen on held-out MS MARCO dev only. The document-first adapter and its merged weights are on Hugging Face (LoRA, merged); the model card's scoring snippet is checked against this repo's scorer (identical token ids, 3.5e-5 log-odds in fp32).
Order gate, pre-registered as doc-first LoRA ≥ control − 0.01 on both datasets:
- NFCorpus passes: +0.0039, 95% CI [−0.0016, +0.0096].
- SciFact misses by 0.0013: −0.0113, CI [−0.0240, +0.0011]. The CI includes zero, and the doc-first LoRA is within 0.005 of the original model there.
This is reported as a miss. A length-matched retrain is reported next to it in section 8: it narrows the gap, but doesn't explain it. This LoRA remains the model for every other result.
- fp32: cached scoring vs full-prompt scoring agree to 2.4e-5 log-odds, with identical rankings.
- bf16: the two paths differ by rounding (up to about 0.3 log-odds) but have the same error against fp32 (0.0533 vs 0.0532): an exact tie that technically fails our pre-registered point-estimate rule by 1e-4, reported as-is. Their nDCG@10 differs by +0.0002 / −0.0006.
All compression results below compare arms within the cached path, so this offset cancels.
Each doc's KV is built once, quantized, dequantized back to bf16 ("fake quant"), and scored. Every format sees identical pairs in identical batches. Only document tokens are quantized; the shared 59-token system and instruction prefix stays BF16 and is stored once per corpus.
Noise floor and margin. Before any compressed run, we scored the BF16 baseline at two batch sizes. That moved scores by up to 0.29 log-odds but nDCG@10 by at most 0.0002. The pre-registered equivalence margin is ±0.002 nDCG@10.
- No measurable loss: the format's paired 95% CI sits inside the margin.
- Within 1 point: the point estimate is ≥ −0.01.
| Format | SciFact nDCG@10 (Δ, 95% CI) | NFCorpus nDCG@10 (Δ, 95% CI) | Verdict (Sci / NFC) | MiB per doc | vs BF16 |
|---|---|---|---|---|---|
| BF16 | 0.7506 | 0.3538 | baseline | 26.40 | 1.00x |
| FP8 e4m3, per-tensor scale | 0.7494 (−0.0013 [−0.0042, +0.0006]) | 0.3532 (−0.0006 [−0.0017, +0.0006]) | 1 pt / none | 13.20 | 2.00x |
| FP8 e4m3, per-head scale | 0.7491 (−0.0015 [−0.0043, −0.0001]) | 0.3541 (+0.0004 [−0.0010, +0.0020]) | 1 pt / none | 13.20 | 2.00x |
| INT8, per-token | 0.7505 (−0.0002 [−0.0008, +0.0004]) | 0.3537 (−0.0001 [−0.0015, +0.0013]) | none / none | 13.41 | 1.97x |
| INT4 KIVI-style, g32 | 0.7517 (+0.0010 [−0.0007, +0.0038]) | 0.3528 (−0.0009 [−0.0027, +0.0007]) | 1 pt / 1 pt | 8.33 | 3.17x |
| INT4 per-token, g32 (diagnostic) | 0.6540 (−0.0966 [−0.120, −0.074]) | 0.3103 (−0.0434 [−0.055, −0.032]) | loss / loss | 8.25 | 3.20x |
"none" = no measurable loss; "1 pt" = within 1 point. KIVI-style INT4 means keys quantized per channel (groups of 32 tokens) and values per token (groups of 32 channels), with fp16 scales and mins.
Storage comes from real packed files, not arithmetic:
- Format: one safetensors file per doc, INT4 two codes per byte, fp16 scales, plus one shared-prefix file per corpus (6.45 MiB). Headers are under 700 bytes.
- Round trip: every sampled file unpacks bit-exactly to the tensors that were scored, and file sizes match the predicted byte counts exactly.
- Corpus totals: BF16 needs 82.9 GiB for SciFact's 3,216 reranked docs; INT4 KIVI needs 26.2 GiB.
- Naive baseline: caching the full prompt prefix per doc in BF16 takes 32.9 MiB per doc, so INT4 KIVI is 3.9x smaller than that.
- The quantization axis beats bit width. INT4 keys quantized per channel are as accurate as INT8 keys quantized per token (relative error 0.0230 vs 0.0225). Per-token INT4 keys shift scores by up to 20 log-odds.
- Relative KV error is a poor proxy for ranking quality. Per-token INT4 keys are "only" about 4.6x worse in relative error but lose up to 0.10 nDCG@10, while FP8's uniform 2.7% error barely moves rankin 8000 gs. Measure the task metric.
- INT8 is slightly safer than FP8 at the same size. FP8 per-head shows a small but significant SciFact loss (−0.0015, CI excludes 0); INT8 shows none. Per-tensor and per-head FP8 tie, as expected for a float format.
- Storing the shared prefix once is free compression. 59 of about 300 tokens are identical across docs, which alone saves 1.24x.
Instead of fewer bits per value, keep fewer tokens. After a doc's KV is built, a fraction of its document tokens is dropped from the cache. Kept tokens keep their original positions (RoPE is already applied to the stored keys), so the query starts exactly where it always did. The shared prefix is never pruned, and the first doc token and the last 8 are always kept. In fp32, scoring on a pruned cache matches a full prompt with the pruned tokens hidden from the query to 2.7e-5 log-odds, so every difference below comes from which tokens are dropped.
Methods, each at 25 / 50 / 75% of document tokens:
- Uniform: evenly spaced tokens.
- Boundary: the first 4 tokens plus the most recent ones (StreamingLLM-style).
- Attention (a): the mean attention a token receives from later document tokens, averaged over all heads and layers. It is computed once per document with no query in sight, so it is query-independent.
- Attention (b): (a) weighted by position, meant to cancel the bias that uniform causal attention would create.
- Random: seeded per document.
- H2O-style (diagnostic): plain accumulated attention (Σ of attention received, no normalization). Reported, never selected.
Same harness, baseline, margin and verdict rules as quantization. The rules, including how a setting is picked for combined compression, were committed before the full run. Δ nDCG@10 vs BF16 (bold = within 1 point):
| Method | SciFact 75% | 50% | 25% | NFCorpus 75% | 50% | 25% |
|---|---|---|---|---|---|---|
| Uniform | −0.0014 | −0.0122 | −0.0617 | −0.0006 | −0.0080 | −0.0194 |
| Random | −0.0009 | −0.0146 | −0.0818 | +0.0002 | −0.0079 | −0.0287 |
| Attention (a) | −0.0092 | −0.0294 | −0.0875 | −0.0063 | −0.0138 | −0.0385 |
| Attention (b) | −0.0101 | −0.0416 | −0.1637 | −0.0091 | −0.0231 | −0.0604 |
| Boundary | −0.0310 | −0.0822 | −0.1488 | −0.0161 | −0.0314 | −0.0504 |
| H2O-style (diagnostic) | −0.0049 | −0.0125 | −0.0383 | −0.0014 | −0.0028 | −0.0113 |
| MiB per doc (vs BF16) | 19.8 (1.33x) | 13.2 (1.99x) | 6.6 (4.01x) | 19.8 (1.33x) | 13.2 (1.99x) | 6.6 (4.01x) |
Bits vs tokens at equal storage (packed BF16 files for the pruned caches, exact sizes):
| Storage per doc | Quantization | Δ SciFact / NFCorpus | Pruning (uniform) | Δ SciFact / NFCorpus |
|---|---|---|---|---|
| ~13.2 MiB | FP8 per-tensor | −0.0013 / −0.0006 | 50% | −0.0122 / −0.0080 |
| ~13.3 MiB | INT8 per-token | −0.0002 / −0.0001 | 50% | −0.0122 / −0.0080 |
| 8.3 vs 6.6 MiB | INT4 KIVI | +0.0010 / −0.0009 | 25% | −0.0617 / −0.0194 |
- Quantization beats pruning at every storage size we measured. At ~13 MiB, FP8 and INT8 lose at most 0.0015 while 50% uniform pruning loses 0.008 to 0.012; INT4 KIVI at 8.33 MiB loses far less than 25% pruning at 6.6 MiB. INT4 KIVI is even as good as the best pruning setting, 75% retention at 19.8 MiB (all within 0.0015 of baseline on both datasets), at 42% of its size. The only pruning setting within one point on both datasets is 75% retention (uniform or random), which saves just 1.33x.
- Attention-based importance doesn't help here. (a) ties random at every retention level. (b) is significantly worse than random at 25% and 50% on both datasets, and the pre-registered (b)-vs-(a) comparison comes out "hurts" at 25%. The reason is in the scores themselves: document tokens attend mostly to nearby earlier tokens, and normalizing by the number of later tokens turns that into a preference for the end of the document (rank correlation with position +0.52 for (a), +0.73 for (b)). Boundary, which keeps the most recent tokens, is the worst method at every retention.
- Keeping the start of the document matters (post hoc). The share of the first 32 doc tokens a method keeps tracks its quality at every retention. At 25%, H2O-style keeps 69% / 66% of them and loses least (−0.038 / −0.011); attention (b) keeps 4% and loses most (−0.164 / −0.060). H2O-style is the only score that beats random (at 25%, both datasets: +0.044 [+0.020, +0.067] and +0.017 [+0.007, +0.028]). A plausible reason is that each document starts with its title. This was not pre-registered; a "keep the first N tokens" arm (equivalent to truncating documents shorter) is the clean follow-up.
- Scores move a lot even when rankings don't. At 75% retention, scores shift by up to 6 to 14 log-odds and 4 to 7.5% of pairs swap order (vs 0.3 log-odds and 0.4% for bf16 noise), yet uniform pruning stays within 0.0015 nDCG@10.
- Pruning isn't a speed lever here. Scoring at 25% retention is only 15 to 23% faster per batch on the Mac, because fixed per-batch costs dominate at this size.
About half of every document token's attention goes to the first token of the shared prefix (an attention sink), which is never pruned. Importance rankings are identical whether computed in MPS bf16 or CPU fp32. Setting for combined compression (pre-registered rule): uniform 75%, combined with FP8 and INT4 KIVI in section 5.
Prune first, then quantize the kept tokens: uniform 75% retention (the Phase 6 pick), then FP8 per-tensor or INT4 KIVI. The shared prefix stays BF16 and positions are unchanged, as before. KIVI's key groups form over the kept tokens, so a 32-token group can span tokens that were far apart in the document. All four arms of each 2×2 (baseline, pruning only, quantization only, both) are scored in the same run. The three single-method arms reproduce their Phase 5 and 6 score files exactly. Rules, including the interaction test and the frontier rule, were committed before the full run.
| Setting | MiB per doc | vs BF16 | SciFact Δ (95% CI) | NFCorpus Δ (95% CI) | Verdict (Sci / NFC) |
|---|---|---|---|---|---|
| BF16 | 26.40 | 1.00x | baseline | baseline | |
| Uniform 75% | 19.82 | 1.33x | −0.0014 [−0.0096, +0.0063] | −0.0006 [−0.0041, +0.0030] | 1 pt / 1 pt |
| FP8 per-tensor | 13.20 | 2.00x | −0.0013 [−0.0042, +0.0006] | −0.0006 [−0.0017, +0.0006] | 1 pt / none |
| INT4 KIVI | 8.33 | 3.17x | +0.0010 [−0.0007, +0.0038] | −0.0009 [−0.0027, +0.0007] | 1 pt / 1 pt |
| Uniform 75% + FP8 | 9.91 | 2.66x | −0.0029 [−0.0112, +0.0046] | −0.0020 [−0.0058, +0.0017] | 1 pt / 1 pt |
| Uniform 75% + INT4 KIVI | 6.27 | 4.21x | −0.0028 [−0.0110, +0.0048] | −0.0001 [−0.0039, +0.0037] | 1 pt / 1 pt |
| Uniform 25% (for comparison) | 6.59 | 4.01x | −0.0617 [−0.0861, −0.0379] | −0.0194 [−0.0282, −0.0108] | loss / loss |
Sizes are exact packed file sizes (bytes = a + b·kept tokens + c·⌈kept/32⌉), checked against real files that round-trip bit-exactly.
Do the losses compound? Per query, the interaction is I = (both − pruning only) − (quantization only − baseline). It is zero if the two losses simply add and negative if quantization costs more on a pruned cache.
| Interaction I (95% CI) | SciFact | NFCorpus |
|---|---|---|
| Uniform 75% + FP8 | −0.0003 [−0.0032, +0.0026] | −0.0008 [−0.0025, +0.0007] |
| Uniform 75% + INT4 KIVI | −0.0024 [−0.0060, +0.0001] | +0.0014 [−0.0004, +0.0033] |
- The losses add; compounding isn't shown. The pre-registered test (CI below zero on both datasets) fails for both formats, and for INT4 the two datasets point in opposite directions. FP8's interaction is inside ±0.004 on both datasets. INT4's is inside ±0.004 on NFCorpus and inside one point on SciFact, where the CI just reaches zero. Part of that SciFact value is INT4 alone scoring slightly above the baseline (+0.0010), a gain that doesn't repeat on the pruned cache.
- Combining is the only way below 8 MiB per doc within one point. At 6.27 MiB, pruning + INT4 is smaller than every single-method setting we measured. At nearly the same size (6.59 MiB), 25% pruning alone is worse by +0.059 [+0.036, +0.083] on SciFact and +0.019 [+0.011, +0.028] on NFCorpus (paired). The pre-registered frontier rule labels it "on the frontier", but only because no single method reaches this size, so there is nothing smaller to compare against.
- Pruning on top of INT4 costs too little to measure. Against INT4 alone: −0.0038 [−0.0117, +0.0034] and +0.0008 [−0.0029, +0.0047]. That is 3.17x to 4.21x for a cost the CIs can't separate from zero.
- Pruning + FP8 isn't worth it for storage. At 9.91 MiB it is larger than INT4 KIVI alone and no better (INT4 minus combined: +0.0040 / +0.0011, CIs include zero). It beats 6 of the 7 smaller single-method settings but can't be separated from INT4 KIVI, so the frontier rule labels it "not separated". It is also measurably worse than pruning alone (about −0.0015 on both datasets), which is FP8's own cost showing up. (It is, however, the fastest format to load from disk; see section 7.)
- The order of the two steps matters for some formats. Formats whose scales depend only on one token (INT8 per-token, KIVI values) give identical results either way. KIVI keys and FP8 scales are computed across tokens, so they change when tokens are removed first (checked in
tests/test_combined.py). We prune first, because that is what gets stored.
Does document-first KV reuse actually pay off when the reranker is served for real? We served the models with vLLM 0.34.0 on one RTX 4090 (24 GB, RunPod), with automatic prefix caching on and vLLM's defaults otherwise (including the block size it picked, 64 tokens). Scores come from the yes/no logprobs of a single generated token (allowed_token_ids = [no, yes], processed logprobs, the model's own sampling config ignored), using the same token ids as every earlier phase.
Settings: query-first (original Qwen3-Reranker-0.6B), doc-first (merged doc-first LoRA), and doc-first with FP8 KV (--kv-cache-dtype fp8, vLLM's default scales). The gate rules were committed before any vLLM run.
The served model is the same model. Against CPU fp32 on 256 pairs, vLLM's mean |Δ log-odds| is 0.051 (doc-first) and 0.052 (query-first), vs 0.053 and 0.085 for our own MPS bf16 path. With BF16 KV, full-test-set nDCG@10 is within ±0.0007 of the HF scores at every concurrency.
Trace 3, the real BEIR query stream: queries in file order, each with its 20 rerank candidates (6,000 / 6,460 requests), cold cache, concurrency 16.
| SciFact | NFCorpus | |
|---|---|---|
| Documents that repeat in the stream | 46.4% | 61.7% |
| Median gap before a document returns | 975 requests | 440 requests |
| Prompt tokens served from cache | ||
| Query-first, BF16 KV (177,664-token cache) | 0.183 | 0.198 |
| Doc-first, BF16 KV (177,664-token cache) | 0.159 | 0.286 |
| Doc-first, FP8 KV (340,992-token cache) | 0.225 | 0.369 |
| Doc-first with an unlimited cache (simulated) | 0.365 | 0.491 |
| nDCG@10 (query-first / doc-first / FP8) | 0.7561 / 0.7507 / 0.7476 | 0.3490 / 0.3549 / 0.3543 |
The same comparison counted in document tokens (query-first reuses almost no document tokens; its hits come from the shared instruction and query block). From results/metrics/vllm.json.
What we found:
- Documents repeat, but most reuse is lost before it pays off. LRU eviction and 64-token block alignment leave doc-first BF16 with 44% (SciFact) and 58% (NFCorpus) of the reuse an unlimited cache would give. On SciFact, query-first actually serves more tokens from cache. Its first 64-token block (system prompt + instruction + the start of the query) is shared by all 20 documents of a query, while doc-first's 59-token shared prefix is shorter than one block and never hits.
- KV compression is a cache-capacity lever. FP8 KV nearly doubles capacity, which raises doc-first's hit rate 1.41x / 1.29x and puts it ahead of query-first on both datasets. The cost is −0.0031 nDCG@10 on SciFact (95% CI [−0.0069, −0.0002]) and −0.0006 on NFCorpus vs vLLM BF16, both within one point. Without calibrated scales, vLLM's FP8 KV is about 5x noisier than BF16 (post-RoPE keys reach 488, above e4m3's 448). Our calibrated FP8 in Phase 5 cost −0.0013 / −0.0006.
- There's a capacity cliff. Rerunning trace 1 (840 SciFact requests, ~281k tokens) reuses 0.1% of tokens with BF16 KV and 91% with FP8. NFCorpus trace 1 (~165k tokens) fits in both (94%).
- The behavior is predictable. A block-level LRU simulator, given only the trace, the block size and the cache capacity, matches every measured hit rate to three decimals (18 setting × dataset × trace cases, cold and warm). Capacity planning for this workload can be done offline.
- At 0.6B, cache hits don't change latency. At concurrency 1, per-request latency is about 15 ms whether 0% or 94% of the prompt is cached. A linear fit over 31,640 requests per setting gives −2.0 to +1.5 ms per 1,000 uncached tokens (|r| ≤ 0.08). Fixed per-request overhead dominates prefill at this model size, so reuse would show up in latency only for larger models or longer documents. Throughput at concurrency 16 varied by up to ~30% between runs at equal hit rates, so we don't rank settings by it.
Traces 1 (same query, 20 docs) and 2 (same doc, 20 queries), concurrency 1/4/16 and warm reruns are in results/vllm/table3.csv; everything is summarized in results/metrics/vllm.json.
Deviation from the pre-registration: the prefix-cache gate (G3) was written for 16-token blocks. vLLM 0.34 chose 64, so G3 fails as written. At the actual block size it passes exactly on all 10 probes (cold request 0 tokens cached; second request exactly the 64-aligned document prefix).
A disk tier for document KV is only worth having if loading a stored KV is faster than rebuilding it. We packed 1,000 documents (500 per dataset, seeded) in each of 8 formats on one RTX 4090 pod's container disk and timed the full path from file to a usable cache on the GPU:
- read: the whole file into memory.
- parse: safetensors header and tensors, on CPU.
- h2d: host to GPU.
- decode: dequantize and attach to the shared prefix on the GPU.
Cold means the file was evicted from the page cache first (posix_fadvise), and a mincore check confirmed 0% of every file was resident before its cold read. Warm is an immediate second read of the same file. Recompute is the prefill of system + instruction + document at batch 1 on the same GPU, p50 34 ms. Every one of the 8,000 files decoded bit-exactly on both CPU and CUDA, and every file size matched its prediction. The rules were committed before the GPU run.
| Format | MiB per doc | Cold p50 (p95), ms | Warm p50, ms | Read, ms | Decode, ms | Cold vs recompute |
|---|---|---|---|---|---|---|
| BF16 | 26.35 | 57.4 (64.4) | 42.7 | 34.2 | 0.8 | 1.67x |
| FP8 per-tensor | 13.18 | 21.7 (24.4) | 9.9 | 14.0 | 4.0 | 0.63x |
| FP8 per-head | 13.18 | 21.6 (24.0) | 9.7 | 14.1 | 3.9 | 0.63x |
| INT8 per-token | 13.38 | 21.6 (22.8) | 9.6 | 14.2 | 3.8 | 0.63x |
| INT4 KIVI | 8.32 | 24.5 (27.5) | 16.7 | 9.0 | 13.1 | 0.71x |
| INT4 per-token (diagnostic) | 8.24 | 22.5 (23.6) | 14.8 | 8.9 | 11.3 | 0.65x |
| Uniform 75% + FP8 | 9.89 | 17.0 (18.2) | 8.0 | 10.5 | 3.9 | 0.49x |
| Uniform 75% + INT4 KIVI | 6.26 | 21.5 (22.8) | 15.4 | 7.1 | 12.4 | 0.62x |
Read and decode columns are cold p50s; sizes are the means of the 1,000 sampled docs. Full per-doc timings are in results/disk/, the summary in results/metrics/disk.json.
Bars are stage means (means add up; p50s don't); the tick on each bar and the dashed recompute line are p50s, matching the table.
- BF16 is slower to load than to recompute. At 57 ms cold vs 34 ms recompute, storing uncompressed document KV on disk doesn't pay off for this model and document length.
- Every compressed format loads faster than recompute, at 0.49 to 0.71x. Compression decides whether a disk tier is worth having at all, not just how big it is.
- This loader is copy-bound, not disk-bound. For BF16, parsing takes 20 ms and even a warm read from RAM takes 18.5 ms: two full CPU copies of a 26 MB file. Halving the bytes (FP8, INT8) cuts the cold load 2.6x, not 2x.
- The smallest format isn't the fastest. INT4 reads 5 ms faster than FP8 and INT8 but spends 11 to 13 ms decoding (unfused PyTorch: nibble unpacking, group expansion, 28 layers of small ops), so it loads slower. Pruning + FP8 is the fastest load (17.0 ms) even though pruning + INT4 is the smallest file. A fused dequantization kernel would likely change this order (not tested).
The order-gate miss in section 1 came with a hypothesis, written down before any BEIR result: the LoRA was trained on MS MARCO passages (median 77 tokens) but evaluated on BEIR documents (median 315 to 351, cut at 256), and doc-first, which reads the whole document before the query, might suffer more from that. To test it, both arms were retrained with the exact Phase 4 recipe on documents as long as BEIR's. The training script is a copy of the Phase 4 one that differs only in its training data and output paths, checked with diff. Each arm got one attempt, and the rules were committed before training.
Data: MS MARCO v1 document ranking (the official files, sha256 recorded). Doc text = title + body, cut to the first 256 tokens. Positives were kept only if their labeled passage appears inside that window. Only 19.8% pass; most of the rest don't contain the passage in the first 3,000 characters at all. Negatives come from the official BM25 top 30, with the same group rules as Phase 4. That gives 12k train and 1k dev queries, with long word-sequence and URL leakage checks against both test sets. Doc-first training inputs are a median 334 tokens (Phase 4: 155; BEIR SciFact's mean doc-first prompt is also 334).
nDCG@10 / MRR@10:
| System | SciFact | NFCorpus |
|---|---|---|
| Original query-first | 0.7555 / 0.7345 | 0.3485 / 0.5662 |
| Query-first LoRA, Phase 4 (passages) | 0.7617 / 0.7396 | 0.3504 / 0.5721 |
| Document-first LoRA, Phase 4 (passages) | 0.7504 / 0.7239 | 0.3543 / 0.5879 |
| Query-first LoRA, long documents | 0.7349 / 0.7057 | 0.3484 / 0.5767 |
| Document-first LoRA, long documents | 0.7300 / 0.7017 | 0.3507 / 0.5873 |
| Δ nDCG@10 [95% CI] | SciFact | NFCorpus |
|---|---|---|
| Doc-first − query-first, Phase 4 | −0.0113 [−0.0243, +0.0014] | +0.0039 [−0.0021, +0.0099] |
| Doc-first − query-first, long documents (order gate) | −0.0049 [−0.0181, +0.0089] pass | +0.0023 [−0.0044, +0.0086] pass |
| Gap change (long gap − Phase 4 gap; pre-registered test) | +0.0063 [−0.0087, +0.0218], not shown | −0.0016 [−0.0080, +0.0045], not shown |
| Doc-first: long − Phase 4 | −0.0204 [−0.0336, −0.0080] | −0.0036 [−0.0091, +0.0017] |
| Query-first: long − Phase 4 | −0.0267 [−0.0426, −0.0118] | −0.0020 [−0.0066, +0.0026] |
| Query-first long − original | −0.0206 [−0.0344, −0.0079] (flag: > 0.01 below) | −0.0001 [−0.0054, +0.0053] |
All CIs in this table use the same 10,000-resample paired bootstrap. The Phase 4 gap's CI here differs slightly from section 1's, which used 1,000 resamples.
- The pre-registered answer is "not shown." The doc-first gap on SciFact narrowed from −0.011 to −0.005 and the order gate now passes on both datasets, but the gap change's CI spans zero. The length hypothesis is neither supported nor ruled out.
- The gate passes for the wrong reason. Both arms got worse on SciFact (−0.020 and −0.027 vs Phase 4, CIs below zero), and the long-document query-first arm falls more than one point below the original model, which trips a pre-registered flag. NFCorpus barely moved.
- The new data changed more than length. Its negatives are much harder lexically: they contain 76% of the query terms vs 59%, and the hardest negative ties or beats the positive in 90% of groups vs 60%. The documents are raw web pages rather than curated passages, and the positives were filtered. Any of these could explain the drop, so this retrain can't isolate length. A cleaner test would lengthen the Phase 4 passages without changing their source or negatives.
- The Phase 4 LoRA stays the model for every result in this repo. It is better in absolute terms on both datasets.
- Truncation: docs are cut at 256 tokens (71 to 77% are truncated), so absolute scores sit below full-length published numbers. Rerank depth is 20.
- Scope: one model (0.6B), two datasets, one seed.
- The order gate misses by 0.0013 on SciFact (see above). The LoRA was trained on short MS MARCO passages (median 77 tokens) and evaluated on longer BEIR docs. The length-matched retrain (section 8) changed negatives, sources and labels along with length, and both of its arms lost quality on SciFact, so it doesn't settle the cause. One attempt per arm.
- Fake quant: values are dequantized to bf16 before attention. Quality and storage are measured; quantized-attention speed is not.
- Post-RoPE keys: keys are quantized after RoPE, because that's what is stored. Pre-RoPE quantization (KVQuant) may do better.
- Strict margin: the ±0.002 margin is about 10x the measured noise. INT4 KIVI lands in "within 1 point" because its CI is wider than the margin (on SciFact, on the upside), not because of a measured loss.
- Shared prefix: quality was measured with each doc's own copy of the shared prefix. Using one canonical copy, as the storage numbers assume, shifts scores by at most 0.048 log-odds (6x below the noise floor).
- 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 by every layer. Learned or per-layer selection may do better. Random pruning uses one seed. The early-token finding is post hoc. Pruning storage is measured on packed BF16 files.
- Combined compression: one pruning setting (uniform 75%, chosen by the Phase 6 rule) and two formats. The interaction CIs on SciFact are about ±0.004 wide, so a compounding effect smaller than that can't be ruled out. "Smallest point within one point" is relative to the settings measured here.
- Disk: one pod's container disk and our own Python loader (two full CPU copies per file, pageable host memory, unfused INT4 dequantization). 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 a 0.6B model at about 300 tokens; the balance depends on model size and document length.
- Hardware: Mac timing comes from a fanless laptop and is only relative.
- Serving: one rented RTX 4090, one run per configuration, vLLM 0.34 defaults (64-token blocks), a closed-loop replay of BEIR test queries with no arrival times, and FP8 KV without calibrated scales. The query-first serving baseline is the original model; the doc-first settings use the LoRA. These are benchmarks, not a production deployment.
- Released weights: MS MARCO is released for non-commercial research, so check its terms before using the adapter commercially.
- Follow-ups: a pre-registered "keep the first N tokens" pruning arm; learned or per-layer token selection; a zero-copy loader and fused INT4 dequantization; 16-token blocks and calibrated FP8 KV scales in vLLM (planned for Phase 8, skipped to stay within budget); a retrain that changes only document length.
python -m venv .venv && source .venv/bin/activate
pip install -r requirements.txt && pip install -e . --no-deps
python scripts/00_download_models.py && python scripts/00_check_env.py
python scripts/01_build_candidates.py
python scripts/02_tokenize.py && python scripts/02b_tokenize_docfirst.py
python scripts/03_score_reference.py --models qwen3 bge
python scripts/04_docfirst_zeroshot.py && python scripts/04b_random_baseline.py
python scripts/05_equality_test.py
python scripts/06a_build_train_data.py
python scripts/06_train_lora.py --order df --grad-ckpt --micro-groups 8 # GPU (RTX 4090), trains and merges
python scripts/06_train_lora.py --order qf --grad-ckpt --micro-groups 8
python scripts/06b_merge_check.py && python scripts/06c_eval_lora.py && python scripts/06d_equality_lora.py
python scripts/07a_noise_floor.py && python scripts/07_quant_sweep.py
python scripts/08_pack_storage.py
python scripts/09a_importance.py && python scripts/09_prune_sweep.py && python scripts/09b_prune_summary.py # pruning (~1.7 h on the Air)
python scripts/10_combined_sweep.py && python scripts/10b_combined_summary.py # combined compression (~1 h on the Air)
python scripts/11a_build_traces.py && python scripts/11c_gate_reference.py # serving traces + gate reference
python scripts/13_make_plots.py # README figures (results/plots/)
python scripts/14_export_app_data.py && python scripts/14b_export_explorer.py # app data (app/data/, app/public/explorer/; 14b needs the p6/p7 tarballs unpacked)Skipping doc-first training: download the merged weights into the path every later script loads from; the scripts check model.safetensors against the sha256 in marginkv_merge.json. The query-first control (used by 06c for the order gate) is not released and still needs training.
hf download ana-lan/marginkv-qwen3-reranker-0.6b-docfirst --local-dir artifacts/lora/df/mergedServing (GPU, separate venv because vLLM pins its own torch), one setting at a time (qf, df, df_fp8):
python -m venv vllm-venv && source vllm-venv/bin/activate
pip install -r requirements-vllm.txt && pip install -e . --no-deps
bash scripts/11_serve.sh df
python scripts/11b_vllm_gate.py --setting df
python scripts/11_vllm_bench.py --setting df --traces t3 t1 t2 --concurrency 16 4 1Disk benchmark (GPU, training venv; about 1.9 h on an RTX 4090 because every file is exactness-checked) and the length-matched retrain (needs the four MS MARCO v1 document-ranking files in data/msmarco_doc/, about 8.9 GB):
python scripts/12_disk_bench.py
python scripts/06e_build_train_data_long.py --pool 110000 # Mac, ~5 min
python scripts/06f_train_lora_long.py --order df --grad-ckpt --micro-groups 4 # GPU, ~65 min each, trains and merges
python scripts/06f_train_lora_long.py --order qf --grad-ckpt --micro-groups 4
python scripts/06g_eval_lora_long.py # Mac, ~85 minSummaries from the committed run files (the pruning summary also needs 09a's importance artifacts; the combined summary loads the model to check a sample of packed files):
tar xzf results/vllm/runs.tar.gz -C results/vllm
python scripts/11d_vllm_summary.py
tar xzf results/scores/p6_scores.tar.gz
python scripts/09b_prune_summary.py
tar xzf results/scores/p7_scores.tar.gz
python scripts/10_combined_sweep.py && python scripts/10b_combined_summary.pyHugging Face release (scripts/15_hf_release.py): --stage check writes the model cards from results files and checks each card's snippet against this repo's scorer offline; --stage upload and --stage verify publish and re-download (sha256 of every file). The record is release/hf_release.json.
Every script writes JSON to results/ with provenance (git commit, package versions, input hashes). Long runs are resumable and have a --smoke N mode.
Tests: python tests/test_<name>.py (plain asserts).
src/marginkv/ config, data, retrieval (BM25), models (Qwen3, bge), kv (cache ops, sweep, quant, pack, prune, combined), serving (vLLM client), train (LoRA), eval
scripts/ numbered pipeline steps
tests/ parity, equality, quantizer, packing, pruning and combined-compression tests
results/ committed JSON: candidates, scores, metrics, traces, vLLM runs, disk benchmarks; plots/ holds the README figures
release/ Hugging Face model cards and the release record
app/ the results explorer (static Next.js site, reads app/data/ and app/public/explorer/, exported from results/)
Builds directly on HyperRAG (An, Cheng, Park, Jiang, 2025) and the LMCache line of work on KV reuse. Quantization follows KIVI (Liu et al., 2024). Pruning baselines follow H2O (Zhang et al., 2023) and StreamingLLM (Xiao et al., 2023). The load-vs-recompute framing follows CacheGen (Liu et al., 2024). The training recipe follows RankLLaMA (Ma et al., 2023). Serving uses vLLM. Model: Qwen3-Reranker-0.6B. Data: BEIR, MS MARCO.
MIT (code). The released adapter and merged weights follow the base model's Apache-2.0 license; see the model cards.