torch-surgeon
Real-time gradient pathology detection for PyTorch — in 2 lines.
A loss curve is a lagging indicator. By the time it shows a problem, vanishing or exploding gradients have been compounding for hundreds of steps. torch-surgeon attaches diagnostic hooks to your model and surfaces per-layer pathologies in real time, before they compound into an unrecoverable run.
Install
pip install torch-surgeon
Usage
from torch_surgeon import Surgeon
surgeon = Surgeon(model, rules="default")
surgeon.attach()
# ... your existing training loop, unchanged ...
for epoch in range(epochs):
loss = criterion(model(x), y)
optimizer.zero_grad()
loss.backward()
optimizer.step()
report = surgeon.report() # per-layer stats dict
surgeon.detach() # clean removal of all hooks
What it detects
| Pathology | Detection method |
|---|---|
| Vanishing gradients | Per-layer norm ratio drops below threshold vs EMA baseline |
| Exploding gradients | Per-layer norm ratio exceeds threshold vs EMA baseline |
| Stagnant layers | Norm near-zero for N consecutive steps — layer stopped learning |
Custom rules
surgeon = Surgeon(model, rules={
"vanishing_threshold": 0.01, # default
"exploding_threshold": 100.0, # default
"stagnant_steps": 50, # default
"log_every": 10, # print summary every N steps
"plot": True, # live matplotlib plot
"verbose": True,
})
How it works
torch-surgeon uses PyTorch's register_full_backward_hook API to intercept gradients
at every leaf layer during the backward pass. Statistics (mean, std, norm) are computed
inside the hook and the raw gradient tensor is discarded immediately — keeping overhead
under 1% on typical training loops.
Pathology detection uses an exponential moving average (EMA) baseline per layer rather than fixed thresholds — so it generalises across architectures without manual tuning.
Performance
Sub-1% training overhead on standard loops. Validated against 100-step timing benchmarks on Linear/ReLU networks. Stats computed in-hook; no tensors stored between steps.
License
MIT
Metadata
Release files for torch-surgeon 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| torch_surgeon-0.1.0.tar.gz | 10.1 kB | Without provenance | Details for torch_surgeon-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| torch_surgeon-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for torch_surgeon-0.1.0-py3-none-any.whl |
Total release size: 18.7 kB
Release files / torch_surgeon-0.1.0.tar.gz
| Download URL | torch_surgeon-0.1.0.tar.gz |
|---|---|
| Size | 10.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a0b7a342e4e6a5f7186ca04f445475135f71f3953bfec2c51a105e0086588d61
|
|
BLAKE2b-256 checksum How to use checksums |
0e8f0c28ab0b77fea35d8714ffb0f801015354a67e7cd4096b8bd3c68c5eb6ff
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.12
|
Release files / torch_surgeon-0.1.0-py3-none-any.whl
| Download URL | torch_surgeon-0.1.0-py3-none-any.whl |
|---|---|
| Size | 8.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2ad8d5f76d75daae7be4169fc759bea339551bcc689ba613618ed194aa1b429d
|
|
BLAKE2b-256 checksum How to use checksums |
04c25d25bbcf73b98fbd32fa0c38c83c902543d50a471927821704421606f8f1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.12
|
Security for torch-surgeon 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.