Skip to main content

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.

Attention Atlas screenshot

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.0

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

Source distribution (sdist)

Source distribution for attnview 0.5.0
File Size Uploaded
attnview-0.5.0.tar.gz 15.1 kB Details

Built distribution (wheel)

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

Total release size: 28.3 kB

Release files / attnview-0.5.0.tar.gz

Download URL attnview-0.5.0.tar.gz
Size 15.1 kB
Tags Source
SHA-256 checksum
How to use checksums
e232c5b61af3c4c48e2820e2b4e1aca48959def133512471fa1414eb48133924
BLAKE2b-256 checksum
How to use checksums
9c0fb45c2326e28fcfe08d619ef974c0777e653856b0fda506968412162062cc
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.0-py3-none-any.whl

Download URL attnview-0.5.0-py3-none-any.whl
Size 13.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
466f52e66e625c7bd1cc56a160936082166a022cdcfee0779500009c1f3a7d4a
BLAKE2b-256 checksum
How to use checksums
97a919b55fc8fa0480c0ce2b6ad0c76475903a774f3e994515d2ae82ebb7752a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.7

Release history Release notifications | RSS feed

0.5.1

2 release files

This release

0.5.0 This release

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.1

2 release files

0.1.0

2 release files

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