johnmarktaylor91/torchlens

Inspect, visualize and intervene on any operation in any neural network. Capture every activation and gradient, the full computational graph and 800+ metadata fields without changing model code. Supports PyTorch, JAX, TensorFlow, MLX, PaddlePaddle and tinygrad. Validated on 6400+ models. Built for researchers and their AI agents.

What it solves

TorchLens addresses the difficulty of inspecting, visualizing, and modifying the internal computations of PyTorch neural networks. It solves the "plumbing" problems of mechanistic interpretability by providing a unified way to extract intermediate activations, gradients, and metadata from any model without requiring manual hooks or code changes.

How it works

Everything begins with the tl.trace function, which runs your model as-is and logs every operation, module, and tensor result. This creates a comprehensive record of the model's computational graph. You can then use this trace to select specific activations, visualize the network structure, intervene on values using "do-calculus" (such as ablating heads or patching activations), and compare different model runs side-by-side.

Who it’s for

It is designed for researchers and developers working on mechanistic interpretability who need to understand how neural networks make decisions, debug model behavior, or experiment with steering model outputs.

Highlights

  • Works with any PyTorch model, including those with complex control flows like loops and conditional branches.
  • Captures over 800 metadata fields per operation, including FLOPs, memory usage, and source code location.
  • Provides a simple, composable syntax for selecting and intervening on activations or gradients.
  • Includes built-in debugging tools for identifying NaNs, dead neurons, and gradient health issues.
  • Supports advanced visualization, including rolled/unrolled views of loops and custom node styling based on execution metrics.

Related

  • Project
  • Project
  • Project
  • Project
  • Project