← all repositories
johnmarktaylor91/torchlens

TorchLens: one call to X-ray any neural network

Mechanistic interpretability has a plumbing problem — extracting activations and gradients from arbitrary models — and this library wants to fix it once and for all.

★674 stars Python ML FrameworksLLMOps · Eval
torchlens
Collecting fresh signals — velocity needs a few days of history.
star history

What it does

tl.trace takes any PyTorch model and its input exactly as they are — no hooks, no wrappers, no code changes — and logs every operation the forward pass executes, down to individual ATen calls. The resulting Trace object holds every activation, gradient, module, parameter and grad_fn node, with 800+ metadata fields per item: output shapes, FLOPs, execution time, even the exact source line that ran each op. It also supports JAX, TensorFlow, MLX, PaddlePaddle and tinygrad, and is validated on 6,400+ models.

The interesting bit

Because the trace holds the whole computational graph, one small composable toolset covers most interpretability chores: selecting units by kind, location or causal influence (receptive/projective fields), intervening with do-calculus-style syntax, patching activations between runs, and diffing traces to find where an intervention changed things most. The selection language is claimed to be provably complete — any set of units describable by kind, location and causal links can be picked with one selector.

Key highlights

  • Zero-intrusion tracing: pass the model and input as-is; saved activations can stay attached to the network for training on losses computed from them.
  • Interventions via trace.fork().do(site, edit) — ablate, steer, or patch activations from another run, with 20+ built-in edits; edits can propagate through the saved graph without rerunning the model.
  • Bundle objects compare traces operation-by-operation and rank where they diverge most.
  • Visualization with sensible defaults: collapse nested modules automatically, draw recurrent nets rolled or unrolled, map node color/size to any metadata.
  • Export everything to pandas; gradients readable at any operation or grad_fn node.

Caveats

  • The README is long on capability lists; the “provably complete” selector claim links to a doc, but you’ll want to verify it yourself before betting a research project on it.
  • Logging every operation of every ATen call is not free — the README offers speed/detail tradeoffs (stop_after, selective saving), implying full capture is the expensive mode.

Verdict

If you do mechanistic interpretability work — or you’re building AI agents that do — this looks like the plumbing layer you’d otherwise rewrite every project. If you just want a couple of layer outputs for feature extraction, it’s a very large hammer for a small nail.

Frequently asked

What is johnmarktaylor91/torchlens?
Mechanistic interpretability has a plumbing problem — extracting activations and gradients from arbitrary models — and this library wants to fix it once and for all.
Is torchlens open source?
Yes — johnmarktaylor91/torchlens is open source, released under the Apache-2.0 license.
What language is torchlens written in?
johnmarktaylor91/torchlens is primarily written in Python.
How popular is torchlens?
johnmarktaylor91/torchlens has 674 stars on GitHub.
Where can I find torchlens?
johnmarktaylor91/torchlens is on GitHub at https://github.com/johnmarktaylor91/torchlens.

heatdrop uses Google Analytics to see which pages get read — nothing else. Your call. How we handle data.