Skip to main content

Chrome DevTools for neural networks — interactive forensic debugging for PyTorch models

Project description

TraceTorch

A forensic trace console for PyTorch models.

Features

  • Zero-code tracing -- wraps any nn.Module via context manager, no hook scattering
  • Per-layer capture -- name, type, shape, dtype, device, params, latency
  • Activation statistics -- mean, std, min, max, NaN count, Inf count per tensor
  • Backward gradient tracing -- grad norm, mean, NaN detection, zero-gradient detection
  • Anomaly detection -- 8 built-in checks: NaN, Inf, dead layers, exploding variance, variance spikes, zero gradients, NaN gradients, empty outputs
  • JSON export -- structured, flat format designed for dashboards and CI pipelines
  • Rich CLI inspector -- nested layer tree, anomaly warnings, model summary in the terminal
  • Accurate latency -- pre-hook to post-hook timing, not just stat-computation overhead

Python PyTorch License

Install

pip install tracetorch

Requires Python 3.11+. Runtime dependencies: PyTorch 2.0+, Rich 13.0+.

Quick start

import torch
import torch.nn as nn
from tracetorch import TraceSession

model = nn.Sequential(
    nn.Linear(784, 256),
    nn.ReLU(),
    nn.Linear(256, 10),
)

session = TraceSession(model)

with session:
    x = torch.randn(32, 784)
    y = model(x)

print(session.summary())
session.export("trace.json")

Inspect the saved trace:

tracetorch inspect trace.json

What gets captured

For each child module reached through model.named_modules():

  • module name, full name, type, and nesting depth
  • direct parameter count (recurse=False)
  • input and output tensor shape, dtype, and device
  • floating tensor statistics: mean, std, min, max, NaN count, Inf count
  • forward latency measured from pre-hook to post-hook
  • backward gradient norm, mean, NaN flag, zero-gradient flag

Root model is not traced; only its children are.

Backward tracing

Run backward inside the session to capture gradient data:

session = TraceSession(model)

with session:
    x = torch.randn(4, 10, requires_grad=True)
    out = model(x)
    loss = out.sum()
    loss.backward()

for layer in session.record.layers:
    print(layer.full_name, layer.grad_norm, layer.has_zero_grad)

Gradient fields stay None / False if no backward pass runs.

Anomaly checks

Type Trigger
nan_activation Output tensor contains NaN
inf_activation Output tensor contains Inf
dead_layer Output mean and std both near zero
exploding_variance Std > 100, or >10x spike from previous layer
high_variance Std between 10 and 100
empty_output No tensor output captured
zero_gradient Backward gradient is all zeros
nan_gradient Backward gradient contains NaN

JSON format

{
  "metadata": {
    "model_name": "Sequential",
    "pytorch_version": "2.x",
    "python_version": "3.11.x",
    "cuda_available": false,
    "active_device": "cpu",
    "total_time_ms": 1.23
  },
  "layers": [
    {
      "name": "0",
      "type": "Linear",
      "full_name": "0",
      "depth": 1,
      "params": 200960,
      "inputs": [
        {
          "shape": [32, 784],
          "dtype": "torch.float32",
          "device": "cpu",
          "mean": 0.0,
          "std": 1.0,
          "min": -3.0,
          "max": 3.0,
          "nan_count": 0,
          "inf_count": 0
        }
      ],
      "outputs": [
        {
          "shape": [32, 256],
          "dtype": "torch.float32",
          "device": "cpu",
          "mean": 0.01,
          "std": 0.58,
          "min": -2.1,
          "max": 2.4,
          "nan_count": 0,
          "inf_count": 0
        }
      ],
      "latency_ms": 0.25,
      "grad_norm": null,
      "grad_mean": null,
      "grad_has_nan": false,
      "has_zero_grad": false
    }
  ],
  "warnings": []
}

Load a saved trace in Python:

from tracetorch.storage import load_json

record = load_json("trace.json")
print(record.metadata)
print(record.layers[0].outputs)

API Reference

TraceSession

session = TraceSession(model, capture_stats=True, model_name=None)

Context manager for one traced execution window.

Property / Method Description
record Collected TraceRecord
anomalies list[Anomaly] populated after exit
export(path) Write JSON, return Path
summary() Compact text summary

Data structures

from tracetorch import TraceRecord, LayerTrace, TensorStats, Anomaly

All dataclasses serialize via .to_dict(). JSON format is flat for dashboard consumption.

CLI

tracetorch inspect trace.json

Prints model summary, nested layer tree with tensor shapes/params/latency/std, and anomaly warnings.

Architecture

PyTorch Model
      │
  PyTorch Hooks
  (forward_pre, forward, backward)
      │
  Trace Collector
  capture shapes, stats, grads
      │
  Anomaly Detector
  8 built-in checks
      │
  Storage Layer
  JSON export / import
      │
  CLI Inspector
  rich terminal output

Development

git clone https://github.com/phraakture/tracetorch.git
cd tracetorch
pip install -e ".[dev]"

pytest                        # tests
ruff check tracetorch tests   # lint
mypy tracetorch               # types

Run the example:

python example.py
tracetorch inspect trace.json

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tracetorch_ml-0.1.0.tar.gz (112.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tracetorch_ml-0.1.0-py3-none-any.whl (15.3 kB view details)

Uploaded Python 3

File details

Details for the file tracetorch_ml-0.1.0.tar.gz.

File metadata

  • Download URL: tracetorch_ml-0.1.0.tar.gz
  • Upload date:
  • Size: 112.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.14

File hashes

Hashes for tracetorch_ml-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a57332e67b877d60ed26ee910a7e6e5bf6c7178985c934498222b47699d8c84d
MD5 29468363c1b5b11dfc9d8522781ce430
BLAKE2b-256 7133307fbb4b08fb844d1041eac4b103f7af4d2d760767264a3ef51590165a3d

See more details on using hashes here.

File details

Details for the file tracetorch_ml-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: tracetorch_ml-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 15.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.14

File hashes

Hashes for tracetorch_ml-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0e4473b17d2cb538fc321728ad18f9499bc8015384f2fae76a34628a1bf1ef0c
MD5 4be2ded7303462689a017dbe6fdd2322
BLAKE2b-256 1f9ee64b682c579d5dfbf58ebb2dc3b45d54ee52f6179400e504d1d1b656d1b3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page