Skip to main content

The Linter for PyTorch: Detects silent training bugs.

Project description

๐Ÿ”ฅ torch-audit

The Linter for PyTorch Models

PyPI License: MIT Python 3.8+ Code Style: Black

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

torch_audit-0.1.0.tar.gz (13.8 kB view details)

Uploaded Source

Built Distribution

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

torch_audit-0.1.0-py3-none-any.whl (17.9 kB view details)

Uploaded Python 3

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

Hashes for torch_audit-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2bbfe6cf5b8a851f8954f47e502c14da3fa6b598ddf9d24d5874ff2885c54726
MD5 03d338a469f08c924b9bf737cc9da1fd
BLAKE2b-256 6a47c193971ce2fb74b1e35f37cb75b3d8498075399e9ec61e4e27b1be4a0944

See more details on using hashes here.

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

Hashes for torch_audit-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2303d0c6206913e2ac5e5cf8acaad12387412be1e2bd921ff44964c4491ded5b
MD5 349ce7dd8762d268fd8615f82463bbbd
BLAKE2b-256 79c4e4eb8bbb5e07ccf3dfc03e6c4fe86a760a05efad879a00805986f78acd80

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