Skip to content

Native model: support positional_embedding_type="shortformer" - #2060

Open
davidquarel wants to merge 1 commit into
TransformerLensOrg:devfrom
davidquarel:native-shortformer-pos-embed
Open

davidquarel wants to merge 1 commit into
TransformerLensOrg:devfrom
davidquarel:native-shortformer-pos-embed

Conversation

@davidquarel

Copy link
Copy Markdown

Description

Shortformer positional embeddings are added to the query and key inputs of every attention layer, not to the residual stream. HookedTransformer supported them with positional_embedding_type="shortformer". In 4.x the native bridge model rejects that option (ValueError: Unsupported positional_embedding_type='shortformer'), and boot_tl_legacy raises NotImplementedError for the legacy shortformer checkpoints (e.g. attn-only-2l-demo = NeelNanda/Attn-Only-2L512W-Shortformer-6B-big-lr), which are still in the supported-model list. Downstream, ARENA's interpretability course currently emulates shortformer with permanent hooks on a standard model, which reset_hooks(including_permanent=True) silently undoes.

This PR adds the option to the native model:

  • NativeModel / NativeBlock / NativeAttention: with "shortformer", W_pos is not added to the residual stream. It is passed to each block, which adds it to the q and k inputs after ln1 (the HookedTransformer semantics); values read the residual alone. pos_embed keeps its bridge component, so hook_pos_embed still fires and edits to it reach attention.
  • tl_legacy.derive_tl_legacy_config: maps shortformer_pos in a legacy config.json to the new option instead of raising.
  • NativeArchitectureAdapter: for shortformer models, declines fold_ln (when the model has a norm) and center_writing_weights. Both assume the q/k inputs are ln1's output (mean-centered, unit scale), which no longer holds once positions are added after it. Without this guard, enable_compatibility_mode() changes the log-probs of a shortformer model with LN or LNPre by 7.5e-2; with it, by under 1e-6 for every normalization type.

input_to_embed is unchanged: it still returns None for shortformer_pos_embed, and resuming with start_at_layer=0 from its residual reproduces the full forward, because the blocks re-derive the positions (tested).

The library change is 34 added and 15 removed lines across three files. No new dependencies. No issue filed for this.

Type of change

  • New feature (non-breaking change which adds functionality)

Testing

Five new tests in tests/unit/model_bridge/test_native_features.py: forward against a hand-written shortformer reference; positions absent from resid_pre and v.hook_in, present in q.hook_in / k.hook_in in every layer; the input_to_embed / start_at_layer round trip; compatibility mode preserves predictions for no norm, LN, LNPre, RMS and RMSPre (this is what exercises the adapter guard); the legacy config mapping.

Against TransformerLens 3.9's HookedTransformer on the real checkpoint (checked by hand, since it needs 3.9): boot_tl_legacy("NeelNanda/Attn-Only-2L512W-Shortformer-6B-big-lr") vs HookedTransformer.from_pretrained_no_processing("attn-only-2l-demo") gives identical logits (max difference 0.0). With enable_compatibility_mode() vs 3.9's default from_pretrained processing, log-probs agree to 1.1e-5 (raw logits differ by a per-position constant from center_unembed).

Run in the project's own uv sync environment on this branch (rebased on dev at 1929d74):

  • make format: no changes. uv run mypy .: no issues.
  • make unit-test: 8788 passed, 57 skipped, 3 xfailed.
  • make docstring-test: 13 passed, 24 skipped.
  • make acceptance-test: 163 passed, 69 skipped.
  • Integration: the tests touching this change (test_bridge_input_to_embed, test_tl_legacy_loader, test_native_training, test_native_norm_inplace_hooks, test_weight_processing_math): 60 passed. The full integration tier runs in CI's coverage job.

Checklist:

  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation (docstrings only; no docs page lists the native model's options)
  • 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

🤖 Generated with Claude Code

Shortformer positional embeddings are added to the query and key inputs of every
attention layer instead of to the residual stream. HookedTransformer supported
them through a config option; the native bridge model rejected the option, and
boot_tl_legacy raised NotImplementedError for the legacy shortformer checkpoints
(e.g. attn-only-2l-demo), which are still in the supported-model list.

- NativeModel / NativeBlock / NativeAttention: with "shortformer", W_pos stays out
  of the residual stream and is passed to each block, which adds it to the q and k
  inputs after ln1 (the HookedTransformer semantics). pos_embed keeps its bridge
  component, so hook_pos_embed still fires and edits to it reach attention.
- tl_legacy: map shortformer_pos in a legacy config.json to the new option instead
  of raising.
- NativeArchitectureAdapter: for shortformer models, decline fold_ln (when there is a
  norm) and center_writing_weights. Both assume the q/k inputs are ln1's output,
  which no longer holds once positions are added after it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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.

1 participant