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.1.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.1-py3-none-any.whl (17.8 kB view details)

Uploaded Python 3

File details

Details for the file torch_audit-0.1.1.tar.gz.

File metadata

  • Download URL: torch_audit-0.1.1.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.1.tar.gz
Algorithm Hash digest
SHA256 a0ed0db5da6197acc4495e8ea73305d6f17606a7d3be052647fc29f12369e48f
MD5 97ffe0bee946455aa17ac068241a6fa5
BLAKE2b-256 16453b9eea4a9db79567e5d1662688b9f51c372b0bd0085ab8dda30d999b1f6d

See more details on using hashes here.

File details

Details for the file torch_audit-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: torch_audit-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 17.8 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 504b17369e93bff1b2373147bcc20a4d311cad68452ffad2b8fcb75988c91577
MD5 20959b9543ee8f56170780edc788154d
BLAKE2b-256 b624ea4505a2784507c0b9abd0e346b0f640fae8a02acc66afdbb028e47b5c88

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