Skip to content

TorchLens for debugging and training networks

When a network misbehaves, the question is usually "which operation first went wrong, and what fed it": the first NaN, the layer whose activations drifted, the branch that ran when it should not have, the tensor that is eating memory. TorchLens records every operation of a forward pass with its inputs, outputs, shapes, bytes, source line and module, so these questions become lookups on a trace instead of a hook-and-print session, and the same records can be sampled during training to watch distributions drift over time.

The lists below start with the one-off questions and move to monitoring a whole training run. Every link is to a file in this repository.

Start here

  • Tutorial notebooks: eleven short notebooks on laptop-sized models. Notebook 01 captures a forward, 02 reads a number out, 03 draws the graph, 04 keeps memory in check and 06 reads gradients.
  • Find the first NaN notebook: the first NaN- or Inf-producing operation, in two lines.
  • See the graph and inspect any operation: a picture of every operation the forward ran, and the output, module and arguments of any one of them, including operations no module returns.
  • Model summaries: a one-call, metadata-only pass that lists every layer with shapes, parameter counts and cost.
  • Debug utilities example script: a tiny model with one unstable operation and the calls that find it.

Locate a fault

  • tl.debug reference: locate non-finite saved tensors, walk dependencies, compare runs, rank cost, when the graph itself is the diagnostic.
  • Extracting source code with TorchLens: the call stack and the actual source line of every operation.
  • Conditional branch attribution: which Python if branch each operation ran under, so you can see the control flow the forward actually took.
  • Containers: the input and output container structure of each operation, for faults inside tuples, dicts and lists.
  • Buffers: BatchNorm running statistics and other registered buffers, recorded alongside activations.
  • Compare two runs in a bundle: the same model before and after a change, diffed operation by operation.

Gradients and the backward pass

Memory and speed

Monitor training

Save, share and export

Connectors and migration