Skip to content

fix: raise on stop_at_layer when no 'blocks' stack is registered - #1789

Merged
jlarson4 merged 4 commits into
TransformerLensOrg:dev-4.xfrom
Aurnawr:fix/stop-at-layer-blocks-guard
Sep 18, 2026
Merged

jlarson4 merged 4 commits into
TransformerLensOrg:dev-4.xfrom
Aurnawr:fix/stop-at-layer-blocks-guard

Conversation

@Aurnawr

@Aurnawr Aurnawr commented Sep 17, 2026

Copy link
Copy Markdown

Description

TransformerBridge.forward(..., stop_at_layer=N) silently did nothing on architectures that don't register a blocks stack (e.g. Raven/Huginn, whose layers are prelude / core_block / coda). The old guard only rejected four known names (L_blocks, H_blocks, encoder_blocks, decoder_blocks) and only set _stop_at_layer_idx when hasattr(self, "blocks"), so Raven fell through both checks and the forward pass ran to completion.

Following @jlarson4's suggested approach in the issue:

  • Allowlist instead of reject-list: stop_at_layer now raises NotImplementedError("stop_at_layer requires a 'blocks' stack; ...") whenever no blocks stack is registered. This covers Raven and any future non-standard adapter, and removes the four-name reject-list.
  • _modules instead of hasattr: added a private helper _has_registered_blocks() that checks "blocks" in the bridge's registered submodules. hasattr(self, "blocks") can return True via __getattr__ falling through to the wrapped HF model. The existing start_at_layer guard now uses the same helper.
  • Test: added tests/unit/model_bridge/test_stop_at_layer_guard.py with a regression test that builds a bare bridge with Raven-style prelude/core_block/coda lists (no checkpoint download) and asserts stop_at_layer=0 raises.

No new dependencies.

Addresses #1769. Per the maintainer's note, the issue should stay open to track actual stop_at_layer support for Raven via top-level wiring.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Note: architectures with L_blocks/H_blocks/encoder_blocks/decoder_blocks still raise NotImplementedError, but the message text changed.

Checklist:

  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • I have not rewritten tests relating to key interfaces which would affect backward compatibility

Tests run locally:

  • uv run pytest tests/unit/model_bridge/test_stop_at_layer_guard.py tests/unit/model_bridge/test_audio_start_at_layer_guard.py -v: all pass; the new test fails on the pre-fix code.
  • black --check / isort --check-only on the changed files: clean.

Replace the stop_at_layer reject-list with an allowlist guard and check
self._modules instead of hasattr for both stop_at_layer and start_at_layer.

Addresses TransformerLensOrg#1769
@Aurnawr
Aurnawr changed the base branch from main to dev-4.x September 17, 2026 12:47
@Aurnawr

Aurnawr commented Sep 17, 2026

Copy link
Copy Markdown
Author

Retargeted this to dev-4.x (it was accidentally opened against main, which pulled in 92 unrelated commits). The new test_stop_at_layer_guard.py passes. The remaining failures (test_generate_stream_batch_decode.py, test_hf_generate_batch_padding.py with AttributeError: ... no attribute '_driver') are in generate_stream, which this PR doesn't touch, so they seem to come from dev-4.x itself. Happy to look into them separately if useful. cc @jlarson4

@jlarson4
jlarson4 changed the base branch from dev-4.x to dev September 17, 2026 13:40
@jlarson4
jlarson4 changed the base branch from dev to dev-4.x September 17, 2026 13:42
@jlarson4

Copy link
Copy Markdown
Collaborator

@Aurnawr I pulled in the latest dev-4.x commits, I unintentionally introduced a bug in the tests for the version of 4.x you based this one, should be resolved now

@jlarson4 jlarson4 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clean implementation of both halves of the approach from the issue, and the regression test is solid. Two comments on the diff's leftover scratch comments and the guard's test coverage, plus one on the docstring.

Comment thread transformer_lens/model_bridge/transformer_bridge.py Outdated
Comment thread transformer_lens/model_bridge/transformer_bridge.py
Comment thread tests/unit/model_bridge/test_stop_at_layer_guard.py
@jlarson4

Copy link
Copy Markdown
Collaborator

This looks great! Thank you for quickly getting to those, I will merge this and make sure it gets into today's release

@jlarson4
jlarson4 merged commit 73abf7c into TransformerLensOrg:dev-4.x Sep 18, 2026
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants