attnview
A notebook-first, dark HTML atlas for Transformer attention patterns.
attnview turns attention matrices into a focused research view: choose a layer,
head, and query position; inspect the strongest destinations; then open the exact
row or full matrix when you need to verify a detail. It is designed for
mechanistic-interpretability work in Jupyter.
Install
pip install -U attnview
For notebooks, install the optional IPython extra when IPython is not already available:
pip install -U "attnview[notebook]"
Quick start
attention has shape [n_heads, query_length, key_length]. The first sequence
axis is the query (destination) position; the second is the key (source) position.
import attnview
view = attnview.display(
attention,
tokens=tokens,
title="My attention study",
)
view
The result is a self-contained HTML object rendered inline by Jupyter. Long prompts stay readable through a local token window and search, while the complete values remain available in the exact-matrix panel.
TransformerLens integration
Pass the ActivationCache returned by run_with_cache. attnview finds canonical
blocks.{layer}.attn.hook_pattern entries, ignores unrelated activations, and
combines all discovered layers into one atlas.
import attnview
tokens = model.to_tokens(prompt)
logits, cache = model.run_with_cache(tokens)
view = attnview.display_cache(
cache,
tokens=model.to_str_tokens(tokens),
)
view
Export the same report as a portable artifact:
attnview.export_cache(
cache,
"attention-atlas.html",
tokens=model.to_str_tokens(tokens),
)
TransformerLens is not a runtime dependency. The adapter accepts any mapping-like cache exposing the same pattern keys, which makes saved fixtures and small experiments easy to inspect.
Cross-attention and explicit labels
For encoder-decoder or other cross-attention experiments, pass query and key labels separately:
attnview.display(
cross_attention,
query_tokens=decoder_tokens,
key_tokens=encoder_tokens,
)
A reproducible research workflow
See the TransformerLens research guide for a complete notebook recipe, a small attention-concentration study, interpretation cautions, and primary-literature references.
API at a glance
| Function | Use |
|---|---|
display |
Render one pattern or an AttentionStudy inline in Jupyter |
render_html |
Return the HTML string for custom notebook pipelines |
export_html |
Write one pattern to a .html file |
from_cache |
Build an AttentionStudy from a TransformerLens-style cache |
display_cache |
Render every attention layer from a cache inline |
export_cache |
Export a cache-backed atlas to a .html file |
Development
python -m pip install -e ".[dev,notebook]"
python -m pytest
python -m build
python -m twine check dist/*
Contributions and bug reports are welcome. Please read CONTRIBUTING.md and the Code of Conduct first.
License
MIT. See LICENSE.
Metadata
Release files for attnview 0.5.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| attnview-0.5.1.tar.gz | 15.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| attnview-0.5.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 28.3 kB
Release files / attnview-0.5.1.tar.gz
| Download URL | attnview-0.5.1.tar.gz |
|---|---|
| Size | 15.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c3fad740d9fb536ebec538ca63acafc0c5e1134bcccae5e43a89849e5311c1e7
|
|
BLAKE2b-256 checksum How to use checksums |
3c358c4d700e809c4079c99ad19b03ef5ac2ab694c9025ec580496e3baab876b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.7
|
Release files / attnview-0.5.1-py3-none-any.whl
| Download URL | attnview-0.5.1-py3-none-any.whl |
|---|---|
| Size | 13.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7d1b0fbf3e8c5c08767411570b89c535e76f593e3e59d7acc9be644bdb0338ee
|
|
BLAKE2b-256 checksum How to use checksums |
fc5704d1f42e00c12be959f89d437af807ab3aa72b1934b3758e228db1486733
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.7
|