A lightweight tracer for understanding & debugging PyTorch neural networks
Project description
Tracy NN
Tracy NN is a lightweight debugging and inspection tool for PyTorch models. It helps you trace and log tensor shapes and operations as data flows through your model — especially useful for understanding complex architectures like transformers.
Features
- Trace any PyTorch
nn.Moduleand log all tensor shapes and operations - Supports custom modules and
nn.Sequentialmodels - Detects common operations like
@,.view,.permute,.transpose, and more - Lightweight: only requires a single forward pass to log everything
- Supports nested modules and submodules with readable indentation
- Hooks into PyTorch without modifying your model
- Context manager support: use it inline with minimal code
- Clean readable terminal logs
Installation
pip install tracy_nn
Usage
Basic usage
import torch
import torch.nn as nn
from tracy_nn import Tracer
x = torch.rand(batch_size, seq_len, d_in)
mha = MHA(d_in, d_out, seq_len, num_heads, context_window, dropout)
tracer = Tracer('MHA')
tracer.start(mha)
output = mha(x)
tracer.stop()
Using the context manager
from tracy_nn import Tracer
tracer = Tracer('MyModel')
with tracer.trace(model):
output = model(input_tensor)
Even shorter with trace_model
from tracy_nn import trace_model
tracer = trace_model(model, 'MyModel')
with tracer.trace(model):
output = model(input_tensor)
Note!
- Do not use
tracy_nnduring training — it will log every operation and slow things down. - Only one forward pass is needed to trace everything.
- Some functional calls like
torch.cat()may not get traced. UseTensor.cat()or their method equivalents for better compatibility. - Standard Python operations like
@,+,/, etc., are translated internally to their traced PyTorch equivalents.
Why did i make it?
I built Tracy NN while struggling to understand matrix transformations in transformers. By using PyTorch’s hook system, I was able to introspect every tensor flowing through my model — and I hope this helps other curious people build a better mental model of how neural networks work.
License
MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tracy_nn-0.1.0.tar.gz.
File metadata
- Download URL: tracy_nn-0.1.0.tar.gz
- Upload date:
- Size: 9.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
97826c296abed787f863241118c4da4b275df1e485da43a30ad4658b5260d72b
|
|
| MD5 |
92f39a15db022639d0093671eb49ac94
|
|
| BLAKE2b-256 |
471a707c76ea60aba0aa031826a5e1cc689e1cc32d6da6f8d5e743af11cc091d
|
File details
Details for the file tracy_nn-0.1.0-py3-none-any.whl.
File metadata
- Download URL: tracy_nn-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c8e5d07562e147ef2653a2cfb95a6412ab5d35fb0c4a5c5836c065784d91383a
|
|
| MD5 |
3cebdf926b1d9e9530ce24f1955935d8
|
|
| BLAKE2b-256 |
aaf0b81aa0a1bc6a3c8cf7aa16c7e17be6ba3d094c39cbadfadb098ac2cd9449
|