A strict, ergonomic, and powerful Spiking Neural Network (SNN) library for PyTorch.
Project description
traceTorch
A strict, ergonomic, and powerful Spiking Neural Network (SNN) library for PyTorch.
traceTorch is bult around a single, highly compositional neuron superclass, replacing the restrictive layer zoo of
countless disjoint neuron types with the LeakyIntegrator superclass. This design encapsulates a massive range of SNN
dynamics:
- Flexible polarity for spike outputs: positive and/or negative or none at all for a readout layer
- Optional synaptic and recurrent signal accumulation into separate hidden states
- Rank-based parameter scoping for per-layer (scalar) or per-neuron (vector) parameters, learnable or static
- Optional Exponential Moving Average (EMA) on any hidden state
All into declarative configuration on one class using sensible, powerful defaults.
By abstracting this complexity, traceTorch provides both the robust simplicity required for fast prototyping via
familiar wrappers and the unprecedented flexibility required for real research and models. In total, traceTorch presents
a total of 12 easy to use layer types which directly integrate into existing PyTorch models and API: LIF, BLIF,
SLIF, RLIF, BSLIF, BRLIF, SRLIF, BSRLIF, Readout, SReadout, RReadout, SRReadout; with an API simple
enough that you can add more with little effort.
Why traceTorch?
Existing SNN libraries often feel restrictive or require verbose state management. Aside from the technical features and capabilities, traceTorch follows a fundamentally different philosophy, revolving around ergonomics and usability:
- Architectural Flexibility: All existing traceTorch layers are just small wrappers of the
LeakyIntegratorsuperclass, and it's incredibly easy to add your own alterations and combinations of the features you like. - Automatic State Management: No need to manually pass hidden states through
.forward(), each layer manages its own hidden states, and calling.zero_states()on a traceTorch model recursively clears all the hidden states the entire model uses, no matter how deeply hidden they are. In a similar style,.detach_states()detaches the states from the current computation graph. - Lazy Initialization: Hidden states are initialized as
Noneand allocated dynamically based on the input shape. This completely eliminates "Batch Size Mismatch" errors during training and inference. - Dimension Agnostic: Whether you are working with
[Time, Batch, Features]or[Batch, Channels, Height, Width]tensors, layers just work. Change a singledimargument during layer initialization to indicate the target dimension the layer acts on. Defaults to-1for MLP,-3would work for CNN (channels are 3rd last in[B, C, H, W]or[C, H, W]). The tensors are automatically move the target dimension to the correct index so that the layers work. - Smooth Constraints: Decay and threshold parameters are constrained via Sigmoid and Softplus respectively. No hard clamping means that gradients flow smoothly and accurately everywhere.
- Rank Based Parameters: Instead of messy flags like
*_is_vectoror*_is_scalar, traceTorch uses a single*_rankinteger to define each parameter scope: 0 for a scalar (per-layer), 1 for a vector (per-neuron). - Sensible, Powerful Defaults: traceTorch defaults to learnable, per-neuron (rank 1) parameters for flexibility and EMA on synaptic and recurrent traces for numerical stability; because real research and real models thrive on heterogeneity. Overridable if you want, but sensible defaults means less boilerplate.
Installation
traceTorch is a PyPI library found here. Requirements for the library are listed
in requirements.txt. Take note that examples found in examples/ may have their own requirements, separate from the
library requirements.
pip install tracetorch
If you want to run the example code without installing the PyPI package, or alternatively want to edit the code yourself, you should install traceTorch as an editable install.
git clone https://github.com/Yegor-men/tracetorch
cd tracetorch
pip install -e .
Quick Start
Making a traceTorch model is barely any different from PyTorch models. Here's how:
1. The "zero-boilerplate" module
Inherit from tracetorch.snn.TTModule instead of pytorch.nn.Module. This gives your model the powerful recursive
methods like zero_states() and detach_states() for free, while still integrating with other PyTorch nn.Module.
import torch
from torch import nn
import tracetorch as tt
from tracetorch import snn
class ConvSNN(snn.TTModule):
def __init__(self):
super().__init__()
self.net = nn.Sequential(
nn.Conv2d(1, 32, 3),
# dim=-3 tells the layer that the 3rd-to-last dimension is the channel dim.
# This works for (B, C, H, W) AND unbatched (C, H, W) inputs automatically.
snn.LIF(num_neurons=32, beta=0.9, dim=-3),
nn.Flatten(),
nn.Linear(32 * 26 * 26, 128),
# Readout layer with learnable decay initialized to scrape various timescales
snn.Readout(128, beta=torch.rand(128)),
# Map the readout layer back down to the desired number of dimensions
nn.Linear(128, 10)
)
def forward(self, x):
return self.net(x)
2. The Training Loop
State management is easily handled outside the forward pass. Simply call .zero_states() on the model to reset all
hidden states to None, and call .detach_states() to detach the current hidden states (used in truncated BPTT or for
online learning).
device = "cuda" if torch.cuda.is_available() else "cpu"
model = ConvSNN().to(device)
optimizer = torch.optim.AdamW(model.parameters(), lr=1e-3)
loss_fn = nn.CrossEntropyLoss()
# Training Step
model.train()
for x, y in dataloader:
x, y = x.to(device), y.to(device)
model.zero_states() # Crucial: Reset hidden states for the batch
optimizer.zero_grad()
# Time loop
spikes = []
for step in range(num_timesteps):
# Just pass x. No state tuples to manage.
spikes.append(model(x))
# Stack output and compute loss
output = torch.stack(spikes)
loss = loss_fn(output.mean(0), y) # Rate coding example
loss.backward()
optimizer.step()
Documentation
The online documentation can be found here. It contains the theory behind
SNNs, the traceTorch API and layers available, as well as a couple tutorials to recreate the code found in
examples/.
Authors
Contributing
Contributions are always welcome. Feel free to fork, submit pull requests or report issues, I will occasionally check in on it.
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 tracetorch-0.9.2.tar.gz.
File metadata
- Download URL: tracetorch-0.9.2.tar.gz
- Upload date:
- Size: 22.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
97b0ef0e8b286010c6f1d1dcd1bfa3c41dc2ec7ef1eaf034e5d82e3aa20c741e
|
|
| MD5 |
30f59de5da035311c44da3650a71551b
|
|
| BLAKE2b-256 |
837c6a0a3c975e7a2e6028e04f0152f20069654718a2e0a39f18dfd3cec368fd
|
File details
Details for the file tracetorch-0.9.2-py3-none-any.whl.
File metadata
- Download URL: tracetorch-0.9.2-py3-none-any.whl
- Upload date:
- Size: 29.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
586049f60b28d1ec8ff28ba7ad0a0f23d72456c2d368c726c5f5071b117c5c5b
|
|
| MD5 |
928d9e1f30940a866dbcdfe3e5aadd71
|
|
| BLAKE2b-256 |
507f882a4d3c4138c1f4b28e6e635586b5fe3151a6826ea1505c929a98818721
|