Repository navigation
Add KPSA: MLP neuron sensitivity from a forward pass - #2076
Open
DavideWiest wants to merge 1 commit into
Open
DavideWiest wants to merge 1 commit into
DavideWiest wants to merge 1 commit into
Conversation
transformer_lens.tools.analysis.kpsa: - mlp_neuron_sensitivity: exact per-context squared gradient norm of every MLP neuron's parameters, for a whole batch from one backward pass (Gram form over the bridge's MLP hooks). - KPSA: landmark (or exact-kernel) atlas over source contexts that estimates those profiles for new contexts from a forward pass; state_dict support. - topk_energy_capture for checking an atlas on held-out contexts. Method: https://arxiv.org/abs/2610.12737 (proposal TransformerLensOrg#2074).
1 task done
Author
|
The coverage job hit its 75-minute timeout after going quiet at 63% (no output from 16:58 to the cancel). The coverage run on the docs/dla-pre-softcap-logits PR stalled the same way today, at 97%. Locally, |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Implements the proposal in #2074. It adds
transformer_lens.tools.analysis.kpsa, which finds the MLP neurons a per-prompt metric depends on most, for many prompts and without a backward pass per prompt. The method is KPSA (arXiv:2610.12737).mlp_neuron_sensitivity(bridge, tokens, metric)gives the exact sensitivity of every neuron for each context: the squared gradient norm of the neuron's own parameters (itsW_in/W_gatecolumn,b_in/b_gateentry andW_outrow). The gradient is a sum over positions, so the energy isd_j^T (X X^T + 1) d_j + a_j^T (G G^T) a_j. That gives every context in a batch from a single backward pass. It reads the bridge's MLP hooks (mlp.hook_in,mlp.{gate,in}.hook_out,mlp.out.hook_in,mlp.out.hook_out).KPSA(bridge).fit(source_batches)stores exact profiles of source contexts in a landmark atlas: an[n_layers * d_mlp, n_prototypes]matrix over k-means prototypes of a query embedding.kernel="exact"keeps every source profile instead..sensitivity(tokens)and.top_neurons(tokens, k)then need one forward pass up toln_finaland one matrix product.state_dict()/load_state_dict()save and restore an atlas.topk_energy_capturechecks an atlas on held-out contexts.It works on raw and compatibility-mode bridges with standard, gated and fused gate-up MLPs. MoE blocks raise
NotImplementedError.Numbers
In the paper (ViT-B/16, against gradient × activation), the atlas raises batched throughput from 551 to 2,041 images/s and cuts peak GPU memory from 4.55 to 0.44 GB. It keeps 81% of the gap between the backprop oracle and a constant baseline.
I also ran this PR on Qwen3-0.6B-Base (bf16, one RTX 5090). The atlas was fit on 50k Wikipedia contexts (1,024 prototypes, 336 MiB) and evaluated on 3,000 held-out contexts:
mlp_neuron_sensitivity(one backward pass)The exact profiles from the bridge match a separate HF implementation (mean Pearson 0.9999). Fitting the atlas took under 5 minutes.
Tests
tests/unit/tools/test_kpsa.pychecks that:.gradare left behind;state_dictround-trips;Docs: rows in the analysis-tools guide and a short section on how to read the output.
Type of change
Checklist: