The Linter for PyTorch: Detects silent training bugs.
Project description
๐ฅ torch-audit
The Linter for PyTorch Models
torch-audit is a "check engine light" for your Deep Learning training loop. It detects silent bugs that don't crash your code but ruin your training.
- ๐ฅ Exploding Gradients: Detects norm spikes.
- ๐ง Zombie Layers: Identifies DDP-unsafe layers that are defined but never used.
- ๐ Hardware Waste: Warns about shapes incompatible with Tensor Cores.
- ๐ง Domain Awareness: Specific modules for NLP (Tokenization waste) and CV (Dead filters).
- ๐ Dead Neurons: Finds layers that output large number of zeros (ReLU death).
๐ฆ Installation
Install the standard version (lightweight):
pip install torch-audit
Optional Integrations:
# For PyTorch Lightning support
pip install "torch-audit[lightning]"
# For Hugging Face Transformers support
pip install "torch-audit[hf]"
# For everything
pip install "torch-audit[all]"
๐ Quick Start (Zero Overhead)
Wrap your training step with auditor.audit_dynamic(). By default, it runs every step, but you can schedule it to run once every 1000 steps for zero performance penalty.
import torch
from torch_audit import Auditor
# 1. Setup Auditor (Audits 1 step, sleeps for 999)
auditor = Auditor(model, config={'interval': 1000})
# 2. Static Audit (Run once before training)
# Checks architecture, unused layers, and weight initialization
auditor.audit_static()
# 3. Training Loop
for batch in dataloader:
# The Context Manager:
# - If active: Hooks attached, full analysis running.
# - If sleeping: Zero overhead (literally just a generic yield).
with auditor.audit_dynamic():
pred = model(batch)
loss = criterion(pred, target)
loss.backward()
optimizer.step()
The Output
When a bug is found, torch-audit prints a beautiful report to your console:
๐ Audit Running (Step 5000)...
โ ๏ธ Audit Report (Step 5000)
โโโโโโโโโโโโโโโโโโโโโณโโโโโโโโโโโโโโโโณโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Type โ Layer โ Message โ
โกโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฉ
โ ๐ด DDP Safety โ ghost_layer โ Layer defined but NEVER called (Zombie). โ
โ ๐ก Activations โ layer4.relu โ 95.5% zeros (Dead Neurons). โ
โ ๐ก Hardware โ fc1 โ Dimensions (127->64) not divisible by 8. โ
โโโโโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
๐งฉ Integrations
We support the ecosystem you already use.
โก PyTorch Lightning
Zero code changes to your loop. Just add the callback.
from lightning.pytorch import Trainer
from torch_audit import Auditor
from torch_audit.callbacks import LightningAuditCallback
auditor = Auditor(model, config={'interval': 100})
trainer = Trainer(callbacks=[LightningAuditCallback(auditor)])
๐ค Hugging Face Trainer
Plug-and-play with the Trainer API.
from transformers import Trainer
from torch_audit import Auditor
from torch_audit.callbacks import HFAuditCallback
auditor = Auditor(model, config={'monitor_nlp': True})
trainer = Trainer(..., callbacks=[HFAuditCallback(auditor)])
๐ง Domain Specific Audits
torch-audit has even more tools for your domain.
๐ NLP Mode
Detects tokenizer issues, padding waste, and untied embeddings.
config = {
'monitor_nlp': True,
'pad_token_id': tokenizer.pad_token_id,
'vocab_size': tokenizer.vocab_size
}
auditor = Auditor(model, config=config)
Catches:
โ ๏ธ "High Padding detected (55% of tokens). 50%+ of compute is wasted."
๐ด "High [UNK] rate (8%). Tokenizer mismatch."
โน๏ธ "Embedding and Output Head are not tied."
๐ผ๏ธ Computer Vision Mode
Detects normalization bugs (0-255 inputs) and dead convolution filters.
auditor = Auditor(model, config={'monitor_cv': True})
Catches:
๐ด "Input values range [0, 255]. Did you forget ToTensor()?"
๐ก "Layer conv2 has 45% dead filters (magnitude ~0)."
โ๏ธ Configuration
You can configure the auditor via a dictionary or the AuditConfig object.
| Parameter | Default | Description |
|---|---|---|
interval |
1 |
Run audit every N steps. Set to 100, 1000 or more for production. |
limit |
None |
Stop auditing after N reports. |
float_threshold |
10.0 |
Max value allowed in inputs before warning. |
monitor_dead_neurons |
True |
Check for activations death. |
monitor_graph |
True |
Check for unused (zombie) layers. |
monitor_nlp |
False |
Enable NLP-specific hooks (requires pad_token_id). |
monitor_cv |
False |
Enable CV-specific hooks. |
๐ ๏ธ Advanced: Manual Triggering
Sometimes you want to audit, for example, when the loss spikes.
loss = criterion(output, target)
if loss.item() > 10.0:
print("Loss spike! Debugging next step...")
auditor.schedule_next_step() # Forces audit on next forward pass
License
Distributed under the MIT License.
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
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 torch_audit-0.1.0.tar.gz.
File metadata
- Download URL: torch_audit-0.1.0.tar.gz
- Upload date:
- Size: 13.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.1 CPython/3.11.4 Windows/10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2bbfe6cf5b8a851f8954f47e502c14da3fa6b598ddf9d24d5874ff2885c54726
|
|
| MD5 |
03d338a469f08c924b9bf737cc9da1fd
|
|
| BLAKE2b-256 |
6a47c193971ce2fb74b1e35f37cb75b3d8498075399e9ec61e4e27b1be4a0944
|
File details
Details for the file torch_audit-0.1.0-py3-none-any.whl.
File metadata
- Download URL: torch_audit-0.1.0-py3-none-any.whl
- Upload date:
- Size: 17.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.1 CPython/3.11.4 Windows/10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2303d0c6206913e2ac5e5cf8acaad12387412be1e2bd921ff44964c4491ded5b
|
|
| MD5 |
349ce7dd8762d268fd8615f82463bbbd
|
|
| BLAKE2b-256 |
79c4e4eb8bbb5e07ccf3dfc03e6c4fe86a760a05efad879a00805986f78acd80
|