attnview
attnview foi feito para pesquisa em notebooks. Ele mostra padrões de atenção
de Transformers em uma saída interativa, com uma saída estática equivalente no
terminal e exportação HTML. 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 testar fora de um notebook? 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.
No notebook
Deixe display(...) como a última linha da célula. O resultado aparece dentro
do notebook e permite trocar head, query, ordenação e matriz completa.
import attnview
attnview.display(attention, tokens=tokens)
Se o seu ambiente ainda não tiver IPython:
pip install 'attnview[notebook]'
Use com sua atenção real
import attnview
# `attention` can be a NumPy array, a PyTorch tensor, or nested Python sequences.
# Saída estática para logs e scripts.
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.
display(...) é a função principal para notebooks. show(...) é a saída estática
de terminal. render_html(...) retorna o documento HTML como string.
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.3.0
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.3.0.tar.gz | 14.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| attnview-0.3.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 27.1 kB
Release files / attnview-0.3.0.tar.gz
| Download URL | attnview-0.3.0.tar.gz |
|---|---|
| Size | 14.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d624e3d69ee41647e5e6430a0c75e7d4c5a1431217e03e374a8c4248d9a55dd9
|
|
BLAKE2b-256 checksum How to use checksums |
f403f299fdcaf5e1d611efed1a32388aa9e1802d749f7fe3de2a0a97a38d1515
|
| 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.3.0-py3-none-any.whl
| Download URL | attnview-0.3.0-py3-none-any.whl |
|---|---|
| Size | 12.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ea9fd03ce87bea60e27cc571c295a8d85eead3a0981690dacfede7c0d0f84697
|
|
BLAKE2b-256 checksum How to use checksums |
fa293e0f66c1e09abedad023bfb8e9a703571c50dd7646c442b20f603d5f4507
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.7
|