Attention heat-map helper for Hugging Face text-generation pipelines
Project description
pipeline-viz
hf_pipeline_viz is a python helper that lets you peek inside a Hugging Face text-generation pipeline and see which context tokens the model attends to for every new word it writes.
Why you might care:
- Model interpretability – spot “sink” heads, long-range dependencies, or where a layer shifts focus from syntax to semantics.
- Prompt engineering & debugging – confirm the model is actually reading the parts of the prompt you think are important.
- Teaching & demos – a single screenshot often explains causal masking better than a paragraph of text.
Installation
pip install hf_pipeline_viz
Quick start
from transformers import pipeline
from hf_pipeline_viz import viz_generate
pipe = pipeline("text-generation", model="openai-community/gpt2-large")
prompt = "The secret to making a good cake is "
viz_generate(pipe, prompt, max_new_tokens=20)
A heat-map is generated; each row is a freshly generated token, each column a context token. Bright squares = high averaged attention.
Fine control
# average only layer 11 over all heads
viz_generate(pipe, prompt, layers=11)
# average layers 8 & 11, but keep heads 0,3,7
viz_generate(pipe, prompt, layers=[8,11], heads=[0,3,7])
# last four layers, single head 5
viz_generate(pipe, prompt, layers=[-4,-3,-2,-1], heads=5,
savefile="attn.png")
Negative indices follow Python slicing rules (-1 = last layer/head).
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file hf_pipeline_viz-0.1.0.tar.gz.
File metadata
- Download URL: hf_pipeline_viz-0.1.0.tar.gz
- Upload date:
- Size: 4.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9522b089830821709d2a8a7d39e73135ff2da472fded3f717ca499a2a9c8956d
|
|
| MD5 |
ee5102bc1d649e83f301240aa39a88cd
|
|
| BLAKE2b-256 |
686877a1d79230e59eae8d7b6c01f296d6d6df483c5c32287c7aa73d6a3b7f32
|
File details
Details for the file hf_pipeline_viz-0.1.0-py3-none-any.whl.
File metadata
- Download URL: hf_pipeline_viz-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
23cb317845e12cd21c5c3d85573fba171da62c32ca111d3fca079311f2477163
|
|
| MD5 |
7ff45c3112dec079a479d800da959eb7
|
|
| BLAKE2b-256 |
372f418b38f9fe03f2d057d3aa2648bfdf9aa66a7ec5cdcc1efa23e477274188
|