Registry / ai-ml / transformer-lens

transformer-lens

JSON →
library3.4.0pypypi✓ verified 81d ago

A library for training and analysing transformer models, focused on mechanistic interpretability. Provides tools to probe, edit, and visualise model internals. Current version 3.1.0, released with support for Python >=3.10, <4.0. Active development.

pip install transformer-lens
INSTALL
IMPORT
SIG · TRANSFORMER-LENS
T
transformer-lens
ai-mlpythonv3.4.0
Install
Import
Disk
Pass rate
0/ 10
Env Coverage0 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v? · pip install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
build_error
glibc
py 3.103.95 runs
timeout
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

HookedTransformer
from transformer_lens import HookedTransformer
from transformer_lens.components import HookPoint
HookedTransformerConfig
from transformer_lens import HookedTransformerConfig
ActivationCache
from transformer_lens import ActivationCache

Load a pretrained model and run a forward pass.

import torch from transformer_lens import HookedTransformer model = HookedTransformer.from_pretrained("tiny-stories-1L-21M") prompts = "The capital of France is" logits = model(prompts) token = logits.argmax(dim=-1).squeeze() print(model.tokenizer.decode(token))
Debug
Known issues
breakingIn version 3.0, the 'transformer_lens' package was renamed from 'transformer-lens' (hyphen) to 'transformer_lens' (underscore) for imports. Old imports using 'transformer_lens' (with hyphen) will fail.
fix
Change all imports to use underscores: 'import transformer_lens'.
affects: >=3.0.0
deprecatedThe old 'utils' module (transformer_lens.utils) has been deprecated in favour of individual submodules. Functions like 'to_numpy' have moved.
fix
Check the changelog for the new location of utility functions.
affects: >=3.0.0
gotchaWhen using 'model.run_with_cache', the returned cache is a dictionary keyed by layer names, but the tensor dimensions can be counterintuitive (batch, pos, d_model). Ensure you permute correctly for visualisation.
fix
Cache tensors are of shape (batch, pos, d_model). Use cache['blocks.0.hook_mlp'].shape to verify.
affects: all
Upgrade
Version history
3.4.0latest on PyPI · released Jun 9, 2026
Audit
Dependencies

No dependency data recorded yet.

Agent activity
40 hits · last 30 days
node
34
OpenAI (training)
1
Resources