attnspectra
attnspectra es una librería para analizar el comportamiento de la atención en modelos transformer mediante métricas espectrales, entropía y visualizaciones.
Instalación
pip install attnspectra
Extras opcionales:
# Soporte para modelos Hugging Face
pip install attnspectra[hf]
# Visualización interactiva (Plotly + widgets)
pip install attnspectra[viz]
# Todo incluido
pip install attnspectra[all]
# Dependencias de desarrollo
pip install attnspectra[dev]
Uso rápido
import attnspectra as aspec
# Suponiendo que ya tienes un modelo y tokenizer
model = ...
tokenizer = ...
# Crear adapter
adapter = aspec.CustomGPTAdapter(
model=model,
tokenizer=tokenizer,
n_layers=8,
n_heads=8,
d_model=512,
)
# Capturar atención y calcular métricas
run, metrics = aspec.capture_and_compute(adapter, input_ids)
# Acceder a métricas (shape: n_layers × n_heads)
print(metrics.A_attn_entropy)
print(metrics.A_effective_rank)
print(metrics.A_attn_distance)
# Visualizar atención
A, tokens = aspec.get_content_attention(run, layer=0, head=0)
fig = aspec.plot_attention_matrix(A, tokens)
Ejemplo con Hugging Face
import attnspectra as aspec
from transformers import AutoTokenizer, AutoModelForCausalLM
model_name = "gpt2"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(
model_name,
attn_implementation="eager",
)
adapter = aspec.HFTransformerAdapter(
model=model,
tokenizer=tokenizer,
architecture="decoder",
)
text = "The quick brown fox jumps over the lazy dog."
inputs = tokenizer(text, return_tensors="pt")
run, metrics = aspec.capture_and_compute(
adapter,
inputs["input_ids"],
)
print(metrics.A_attn_entropy.shape)
A, tokens = aspec.get_content_attention(run, layer=0, head=0)
fig = aspec.plot_attention_matrix(A, tokens)
Qué puedes hacer con attnspectra
- Analizar patrones de atención en modelos transformer
- Estudiar estructura espectral de matrices de atención
- Comparar comportamiento entre diferentes inputs o estilos
- Visualizar matrices de atención y métricas por capa y cabeza
Funcionalidades principales
- Captura de atención mediante adapters flexibles
- 14 métricas (entropía, rango efectivo, espectro, anisotropía, etc.)
- Análisis por capa y cabeza
- Visualizaciones con matplotlib y modo interactivo con Plotly (opcional)
- Experimentos automatizados (degradación, comparación de estilos)
Documentación completa
Consulta el repositorio para ejemplos, teoría y documentación detallada:
https://github.com/caro370/INSO_TFG
Requisitos
- Python ≥ 3.10
- PyTorch ≥ 2.1
- NumPy ≥ 1.24
- Matplotlib ≥ 3.7
Metadata
Release files for attnspectra 0.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| attnspectra-0.1.3.tar.gz | 39.8 kB | Without provenance | Details for attnspectra-0.1.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| attnspectra-0.1.3-py3-none-any.whl | Python 3 | none | any | Details for attnspectra-0.1.3-py3-none-any.whl |
Total release size: 84.8 kB
Release files / attnspectra-0.1.3.tar.gz
| Download URL | attnspectra-0.1.3.tar.gz |
|---|---|
| Size | 39.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ba9138489a9f37184dc2bf1799928324456cf20d53bdc9060d37c16cb271a272
|
|
BLAKE2b-256 checksum How to use checksums |
fab2dfc0e90cb5cdef1046d0b65cbfc088277e87866e165fe21153992b8ecabd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.0
|
Release files / attnspectra-0.1.3-py3-none-any.whl
| Download URL | attnspectra-0.1.3-py3-none-any.whl |
|---|---|
| Size | 45.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1071044cf861f55a4cde4539976128f35f944096cce4bb3d7697aed67f78d195
|
|
BLAKE2b-256 checksum How to use checksums |
edec7958aa228118cc4062b94aa260257b5a4f198da5cfafa72d731129f93fd7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.0
|
Security for attnspectra 0.1.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.