Skip to main content

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:

  1. Needle-in-a-haystack (NIAH) – insert a known fact into a long context at varying depths and lengths, then measure retrieval accuracy (ROUGE-L).
  2. 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

NIAH Heatmap

Retrieval Head Detection

Retrieval Head Detection Heatmap

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

retrieval_heads-0.1.0.tar.gz (17.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

retrieval_heads-0.1.0-py3-none-any.whl (19.5 kB view details)

Uploaded Python 3

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

Hashes for retrieval_heads-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f08ff59e5feac80e475fd54e2859f821b42302f866a4604a22b2eb2b0c7cb496
MD5 b0e43cc722f98e01da3d43894ad7b80f
BLAKE2b-256 503d0a67d6876074d89017ec6aa00cdf634bbaa0c683b44460b0c3edd6871fdf

See more details on using hashes here.

Provenance

The following attestation bundles were made for retrieval_heads-0.1.0.tar.gz:

Publisher: python-publish.yml on maxzuo/retrieval-heads

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for retrieval_heads-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d7c7ebabb7e68a4b65490f0070998416bfbe160bbd23522aab0ed3492732bbb2
MD5 a201d67c6af46c5e42a9ab4f97f66302
BLAKE2b-256 970615a1b36bc26eebae8e53605e676477bf48248bf21db48f851e19747e1590

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page