Retrieval Head detection in LLMs with vLLM
Project description
retrieval-heads
Retrieval head detection in LLMs using vLLM and nnsight activation tracing.
This is my attempt to faithfully reproduce Retrieval Head Mechanistically Explains Long-Context Factuality, and should work out of the box with any model that uses vLLM's Attention or GatedDeltaNetAttention implementations.
Two main workflows:
- Needle-in-a-haystack (NIAH) – insert a known fact into a long context at varying depths and lengths, then measure retrieval accuracy (ROUGE-L).
- Retrieval head detection – trace query/key activations through every attention head on NIAH results to identify which heads are responsible for retrieval.
Example Results
NIAH Heatmap
Retrieval Head Detection
Setup
Installation:
git clone https://github.com/maxzuo/retrieval-heads.git
pip install -e .
Tested using Python 3.12 and vLLM 0.19.0.
Usage
NIAH sweep
retrieval-heads.niah --config configs/qwen3_5_9b.yaml
Runs the needle-in-a-haystack evaluation across a grid of context lengths and
document depths. Results are written to output_dir as results.jsonl (one
JSON record per cell) alongside the resolved config.yaml.
Any config field can be overridden via CLI flags:
retrieval-heads.niah --config configs/qwen3_5_9b.yaml \
--model.max-model-len 16384 \
--output-dir ./results/short
Retrieval head detection
retrieval-heads.detect --config configs/detect.yaml
Takes NIAH result files as input, traces each forward pass with nnsight to
capture per-head query/key matrices, and scores each head on whether it attends
to the needle span. Outputs detected.json and detected-agg.json.
Visualization
retrieval-heads.visualize niah --results results/qwen3_5_9b/results.jsonl
retrieval-heads.visualize detect --results results/detect/detected-agg.json
Configuration
Configs are YAML files with the following sections:
model:
model: Qwen/Qwen3.5-9B
max_model_len: 32768
dtype: bfloat16
chat_template: path/to/template.jinja
language_model_only: true
haystack:
haystack_dir: ./PaulGrahamEssays
needle: "\nThe best thing to do in San Francisco is eat a sandwich...\n"
retrieval_question: "What is the best thing to do in San Francisco?"
sweep:
context_lengths: {min: 1000, max: 32000, intervals: 31}
document_depths: {min: 0, max: 100, intervals: 10}
output_dir: ./results/qwen3_5_9b
Sweep dimensions accept either a {min, max, intervals} shorthand or an
explicit list of values.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file retrieval_heads-0.1.0.tar.gz.
File metadata
- Download URL: retrieval_heads-0.1.0.tar.gz
- Upload date:
- Size: 17.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f08ff59e5feac80e475fd54e2859f821b42302f866a4604a22b2eb2b0c7cb496
|
|
| MD5 |
b0e43cc722f98e01da3d43894ad7b80f
|
|
| BLAKE2b-256 |
503d0a67d6876074d89017ec6aa00cdf634bbaa0c683b44460b0c3edd6871fdf
|
Provenance
The following attestation bundles were made for retrieval_heads-0.1.0.tar.gz:
Publisher:
python-publish.yml on maxzuo/retrieval-heads
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
retrieval_heads-0.1.0.tar.gz -
Subject digest:
f08ff59e5feac80e475fd54e2859f821b42302f866a4604a22b2eb2b0c7cb496 - Sigstore transparency entry: 1928811811
- Sigstore integration time:
-
Permalink:
maxzuo/retrieval-heads@8cd65228cbbb9b514609170ed8af8a08116804a2 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/maxzuo
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@8cd65228cbbb9b514609170ed8af8a08116804a2 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file retrieval_heads-0.1.0-py3-none-any.whl.
File metadata
- Download URL: retrieval_heads-0.1.0-py3-none-any.whl
- Upload date:
- Size: 19.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d7c7ebabb7e68a4b65490f0070998416bfbe160bbd23522aab0ed3492732bbb2
|
|
| MD5 |
a201d67c6af46c5e42a9ab4f97f66302
|
|
| BLAKE2b-256 |
970615a1b36bc26eebae8e53605e676477bf48248bf21db48f851e19747e1590
|
Provenance
The following attestation bundles were made for retrieval_heads-0.1.0-py3-none-any.whl:
Publisher:
python-publish.yml on maxzuo/retrieval-heads
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
retrieval_heads-0.1.0-py3-none-any.whl -
Subject digest:
d7c7ebabb7e68a4b65490f0070998416bfbe160bbd23522aab0ed3492732bbb2 - Sigstore transparency entry: 1928811924
- Sigstore integration time:
-
Permalink:
maxzuo/retrieval-heads@8cd65228cbbb9b514609170ed8af8a08116804a2 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/maxzuo
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@8cd65228cbbb9b514609170ed8af8a08116804a2 -
Trigger Event:
workflow_dispatch
-
Statement type: