Skip to main content

attnview

attnview mostra padrões de atenção de Transformers no terminal ou em um HTML interativo. A entrada é uma camada no formato [n_heads, query_length, key_length]: linhas são queries; colunas são keys.

Comece aqui — dois comandos

pip install --upgrade attnview

Quer saber se está funcionando? Rode exatamente isto:

attnview demo

Você verá os pesos de uma query, com tokens e posições explícitos. Para gerar o relatório HTML:

attnview demo --output attention.html

O comando informa o caminho do arquivo. Abra attention.html no navegador; não há servidor para iniciar nem URL para procurar.

Use com sua atenção real

import attnview

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

# Cria o arquivo HTML e informa onde ele foi salvo.
attnview.show(attention, tokens=tokens, output="attention.html")

Se seus dados já estão em JSON, você também pode renderizá-los diretamente:

attnview render attention.json --tokens tokens.json
attnview render attention.json --tokens tokens.json --output attention.html

attention.json contém a matriz [heads, queries, keys]; tokens.json é uma lista como ["[BOS]", "The", "cat", "sat", "."].

Como ler a tela

A versão 0.2 começa pela pergunta: para quais keys esta query distribui atenção? Escolha query=2 na API ou --query 2 no terminal. No HTML, clique no token; trocar de head preserva essa seleção. Todos os índices começam em zero.

Os destinos aparecem na ordem da sequência, acompanhados do peso bruto. No HTML, você pode ordenar a tabela por peso ou abrir a matriz completa. Barras representam magnitude relativa ao maior peso absoluto da query; os dados não são normalizados. O terminal imprime um relatório estático, inclusive sem cores; não é uma TUI.

  • Rows are queries: the token doing the attending.
  • Columns are keys: the token being attended to.
  • Repeated tokens are distinguished by their sequence positions.
  • Weight displays use six significant digits; the expanded HTML matrix exposes the stored numeric values.

API Python

attnview.show(
    attention,
    tokens=None,
    query_tokens=None,
    key_tokens=None,
    head=0,
    query=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.2.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.2.0
File Size Uploaded
attnview-0.2.0.tar.gz 13.8 kB Details

Built distribution (wheel)

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

Total release size: 26.0 kB

Release files / attnview-0.2.0.tar.gz

Download URL attnview-0.2.0.tar.gz
Size 13.8 kB
Tags Source
SHA-256 checksum
How to use checksums
e96491c6c8aa2a29d8aba687ac8975584d3e584431fb4dec316aacd51396312c
BLAKE2b-256 checksum
How to use checksums
59c0bd78ec7b99849861bd62f7819fde79a245f9c5c952f51e8a10264d2b9f99
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.2.0-py3-none-any.whl

Download URL attnview-0.2.0-py3-none-any.whl
Size 12.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
afd55fab7998de5be02d736f1d3dbd6f45de8823b7545ee4d737b0cade7a7e88
BLAKE2b-256 checksum
How to use checksums
c947c3a636579eea0dfdfd1f5604bd65a5abe424750b49535448e7952889f6a9
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

This release

0.2.0 This release

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