Migrating to TransformerLens 4.0¶
TransformerLens 4.0 removes the legacy Hooked* model stack.
TransformerBridge was introduced in 3.0 as the recommended path, and is now the only path. This guide will detail what was removed and how to recreate those features in the new system. For the deeper conceptual differences between the HookedTransformer API and the bridge (hook names, weight processing, per-API recipes), see the 3.0 migration guide. The API-mapping table from HookedTransformer to TransformerBridge is accurate with the final state of all those feature.
Our intention in making this change is to unify future research, as well as future contributions. Having two parallel systems running covering the same ground created much confusion in where features exist, how they should be accessed, and where issues needed to be fixed. Additionally, it created a dual mandate to repair both any time an issue was reported, costing additional man hours.
We will still be accepting issues and resolving any bugs on the existing HookedTransformer system, but they will be silo’ed to the 3.x branch. We will not be accepting new features or models.
The Core Change¶
# Removed in 4.0
from transformer_lens import HookedTransformer
model = HookedTransformer.from_pretrained("gpt2")
# 4.0
from transformer_lens.model_bridge import TransformerBridge
model = TransformerBridge.boot_transformers("gpt2")
model.enable_compatibility_mode() # HookedTransformer-equivalent numerics
For anyone migrating existing HookedTransformer work to the latest system, enable_compatibility_mode() reproduces its default weight
processing (LayerNorm folding, center_writing_weights, center_unembed),
verified against frozen HookedTransformer reference activations. Omit it to
work with raw HuggingFace weights (the equivalent of the old
from_pretrained_no_processing).
Removed names and their replacements¶
Removed in 4.0 |
Replacement |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Model names/aliases in |
|
|
|
Handled internally by the bridge’s adapters; legacy TL-format repos load via |
Accessing a removed name from the top-level package raises an AttributeError
naming its replacement (e.g. from transformer_lens import HookedTransformer).
Weight accessors: W_pos / W_E_pos¶
4.0 adds direct accessors, so no manual concatenation is needed:
from transformer_lens.model_bridge import TransformerBridge
bridge = TransformerBridge.boot_transformers("gpt2")
bridge.enable_compatibility_mode() # to match HookedTransformer's processed W_pos
bridge.W_pos # (n_ctx, d_model)
bridge.W_E_pos # concatenated [W_E; W_pos]
Without enable_compatibility_mode() these return raw HuggingFace weights;
HookedTransformer’s defaults center the writing weights, so match the
processing to match the numbers.
train / utils¶
# Removed
from transformer_lens.train import train, HookedTransformerTrainConfig
from transformer_lens import utils
# 4.0
from transformer_lens.tools.training import train, TrainConfig
from transformer_lens import utilities as utils # identical names
Traps¶
HookedEncoder.encoder_outputdoes not map tobridge.encoder_output. The bridge’sencoder_outputis the audio frame-entry method (HuBERT/Wav2Vec2) and refuses BERT. A name-only migration will pass adir()parity check but fail at runtime — run a BERT encoder throughbridge(...)/run_with_cacheinstead.
Kept¶
HookedRootModule and HookPoint are kept — they are the supported way to
add TransformerLens-style hooks to an arbitrary nn.Module, and are unrelated
to the model-class removal.
Prerelease note¶
All three 4.0 prereleases still shipped HookedTransformer. If you adopted a
4.0 beta, this final release is a second breaking step for the removed names
above.