transformer-lens
An implementation of transformers tailored for mechanistic interpretability.
What it is and what it does
TransformerLens is a library for mechanistic interpretability—the practice of reverse-engineering what algorithms a trained transformer learned by inspecting its weights and internal activations. It lets you load open-source language models, run them on text, and intercept the numerical values flowing through every layer and attention head. You can cache these activations, edit them mid-run, or replace them entirely to see how the model's output changes.
The library's main entry point is TransformerBridge, which handles loading models from HuggingFace and exposes their internal state. It depends on torch, transformers, and huggingface-hub for model loading, plus utilities like einops for tensor manipulation and wandb for experiment tracking. The core use case is research: studying how models solve specific tasks, finding which neurons or circuits are responsible for particular behaviors, and testing hypotheses about learned algorithms by ablating or patching activations.
Use it for:
- Identify which attention heads and neurons are responsible for specific model behaviors by ablating or patching their activations
- Replicate published mechanistic interpretability research that studies induction heads, indirect object identification, or circuit discovery
- Debug unexpected model outputs by inspecting intermediate layer activations and tracing information flow through the network
- Train and analyze decision transformers or other custom architectures by hooking into their internal states during inference
- Study how language models encode and process linguistic structure across different layers and attention patterns
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
TransformerLens loads and inspects the internal activations of transformer language models, letting you cache, edit, and analyze what happens inside the model as it processes text.
Yes, if you are doing mechanistic interpretability research or need to inspect transformer internals. The library is actively maintained, has low install friction, and is licensed permissively. No, if you only need standard inference or fine-tuning—the 21 dependencies and focus on activation inspection add complexity unnecessary for typical transformer use. Yes-with-conditions if you work with gated models: you must set HF_TOKEN in your environment.
Install
transformer-lens on PyPI
pip
pip install transformer-lensuv
uv add transformer-lenspoetry
poetry add transformer-lensInstalling transformer-lens
Before you install
Low friction installation with a pure-Python wheel. Active maintenance with a release 3 days old. Requires 21 runtime dependencies including torch, transformers, and huggingface-hub, which are substantial but standard for transformer work.
License in practice
MIT license is permissive—you can use, modify, and distribute this package freely with minimal restrictions, making it suitable for both research and commercial projects.
Quickstart
pip install transformer_lens
from transformer_lens.model_bridge import TransformerBridge
bridge = TransformerBridge.boot_transformers("gpt2", device="cpu")
logits, activations = bridge.run_with_cache("Hello World")
Requires Python 3.10 or later. Gated models (Llama, Mistral, Gemma) require HF_TOKEN environment variable set with a HuggingFace API token.
Verify before relying
- Whether the 15,000+ supported models across 140+ architecture families claim is current as of version 3.7.1
- Performance characteristics and memory overhead when caching activations on large models
- Compatibility guarantees with specific transformer architecture versions beyond the quick-start examples
Package facts
| License | MIT (permissive) |
| Python support | supports the current Python release (<4.0,>=3.10) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 21 — accelerate, beartype, better-abc, datasets, einops, fancy-einsum, huggingface-hub, jaxtyping, numpy, packaging, pandas, protobuf, rich, sentencepiece, torch, tqdm, transformers-stream-generator, transformers, typeguard, typing-extensions, wandb |
| Maintenance | actively maintained — 3 days since the last release |
| First released | |
| Downloads | 88,225/month — #13,740 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: transformer_lens-3.7.1-py3-none-any.whl
Tags
More Artificial Intelligence packages
LiteLLM provides a unified Python interface to…
permissive · top 100 on PyPI
huggingface-hubClient library and CLI tool for downloading,…
permissive · top 100 on PyPI
langchainLangChain provides a framework for building…
permissive · top 1,000 on PyPI
hf-xethf-xet provides chunk-based deduplication and…
permissive · top 1,000 on PyPI
tokenizersTokenizers converts raw text into token…
permissive · top 1,000 on PyPI
transformersTransformers provides a unified framework for…
permissive · top 1,000 on PyPI
sae-lensSAE Lens trains and analyzes sparse…
permissive · top 15,000 on PyPI
feature-engineFeature-engine provides transformers for…
permissive · top 15,000 on PyPI
spacy-transformersIntegrates pretrained transformer models (BERT,…
permissive · top 15,000 on PyPI
pytorch-pretrained-bertProvides PyTorch implementations of BERT, GPT,…
permissive · top 15,000 on PyPI
local-attentionImplements local windowed attention for…
permissive · top 15,000 on PyPI
interpretInterpretML provides interpretable machine…
unclear · top 15,000 on PyPI
curated-transformersCurated Transformers provides PyTorch…
permissive · top 5,000 on PyPI
vit-pytorchProvides PyTorch implementations of Vision…
permissive · top 15,000 on PyPI
transformer-engineTransformer Engine accelerates Transformer…
unclear · top 15,000 on PyPI