Repository navigation
Native model: support positional_embedding_type="shortformer" - #2060
Open
davidquarel wants to merge 1 commit into
Open
davidquarel wants to merge 1 commit into
davidquarel wants to merge 1 commit into
Conversation
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 was referenced Nov 26, 2026
This branch has not been deployed
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.
Description
Shortformer positional embeddings are added to the query and key inputs of every attention layer, not to the residual stream.
HookedTransformersupported them withpositional_embedding_type="shortformer". In 4.x the native bridge model rejects that option (ValueError: Unsupported positional_embedding_type='shortformer'), andboot_tl_legacyraisesNotImplementedErrorfor 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, whichreset_hooks(including_permanent=True)silently undoes.This PR adds the option to the native model:
NativeModel/NativeBlock/NativeAttention: with"shortformer",W_posis not added to the residual stream. It is passed to each block, which adds it to the q and k inputs afterln1(theHookedTransformersemantics); values read the residual alone.pos_embedkeeps its bridge component, sohook_pos_embedstill fires and edits to it reach attention.tl_legacy.derive_tl_legacy_config: mapsshortformer_posin a legacyconfig.jsonto the new option instead of raising.NativeArchitectureAdapter: for shortformer models, declinesfold_ln(when the model has a norm) andcenter_writing_weights. Both assume the q/k inputs areln1'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 withLNorLNPreby 7.5e-2; with it, by under 1e-6 for every normalization type.input_to_embedis unchanged: it still returnsNoneforshortformer_pos_embed, and resuming withstart_at_layer=0from 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
Testing
Five new tests in
tests/unit/model_bridge/test_native_features.py: forward against a hand-written shortformer reference; positions absent fromresid_preandv.hook_in, present inq.hook_in/k.hook_inin every layer; theinput_to_embed/start_at_layerround 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
HookedTransformeron the real checkpoint (checked by hand, since it needs 3.9):boot_tl_legacy("NeelNanda/Attn-Only-2L512W-Shortformer-6B-big-lr")vsHookedTransformer.from_pretrained_no_processing("attn-only-2l-demo")gives identical logits (max difference 0.0). Withenable_compatibility_mode()vs 3.9's defaultfrom_pretrainedprocessing, log-probs agree to 1.1e-5 (raw logits differ by a per-position constant fromcenter_unembed).Run in the project's own
uv syncenvironment on this branch (rebased ondevat 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.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:
🤖 Generated with Claude Code