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.2, 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.2
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 = extract_attention(model, inputs, heads=[(0, 3), (12, 7)])
head = patterns[("text", 0, 3)] # CPU tensor: [batch, query tokens, key tokens].
Use the same call with AutoModelForImageTextToText and processor outputs:
# (layer, head) means text attention; qualify a vision head with its stream.
patterns = 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. 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 = 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 development checkout:
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/*
Tests and the example notebook are development-only and 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.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| attnread-0.1.2.tar.gz | 9.9 kB | Without provenance | Details for attnread-0.1.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| attnread-0.1.2-py3-none-any.whl | Python 3 | none | any | Details for attnread-0.1.2-py3-none-any.whl |
Total release size: 17.3 kB
Release files / attnread-0.1.2.tar.gz
| Download URL | attnread-0.1.2.tar.gz |
|---|---|
| Size | 9.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
849b05b99c6b1093f971b31ad5e1d4974557152ec12758f4a20c310599caceb5
|
|
BLAKE2b-256 checksum How to use checksums |
5dd0e2d733984bba9f608ee2b052af9277055f017cd01082cdeea105ab48f80f
|
| 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.2-py3-none-any.whl
| Download URL | attnread-0.1.2-py3-none-any.whl |
|---|---|
| Size | 7.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
478ab9d4543e28713d3104645521689593c5e669c422413f64c8ef29966a1e60
|
|
BLAKE2b-256 checksum How to use checksums |
bab1df400ea098ccee2fb9fffb4d3b12704a262a4ef658373990c4883fd6db60
|
| 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.2
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.