Steering along the token axis
spectral-trust has so far measured the attention graphs of a transformer. Version 0.5.0 also acts on them. It filters the residual stream along the token axis, in the frequency basis of the model’s own attention graph, with low-pass, high-pass and band filters and a causal mode for generation. It patches a single band of graph frequencies from a clean run into a corrupted one. And it removes attention edges while the first token, the attention sink, keeps its share.
This post gives the idea, three results, what the edits cost and one measurement story. Every number comes from the use cases that ship with the release, where each result stands next to the controls that could have produced it.
Frequencies along the token axis
Most steering methods act on the hidden dimension: they add a vector at every position. The residual stream has a second axis, the tokens. At one layer it is a matrix with one row per token, so each of its columns is a signal over the tokens.
That layer’s attention, averaged over heads and made symmetric, is a graph on the same tokens, and the eigenvectors of its Laplacian are its frequencies. A low frequency changes little between tokens that attend strongly to each other, and a high frequency changes across those links.
A low-pass filter keeps the smooth part, pulling each token towards the tokens it is linked to. A high-pass filter keeps what sets a token apart from its neighbours. The edit is X′ = X + s (g(L) X − X), with g the response of the filter and s its strength, computed as a Chebyshev polynomial in the Laplacian L, with no eigendecomposition. With causal=True each position is filtered on the graph of its own prefix, so cached generation gives the same tokens as recomputing the sequence.
I am not aware of earlier work that intervenes on a language model’s residual stream in the graph-frequency basis of its own attention. Token-axis filters themselves are older. Language Through a Prism (Tamkin, Jurafsky and Goodman, NeurIPS 2020) and Spectral Probing (Müller-Eberstein, van der Goot and Plank, EMNLP 2022) filter frozen representations in a fixed positional basis, the discrete cosine transform, to probe what they encode at different scales. They read representations rather than intervene on a model through its attention graph.
Where the removed attention goes
Removing an attention edge leaves a row that no longer sums to one, and the missing mass has to go somewhere. remove_edges makes that choice an argument. The first use case removes the edges from the final position to the three name tokens in the GPT-2 name movers 9.6, 9.9 and 10.0, on 200 IOI prompts. Before the edit, the logit difference between the right and the wrong name is 3.64.
With the sink held fixed, the default, the logit difference changes by −0.79 [−0.90, −0.68]. Plain renormalisation gives −0.12 [−0.23, −0.01], and zeroing gives +0.08 [−0.03, 0.18], an interval that includes zero. The same edges carry a fifth of the logit difference, a thirtieth or nothing, and one keyword decides which.
These edges hold 0.77 of the row, against 0.20 for the first token and 0.03 for the rest. Plain renormalisation hands the freed mass mostly to the first token, whose share rises to 0.87. Zeroing hands it to no token. Holding the sink fixed hands it to the remaining content tokens and multiplies their attention by a median factor of 13 to 181, depending on the head, so the −0.79 includes that amplification. When the removed edges carry little of the row, the choice barely matters: in 20 random head triples, where they carry 0.15, all three choices give −0.06 or −0.07.
Steering against controls of the same size
The steering use case filters the residual stream after each block of GPT-2 small. The effect is the IOI logit difference, the side effect the next-token loss on WikiText-2. Each edit is compared with the same filter on two graphs, rescaled to the same edit norm: the chain graph over positions, and the attention graph with each row’s weights shuffled among the keys of its prefix.
The edits are strong. A full-strength low-pass after block 5 changes the logit difference by −3.21 [−3.62, −2.80], most of the clean 3.64. But most of that follows the size of the edit. In the 66 cells where the edit is not the identity, the effect of the attention graph differs from both controls in 24, above both in 12 and below both in 10. On the effect alone, it has no consistent advantage.
The side effect is where the graph matters. At full strength, smoothing along the attention graph costs 1.4 to 2.5 times the loss of a smoothing of the same size along positions or along the shuffled graph, at every layer from 2 to 10. After block 5 it sits on the same curve of effect against cost as the shuffled graph and the prefix mean, only further along it. It helps in one place: after block 11, a half-strength high-pass raises the logit difference by 0.61 [0.52, 0.69] for 0.06 nats, where the shuffled graph gets 0.35 for 0.05 nats and the path graph 0.35 for 0.10. That layer was picked after the run, among 24 layer and filter pairs, so it is a lead to test, not a finding.
Patching one band of frequencies
Frequency-resolved patching asks which part of an activation, along the token axis, carries an effect. It copies the coefficients of one band of the clean attention graph’s eigenvectors from the clean run and keeps every other coefficient from the corrupted run. The full band is plain patching.
On 200 IOI prompt pairs, the lowest of eight bands of the attention basis at the input of block 10 of GPT-2 small, two or three modes, recovers 0.51 [0.46, 0.55] of the logit difference. A random subspace of the same size recovers at most 0.18 (the 95th percentile of 50 draws), the best positional band 0.22, and the same band of the corrupted run’s graph 0.07. The pass is narrow: it is the only one of 96 cells at 0.50 or above, and on the odd-indexed half of the prompts it falls below.
Position beats frequency here. Patching the final position alone recovers 1.34 at the same layer, more than the full patch: on IOI the answer is gathered at one token. Frequency methods should help most where information is spread over many tokens, which IOI is not. What this result shows is a method that separates bands that matter from bands that do not, with controls that can say no.
What the edits cost
A separate use case measures what a filter does to the rest of GPT-2 small and Phi-3-mini, against size-matched controls.
Low-pass edits up to 0.27 of the residual’s norm on GPT-2 and 0.29 on Phi-3 leave greedy generation as clean as without an edit. They do cost more next-token KL than the size-matched controls, about 1.4 times the shuffled and chain graphs in the median on GPT-2. High-pass and band-pass filters at full strength are edits of 0.72 to 1.16 of the norm, and they break the model, as the controls do: up to 90% of continuations degenerate on GPT-2 and 75% on Phi-3. At full strength they are ablations, not steering.
Two new warnings follow from this. graph_filter warns when an edit exceeds half the residual’s norm, and remove_edges warns when keeping the sink multiplies the kept edges of a row by more than 10.
Which words split Phi-3’s attention graph
In Phi-3-mini-4k-instruct, the Fiedler value of the attention graph at layer 3 averages 0.929 over 72 active sentences and 0.361 over their passive twins. It looks like the model setting the passive voice apart.
It is not the voice. Passives without an agent (“The cat was chased.”) do not drop, and actives with a non-agentive “by” (“The cat slept by the door.”) drop as much as passives. In that frame, eight frequent prepositions (to, of, for, with, at, by, from, under) give 0.370 to 0.379, and four others (near, behind, after, beside) give 0.928 to 0.931.
A plain baseline does as well. The share of attention on the first token separates every pair at layers 3 to 5, as the Fiedler value does, and the spectrum adds only where the split falls. Nor does the drop yield to repair. At layer 2, each against a matched random control, the graph low-pass moves the Fiedler value the wrong way, removing the edges into “was” and “by” closes 1% to 3% of the gap, and a steering vector beats a random one only at a strength that breaks the model.
A spectral number that moves is a hypothesis. The library ships its baselines next to its measurements so that such checks are cheap, and here they turned a story about grammar into one about a few prepositions.
Try it
pip install --upgrade spectral-trust
Smoothing the residual stream of GPT-2 along its attention graph after block 5, then restoring the model:
import spectral_trust as st
model = st.SpectralModel.from_pretrained("gpt2")
tok = model.tokenizer
ids = tok("When Mary and John went to the store, John gave a drink to",
return_tensors="pt").input_ids
mary, john = tok(" Mary").input_ids[0], tok(" John").input_ids[0]
def logit_diff(logits):
return (logits[0, -1, mary] - logits[0, -1, john]).item()
print(logit_diff(model(ids).logits)) # clean
with st.intervene(model) as iv:
iv.resid(5).graph_filter("lowpass", cutoff=1.0, strength=1.0,
causal=True, token_span=(1, None))
print(logit_diff(model(ids).logits)) # smoothed after block 5
print(logit_diff(model(ids).logits)) # the edit is gone
The same context manager takes remove_edges(mask, keep_sink=True) on any head, and st.Patcher patches one band with basis="clean_attention", "path" or "random". 0.5.0 also brings activation patching, head ablation, activation steering and custom hooks to Hugging Face models, all undone on exit.
Four notebooks run in Colab:
- Quickstart
- Animated views
- Interventions: sink-preserving ablation, graph-frequency steering and frequency-resolved patching on GPT-2
- Phi-3 prepositions, the measurement story above, on a GPU runtime
The use cases give the setup, controls and limits of each result: edge ablation and the sink, graph-frequency steering, frequency-resolved patching, graph-filter side effects and Phi-3 prepositions. The interventions tutorial covers the API. The release is on PyPI and GitHub, with every change in the changelog.
spectral-trust is used in work by researchers at Cornell, Goodfire AI, Imperial College London and École polytechnique, and the work of Bao et al. (arXiv:2608.03015) has been accepted at EACL 2027.