Repository navigation
Add Logit Lens and Tuned Lens methods #161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
b902f65
Add Logit Lens and Tuned Lens methods
08ca4ce
splitters: initialize the all layers splitter
AntoninPoche 9c8b0f1
all layers splitters: add apply_head
AntoninPoche dde35c1
all layers splitter: fix
AntoninPoche eee8564
fixup! all layers splitter: fix
AntoninPoche 91a98ff
feat(lens): add efficient logit and tuned lens support
f24bb91
chore: merge fixes into lenses
543fb12
Merge branch 'main' of github.com:FOR-sight-ai/interpreto into lenses
AntoninPoche d9628eb
Main changes:
94c3377
AllLayersSplitter: catch missing tokenizer
AntoninPoche 0071258
feat(lens): simplify API and visualization
ac11ed5
feat(lens): add optional alignment for generated token predictions
310f739
feat(lens): improve token alignment and visualization
cdee532
fix(lens): support light and dark visualization themes
874d4ae
fix(lens): use nnsight generation and expand model coverage
11ae09d
test AllLayersSplitters: fix device
AntoninPoche 5ac32e3
fixup! test AllLayersSplitters: fix device
AntoninPoche 3331d7b
0.5.1 -> 0.5.2
AntoninPoche File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| # All Layers Splitter | ||
|
|
||
| `AllLayersSplitter` captures the residual stream before the first transformer block and after every block. It also | ||
| applies the wrapped model's native normalization, pooling, and prediction head to one or more residual states. | ||
|
|
||
| ::: interpreto.AllLayersSplitter | ||
| handler: python | ||
| options: | ||
| show_root_heading: true | ||
| show_source: true |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| # Lens | ||
|
|
||
| Lens methods decode the residual stream at every transformer block boundary. Pass a model repository ID directly for | ||
| the common case, or provide a configured [`AllLayersSplitter`](concepts/splitters/all_layers_splitter.md) when you need | ||
| to select a model class, tokenizer, device, or transformer block path. | ||
|
|
||
| Inference accepts one text at a time. To explain several texts, iterate over them. `TunedLens.fit()` accepts either one | ||
| text or an iterable and trains on each text sequentially. | ||
|
|
||
| ## Classification with Logit Lens | ||
|
|
||
| ```python | ||
| from transformers import AutoModelForSequenceClassification | ||
|
|
||
| from interpreto import AllLayersSplitter, LogitLens, plot_lens | ||
|
|
||
| text = "Interpreto makes model decisions easier to inspect." | ||
| splitter = AllLayersSplitter( | ||
| "distilbert-base-uncased-finetuned-sst-2-english", | ||
| automodel=AutoModelForSequenceClassification, | ||
| ) | ||
| lens = LogitLens(splitter, top_k=2) | ||
| results = lens(text) | ||
|
|
||
| plot_lens(results, text, tokenizer=splitter.tokenizer, label_names=["negative", "positive"]) | ||
| ``` | ||
|
|
||
| ## Generation with Tuned Lens | ||
|
|
||
| ```python | ||
| from interpreto import TunedLens, plot_lens | ||
|
|
||
| text = "Paris is the capital of" | ||
| lens = TunedLens("distilgpt2", top_k=3) | ||
| lens.fit(["Paris is the capital of France.", "Rome is the capital of Italy."], epochs=1) | ||
| results = lens(text) | ||
|
|
||
| plot_lens(results, text, tokenizer=lens.splitter.tokenizer) | ||
| ``` | ||
|
|
||
| For causal language models, predictions are aligned with their observed target tokens by default. Each column therefore | ||
| shows a target token, while the bottom `Input` row shows the preceding token that produced its prediction. Correct top | ||
| predictions are outlined in green. | ||
|
|
||
| `generate()` uses the same convention. Include the prompt when plotting so the visualization can display the input to | ||
| the first generated prediction: | ||
|
|
||
| ```python | ||
| prompt = "Although the committee initially rejected the proposal," | ||
| generated_text, generated_results = lens.generate(prompt, max_new_tokens=10) | ||
|
|
||
| plot_lens(generated_results, prompt + generated_text, tokenizer=lens.splitter.tokenizer) | ||
| ``` | ||
|
|
||
| Set `align=False` on both calls for the conventional next-token view, where each column contains the prediction made | ||
| after its displayed input token: | ||
|
|
||
| ```python | ||
| generated_text, generated_results = lens.generate(prompt, max_new_tokens=10, align=False) | ||
| plot_lens( | ||
| generated_results, | ||
| prompt + generated_text, | ||
| tokenizer=lens.splitter.tokenizer, | ||
| align=False, | ||
| ) | ||
| ``` | ||
|
|
||
| ## `LogitLens` | ||
|
|
||
| ::: interpreto.LogitLens | ||
| handler: python | ||
| options: | ||
| show_root_heading: true | ||
| show_source: true | ||
|
|
||
| ## `TunedLens` | ||
|
|
||
| ::: interpreto.TunedLens | ||
| handler: python | ||
| options: | ||
| show_root_heading: true | ||
| show_source: true | ||
|
|
||
| ## `plot_lens` | ||
|
|
||
| `plot_lens` renders model outputs from the final layer down to the embeddings, followed by the actual input tokens. The | ||
| `Embeddings` row is the prediction head applied before the first transformer block; it is not the raw model input. The | ||
| visible cells contain only the top prediction and use color intensity to show relative confidence. A green outline marks | ||
| a correct prediction. Hover over a cell to inspect its numerical score and remaining top-k results. | ||
|
|
||
| ::: interpreto.visualizations.plot_lens |
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.