spectral-trust 0.4.0
spectral-trust turns the attention of a transformer into graphs over the tokens and measures them: the Laplacian spectrum of every head, and the frequency content of the residual stream on those graphs, from one forward pass. Version 0.4.0 makes the library easier to use next to the tools people already have. One call returns every measurement, adapters read TransformerLens, nnsight and Hugging Face runs, the results can draw themselves, the core is faster, and a few fixes change results for existing callers.
One call
The new front door is SpectralModel. from_pretrained loads a Hugging Face causal language model with eager attention, or wraps one that is already loaded, and run_with_spectra runs it once and returns a SpectralCache.
import spectral_trust as st
model = st.SpectralModel.from_pretrained("Qwen/Qwen3-0.6B")
cache = model.run_with_spectra("The cat sat on the mat because it was warm.")
print(cache["blocks.10.attn.fiedler_value"]) # one value per head
print(cache["blocks.10.graph.hfer"]) # the head-averaged graph
The keys follow the naming of TransformerLens. blocks.{l}.attn.{metric} holds one value per head, from the attention weights alone, blocks.{l}.resid.{metric} the per-head metrics that also read the hidden states, and blocks.{l}.graph.{metric} the metrics of the head-averaged graph. cache.layer(10).head(3) gives every measurement of one head, cache.to_dataframe() a long table, and a list of texts runs as a batch.
TransformerLens, nnsight and Hugging Face
If you already have a run, you do not need a second forward pass. st.from_transformer_lens reads the attention patterns and the residual stream from a TransformerLens cache and returns the same SpectralCache.
TransformerBridge (TransformerLens 4.3.0): the cache, then the nine measurements of layer 5, head 1.st.from_nnsight does the same for tensors saved inside an nnsight trace, and st.from_hf_outputs for a Hugging Face forward pass run with output_attentions=True. Neither TransformerLens nor nnsight is a dependency of the core install. Patch, ablate or steer with those libraries, then measure the attention graphs here.
Views
A cache can draw itself. cache.graph(10, head=3) shows the attention graph of one head, with the tokens as nodes coloured by the Fiedler vector. cache.heatmap("fiedler_value") shows one metric over layers and heads, and cache.spectrum(10) the Laplacian spectrum of a layer. Each view is self-contained SVG with tooltips, renders inline in Jupyter and saves as a standalone HTML file that opens offline.
Speed
I timed the same job in both versions: the hidden-state metrics (energy, smoothness, spectral entropy and HFER) for the 28 layers of Qwen3-0.6B on one prompt of 512 tokens, on the head-averaged graph of each layer and on the graph of each of its 16 heads.
analyze_layer over the layers and their heads, one timestamp per layer. Right: one run_with_spectra call, timestamps taken as its per-layer functions return. It also computes the per-head attention metrics (pale segments), which the 0.3.0 loop does not. CPU only, 2 threads, a laptop CPU, torch 2.15.0.Only part of the gap comes from the default solver: 0.4.0 is still 2.9× faster than 0.3.0 with the dense solver. The Dirichlet energy and the smoothness index are now trace-only sums that never form a d by d matrix, analyze_layer projects the signals onto the eigenbasis once instead of three times, and the heads of a layer are decomposed in one batched call.
Fixes that change results
- The default
eigen_solveris now"dense". Above 50 tokens the old"sparse"default kept 50 modes, so HFER and spectral entropy came from a truncated basis. On the prompt of the race, it gives an HFER of 0.00013 at layer 2, where the full basis gives 0.0028. per_head_metricswithnormalization="rw"symmetrised the operator before the eigensolve, so it reported the spectrum of a different matrix. It now reports that of the random-walk operator. Results with the defaultsymnormalisation are unchanged.connectivitynow uses a tolerance that scales with the size of the graph and the floating-point precision. The fixed threshold of 0.3.0 could report a connected graph as disconnected because of float32 noise on the zero mode.- Hybrid models such as Qwen3.5 return attention for their full-attention layers only.
analyze_textnow pairs each attention tensor with the output of its own layer and reports the true layer index. In 0.3.0 the hybrid metrics used the hidden states of another layer.
The default normalisation of the command line also moves from rw to sym. With normalization="sym" and eigen_solver="dense", 0.4.0 reproduces 0.3.0 to float32 rounding: in my timing logs both versions give the same HFER on all 28 layers. The full list is in the changelog.
Install and links
pip install --upgrade spectral-trust
The extras spectral-trust[tl] and spectral-trust[nnsight] add TransformerLens and nnsight. The package is on PyPI, the source is on GitHub, and the documentation has three tutorials and the API reference.
spectral-trust is used in work by researchers at Cornell, Goodfire AI, Imperial College London and École polytechnique (arXiv:2608.03015, arXiv:2610.16709).