Skip to main content

attnview

attnview is a small, terminal-first viewer for transformer attention patterns. It accepts a single attention layer shaped [n_heads, query_length, key_length], keeps query and key axes explicit, and renders either a readable terminal heatmap or a self-contained interactive HTML report.

pip install attnview
import attnview

# `attention` can be a NumPy array, a PyTorch tensor, or nested Python sequences.
attnview.show(attention, tokens=["[BOS]", "The", "cat", "sat", "."])

# Write a standalone report that can be opened or shared without a server.
attnview.show(attention, tokens=tokens, output="attention.html")

Reading the view

  • Rows are queries: the token doing the attending.
  • Columns are keys: the token being attended to.
  • The selected head is rendered in the main heatmap; the HTML report also offers one-click head selection through miniature maps.
  • Amber marks high attention, teal marks moderate attention, and the darkest cells are near zero. This deliberately avoids a rainbow palette that makes magnitude difficult to read.

API

attnview.show(
    attention,
    tokens=None,
    query_tokens=None,
    key_tokens=None,
    head=0,
    output=None,
    top_k=3,
    title="Attention Explorer",
)

Pass tokens for self-attention. For cross-attention, pass query_tokens and key_tokens independently. With output=None (the default), show prints to the current terminal. With an .html path, it writes a standalone HTML document and returns its Path.

render_html(...) returns the standalone HTML string when embedding it elsewhere.

Requirements

Python 3.10+ and Rich. NumPy and PyTorch are not required: if they are installed, their arrays/tensors are accepted directly.

Development

python -m pip install -e '.[dev]'
python -m pytest
python -m build
python -m twine check dist/*

License

MIT

Metadata

Release files for attnview 0.1.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.1.0
File Size Uploaded
attnview-0.1.0.tar.gz 11.4 kB Details

Built distribution (wheel)

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

Total release size: 21.9 kB

Release files / attnview-0.1.0.tar.gz

Download URL attnview-0.1.0.tar.gz
Size 11.4 kB
Tags Source
SHA-256 checksum
How to use checksums
74fd73ef6dac796e0573904e06c07c87dad4dc13c1ad68f6147b4fe6f43471e9
BLAKE2b-256 checksum
How to use checksums
9b2c5aeeaee4faf639e95c5f552329ff6970a28e9096e8013aa9815336441f32
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.1.0-py3-none-any.whl

Download URL attnview-0.1.0-py3-none-any.whl
Size 10.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c2e505c854c1c0f485e778e75ba9840d21de3f7974fb9c13d9cb9e67663341a9
BLAKE2b-256 checksum
How to use checksums
f2f926503fd7ccefe6010fd74757eecb9c6248a4c781afbbcf8564eb17d0aaf3
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

0.5.0

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

This release

0.1.0 This release

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