$ show jessevig/bertviz
bertviz is a Python library for interactive visualization of attention in Transformer-based NLP models. It provides three complementary views: Head View (per-head attention patterns), Model View (attention flow between tokens across layers), and Neuron View (query/key interactions within individual attention heads). The library is designed for Jupyter notebooks, rendering D3.js-based interactive HTML directly in cell output. It accepts attention tensors from most HuggingFace models via output_attentions=True, and includes a vendored, modified copy of older HuggingFace transformers code to expose query/key vectors for the Neuron View on BERT, GPT-2, RoBERTa, XLNet, and similar architectures.
The LLM identified 3 files worth highlighting for reuse:
Provides format_attention() and related helpers that normalize attention tensors from various model outputs into a consistent, JSON-serializable structure for visualization. This is an isolated utility that could be reused in any project needing to extract or standardize attention weights from transformer models.
A self-contained D3.js module that renders interactive attention head visualizations from a JSON data structure. It demonstrates non-obvious skill in building responsive, hoverable visualizations and could be extracted and reused with any attention data source, independent of the Python backend.
Implements the logic for extracting query/key/attention tensors from the vendored transformer models and rendering the neuron-level view. The pattern of instrumenting model internals to expose attention components is reusable for interpretability tooling, though it is coupled to the custom model wrappers in transformers_neuron_view.