-
Notifications
You must be signed in to change notification settings - Fork 687
fix: raise on stop_at_layer when no 'blocks' stack is registered #1789
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
jlarson4
merged 4 commits into
TransformerLensOrg:dev-4.x
from
Aurnawr:fix/stop-at-layer-blocks-guard
Sep 18, 2026
+85
−24
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9b31d46
fix: raise on stop_at_layer when no 'blocks' stack is registered
Aurnawr cf612fb
Merge remote-tracking branch 'origin/dev-4.x' into fix/stop-at-layer-…
jlarson4 ca4fa17
docs: note NotImplementedError in stop_at_layer docstring
Aurnawr c82e99e
test: cover a wrapped model exposing its own .blocks
Aurnawr 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| from types import SimpleNamespace | ||
|
|
||
| import pytest | ||
| import torch | ||
| import torch.nn as nn | ||
|
|
||
| from transformer_lens.config import TransformerBridgeConfig | ||
| from transformer_lens.model_bridge import TransformerBridge | ||
|
|
||
|
|
||
| def _bare_bridge(**block_lists: nn.Module) -> TransformerBridge: | ||
| """A TransformerBridge with only the given block lists registered (no HF model).""" | ||
| bridge = TransformerBridge.__new__(TransformerBridge) | ||
| nn.Module.__init__(bridge) | ||
| bridge.cfg = TransformerBridgeConfig( | ||
| d_model=8, | ||
| d_head=4, | ||
| n_layers=1, | ||
| n_ctx=16, | ||
| d_vocab=32, | ||
| d_mlp=16, | ||
| n_heads=2, | ||
| architecture="RavenForCausalLM", | ||
| ) | ||
| for name, module in block_lists.items(): | ||
| bridge.add_module(name, module) | ||
| return bridge | ||
|
|
||
|
|
||
| def test_stop_at_layer_raises_without_blocks_stack() -> None: | ||
| """Raven-style prelude/core_block/coda lists must not silently ignore stop_at_layer.""" | ||
| bridge = _bare_bridge( | ||
| prelude=nn.ModuleList([nn.Identity()]), | ||
| core_block=nn.ModuleList([nn.Identity()]), | ||
| coda=nn.ModuleList([nn.Identity()]), | ||
| ) | ||
| with pytest.raises(NotImplementedError, match="stop_at_layer requires a 'blocks' stack"): | ||
| bridge.forward(torch.zeros(1, 3, dtype=torch.long), stop_at_layer=0) | ||
|
|
||
|
|
||
| def test_blocks_guard_ignores_unregistered_blocks_attribute() -> None: | ||
| """A wrapped HF model exposing `.blocks` must not satisfy the guard via __getattr__.""" | ||
| bridge = _bare_bridge() | ||
| bridge.__dict__["original_model"] = SimpleNamespace(blocks=[object()]) | ||
| assert hasattr(bridge, "blocks") # the trap: __getattr__ falls through to the HF model | ||
| assert not bridge._has_registered_blocks() | ||
| with pytest.raises(NotImplementedError, match="stop_at_layer requires a 'blocks' stack"): | ||
| bridge.forward(torch.zeros(1, 3, dtype=torch.long), stop_at_layer=0) | ||
| with pytest.raises(NotImplementedError, match="start_at_layer requires a 'blocks' stack"): | ||
| bridge.forward(torch.zeros(1, 3, 8), start_at_layer=0) | ||
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.
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.