Results
BEIR test sets, BM25 top 100, top 20 reranked, documents truncated to 256 tokens. nDCG@10 / MRR@10:
Table with columns: System, SciFact, NFCorpus| System | SciFact | NFCorpus |
|---|
| BM25 | 0.6791 / 0.6431 | 0.3187 / 0.5245 |
| Qwen3-Reranker-0.6B, original (query first) | 0.7555 / 0.7345 | 0.3485 / 0.5662 |
| Query-first LoRA (control, same recipe) | 0.7617 / 0.7396 | 0.3504 / 0.5721 |
| Document first, zero-shot | 0.2855 / 0.2219 | 0.2350 / 0.3705 |
| Document first, this adapter | 0.7504 / 0.7239 | 0.3543 / 0.5879 |
Order gate (document first minus the query-first control, needs > −0.01; 95% paired bootstrap CI): SciFact −0.0113 [−0.0240, +0.0011], fail; NFCorpus +0.0039 [−0.0016, +0.0096], pass. SciFact misses by 0.0013 with a CI that includes 0; reported as a fail, not rounded away.
The stored document KV compresses well with this model: INT4 (KIVI-style) 8.33 MiB/doc vs 26.40 MiB BF16 (3.17x) at +0.0010 / −0.0009 nDCG@10 (SciFact / NFCorpus, vs the BF16 cache); with 75% token pruning, 6.27 MiB (4.21x) at −0.0028 / −0.0001. Details, serving and disk results: repo, results explorer.
Prompt layout (this is what the adapter was trained on)
cached, query-independent: <system prefix><Instruct>: {instruction}\n<Document>: {doc}\nper query: <Query>: {query}<assistant suffix>
Each piece is tokenized separately and the ids are concatenated (see doc_ids / query_ids below). The newline after the document belongs to the document piece, so text ending in . tokenizes as it would naturally. Score = logit("yes") minus logit("no") at the last position. In the evaluation each document was cut to 256 tokens before the newline was added; the snippet does not truncate, and longer documents were not tested. Any other layout gives poor rankings: the base model read document first scores 0.2855 nDCG@10 on SciFact.
To reuse the document KV, run doc_ids(doc) once with use_cache=True, keep past_key_values, and feed query_ids(query) with position ids continuing from len(doc_ids(doc)). The repo's marginkv/kv/cache_ops.py does this in batches and is checked against full prompts (at most 3e-5 log-odds in fp32).
Usage
import torchfrom peft import PeftModelfrom transformers import AutoModelForCausalLM, AutoTokenizer BASE, BASE_REVISION = "Qwen/Qwen3-Reranker-0.6B", "e61197ed45024b0ed8a2d74b80b4d909f1255473"ADAPTER = "ana-lan/marginkv-qwen3-reranker-0.6b-docfirst-lora" tok = AutoTokenizer.from_pretrained(BASE, revision=BASE_REVISION)model = AutoModelForCausalLM.from_pretrained(BASE, revision=BASE_REVISION, dtype=torch.float32)model = PeftModel.from_pretrained(model, ADAPTER).merge_and_unload().eval() INSTRUCTION = 'Given a web search query, retrieve relevant passages that answer the query'PREFIX = '<|im_start|>system\nJudge whether the Document meets the requirements based on the Query and the Instruct provided. Note that the answer can only be "yes" or "no".<|im_end|>\n<|im_start|>user\n'SUFFIX = '<|im_end|>\n<|im_start|>assistant\n<think>\n\n</think>\n\n'YES, NO = tok.convert_tokens_to_ids("yes"), tok.convert_tokens_to_ids("no") def ids(text): return tok(text, add_special_tokens=False)["input_ids"] def doc_ids(doc): """Query-independent part: compute its KV once per document and reuse it. doc = title + " " + text.""" return ids(PREFIX) + ids(f"<Instruct>: {INSTRUCTION}\n<Document>:") + ids(" " + doc + "\n") def query_ids(query): return ids("<Query>:") + ids(" " + query) + ids(SUFFIX) @torch.no_grad()def score(query, doc): """Log-odds of "yes" (higher = more relevant).""" x = torch.tensor([doc_ids(doc) + query_ids(query)]) h = model.model(input_ids=x).last_hidden_state[:, -1].float() logits = h @ model.lm_head.weight[[NO, YES]].float().T return (logits[0, 1] - logits[0, 0]).item() title, text = "Scurvy", "Scurvy is a disease caused by a lack of vitamin C."print(score("What causes scurvy?", title + " " + text))
Tested with transformers 5.23.0, peft 0.22.0, torch 2.15.0 (on transformers 4.x use torch_dtype= instead of dtype=). The release script checks that this exact snippet reproduces the repo's scorer (identical token ids, CPU fp32 log-odds within 0.0001).
Training
Listwise softmax cross-entropy over groups of 8 (1 positive, 7 hard negatives) from Tevatron/msmarco-passage, 12,000 training queries; LoRA r 16, alpha 32, dropout 0.05, on q_proj, k_proj, v_proj, o_proj, gate_proj, up_proj, down_proj; lr 0.0001, 1 epoch, 8 groups per step. Checkpoint chosen by MRR on 1,000 held-out MS MARCO training queries (never on BEIR). Trained on one RTX 4090; the query-first control used the same recipe and data.
Limitations
- Trained on MS MARCO passages (median 77 tokens), evaluated on BEIR documents truncated to 256 tokens; one model size, two datasets, one seed. A length-matched retrain did not explain the SciFact gap (see the repo).
- Only the default instruction was tested.
- MS MARCO is released for non-commercial research; check its terms before commercial use. The adapter itself follows the base model's Apache-2.0 license.
Credits
Qwen3-Reranker; HyperRAG (An et al., 2025, arXiv 2504.02921), which introduced document-side KV reuse for decoder-only rerankers; MS MARCO and the Tevatron copy used for training.