Skip to main content

AttnRead

Recover attention weights without changing the attention backend.

AttnRead is a lightweight Python toolkit for extracting attention patterns from optimized transformer attention implementations, including PyTorch SDPA and FlashAttention 2.

Using identity-value passes and activation restoration, AttnRead recovers attention patterns while preserving the model's downstream computation. No fallback to eager attention required.

Version 0.1.1, distributed under the MIT License. The distribution and Python import name are both attnread.

Installation

Use Python 3.11 or newer in a venv. Install your preferred CPU/CUDA PyTorch build first, then install AttnRead:

python -m pip install attnread==0.1.1

Usage

import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
from attnread import extract_attention

checkpoint = "Qwen/Qwen3-4B"
model = AutoModelForCausalLM.from_pretrained(
    checkpoint, dtype=torch.bfloat16,
    attn_implementation="sdpa",
).to("cuda").eval().requires_grad_(False)
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
inputs = tokenizer("Describe the colors of an autumn leaf.",
                   return_tensors="pt").to(model.device)

patterns, checks = extract_attention(model, inputs, heads=[(0, 3), (12, 7)])
head = patterns[("text", 0, 3)]  # CPU tensor: [batch, query tokens, key tokens].
assert checks["probe_logit_max_abs"] == 0

Use the same call with AutoModelForImageTextToText and processor outputs:

# (layer, head) means text attention; qualify a vision head with its stream.
patterns, checks = extract_attention(
    vlm, processed_inputs, heads=[(12, 3), ("vision", 4, 2)],
)
text = patterns[("text", 12, 3)]
vision = patterns[("vision", 4, 2)]

Selection. heads=None extracts every discovered text head. A sequence of (layer, head) or (stream, layer, head) addresses extracts only those heads; returned keys always use the full triple, in request order. Layers and heads are zero-based within each stream, following discovered module order (reported in checks["modules"]). Use explicit paths for sparse/hybrid stacks or unusual module naming. Different streams can have different sequence lengths, so results are a dictionary. Probes are grouped by layer and save/transfer only selected heads. The underlying model still computes all heads; head selection reduces saved memory, not the number of identity probes. Start with one short prefix and one head. Unknown, duplicate or out-of-range addresses raise ValueError.

Model interface. Pass an ordinary HF model in evaluation mode and its preprocessed tensor input mapping. Discovery recognizes separate q_proj, k_proj, v_proj with o_proj/out_proj, GPT-2's fused c_attn/c_proj, and Qwen3-VL vision qkv/proj. Vision module paths identify the vision stream; others belong to text. For unusual or ambiguous naming, supply explicit paths:

patterns, checks = extract_attention(model, inputs, heads=[("text", 0, 1)],
    attention_modules={"text": ["model.layers.7.self_attn"]})
# With explicit lists, layer 0 denotes the first entry in that list.

Dependencies. AttnRead requires Python 3.11 or newer, NNsight 0.7.0, Transformers 5.19.x, and PyTorch 2.14.x. Install a PyTorch build suitable for your CPU or GPU. FlashAttention 2 is optional and installed separately.

Development

From the standalone source directory:

python -m venv .venv
source .venv/bin/activate
# Install your preferred CPU/CUDA PyTorch build first.
python -m pip install -e '.[dev]'
python -m pytest -q test_attnread.py
python -m build
python -m twine check --strict dist/*

The source distribution includes the package tests and release guide. The example notebook uses in-memory inputs and is excluded from release archives.

License

MIT License. Copyright (c) 2026 Etha Tianze Hua. The full license text is included in the source archive and wheel.

Metadata

Release files for attnread 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for attnread 0.1.1
File Size Uploaded Provenance Actions
attnread-0.1.1.tar.gz 13.3 kB Without provenance Details for attnread-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for attnread 0.1.1
File Interpreter ABI Platform Actions
attnread-0.1.1-py3-none-any.whl Python 3 none any Details for attnread-0.1.1-py3-none-any.whl

Total release size: 21.4 kB

Release files / attnread-0.1.1.tar.gz

Download URL attnread-0.1.1.tar.gz
Size 13.3 kB
Tags Source
SHA-256 checksum
How to use checksums
a095310bc8591daefe096aa64e3bbc56ecca7ab4581055ae62c1006a777e84e4
BLAKE2b-256 checksum
How to use checksums
2b59de743791f282964356d47c748254200496cc817e68ed8d19ea27d8e7791c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.11

Release files / attnread-0.1.1-py3-none-any.whl

Download URL attnread-0.1.1-py3-none-any.whl
Size 8.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0003bc4658f767ea3a5b4d74a42bf1bcd8dfccd0df7b3a287c1991eea437ff4f
BLAKE2b-256 checksum
How to use checksums
d6b10df41aac906f2362951709d625fab99a504485a344b4e8711a2b87e02921
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.11

Security for attnread 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page