Add Direct Logit Attribution tool (#1263)#1369
Open
azrabano23 wants to merge 1 commit into
Open
Conversation
Add transformer_lens/tools/analysis/direct_logit_attribution.py, a single-call DLA analysis that decomposes a logit (or logit difference) into per-component, per-layer (logit-lens), or per-head contributions. Wraps the existing ActivationCache primitives (decompose_resid / accumulated_resid / stack_head_results / logit_attrs) and works with both HookedTransformer and TransformerBridge, since they share the cache API. Returns a DirectLogitAttribution dataclass (attribution tensor + aligned labels, plus a top(k) helper). Adds integration tests asserting the exact DLA correctness invariant on both systems: the complete decomposition reconstructs the model's real logit up to the unembedding bias b_U. Closes TransformerLensOrg#1263
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.
Summary
Closes #1263.
Adds a single-call Direct Logit Attribution (DLA) tool at
transformer_lens/tools/analysis/direct_logit_attribution.py, built for theTransformerBridgesystem (and working unchanged onHookedTransformer, sinceboth share the
ActivationCacheAPI).DLA decomposes a model's output logit — or a logit difference between a
correct and an incorrect token — into the additive contributions of upstream
components. The tool wraps the existing
ActivationCacheprimitives(
decompose_resid/accumulated_resid/stack_head_results/logit_attrs)into one ergonomic entry point.
API
unit="component"— embedding + each layer's attn/MLP output (decompose_resid)unit="layer"— cumulative residual stream per sublayer, i.e. logit-lens (accumulated_resid)unit="head"— each attention head + a remainder term (stack_head_results)posselects the position to attribute (default-1;Nonekeeps all positions)cachelets you reuse a precomputedActivationCacheinstead of re-running the modelReturns a
DirectLogitAttributiondataclass (attributiontensor aligned withlabels, plus atop(k)helper).Correctness
The integration tests assert the exact DLA invariant on both
HookedTransformerandTransformerBridge(compatibility mode): a completedecomposition reconstructs the model's real logit. DLA attributes only the
W_U-direction part of a logit, so the invariant isand for a token difference the bias terms do not generally cancel (gpt2's
folded
ln_finalbias makes them differ), so the test compares againstlogit_diff - (b_U[correct] - b_U[incorrect]). The tests are written to fail ifthe attribution is only superficially correct.
Testing
tests/integration/model_bridge/test_direct_logit_attribution.py— 13 tests(component/layer/head reconstruction on HT + Bridge, labels/shape, cache reuse,
pos=None,top(), and argument validation). Placed inintegration/pertests/AGENTS.mdsince it loads gpt2.make check-formatanduv run mypyon the new module both pass.